Change this code to your needs:
//_____________________________________________________________________//
//ads_queueexpr
[DllImport("accore.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "ads_queueexpr")]
extern static private int ads_queueexpr(byte[] command);
[CommandMethod("tJL", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void TestConnectedLines()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PromptEntityOptions pso = new PromptEntityOptions("\nPick a single line to join: ");
pso.SetRejectMessage("\nObject must be of type Line!");
pso.AddAllowedClass(typeof(Line), false);
PromptEntityResult res = ed.GetEntity(pso);
if (res.Status != PromptStatus.OK) return;
using (DocumentLock doclock = doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable( "PICKFIRST",1);
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable( "PEDITACCEPT",0);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
List<ObjectId> ids = JoinLines(btr, res.ObjectId);
ObjectId[] lines = ids.ToArray();
ed.SetImpliedSelection(lines);
PromptSelectionResult chres = ed.SelectImplied();
if (chres.Status != PromptStatus.OK)
{
ed.WriteMessage("\nNothing added in the chain!");
return;
}
else
{
ed.WriteMessage(chres.Value.Count.ToString());
}
SelectionSet newset = SelectionSet.FromObjectIds(lines);
ed.SetImpliedSelection(newset);
string handles = "";
foreach (SelectedObject selobj in newset)
{
Entity subent = tr.GetObject(selobj.ObjectId, OpenMode.ForWrite) as Entity;
string hdl=string.Format("(handent \"{0}\")",subent.Handle.ToString());
handles= handles + hdl + " ";
}
System.Text.UnicodeEncoding uEncode = new System.Text.UnicodeEncoding();
// if PEDITACCEPT is set to 1 enshort the command avoiding "_Y" argument:
ads_queueexpr(uEncode.GetBytes("(COMMAND \"_.PEDIT\" \"_M\"" + handles + "\"\"" + "\"_Y\" \"_J\" \"\" \"\")"));
tr.Commit();
}
}
}
private void SelectConnectedLines(BlockTableRecord btr, List<ObjectId> ids, ObjectId id)
{
Entity en = id.GetObject(OpenMode.ForRead, false) as Entity;
Line ln = en as Line;
if (ln != null)
foreach (ObjectId idx in btr)
{
Entity ex = idx.GetObject(OpenMode.ForRead, false) as Entity;
Line lx = ex as Line;
if (((ln.StartPoint == lx.StartPoint) || (ln.StartPoint == lx.EndPoint)) ||
((ln.EndPoint == lx.StartPoint) || (ln.EndPoint == lx.EndPoint)))
if (!ids.Contains(idx))
{
ids.Add(idx);
SelectConnectedLines(btr, ids, idx);
}
}
}
public List<ObjectId> JoinLines(BlockTableRecord btr, ObjectId id)
{
List<ObjectId> ids = new List<ObjectId>();
SelectConnectedLines(btr, ids, id);
return ids;
}
//_____________________________________________________________________//
_____________________________________
C6309D9E0751D165D0934D0621DFF27919