明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1633|回复: 0

[分享]用C#实现命令“创建要素”的实现方法

[复制链接]
发表于 2010-9-25 10:10:00 | 显示全部楼层 |阅读模式

平台:vs.net2008 AutoCAD Map3d 2010

 

部份实现代码:


using OSGeo.MapGuide;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.Gis.Map.Platform;
using Autodesk.Gis.Map.Platform.Interop;


        /// <summary>
        /// 创建要素
        /// </summary>
        public void CreateFeature()
        {
            AcMapMap currentMap = AcMapMap.GetCurrentMap();
            MgLayerCollection layers = currentMap.GetLayers();

            MgLayerBase layer = SelectLayer(layers);
            MgClassDefinition classDef = layer.GetClassDefinition();
            MgPropertyDefinitionCollection propDefs = classDef.GetProperties();

            ObjectIdCollection objIds = CGcEditor.SelectEntities();
             int count = 0;
             Database db =
                     Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
             TransactionManager tm = db.TransactionManager;
             using (Transaction transaction = tm.StartTransaction())
             {
                 foreach (ObjectId objId in objIds)
                 {
                     Entity entity = (Entity)tm.GetObject(objId, OpenMode.ForWrite, false);
                     bool addSuccess = AddFeature(layer, entity);
                     if (addSuccess)
                     {
                         count++;
                         entity.Erase();
                     }
                 }

                 string msg = "Convert the entity to feature successfully.";
                 if (count > 1)
                 {
                     msg = string.Format("Converted {0} entities to features successfully!", count);
                 }
                 if (count > 0)
                 {
                     CGcEditor.ShowMessage(msg);
                 }
                 transaction.Commit();
             }
        }

 

 


        /// <summary>
        /// Entity -> MgGeometry
        /// </summary>
        private bool AddFeature(MgLayerBase layer, Entity entity)
        {
            if (layer == null || entity == null)
                return false;

            MgGeometry geometry = CEntityToGeometryConverter.EntityToGeometry(entity);
            if (geometry == null)
                return false;

            MgClassDefinition classDef = layer.GetClassDefinition();
            string featClassName = layer.FeatureClassName;
            MgPropertyDefinitionCollection propDefs = classDef.GetProperties();
            MgGeometricPropertyDefinition geoPropDef = propDefs.GetItem(classDef.DefaultGeometryPropertyName)
                                                                    as MgGeometricPropertyDefinition;
            
            int layerGeometryType = geoPropDef.GeometryTypes;           
            int geoType = geometry.GetGeometryType();
            if (!GeometryTypeValidate(layerGeometryType, geoType))
            {
                CGcEditor.ShowMessage("Geometry type of the entity is invalid to the targeting layer.");
                return false;
            }

            MgPropertyCollection props = new MgPropertyCollection();
            props = InputPropertyValues(propDefs, geometry);
            if (props != null)
            {
                MgInsertFeatures insertFeat = new MgInsertFeatures(featClassName, props);
                MgFeatureCommandCollection featCommands = new MgFeatureCommandCollection();
                featCommands.Add(insertFeat);
                try
                {
                    layer.UpdateFeatures(featCommands);
                }
                catch (System.Exception e)
                {
                    CGcEditor.ShowMessage("Failed to add a Feature.");
                    CGcEditor.ShowMessage(e.Message);
                    return false;
                }
                return true;
            }
            else
            {
                CGcEditor.ShowMessage("Operation cancelled");
                return false;
            }
        }

 

 


        /// <summary>
        /// Convert Entity to MgGeometry.
        /// </summary>
        public static MgGeometry EntityToGeometry(Entity entity)
        {
            if (entity == null)
                return null;
            MgGeometry geometry = null;

            string entityType = entity.GetType().Name;

            switch (entityType)
            {
                case "DBPoint":
                    geometry = PointGeometryConvert((DBPoint)entity);
                    break;

                case "Line":
                    geometry = LineGeometryConvert((Line)entity);
                    break;

                case "Polyline":
                    geometry = PolylineGeometryConvert((Polyline)entity);
                    break;

                case "MPolygon":
                    geometry = MPolygonGeometryConvert((MPolygon)entity);
                    break;

                default:
                    break;
            }
            return geometry;
        }

评分

参与人数 1威望 +1 明经币 +1 收起 理由
雪山飞狐_lzh + 1 + 1 【好评】表扬一下

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-26 01:39 , Processed in 0.159331 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表