Here is a sample code that uses the "Entity.JoinEntity" method to demonstrate joining of 2d/3d polylines
[CommandMethod("joinPline")]
static public void joinPolylines()
{
Document document =
Application.DocumentManager.MdiActiveDocument;
Editor ed = document.Editor;
Database db = document.Database;
PromptEntityOptions peo1
= new PromptEntityOptions("\nSelect source polyline : ");
peo1.SetRejectMessage("\nInvalid selection...");
peo1.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline),
true
);
peo1.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline2d),
true
);
peo1.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline3d),
true
);
PromptEntityResult pEntrs = ed.GetEntity(peo1);
if (PromptStatus.OK != pEntrs.Status)
return;
ObjectId srcId = pEntrs.ObjectId;
PromptEntityOptions peo2
= new PromptEntityOptions("\nSelect polyline to join : ");
peo2.SetRejectMessage("\nInvalid selection...");
peo2.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline),
true
);
peo2.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline2d),
true
);
peo2.AddAllowedClass
(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline3d),
true
);
pEntrs = ed.GetEntity(peo2);
if (PromptStatus.OK != pEntrs.Status)
return;
ObjectId joinId = pEntrs.ObjectId;
try
{
using (Transaction trans
= db.TransactionManager.StartTransaction())
{
Entity srcPLine
= trans.GetObject(
srcId,
OpenMode.ForRead
) as Entity;
Entity addPLine
= trans.GetObject(
joinId,
OpenMode.ForRead
) as Entity;
srcPLine.UpgradeOpen();
srcPLine.JoinEntity(addPLine);
addPLine.UpgradeOpen();
addPLine.Erase();
trans.Commit();
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
