这几天需要一个选择实体的触发事件,找啊试啊终于完成,分享下:
使用Addselectchang加载
使用Removeselectchang卸载
-
- using System;
- using Autodesk..Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.EditorInput;
- [assembly: CommandClass(typeof(Sample.ObjectErasedEvent))]
- namespace Sample
- {
- public class ObjectErasedEvent
- {
- Autodesk.AutoCAD.EditorInput.Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- [CommandMethod("Addselectchang")]
- public void AddDocEvent()
- {
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- acDoc.ImpliedSelectionChanged += new EventHandler(doc_ImpliedSelectionChanged);
- }
- [CommandMethod("Removeselectchang")]
- public void RemoveDocEvent()
- {
- // Get the current document
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- acDoc.ImpliedSelectionChanged -= new EventHandler(doc_ImpliedSelectionChanged);
- }
- public void doc_ImpliedSelectionChanged(object sender, EventArgs e)
- {
- PromptSelectionResult pkf = ed.SelectImplied();
- if (pkf.Status != PromptStatus.OK) return;
- ObjectId[] objIds = pkf.Value.GetObjectIds();
- String oids = "";
- foreach (ObjectId objId in objIds)
- {
- oids += "\n " + objId.ToString();
- }
- Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\n选择的对像ObjectId为:" +
- oids + "\n共选择了对像个数是:" + objIds.Length.ToString());
- }
- }
- }
处理Document事件文档事件对应AutoCAD中的文档窗口,文档事件只和它关联的文档建立联系,因此如果文档事件需要在每个文档中注册,必须使用DocumentCollection对象的DocumentCreated事件来注册对每一个打开或创建的文档进行事件的注册。下面是一些常见的事件及触发事件的说明
事件
说明
BeginDocumentClose
当收到关闭文档的请求时触发
CloseAborted
当试图放弃关闭文档时触发
CloseWillStart
当BeginDocumentClose已被触发,并在文档关闭前触发
CommandCancelled
命令在执行完之前被取消后触发
CommandEnded
命令执行完后触发
CommandFailed
命令不是由于取消而执行失败后触发
CommandWillStart
命令被提交,但在执行前触发
ImpliedSelectionChanged
当前向选择集(pickfirst selection set)发生改变后触发
UnknownCommand
当命令行中收到不可知命令后触发 |