Now let’s see how to automatically create all the code introduced in the previous posts with the assistance of the RevitAddinWizard in a few seconds.
The following wizard pages will guide you through setting proper options to create document even handlers, subscriber, and registration code at your will:
The resultant document event handlers (in source DocEventHandlersTest.cs) look like:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCSProject
{
public class DocEventHandlersTest
{
public static void DocEvent_DocumentClosing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentClosing");
}
public static void DocEvent_DocumentPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinted");
}
public static void DocEvent_DocumentPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinting");
}
public static void DocEvent_DocumentSaved_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaved");
}
public static void DocEvent_DocumentSavedAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavedAs");
}
public static void DocEvent_DocumentSaving_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaving");
}
public static void DocEvent_DocumentSavingAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavingAs");
}
public static void DocEvent_ViewPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinted");
}
public static void DocEvent_ViewPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinting");
}
}
}
The resultant document event subscriber (DocEventSubscriber.cs) looks like:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCSProject
{
public class DocEventSubscriber
{
private UIControlledApplication _uiCtrApp;
private static RvtApplication _app;
private static bool _registered;
public DocEventSubscriber(UIControlledApplication app)
{
_uiCtrApp = app;
_registered = false;
_uiCtrApp.ViewActivated += UIAppEvent_ViewActivated_Handler;
_uiCtrApp.ApplicationClosing += UIAppEvent_ApplicationClosing_Handler;
}
public static void SubscribeForApplication(RvtApplication app)
{
app.DocumentOpened += AppEvent_DocumentOpened_Handler;
app.DocumentCreated += AppEvent_DocumentCreated_Handler;
app.DocumentClosing += AppEvent_DocumentClosing_Handler;
}
public static void UnsubscribeForApplication(RvtApplication app)
{
foreach (RvtDocument doc in app.Documents)
{
UnsubscribeFromDoc(doc);
}
app.DocumentOpened -= AppEvent_DocumentOpened_Handler;
app.DocumentCreated -= AppEvent_DocumentCreated_Handler;
app.DocumentClosing -= AppEvent_DocumentClosing_Handler;
}
public static void SubscribeToDoc(RvtDocument doc)
{
doc.DocumentClosing += DocEventHandlersTest.DocEvent_DocumentClosing_Handler;
doc.DocumentPrinted += DocEventHandlersTest.DocEvent_DocumentPrinted_Handler;
doc.DocumentPrinting += DocEventHandlersTest.DocEvent_DocumentPrinting_Handler;
doc.DocumentSaved += DocEventHandlersTest.DocEvent_DocumentSaved_Handler;
doc.DocumentSavedAs += DocEventHandlersTest.DocEvent_DocumentSavedAs_Handler;
doc.DocumentSaving += DocEventHandlersTest.DocEvent_DocumentSaving_Handler;
doc.DocumentSavingAs += DocEventHandlersTest.DocEvent_DocumentSavingAs_Handler;
doc.ViewPrinted += DocEventHandlersTest.DocEvent_ViewPrinted_Handler;
doc.ViewPrinting += DocEventHandlersTest.DocEvent_ViewPrinting_Handler;
}
public static void UnsubscribeFromDoc(RvtDocument doc)
{
doc.DocumentClosing -= DocEventHandlersTest.DocEvent_DocumentClosing_Handler;
doc.DocumentPrinted -= DocEventHandlersTest.DocEvent_DocumentPrinted_Handler;
doc.DocumentPrinting -= DocEventHandlersTest.DocEvent_DocumentPrinting_Handler;
doc.DocumentSaved -= DocEventHandlersTest.DocEvent_DocumentSaved_Handler;
doc.DocumentSavedAs -= DocEventHandlersTest.DocEvent_DocumentSavedAs_Handler;
doc.DocumentSaving -= DocEventHandlersTest.DocEvent_DocumentSaving_Handler;
doc.DocumentSavingAs -= DocEventHandlersTest.DocEvent_DocumentSavingAs_Handler;
doc.ViewPrinted -= DocEventHandlersTest.DocEvent_ViewPrinted_Handler;
doc.ViewPrinting -= DocEventHandlersTest.DocEvent_ViewPrinting_Handler;
}
public static void UIAppEvent_ViewActivated_Handler(Object sender, EventArgs args)
{
ViewActivatedEventArgs specArgs = args as ViewActivatedEventArgs;
if (!_registered) // Only subscribing once!
{
_app = specArgs.Document.Application;
SubscribeForApplication(_app);
_registered = true;
}
}
public static void UIAppEvent_ApplicationClosing_Handler(Object sender, EventArgs args)
{
ApplicationClosingEventArgs specArgs = args as ApplicationClosingEventArgs;
if (_app != null && _registered)
{
UnsubscribeForApplication(_app);
_registered = false;
}
}
public static void AppEvent_DocumentCreated_Handler(Object sender, EventArgs args)
{
DocumentCreatedEventArgs specArgs = args as DocumentCreatedEventArgs;
SubscribeToDoc(specArgs.Document);
}
public static void AppEvent_DocumentOpened_Handler(Object sender, EventArgs args)
{
DocumentOpenedEventArgs specArgs = args as DocumentOpenedEventArgs;
SubscribeToDoc(specArgs.Document);
}
public static void AppEvent_DocumentClosing_Handler(Object sender, EventArgs args)
{
DocumentClosingEventArgs specArgs = args as DocumentClosingEventArgs;
UnsubscribeFromDoc(specArgs.Document);
}
}
}
The registration code in the OnStartup() of the chosen external application:
public Result OnStartup(UIControlledApplication uiApp)
{
_cachedUiCtrApp = uiApp;
try
{
RibbonPanel ribbonPanel = CreateRibbonPanel();
//…
DocEventSubscriber docEventScrscipber = new DocEventSubscriber(_cachedUiCtrApp); //Subscribe to all documents in the whole Revit session.
return Result.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show( ex.ToString() );
return Result.Failed;
}
}
Give the RevitAddinWizard a try and you will get all these in a moment.
Links to some related articles:
Manage Revit Application Events of Revit API
Use RevitAddinWizard to Add Revit Application Event Handlers of Revit API
Implement Revit FailuresProcessing Event Hanlders of Revit API
Use RevitAddinWizard to Implement Revit FailuresProcessing Event Hanlders of Revit API
Manage Revit UIApplication Events
Manage Revit Document Events of Revit API - 3 Document Event Handler of RevitAddinWizard
Recent Comments