david.xw 发表于 2011-8-2 09:37:00

AUTOCAD 选择对像后触发事件

这几天需要一个选择实体的触发事件,找啊试啊终于完成,分享下:
使用Addselectchang加载
使用Removeselectchang卸载
using System;
using Autodesk..Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;

namespace Sample
{
    public class ObjectErasedEvent
    {
      Autodesk.AutoCAD.EditorInput.Editored = Application.DocumentManager.MdiActiveDocument.Editor;
      
      public void AddDocEvent()
      {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            acDoc.ImpliedSelectionChanged += new EventHandler(doc_ImpliedSelectionChanged);
      }
      
      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
当命令行中收到不可知命令后触发

sieben 发表于 2011-8-2 10:29:00

学习了,第一次知道有这个事件.

shouhua000 发表于 2011-9-16 17:21:00

学习学习,正在研究这方面的东西

zhaiyake 发表于 2011-12-17 16:34:00

这些事件在哪里啊,我怎么没有发现,我用的是2006版本的cad

sieben 发表于 2011-12-17 18:25:00

较新的版本才会有,应该是2008之后版本才有

icefire 发表于 2012-1-11 10:56:00

学习了,正在进步中

游天居士 发表于 2012-10-31 16:41:00

DocumentCollection对象的DocumentCreated事件来注册对每一个打开或创建的文档进行事件的注册。
代码没有发全啊。

易晨托 发表于 2013-11-19 10:55:00

如何在DocumentCollection找到我要的文档啊。。。

cdinten 发表于 2014-1-16 20:36:00

AutoCAD里面的事件很麻烦啊

sdaulj 发表于 2014-5-6 23:01:00

学习了,感谢!
页: [1]
查看完整版本: AUTOCAD 选择对像后触发事件