通過Map 3D API讀取線狀要素的節點座標

峻祁連發表於2014-07-10

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("\n geo is MgCurveString.");

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

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

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

                            msg = string.Format(
                                "\nend point: x= {0}, y={1}",
                                arcSeg.EndCoordinate.X,
                                arcSeg.EndCoordinate.Y);
                            ed.WriteMessage(msg);
                        }
                        if (seg is MgLinearSegment)
                        {
                            ed.WriteMessage("\nthis 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(
                                    "\n 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(
                            "\n x = {0}, y={1} ", x, y));
                    }

                }

            }
        }
        trans.Commit();
    }

}



}

}

相關文章