EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

萧闫子 发表于 2014-1-8 15:32:21

[资料] AutoCAD .NET应用中使用“命令前选择集”<selection>



Using the pickfirst selection from an AutoCAD .NET applicationAutoCAD .NET应用中使用“命令前选择”

许多基于选择实体的AutoCAD命令支持两种工作方式:"选择-命令"或"命令-选择"。"选择-命令",即如果用户执行命令前已经选择一系列实体,运行程序后将不需要再进行选择。
这可以通过AutoCAD中称为pickfist的类或虚选择集来实现. 要在你的命令中使用这个特性,你首先要使用一个叫做“UsePickSet”的命令标志符:通过它告诉AutoCAD编辑器执行命令时不要将之前的选择清除. 然后命令执行时对"待编辑实体"执行SelectImplied()方法,对命令前选择集进行获取以判断选择集是否有效. 此时也可以使用SetImpliedSelection()清除"命令前选择集".
This is enabled using something called the pickfirst or implied selection set inside AutoCAD.To take advantage of this feature inside your commands the first thing to do is to use a special command-flag called "UsePickSet": this tells the AutoCAD editor not to clear the pickfirst set when the command is invoked.Next the command implementation will use the SelectImplied() method on the editor object to retrieve the pickfirst set, should one be available.At this stage it's also good practice to clear the pickfirst set with SetImpliedSelection().
下面的c#代码
The following C# sample shows a few extras, such as how to fall back on asking the user to select the objects if there isn't a pickfirst set. As for what we do with the selected entities... in this simple example each entity is opened for read and has its list() method called to dump out its contents to the command-line (just as the LIST command does). You could, of course, do something much more interesting with the object at this point (I'll try to show more examples of getting specific entity properties in a later post).
Here's the code:

using Autodesk.AutoCAD;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace SelectionTest
{
public class PickfirstTestCmds
{
// Must have UsePickSet specified
[CommandMethod("PFT", CommandFlags.UsePickSet |
                        CommandFlags.Redraw |
                        CommandFlags.Modal)
static public void PickFirstTest()
{
   Document doc =
      Application.DocumentManager.MdiActiveDocument;
   Editor ed = doc.Editor;
   try
   {
      PromptSelectionResult selectionRes =
      ed.SelectImplied();
      //如果没用可用的命令选择集
      // If there's no pickfirst set available...
      if (selectionRes.Status == PromptStatus.Error)
      {
      // ... ask the user to select entities
      PromptSelectionOptions selectionOpts =
         new PromptSelectionOptions();
      selectionOpts.MessageForAdding =
         "\nSelect objects to list: ";
      selectionRes =
         ed.GetSelection(selectionOpts);
      }
      else
      {
      // If there was a pickfirst set, clear it
      ed.SetImpliedSelection(new ObjectId);
      }
      // If the user has not cancelled...
      if (selectionRes.Status == PromptStatus.OK)
      {
      // ... take the selected objects one by one
      Transaction tr =
         doc.TransactionManager.StartTransaction();
      try
      {
         ObjectId[] objIds = selectionRes.Value.GetObjectIds();
         foreach (ObjectId objId in objIds)
         {
            Entity ent =
            (Entity)tr.GetObject(objId, OpenMode.ForRead);
            // In this simple case, just dump their properties
            // to the command-line using list
            ent.List();
            ent.Dispose();
         }
         // Although no changes were made, use Commit()
         // as this is much quicker than rolling back
         tr.Commit();
      }
      catch (Autodesk.AutoCAD.Runtime.Exception ex)
      {
         ed.WriteMessage(ex.Message);
         tr.Abort();
      }
      }
   }
   catch(Autodesk.AutoCAD.Runtime.Exception ex)
   {
      ed.WriteMessage(ex.Message);
   }
}
}
}


页: [1]
查看完整版本: [资料] AutoCAD .NET应用中使用“命令前选择集”<selection>