Hello,everyone.I want to create a command to select all the lines automatically,without demanding users to drag the cursor to select.The code below is all I can get by my own.Can anyone help me improve it ?
[CommandMethod("AllLines")]
public void seletcAllLines()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
//define the criteria for selecting lines only
TypedValue[] tv = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LINE") };
SelectionFilter flt = new SelectionFilter(tv);
/*This method needs to drag the cursor,which I think is not smart.
I will want to improve it.*/
PromptSelectionOptions optSel = new PromptSelectionOptions();
optSel.MessageForAdding = "You will need to drag this cursor to select.";
PromptSelectionResult resSel = ed.GetSelection(optSel,flt);
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") //Is there any better ideal to write the condition,the"typeof(ent) is Line" just gets an error!
{
Line myLine = (Line)trans.GetObject(sSetEntId, OpenMode.ForWrite);
ed.WriteMessage("\n"+myLine.StartPoint.ToString()+ myLine.EndPoint.ToString());
}
}
trans.Commit();
}
catch(System.Exception ex)
{
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
}