using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace SelectionTest
{
public class PickfirstTestCmds
{
//必须指定UsePickSet标志
**[CommandMethod("MyPickFirst", CommandFlags.UsePickSet | CommandFlags.Redraw | CommandFlags.Modal)]**
static public void MyPickFirst()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
try
{
//得到pickfirst集
PromptSelectionResult selectionRes = ed.SelectImplied();
//如果没有pickfirst集
if (selectionRes.Status == PromptStatus.Error)
{
//请求用户选择实体
PromptSelectionOptions selectionOpts = new PromptSelectionOptions();
selectionOpts.MessageForAdding = "\nSelect objects to list: ";
selectionRes = ed.GetSelection(selectionOpts);
}
else//如果有pickfirst集,清除它或者取出来用于其他
{
ed.SetImpliedSelection(new ObjectId[0]);
}
//如果用户在此时还没有取消操作
if (selectionRes.Status == PromptStatus.OK)
{
//遍历被选择的对象
Transaction tr = doc.TransactionManager.StartTransaction();
try
{
ObjectId[] objIds = selectionRes.Value.GetObjectIds();
foreach (ObjectId objId in objIds)
{
Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForRead);
//此例中,只是dump它的的属性到命令行中
ent.List();
ent.Dispose();
}
//尽管没有对实体作改变,但使用Commit(),因为这样比回滚操//作要快些
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message);
tr.Abort();
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
}
}
CAD.NET 选择集操作
最新推荐文章于 2025-10-22 11:47:43 发布
本文详细探讨了CAD.NET中选择集的操作,包括如何创建、修改和使用选择集来实现对CAD对象的选择与操作。通过实例,阐述了如何高效地管理和应用选择集,以提升CAD应用程序的交互性和功能。
1834

被折叠的 条评论
为什么被折叠?



