Revit同步事件

时间:2019-02-04 16:05:45

标签: api synchronization revit

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

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

我以前可以通过application.ViewActivated += ....来做到这一点。我想知道这是否与DB vs 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");
        }

    }

}

1 个答案:

答案 0 :(得分:0)

尝试

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

在您的OnStartup()方法中。

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

可以通过OnStartup的参数访问引发UIApplication.ControlledApplicationDocumentSynchronizingWithCentralEventArgs对象。