Message 1 of 11
Joining Line Objects into a Single Polyline
Not applicable
08-21-2017
07:56 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.