天天看點

擷取選中地物的Geometry對象,及把Geometry對象顯示在地圖上

下面是Map 3D開發時幫助調試檢查用的工具類,也是經常會用的的功能。貼在這裡共享備查。

擷取選中地物的Geometry對象,及把Geometry對象顯示在地圖上

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;

        }

        /// Add a gemetry to a layer so that it is displayed on map

        /// <param name="layer"></param>

        /// <param name="geometry"></param>

        static public bool AddFeature(MgLayerBase layer, MgGeometry geometry)

            if (layer == null )

                return false;

            if (geometry == null)

            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");

        /// Validate whether the Geometry Type of Layer and MgGeometry to be added in match.

        /// <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)

            else if (geoType == MgGeometryType.Polygon || geoType == MgGeometryType.CurvePolygon

                    || geoType == MgGeometryType.MultiPolygon)

                if ((layerGeoType & MgFeatureGeometricType.Surface) != 0)

            return match;

    }

}

擷取選中地物的Geometry對象,及把Geometry對象顯示在地圖上

郵箱:[email protected] 

轉載請保留此資訊。

本文轉自峻祁連. Moving to Cloud/Mobile部落格園部落格,原文連結:http://www.cnblogs.com/junqilian/archive/2011/10/12/2208838.html,如需轉載請自行聯系原作者

繼續閱讀