聲明:本文爲轉載,非原創,如有侵權,請告知,本人會盡快刪除。
原文地址:http://bbs.mjtd.com/thread-88552-1-1.html
這幾天需要一個選擇實體的觸發事件,找啊試啊終於完成,分享下:
使用Addselectchang加載
using System;
using Autodesk.AutoCAD.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 | 當命令行中收到不可知命令後觸發 |