Read coordinates of line features with Map 3D API

By Daniel Du

I am asked how to get the coordinates of each vertices from a line feature, for example, a line feature which is created from an AutoCAD polyline .

image

For an AutoCAD polyline which only contains lines, when it is converted to feature with Map 3D command “Create from Geometry”, the feature type will be MgCurveString which contains only one linear segments.

image

For AutoCAD polyline which contains arc, the feature after conversion will be several segments, including arc segments and linear segments.

image
Here is the code snippet:
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 = Application.DocumentManager.MdiActiveDocument.Editor;
            Transaction trans = 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 geometry property name may differ for your data source
                        MgByteReader byteRdr = ftrRdr.GetGeometry("Geometry");
                        MgAgfReaderWriter wtr = new MgAgfReaderWriter();
                        MgGeometry geom = wtr.Read(byteRdr);

                        if (geom is MgCurveString)
                        {
                            var cs = geom as MgCurveString;
                            ed.WriteMessage("\nGeo 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 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();
            }
        }
    }
}

 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading