zoukankan      html  css  js  c++  java
  • 通过Map 3D API读取线状要素的节点坐标

    By Daniel Du

    在Map 3D中可以使用Create from Geometry命令把AutoCAD实体转换成Map 3D中的FDO要素,比如可以把AutoCAD的polyline转换成FDO线状要素。

    image

    对于只包含直线的AutoCAD polyline,在转成FDO要素后,将是一个MgCurveString对象,并且只包含一个LinearSegment。

    image

    如果AutoCAD polyine中包含弧Arc, 那转换出来的FDO要素对象,将是一个包含多个segment的MgCurveString对象。其中有Arc Segment也有linear segment。

    image

    下面是对这样的线状要素读取坐标点的代码:

    using System;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.EditorInput;
    using Autodesk.Gis.Map.Platform.Interop;
    using Autodesk.Gis.Map.Platform;
    using OSGeo.MapGuide;

    // This line is not mandatory, but improves loading performances
    [assembly: CommandClass(typeof(GetFeatureType.MyCommands))]

    namespace GetFeatureType
    {

    public class MyCommands
    {


    // Modal Command with localized name
    [CommandMethod("getPolylineCoordinates")]
    public void MyCommand() // This method can have any name
    {
        Editor ed = Autodesk.AutoCAD.ApplicationServices.Application
            .DocumentManager.MdiActiveDocument.Editor;

        Transaction trans = Autodesk.AutoCAD.ApplicationServices.Application
            .DocumentManager.MdiActiveDocument.Database.TransactionManager
            .StartTransaction();

        using (trans)
        {
            // Get the Map Object
            AcMapMap currentMap = AcMapMap.GetCurrentMap();

            // Prompt user to Select Feature in Map
            PromptSelectionOptions psop = new PromptSelectionOptions();
            psop.MessageForAdding = "Select the FDO Feature in Map 3D to read Data : ";
            psop.SingleOnly = true;
            PromptSelectionResult psResult = ed.GetSelection(psop);

            if (psResult.Status == PromptStatus.OK)
            {
                SelectionSet selSet = psResult.Value;

                // Get Map Selectionset from AutoCAD SelectionSet
                MgSelectionBase mapSelBase = AcMapFeatureEntityService
                    .GetSelection(selSet);
                AcMapLayer mapLayer = AcMapFeatureEntityService
                    .GetLayer(psResult.Value[0].ObjectId);

                //Get the ID of the selected Parcel
                MgFeatureReader ftrRdr = mapSelBase.GetSelectedFeatures(
                    mapLayer, mapLayer.FeatureClassName, false);

                while (ftrRdr.ReadNext())
                {
                    MgClassDefinition cd = ftrRdr.GetClassDefinition();

                    //the geomety property name maybe different for your
                    //data source
                    MgByteReader byteRdr = ftrRdr.GetGeometry("Geometry");
                    MgAgfReaderWriter wtr = new MgAgfReaderWriter();

                    MgGeometry geom = wtr.Read(byteRdr);

                    if (geom is OSGeo.MapGuide.MgCurveString)
                    {
                        var cs = geom as MgCurveString;

                        ed.WriteMessage(" geo is MgCurveString.");

                        for (int i = 0, segmentCount = cs.Count; i < segmentCount; i++)
                        {
                            var seg = cs.GetSegment(i);
                            if (seg is MgArcSegment)
                            {
                                ed.WriteMessage(" this is an Arc Segment.");
                                var arcSeg = seg as MgArcSegment;

                                string msg = string.Format(
                                    " start point: x= {0}, y={1}",
                                    arcSeg.StartCoordinate.X,
                                    arcSeg.StartCoordinate.Y);
                                ed.WriteMessage(msg);

                                msg = string.Format(
                                    " control point  x= {0}, y={1}",
                                    arcSeg.ControlCoordinate.X,
                                    arcSeg.ControlCoordinate.Y);
                                ed.WriteMessage(msg);

                                msg = string.Format(
                                    " end point: x= {0}, y={1}",
                                    arcSeg.EndCoordinate.X,
                                    arcSeg.EndCoordinate.Y);
                                ed.WriteMessage(msg);
                            }
                            if (seg is MgLinearSegment)
                            {
                                ed.WriteMessage(" this is a linear Segment.");

                                var linearSeg = seg as MgLinearSegment;
                                var interator = linearSeg.GetCoordinates();
                                while (interator.MoveNext())
                                {
                                    var x = interator.GetCurrent().X;
                                    var y = interator.GetCurrent().Y;

                                    ed.WriteMessage(string.Format(
                                        " x = {0}, y={1} ", x, y));
                                }
                            }

                        }
                    }
                    if (geom is OSGeo.MapGuide.MgLineString)
                    {
                        var ls = geom as MgLineString;
                        var interator = ls.GetCoordinates();
                        while (interator.MoveNext())
                        {
                            var x = interator.GetCurrent().X;
                            var y = interator.GetCurrent().Y;

                            ed.WriteMessage(string.Format(
                                " x = {0}, y={1} ", x, y));
                        }

                    }

                }
            }
            trans.Commit();
        }

    }



    }

    }

  • 相关阅读:
    [Codeforces 1245D] Shichikuji and Power Grid (最小生成树)
    [BZOJ 1535] [Luogu 3426]SZA-Template (KMP+fail树+双向链表)
    [BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂)
    [Codeforces 1246B] Power Products (STL+分解质因数)
    [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)
    [BZOJ5306] [HAOI2018]染色(容斥原理+NTT)
    [Codeforces 1239D]Catowise City(2-SAT)
    [BZOJ 3527] [ZJOI2014]力(FFT)
    [BZOJ 3456]城市规划(cdq分治+FFT)
    【2-SAT(最小字典序/暴力染色)】HDU1814-Peaceful Commission
  • 原文地址:https://www.cnblogs.com/junqilian/p/3836094.html
Copyright © 2011-2022 走看看