【问题标题】:Revit Synchronization eventRevit 同步事件
【发布时间】:2019-06-28 10:03:49
【问题描述】:

从这个开始... https://github.com/jeremytammik/RevitLookup/blob/master/CS/EventTrack/Events/ApplicationEvents.cs

我正在尝试为同步事件添加事件侦听器。下面的代码会抛出一个错误,指出 m_app 为空。 Revit 启动时我可以不订阅此事件吗?

我之前可以通过 application.ViewActivated += .... 做到这一点。我想知道这是否与 DB 与 UI 驱动的事件有关,以及何时允许订阅它们?我只是不知道。

  namespace RevitModelHealth
{
    public class checkHealth : IExternalApplication
    {
        // Document doc;
        static public Autodesk.Revit.ApplicationServices.Application m_app = null;

        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {

            m_app.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral);
            return Result.Succeeded;
        }

        void m_app_DocumentSavingToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs e)
        {
            MessageBox.Show("asd","asd");
        }

    }
}

这里是更新的代码,反映了我对第一个答案的回应。加载文档时会打开消息框。当我尝试初始化同步事件处理程序时没有抛出任何错误,但是,在同步事件之前或之后都没有打开任何消息框。

  public class checkHealth : IExternalApplication
    {
        // Document doc;
        static public Autodesk.Revit.ApplicationServices.Application m_app;


        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            application.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(app_DocOpened);
            return Result.Succeeded;
        }

        public void app_DocOpened(object sender, DocumentOpenedEventArgs args)
        {
            MessageBox.Show("asd","asd");

            m_app.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral);
            m_app.DocumentSynchronizedWithCentral += new EventHandler<Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs>(m_app_DocumentSavedToCentral);
        }

        void m_app_DocumentSavingToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs e)
        {
            MessageBox.Show("sync", "sync");
        }

        void m_app_DocumentSavedToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs e)
        {
            MessageBox.Show("Doone", "Done");
        }
    }

这行得通....在很大程度上要感谢 SDK 示例项目 EventsMonitor

namespace RevitModelHealth
{
    public class checkHealth : IExternalApplication
    {


        public Result OnShutdown(UIControlledApplication application)
        {
            return Result.Succeeded;
        }

        public Result OnStartup(UIControlledApplication application)
        {
            application.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(app_syncStart);
            application.ControlledApplication.DocumentSynchronizedWithCentral += new EventHandler<DocumentSynchronizedWithCentralEventArgs>(app_syncOver);
            return Result.Succeeded;
        }

        public void app_syncStart(object o ,DocumentSynchronizingWithCentralEventArgs args)
        {
            MessageBox.Show("","Stasrting");
        }

        public void app_syncOver(object o,DocumentSynchronizedWithCentralEventArgs args)
        {
            MessageBox.Show("", "Over");
        }

    }

}

【问题讨论】:

    标签: api synchronization revit


    【解决方案1】:

    试试

    application.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral)
    

    在您的 OnStartup() 方法中。

    调用失败,因为实例成员 m_app 被初始化为 null。

    引发DocumentSynchronizingWithCentralEventArgsUIApplication.ControlledApplication 对象可从参数访问到 OnStartup。

    【讨论】:

    • 谢谢。但是事件不在 UIControlledApplication 空间中,而是在 ApplicationServices.Application 空间中。我可以注册一个“DocumentOpened”事件并从该函数创建新的事件处理程序,没有例外,但是,当我同步时,该事件不会触发。我将更新代码以说明...
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2020-06-19
    • 2021-05-15
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多