Joining Line Objects into a Single Polyline

Joining Line Objects into a Single Polyline

Anonymous
Not applicable
7,499 Views
10 Replies
Message 1 of 11

Joining Line Objects into a Single Polyline

Anonymous
Not applicable

I'm using C# and the AutoCAD .net framework all I'm trying to do is join lines into a poly line. I have a List of Lines and I'm trying to iterate through the list, joining each line together.

 

The simplified code is shown below:

		private static List<Line> weldLines = new List<Line>();

		private static void joinSelectedWeldLines()
		{
			//initialize needed components
			Document doc = Application.DocumentManager.MdiActiveDocument;
			Database db = doc.Database;
			Editor ed = doc.Editor;

			bool firstLine = true;
			Line sourceLine = new Line();


			try
			{
				using (Transaction TR = db.TransactionManager.StartTransaction())
				{
					//loop through each line in the List "weldLines"
					foreach (Line line in weldLines)
					{
						if (firstLine) //if it's the first line in the weldLine list, make it the source line.
						{
							sourceLine = line;
							firstLine = false; //no longer the first line.
						}
						else if (!firstLine)
						{

							Entity srcLine = TR.GetObject(sourceLine.Id, OpenMode.ForRead) as Entity;
							Entity addLine = TR.GetObject(line.Id, OpenMode.ForRead) as Entity;

							srcLine.UpgradeOpen();
							srcLine.JoinEntity(addLine);

							addLine.UpgradeOpen();
							addLine.Erase();
						}
						

					}

					TR.Commit();

				}
			} catch (System.Exception ex)
			{
				ed.WriteMessage(ex.Message + "\n");
			}


		}

When running this, i'm getting errors such as eNotApplicable.

 

Does it have to do with the Lines having to be converted to Polylines before joining them? If so, I could not figure out how to do that either.

 

Would anyone recommend a different/better approach to this?

 

Thank you in advance.

0 Likes
7,500 Views
10 Replies
Replies (10)
Message 2 of 11

_gile
Consultant
Consultant

Hi

 

May be you can get some inspiration from this message. It is using the GeometryExtension library posted in the same thread.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 11

ActivistInvestor
Mentor
Mentor

The JoinEntity() method uses protocol extensions that allow each type of object to define what 'join' means.

 

When you try to Join one line with another, the result will be another line, not a polyline, and of course, that requires that the two lines are colinear. They can be overlapping or disjoint, but they must be collinear.

 

To join one or more lines and/or arcs into a polyline, you have to start with a polyline, not a line. So, convert the first line to a polyline, and join the remaining lines to it.

 

See this post for code to convert a line or arc to a polyline.

Message 4 of 11

FRFR1426
Collaborator
Collaborator

I advise you to read the documentation of the Entity.JoinEntities method in order to better understand how it works.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 5 of 11

ActivistInvestor
Mentor
Mentor

@FRFR1426 wrote:

I advise you to read the documentation of the Entity.JoinEntities method in order to better understand how it works.


Thanks.

 

I've had to add the AcDbJoinEntityPE protocol to a quite a few custom object classes in the past, so I'm reasonably familiar with how it works.

0 Likes
Message 6 of 11

aimad_akourim_96
Explorer
Explorer

This looks rather complex to implement in .NET. What if we call a Lisp routine (which can handle it easily and very quickly) using the Invoke method?

0 Likes
Message 7 of 11

ActivistInvestor
Mentor
Mentor

How can LISP handle this more easily and quickly than the managed api? 

0 Likes
Message 8 of 11

FRFR1426
Collaborator
Collaborator

 

[CommandMethod("JOIN")]
public static void Join()
{
    Document document = Application.DocumentManager!.MdiActiveDocument!;
    var filter = new SelectionFilter([new TypedValue(0, "LINE")]);
    var options = new PromptSelectionOptions
    {
        MessageForAdding = "Select lines to join:"
    };
    PromptSelectionResult result = document.Editor!.GetSelection(options, filter)!;
    if (result.Status == PromptStatus.Cancel) return;
    if (result.Value!.Count == 0) return;
    using Transaction transaction = document.TransactionManager!.StartTransaction()!;
    var lines = new List<Entity>();
    foreach (SelectedObject selectedObject in result.Value!)
    {
        var line = (Line)transaction.GetObject(selectedObject.ObjectId, OpenMode.ForRead)!;
        lines.Add(line);
    }
    if (lines.Count > 0)
    {
        var polyline = new Polyline();
        var currentSpace = (BlockTableRecord)transaction.GetObject(document.Database!.CurrentSpaceId, OpenMode.ForWrite)!;
        currentSpace.AppendEntity(polyline);
        transaction.AddNewlyCreatedDBObject(polyline, add: true);
        // Need at least one vertex for JoinEntities, if not, it will throw an exception eNotApplicable
        var line = (Line)lines[0]!;
        polyline.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
        polyline.JoinEntities(lines.ToArray());
    }
    transaction.Commit();
}

The lines will stay there, so if you don't need them anymore, you have to delete them.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 9 of 11

FRFR1426
Collaborator
Collaborator

My response was directed at the OP, not you. I'm sure you know how it works. But sometimes, people don't read the documentation carefully or interpret it incorrectly.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 10 of 11

ActivistInvestor
Mentor
Mentor

No problem, however it was in reply to my post:

 

FRFR1426

in reply to ActivistInvestor

‎08-23-2017 10:05 AM

0 Likes
Message 11 of 11

engrjvb
Advocate
Advocate

engrjvb_0-1760059610068.png

Check this documentation. It's better to use polylines instead.

Automation is to your time as compound interest is to your money
0 Likes