Revit二次开发入门:第九章事件

1.事件简介

2.界面级别事件

UIApplication/UICntrolledApplication

对话框显示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:

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:

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("关闭", "窗口已关闭");
}

1559618164748

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();
        }
    }
}

1559621417955

    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();
}