获取选中地物的Geometry对象,及把Geometry对象显示在地图上

下面是Map 3D开发时帮助调试检查用的工具类,也是经常会用的的功能。贴在这里共享备查。


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().DefaultGeometryPropertyName;

                     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.DefaultGeometryPropertyName)
                                                                     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.DefaultGeometryPropertyName,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 cancelled ");
                 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;
        }


    }
}

你可能感兴趣的:(try)