zoukankan      html  css  js  c++  java
  • 获取选中地物的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;
            }


        }
    }
    作者:峻祁连
    邮箱:junqilian@163.com
    出处:http://junqilian.cnblogs.com
    转载请保留此信息。
  • 相关阅读:
    虚拟机中对centOS7.4配置静态ip
    mybatis使用中出现的错误!
    http中get和post方法区别
    java中堆和栈的区别
    struts2工作流程
    springmvc工作流程
    JDBC访问数据库流程
    并行程序设计模式-Master-Worker模式-Guarded Suspension模式-不变模式-生产者-消费者模式的理解
    Future模式个人理解
    分布式系统一致性问题和Raft一致性算法
  • 原文地址:https://www.cnblogs.com/junqilian/p/2208838.html
Copyright © 2011-2022 走看看