如何不用拖选就能把所有的直线选中
本帖最后由 介之推 于 2011-8-12 12:40 编辑大家好,通过定义选择集来选择时需要在窗口上用鼠标拖动光标来选择,有没有不用拖选就能直接选中所有的直线?下面的代码需要手动选才行。
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*/
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") //请问有更好的判断方法吗?typeof(ent) is Line在这里出错。
{
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);
}
}
}
试试直接用ObjectId——
foreach (ObjectId sSetEntId in ids)
{
//Entity ent = (Entity)trans.GetObject(sSetEntId, OpenMode.ForWrite);
if (sSetEntId.ObjectClass.DFXName == "LINE")
{
Line myLine = (Line)trans.GetObject(sSetEntId, OpenMode.ForWrite);
ed.WriteMessage("\n"+myLine.StartPoint.ToString()+ myLine.EndPoint.ToString());
}
}
遍历数据库
if (ent.GetType().Name=="Line") //请问有更好的判断方法吗?typeof(ent) is Line在这里出错
1,ent is Line
2, ent.GetType(). == typeof(Line)
3,public Autodesk.AutoCAD.EditorInput.PromptSelectionResult SelectAll(Autodesk.AutoCAD.EditorInput.SelectionFilter filter)
Autodesk.AutoCAD.EditorInput.Editor 的成员 vs2008的话试试linq吧 TypedValue[] tv = new TypedValue[] { new TypedValue(0, "LINE") };
有了这句话,不可能选中非line对象的
页:
[1]