明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 11195|回复: 15

[事件] AUTOCAD 选择对像后触发事件

  [复制链接]
发表于 2011-8-2 09:37 | 显示全部楼层 |阅读模式
这几天需要一个选择实体的触发事件,找啊试啊终于完成,分享下:

使用Addselectchang加载
使用Removeselectchang卸载

  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.ApplicationServices;
  5. using Autodesk.AutoCAD.EditorInput;

  6. [assembly: CommandClass(typeof(Sample.ObjectErasedEvent))]

  7. namespace Sample
  8. {
  9.     public class ObjectErasedEvent
  10.     {
  11.         Autodesk.AutoCAD.EditorInput.Editor  ed = Application.DocumentManager.MdiActiveDocument.Editor;

  12.         [CommandMethod("Addselectchang")]
  13.         public void AddDocEvent()
  14.         {
  15.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  16.             acDoc.ImpliedSelectionChanged += new EventHandler(doc_ImpliedSelectionChanged);
  17.         }

  18.         [CommandMethod("Removeselectchang")]
  19.         public void RemoveDocEvent()
  20.         {
  21.             // Get the current document
  22.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  23.             acDoc.ImpliedSelectionChanged -= new EventHandler(doc_ImpliedSelectionChanged);
  24.         }

  25.         public void doc_ImpliedSelectionChanged(object sender, EventArgs e)
  26.         {
  27.             PromptSelectionResult pkf = ed.SelectImplied();
  28.             if (pkf.Status != PromptStatus.OK) return;
  29.             ObjectId[] objIds = pkf.Value.GetObjectIds();
  30.             String oids = "";

  31.             foreach (ObjectId objId in objIds)
  32.             {
  33.                 oids += "\n " + objId.ToString();               
  34.             }

  35.             Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\n选择的对像ObjectId为:" +
  36. oids + "\n共选择了对像个数是:" + objIds.Length.ToString());
  37.         }
  38.     }
  39. }
处理Document事件
文档事件对应AutoCAD中的文档窗口,文档事件只和它关联的文档建立联系,因此如果文档事件需要在每个文档中注册,必须使用DocumentCollection对象的DocumentCreated事件来注册对每一个打开或创建的文档进行事件的注册。
下面是一些常见的事件及触发事件的说明

事件


说明


BeginDocumentClose


当收到关闭文档的请求时触发


CloseAborted


当试图放弃关闭文档时触发


CloseWillStart


当BeginDocumentClose已被触发,并在文档关闭前触发


CommandCancelled


命令在执行完之前被取消后触发


CommandEnded


命令执行完后触发


CommandFailed


命令不是由于取消而执行失败后触发


CommandWillStart


命令被提交,但在执行前触发


ImpliedSelectionChanged


当前向选择集(pickfirst selection set)发生改变后触发


UnknownCommand


当命令行中收到不可知命令后触发


发表于 2011-8-2 10:29 | 显示全部楼层
学习了,第一次知道有这个事件.
发表于 2011-9-16 17:21 | 显示全部楼层
学习学习,正在研究这方面的东西
发表于 2011-12-17 16:34 | 显示全部楼层
这些事件在哪里啊,我怎么没有发现,我用的是2006版本的cad
发表于 2011-12-17 18:25 | 显示全部楼层
较新的版本才会有,应该是2008之后版本才有
发表于 2012-1-11 10:56 | 显示全部楼层
学习了,正在进步中
发表于 2012-10-31 16:41 | 显示全部楼层
DocumentCollection对象的DocumentCreated事件来注册对每一个打开或创建的文档进行事件的注册。
代码没有发全啊。
发表于 2013-11-19 10:55 | 显示全部楼层
如何在DocumentCollection找到我要的文档啊。。。
发表于 2014-1-16 20:36 | 显示全部楼层
AutoCAD里面的事件很麻烦啊
发表于 2014-5-6 23:01 | 显示全部楼层
学习了,感谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 20:04 , Processed in 0.185824 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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