Extracting vertices of a region as 2d points list in C#

Extracting vertices of a region as 2d points list in C#

Anonymous
Not applicable
2,084 Views
2 Replies
Message 1 of 3

Extracting vertices of a region as 2d points list in C#

Anonymous
Not applicable

Hi,

 

I am new to c# and wanted to know how to extract the vertices of a region as 2d points list in C#. I want to use this point list to create a polyline.

Thank you

 

Best,

Vaibhav

0 Likes
Accepted solutions (1)
2,085 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

Assuming all segments of the region boundary are line segments, you can the Brep (Boundary REPresentation) API (requires referencing the acdbmgdbrep.dll) to get the region edges first vertices.

 

Here's an example:

using Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using System.Linq;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace RegionToPolyline
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect Region: ");
            options.SetRejectMessage("\nSelected object is nor a Region.");
            options.AddAllowedClass(typeof(Region), true);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var region = (Region)tr.GetObject(result.ObjectId, OpenMode.ForWrite);
                var normal = region.Normal;
                var plane = new Plane(Point3d.Origin, normal);
                Point2d[] points;
                using (var brep = new Brep(region))
                {
                    points = brep.Edges.Select(e => e.Vertex1.Point.Convert2d(plane)).ToArray();
                }
                region.Erase();
                using (var pline = new Polyline(points.Length))
                {
                    for (int i = 0; i < points.Length; i++)
                    {
                        pline.AddVertexAt(i, points[i], 0.0, 0.0, 0.0);
                    }
                    pline.Closed = true;
                    pline.Normal = normal;
                    var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    curSpace.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                }
                tr.Commit();
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

Anonymous
Not applicable

Hi@_gile,

 

Thank you for the help.

I also wanted to know if you could guide me to good resources to learn about how to use various commands in AutoCAD C#.

0 Likes