The following is a tool class used for debugging and checking during Map 3D development. It is also a frequently used function. Paste it here for future reference sharing.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using OSGeo. MapGuide;
Using Autodesk. AutoCAD. EditorInput;
Using Autodesk. Gis. Map. Platform. Interop;
Using Autodesk. Gis. Map. Platform. Utils;
Namespace TestSolution
{
Public class MapHelper
{
/// <Summary>
/// Get the geometry objects of selected features from map
/// </Summary>
/// <Returns> </returns>
Static public IList <MgGeometry> GetSelectedFeatureGeometry ()
{
IList <MgGeometry> list = new List <MgGeometry> ();
PromptSelectionResult selResult =
Autodesk. AutoCAD. ApplicationServices. Application. DocumentManager. MdiActiveDocument. Editor. GetSelection ();
If (selResult. Status = PromptStatus. OK)
{
SelectionSet selSet = selResult. Value;
// Get all the features in SelectionSet
MgSelectionBase selectionBase = AcMapFeatureEntityService. GetSelection (selSet );
Foreach (MgLayerBase layer in selectionBase. GetLayers ())
{
MgFeatureReader featureReader = selectionBase. GetSelectedFeatures (layer, layer. FeatureClassName, false );
// Get the name of Geometry Property
String gepPropertyName = layer. GetClassDefinition (). defaultrypropertyname;
If (string. IsNullOrEmpty (gepPropertyName ))
{
Util. PrintLn ("No geometry property exists in layer" + layer. Name );
Continue;
}
While (featureReader. ReadNext ())
{
MgByteReader byteReader = featureReader. GetGeometry (gepPropertyName );
MgAgfReaderWriter agfReader = new MgAgfReaderWriter ();
MgGeometry geomtry = agfReader. Read (byteReader );
List. Add (geomtry );
}
FeatureReader. Close ();
}
}
Return list;
}
/// <Summary>
/// Add a gemetry to a layer so that it is displayed on map
/// </Summary>
/// <Param name = "layer"> </param>
/// <Param name = "geometry"> </param>
/// <Returns> </returns>
Static public bool AddFeature (MgLayerBase layer, MgGeometry geometry)
{
If (layer = null)
Return false;
If (geometry = null)
Return false;
MgClassDefinition classDef = layer. GetClassDefinition ();
String featClassName = layer. FeatureClassName;
MgPropertyDefinitionCollection propDefs = classDef. GetProperties ();
MgGeometricPropertyDefinition geoPropDef = propDefs. GetItem (classDef. defaultrypropertyname)
As MgGeometricPropertyDefinition;
// Default Geometry Type of current layer.
Int layerGeometryType = geoPropDef. GeometryTypes;
// Geometry Type converted from acdbEntity.
Int geoType = geometry. GetGeometryType ();
// If (! GeometryTypeValidate (layerGeometryType, geoType ))
//{
// Util. PrintLn ("Geometry type of the entity is invalid to the targeting layer .");
// Return false;
//}
MgPropertyCollection props = new MgPropertyCollection ();
MgAgfReaderWriter writer = new MgAgfReaderWriter ();
MgByteReader byteReader = writer. Write (geometry );
Props. Add (new MgGeometryProperty (classDef. defaultrypropertyname, byteReader ));
If (props! = Null)
{
MgInsertFeatures insertFeat = new MgInsertFeatures (featClassName, props );
MgFeatureCommandCollection featCommands = new MgFeatureCommandCollection ();
FeatCommands. Add (insertFeat );
Try
{
Layer. UpdateFeatures (featCommands );
}
Catch (System. Exception e)
{
Util. PrintLn ("Failed to add a Feature .");
Util. PrintLn (e. Message );
Return false;
}
Return true;
}
Else
{
Util. PrintLn ("Operation canceled ");
Return false;
}
}
/// <Summary>
/// Validate whether the Geometry Type of Layer and MgGeometry to be added in match.
/// </Summary>
/// <Param name = "layerGeoType"> Geometry Type of Layer </param>
/// <Param name = "geoType"> Geometry Type of the MgGeometry convert from Entity </param>
/// <Returns> true if match </returns>
Private bool GeometryTypeValidate (int layerGeoType, int geoType)
{
Bool match = false;
If (geoType = MgGeometryType. Point | geoType = MgGeometryType. MultiPoint)
{
If (layerGeoType & MgFeatureGeometricType. Point )! = 0)
Match = true;
}
Else if (geoType = MgGeometryType. LineString | geoType = MgGeometryType. MultiLineString)
{
If (layerGeoType & MgFeatureGeometricType. Curve )! = 0)
Match = true;
}
Else if (geoType = MgGeometryType. Polygon | geoType = MgGeometryType. CurvePolygon
| GeoType = MgGeometryType. MultiPolygon)
{
If (layerGeoType & MgFeatureGeometricType. Surface )! = 0)
Match = true;
}
Return match;
}
}
}