How to get a polyline from region

How to get a polyline from region

reithinger
Advocate Advocate
681 Views
4 Replies
Message 1 of 5

How to get a polyline from region

reithinger
Advocate
Advocate

Hi, 

I have a Problem.

I have a Region and i want to get a polyline from all the Region

 

Screenshot 2025-08-21 082537.jpg

 

 

The Red Line is the Region and the Green Line is the polyline that i have...

This isn't what a want.

I make this

// Regionen vereinigen und äußere Kontur als Polyline erzeugen
using (var reg = MergeRegions(regions, AcDataService.BooleanOperationType.BoolUnite))
{
   if (reg != null)
   {
      var brep = new AcBounds.Brep(reg);
      var points = new List<Point2d>();
      var face = brep.Faces.First();
      foreach (var loop in face.Loops)
      {
         if (loop.LoopType == AcBounds.LoopType.LoopExterior)
         {
            foreach (var vertex in loop.Vertices)
            {
               points.Add(new Point2d(vertex.Point.X, vertex.Point.Y));
            }
            break;
         }
      }
      // Polyline aus Punkten erzeugen
      myRegionPolyline = CreatePolyline(points);
   }
}
myRegionPolyline.ColorIndex = 3; // Set color index for the polyline
curSpace.AppendEntity(myRegionPolyline);
tr.AddNewlyCreatedDBObject(myRegionPolyline, true);

I make a Merge von Region with Unite and that i use the Brep, but something isn't correct.

What is my mystake ?

Thanks Fabrice

0 Likes
Accepted solutions (1)
682 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

You can use the Region.GetCurves() extension method from GeometryExtensions library.

/// <summary>
/// Gets the curves constituting the boundaries of the region.
/// </summary>
/// <param name="region">The instance to which this method applies.</param>
/// <param name="tolerance">Tolerance used in curve end points comparison.</param>
/// <returns>Curve collection.</returns>
/// <exception cref="System.ArgumentNullException">ArgumentException is thrown if <paramref name="region"/> is null.</exception>
public static IEnumerable<Curve> GetCurves(this Region region, Tolerance tolerance = default)
{
    Assert.IsNotNull(region, nameof(region));

    if (tolerance.Equals(default(Tolerance)))
        tolerance = Tolerance.Global;

    using (var brep = new Brep(region))
    {
        foreach (var loop in brep.Faces.SelectMany(face => face.Loops))
        {
            var curves3d = loop.GetNativeCurves().ToArray();
            if (curves3d.Length == 1)
            {
                yield return Curve.CreateFromGeCurve(curves3d[0]);
            }
            else if (curves3d.TryConvertToCompositeCurve(out CompositeCurve3d compositeCurve, tolerance, c => c is LineSegment3d || c is CircularArc3d))
            {
                yield return (Polyline)Curve.CreateFromGeCurve(compositeCurve);
            }
            else
            {
                foreach (Curve3d curve3d in curves3d)
                {
                    yield return Curve.CreateFromGeCurve(curve3d);
                }
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

reithinger
Advocate
Advocate

Hi _Gile,

thanks for your help.

Do you know why my code not really do ?

Thanks Fabrice

0 Likes
Message 4 of 5

_gile
Consultant
Consultant

@reithinger  a écrit :

Do you know why my code not really do ?


Because your code uses the vertices (points) of the loop, instead of the edges (curves). The points do not contains the informations required to create the arc segments.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

reithinger
Advocate
Advocate

Ok,

I Understand.

Thanks for your reply.

It works.

Thanks Fabrice

0 Likes