Author Topic: Convert set of connected lines and arcs to polyline  (Read 1658 times)

0 Members and 1 Guest are viewing this topic.

CrockettScience

  • Mosquito
  • Posts: 14
Convert set of connected lines and arcs to polyline
« on: October 11, 2019, 05:22:25 PM »
Something that should be relatively simple has been a nightmare for me for days now.

I have a set of lines and arcs that reside on the z=0 plane and are all connected EndPoint to Startpoint (such that curves.Endpoint == curves[i+1].StartPoint). I need to convert them to a single polyline. The code I have at this point is below:

Code: [Select]
var poly = new Polyline();
                   
poly.AddVertexAt(0, Point2d.Origin, 0, 0, 0);

foreach (var curve in curves) {
                   
     poly.SetPointAt(poly.NumberOfVertices - 1, new Point2d(curve.StartPoint.X, curve.StartPoint.Y));
     poly.JoinEntity(curve);
                   
     curve.Erase();
     curve.Dispose();
}

This code fails at poly.JoinEntity with eNotApplicable. Usually, this means they are not connected properly, but using the live debugger, I can see that at the moment poly.JoinEntity is executed (which happens to be the second iteration of the loop), the endpoint of poly is indeed equal to the startpoint of curve. Obvious considering I literally force it to with the SetPointAt Method.

I've tried everything and I'm now convinced it's some weird coordinate system skull fuckery. According to most similar help threads (all of which only solve the problem of converting one arc or line to polyline) their conversion usually involves some ECS stuff for reasons I don't know, and as per the code etiquette for helping those at the AutoCAD help center forum, all the code is just shit into the thread without and explanation or context.

Now the code I have is this
Code: [Select]
var poly = new Polyline();
                   
poly.AddVertexAt(0, Point2d.Origin, 0, 0, 0);

foreach (var curve in curves) {
     var start = curve.StartPoint.TransformBy(curve.Ecs.Inverse());
                   
     if(curve is Arc)
          poly.TransformBy(curve.Ecs);
                   
      poly.Thickness = curve is Line line ? line.Thickness : ((Arc) curve).Thickness;
                   
          poly.SetPointAt(poly.NumberOfVertices - 1, new Point2d(start.X, start.Y));
                   
      if(curve is Line)
          poly.AddVertexAt(poly.NumberOfVertices, curve.EndPoint.FlatCast(), 0, 0, 0);
      else
           poly.JoinEntity(curve);
                   
      poly.SetPropertiesFrom(curve);
                   
      curve.Erase();
      curve.Dispose();
}

This actually compiles and runs, but the resultant polyline is a jumbled mess.

Any idea where I can go from here, and/or has some insight as to what the ECS portion is accomplishing?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Convert set of connected lines and arcs to polyline
« Reply #1 on: October 11, 2019, 05:36:09 PM »
hi,

You should try to use the PolylineSegmentCollection.Join() method (and the PolylineSegment class) from the GeometryExtensions library.
Speaking English as a French Frog