Revit二次开发入门:第九章事件
1.事件简介
-
事件是什么
事件是Revit界面或API执行过程中触发的特定的动作
-
事件和委托的区别
事件是一种特殊的委托,或者说是受限制的委托,是委托一种特殊应用,只能施加+=,-+操作符。使用中,委托常用来表达回调,事件表达外发的接口
-
如何订阅事件
步骤一:需要一个函数在得到事件通知后进行响应和处理
步骤二:使用之前定义的处理函数在Revit中注册该事件
-
如何注销事件
如果实在IExternalApplication.OnStartup()函数中注册的时间,通常建议在IExternalApplication.OnShutdown()函数中注销。
-
如何触发事件
通过注册事件,一个插件程序能在某个动作即将发生或刚发生后得到通知,然后根据这个动作采取行动。某些时间是成对的,一个触发在某个动作发生前(Pro-event),另一个则触发在这个动作发生后(Post-event)。
-
预备事件Pre-Event/事后事件Post-Event
- DocumentClosing
- DocumentClosed
-
级别
- 界面UI级别
- 应用Application级别
- 文档Document级别
-
事件也分为
- 数据库(DB)事件
- 界面(UI)事件
-
一些事件是只读的,这意味着在执行这些事件的时候,模型是无法被修改的。
-
一些常规事件(非只读)在执行时也有可能无法修改模型,这是因为该模型处在一个无法被修改的状态。
-
可以通过属性Document.IsModfiable和Document.IsReadOnly来检查文档是否可以被修改。
2.界面级别事件
UIApplication/UICntrolledApplication
- ApplicationClosing
- DialogBoxClosing
- DisplayingOptionsDialog
- ViewActivated/ViewActivating
- DockableFrameFocusChanged
- DockableFrameVisibilityChanged
- Idling
对话框显示:UIControlledApplication.DialogBoxShowing
订阅:application.DialogBoxShowing+=application_DialogBoxShowing;
处理事件:
void application_DialogBoxShowing(Object sender,DialogBoxShowingEventArgs e){
var tdse=e as TaskDialogShowingEventArgs;
}
闲置:UIControlledApplication.Idling;
订阅:application.Idling+=application_Idling;
处理事件:
void application_DialogBoxShowing(Object sender,IdlingEventArgs e){
var uiapp=sender as UIApplication;
}
3.应用级别事件
Application/ControlledApplication:
- ApplicationInitialized
- DocumentChanged
- DocumentClosed/Documentclosing
- DocumentOpend/DocumentOpening
- DocumentPrinted/DocumentPrinting
- DocumentSaved/DocumentSaving
- DocumentSynchronizedWithCentral/DocumentSynchronizingWithCentral
- FamilyLoadedIntoDocument/FamilyLoadingIntoDocument
- FiledImport/FileImport/FileExported/FileExporting
- ProgressChanged
- FailuresProcessing
DocumentClosing:当Revit要关闭一个文档前触发:
public event EventHandler<DocumentClosingEventArgs> DocumentClosing;
ControlledApp.DocumentClosing+=ControlledApp_DocumentClosing;
void ControlledApp_DocumentClosing(object sender, DocumentClosingEventArgs e)
{
Document doc = e.GetDocument();
}
DocumentChanged:当事务被提交、撤销或重做的时候触发,表示文档被修改了:
public event EventHandler<DocumentClosingEventArgs> DocumentChanged;
ControlledApp.DocumentChanged+=ControlledApp_DocumentChanged;
void ControlledApp_DocumentChanged(object sender, DocumentClosingEventArgs e) {
Document doc = e.GetDocument();
ICollection<ElementId> added = e.GetAddedElementIds();
ICollection<ElementId> deleted = e.GetDeletedElementIds();
ICollection<ElementId> modified = e.GetModifiedElementIds();
}
4.文档级别事件
Document:
- DocumentClosing
- DocumentPrinted/DocumentPrinting
- DocumentSaved/DocumentSaving
- ViewPrinted/ViewPrinting
5.外部事件
6.DMU事件
7.实例练习
- 应用级别:所有文档修改都会弹出对话框
Application app = commandData.Application.Application;
app.DocumentChanged += appChange;
private void appChange(object sender, DocumentChangedEventArgs e)
{
TaskDialog.Show("ok", "已改动");
}
- 文档级别:只有当前文档关闭会弹出对话框
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
doc.DocumentClosing += docClosing;
private void docClosing(object sender, DocumentClosingEventArgs e)
{
TaskDialog.Show("关闭", "窗口已关闭");
}
- 外部事件:通过点击按钮触发
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace EWpf {
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class EventTest : IExternalCommand {
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
Application app = commandData.Application.Application;
MainWindow m = new MainWindow();
m.ShowDialog();
return Result.Succeeded;
}
}
public class ExternalCommand : IExternalEventHandler {
public void Execute(UIApplication app) {
TaskDialog.Show("cs", "已确认");
}
public string GetName() {
return "名称";
}
}
}
using Autodesk.Revit.UI;
using System.Windows;
namespace EWpf {
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window {
private ExternalEvent ee = null;
private ExternalCommand cmd = null;
public MainWindow() {
InitializeComponent();
if (cmd == null) {
cmd = new ExternalCommand();
}
if (ee == null) {
ee = ExternalEvent.Create(cmd);
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
Close();
ee.Raise();
}
}
}
- 空闲事件
commandData.Application.Idling += IdingTest;
private void IdingTest(object sender, IdlingEventArgs e) {
UIApplication m_uiapp = sender as UIApplication;
Document m_doc = m_uiapp.ActiveUIDocument.Document;
Transaction tr = new Transaction(m_doc, "idling");
tr.Start();
ElementId id = new ElementId(338378);
TextNote tn = m_doc.GetElement(id) as TextNote;
string str = tn.Text;
int.TryParse(str, out int i);
tn.Text = (i + 1).ToString();
tr.Commit();
}