List
<Point3d
> foundPointLst
= new List
<Point3d
>(); List
<LineSegment3d
> lineLst
= new List
<LineSegment3d
>();
int counter = 1;
public bool select()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
TypedValue
[] tv
= new TypedValue
[] { new TypedValue
((int)DxfCode
.Start,
"LINE") }; SelectionFilter flt
= new SelectionFilter
(tv
);
PromptSelectionOptions optSel
= new PromptSelectionOptions
(); optSel.MessageForAdding = "Select the lines";
PromptSelectionResult resSel = ed.GetSelection(optSel, flt);
if (resSel.Status != PromptStatus.OK)
return false;
SelectionSet selSet = resSel.Value;
ObjectId[] ids = selSet.GetObjectIds();
foreach (ObjectId sSetEntId in ids)
{
Entity ent = (Entity)trans.GetObject(sSetEntId, OpenMode.ForWrite);
if (ent.GetType().Name == "Line")
{
Line myLine = (Line)trans.GetObject(sSetEntId, OpenMode.ForWrite);
LineSegment3d li
= new LineSegment3d
(myLine
.StartPoint, myLine
.EndPoint);
lineLst.Add(li);
foundPointLst.Add(li.StartPoint);
foundPointLst.Add(li.EndPoint);
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
return false;
}
trans.Commit();
return true;
}
}
public void eraseLine(List<LineSegment3d> lineLst, List<Point3d> pointLst)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
foreach (LineSegment3d vli in lineLst)
{
bool existStartPt = pointLst.Exists(pt =>
{
if (pt == vli.StartPoint)
return true;
return false;
});
bool existEndPt = pointLst.Exists(pt =>
{
if (pt == vli.EndPoint)
return true;
return false;
});
//if (pointLst.Contains(vli.StartPoint) && pointLst.Contains(vli.EndPoint))
if (existStartPt && existEndPt)
{
pointLst.Remove(vli.StartPoint);
pointLst.Remove(vli.EndPoint);
lineLst.Remove(vli);
vli.Dispose();
counter++;
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
return ;
}
trans.Commit();
}
}
[CommandMethod("test")]
public void testLine()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//Initialization
foundPointLst.Clear();
lineLst.Clear();
counter = 1;
//select lines and fill the lists
if (select())
{
//succes
eraseLine(lineLst, foundPointLst);
//get the counter
ed.WriteMessage("\n{0}", counter);
//output the number
ed.WriteMessage("\n line numbers: {0}", lineLst.Count);
ed.WriteMessage("\n Points numbers: {0}",foundPointLst.Count);
}
else
{
//some stuff when failed to select
}
trans.Commit();
}
}