How to convert a list of Curve, which contains Lines and Arcs, to a Polyline?

How to convert a list of Curve, which contains Lines and Arcs, to a Polyline?

winderson2GYXS
Contributor Contributor
477 Views
3 Replies
Message 1 of 4

How to convert a list of Curve, which contains Lines and Arcs, to a Polyline?

winderson2GYXS
Contributor
Contributor

I'm trying to create a Polyline from a list of Curve objects, my list contains Lines and Arcs, these curves are not in the document, I'm creating then by code. I managed to generated a Polyline usingthe points of these curves and adding as a Vertex to the Polyline, it works but I have a problem with my loops.

I tried different ways to generate the vertex, using both start and end points of the curves generated extra lines with equal start and end points, using only the start point but arcs are not correctly generated, then I tried to verify if the point already exists before adding as a vertex on the code bellow, but it creates a extra line on arcs.

Has someone managed to generated something like this that can help me? Thanks in advance.

 

private Polyline CurvesToPolyline(List<Curve> curves, bool closed)
{
	// Cria uma Polyline
	Polyline pline = new Polyline();

	// Transforma as curvas em elementos da Polyline
	for (int i = 0; i < curves.Count; i++)
	{
		Curve curve = curves[i];

		if (curve is Line)
		{
			Line line = (Line)curve;

			if (i == 0 || !line.StartPoint.IsEqualTo(pline.GetPoint3dAt(pline.NumberOfVertices - 1)))
			{
				pline.AddVertexAt(pline.NumberOfVertices, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0.0, 0.0, 0.0);
			}

			pline.AddVertexAt(pline.NumberOfVertices, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0.0, 0.0, 0.0);
		}
		else if (curve is Arc)
		{
			Arc arc = (Arc)curve;

			if (i == 0 || !arc.StartPoint.IsEqualTo(pline.GetPoint3dAt(pline.NumberOfVertices - 1)))
			{
				pline.AddVertexAt(pline.NumberOfVertices, new Point2d(arc.StartPoint.X, arc.StartPoint.Y), GetArcBulge(arc), 0.0, 0.0);
			}

			pline.AddVertexAt(pline.NumberOfVertices, new Point2d(arc.EndPoint.X, arc.EndPoint.Y), 0.0, 0.0, 0.0);
		}

	}

	if (closed)
	{
		pline.Closed = true;
	}


	return pline;
}

 

0 Likes
Accepted solutions (1)
478 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

You can use or get some inspiration from the Join method of the PolylineSegmentCollection class in the GeometryExtensions library.

Here's a using example.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

_gile
Consultant
Consultant
Accepted solution

You could also use the Entity.JoinEntities method but to get it work, you have to convert the line or arc the method applies to into a Polyline.

Here's an example.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

winderson2GYXS
Contributor
Contributor

Thanks a lot for the help _gile! Both solutions work greatly, in the end I choose to use Entity.JoinEntities, and add RemoveVertexAt(0) to remove the first vertex wich I created using the first curve point.

 

private Polyline CurvesToPolyline(List<Curve> curves, bool closed)
{
	Polyline pline = new Polyline();

	Entity[] entites = curves.ToArray();

	pline.AddVertexAt(0, new Point2d(curves[0].StartPoint.X, curves[0].StartPoint.Y),0,0,0);

	pline.JoinEntities(entites);

	pline.RemoveVertexAt(0);

	if (closed)
	{
		pline.Closed = true;
	}

	return pline;
}

 

0 Likes