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

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 807|回复: 0
打印 上一主题 下一主题

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

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12404

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 15:32:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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:

  1. using Autodesk.AutoCAD;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.EditorInput;
  6. namespace SelectionTest
  7. {
  8. public class PickfirstTestCmds
  9. {
  10. // Must have UsePickSet specified
  11. [CommandMethod("PFT", CommandFlags.UsePickSet |
  12.                         CommandFlags.Redraw |
  13.                         CommandFlags.Modal)
  14. static public void PickFirstTest()
  15. {
  16.    Document doc =
  17.       Application.DocumentManager.MdiActiveDocument;
  18.    Editor ed = doc.Editor;
  19.    try
  20.    {
  21.       PromptSelectionResult selectionRes =
  22.       ed.SelectImplied();
  23.       //如果没用可用的命令选择集
  24.       // If there's no pickfirst set available...
  25.       if (selectionRes.Status == PromptStatus.Error)
  26.       {
  27.       // ... ask the user to select entities
  28.       PromptSelectionOptions selectionOpts =
  29.          new PromptSelectionOptions();
  30.       selectionOpts.MessageForAdding =
  31.          "\nSelect objects to list: ";
  32.       selectionRes =
  33.          ed.GetSelection(selectionOpts);
  34.       }
  35.       else
  36.       {
  37.       // If there was a pickfirst set, clear it
  38.       ed.SetImpliedSelection(new ObjectId[0]);
  39.       }
  40.       // If the user has not cancelled...
  41.       if (selectionRes.Status == PromptStatus.OK)
  42.       {
  43.       // ... take the selected objects one by one
  44.       Transaction tr =
  45.          doc.TransactionManager.StartTransaction();
  46.       try
  47.       {
  48.          ObjectId[] objIds = selectionRes.Value.GetObjectIds();
  49.          foreach (ObjectId objId in objIds)
  50.          {
  51.             Entity ent =
  52.             (Entity)tr.GetObject(objId, OpenMode.ForRead);
  53.             // In this simple case, just dump their properties
  54.             // to the command-line using list
  55.             ent.List();
  56.             ent.Dispose();
  57.          }
  58.          // Although no changes were made, use Commit()
  59.          // as this is much quicker than rolling back
  60.          tr.Commit();
  61.       }
  62.       catch (Autodesk.AutoCAD.Runtime.Exception ex)
  63.       {
  64.          ed.WriteMessage(ex.Message);
  65.          tr.Abort();
  66.       }
  67.       }
  68.    }
  69.    catch(Autodesk.AutoCAD.Runtime.Exception ex)
  70.    {
  71.       ed.WriteMessage(ex.Message);
  72.    }
  73. }
  74. }
  75. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-16 11:09

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表