平台: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; } |