CAD.NET 选择集操作

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);
            }
        }

    }
}

你可能感兴趣的:(AutoCAD,.NET开发)