We have demonstrated how to manage Revit application events and how to use Revit Addin Wizard (RevitAddinWizard) to create Revit application event handlers and register them automatically.
At that time, however, the event arguments were not differentiated. Instead, they were all EventArgs as in the following sample code:
public class AppEventHandler
{
public static void AppEvent_DocumentChanged_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentChanged");
}
public static void AppEvent_DocumentClosed_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentClosed");
}
public static void AppEvent_DocumentClosing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentClosing");
}
public static void AppEvent_DocumentCreated_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentCreated");
}
public static void AppEvent_DocumentCreating_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentCreating");
}
public static void AppEvent_DocumentOpened_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentOpened");
}
public static void AppEvent_DocumentOpening_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentOpening");
}
public static void AppEvent_DocumentPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinted");
}
public static void AppEvent_DocumentPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinting");
}
public static void AppEvent_DocumentSaved_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaved");
}
public static void AppEvent_DocumentSavedAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavedAs");
}
public static void AppEvent_DocumentSaving_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaving");
}
public static void AppEvent_DocumentSavingAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavingAs");
}
public static void AppEvent_DocumentSynchronizedWithCentral_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSynchronizedWithCentral");
}
public static void AppEvent_DocumentSynchronizingWithCentral_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSynchronizingWithCentral");
}
public static void AppEvent_FailuresProcessing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("FailuresProcessing");
}
public static void AppEvent_FileExported_Handler(Object sender, EventArgs args)
{
MessageBox.Show("FileExported");
}
public static void AppEvent_FileExporting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("FileExporting");
}
public static void AppEvent_FileImported_Handler(Object sender, EventArgs args)
{
MessageBox.Show("FileImported");
}
public static void AppEvent_FileImporting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("FileImporting");
}
public static void AppEvent_ViewPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinted");
}
public static void AppEvent_ViewPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinting");
}
}
It is not so friendly and will leave the annoying work to users, manually casting the generic EventArgs to the right Revit application event type in each handler. For example, if we want to access some specific information for the DocumentClosing event, the generic EventArgs needs to be casted to the specific DocumentClosingEventArgs. After that, it is possible to access to some information like whether the document is cancellable and/or what its title is.
Revit Addin Wizard (RevitAddinWizard) knew this issue and always kept it in mind. With the upgrading work to support Revit API 2012, the wizard engine has been enhanced dramatically too. Now all the event argument types are properly delegated.
Let’s go over the whole process again.
Select the project, and choose Add -> New Item from its context menu or the Add New Item from the Project menu, then the following dialog will appear:
Choose the Revit Addin from the Categories pane and click on the Application Event Handlers on the right Templates pane, and give a new name for the item source file or accept the default one.
The welcome page will show up:
Click the Next button to continue and the Application Events page will come out:
Please check some events of interest and click the Next button again to continue. The following Event Handlers page will show up:
In this page, the event handler class name defaulted the same as the source file holding it can be changed, handler naming convention can be specified, the External Application to register (and unregister) the event handlers can be chosen, and whether some default responses will be added to the handlers can be opted.
After the Next button is clicked one more time, the summary final page will give you a chance to review all options that have been set for the Revit Application Event Handlers, and if necessary you can go back to previous pages to make changes or even cancel the whole process.
When the Finish button is pressed, the Revit Application Event Handlers will be created automatically with some default message responses if specified.
Here is the C# version:
public class AppEventHandler
{
public static void ApplicationEvent_DocumentChanged_Handler(object sender, DocumentChangedEventArgs e)
{
MessageBox.Show("DocumentChanged");
}
public static void ApplicationEvent_DocumentClosed_Handler(object sender, DocumentClosedEventArgs e)
{
MessageBox.Show("DocumentClosed");
}
public static void ApplicationEvent_DocumentClosing_Handler(object sender, DocumentClosingEventArgs e)
{
MessageBox.Show("DocumentClosing");
}
public static void ApplicationEvent_DocumentCreated_Handler(object sender, DocumentCreatedEventArgs e)
{
MessageBox.Show("DocumentCreated");
}
public static void ApplicationEvent_DocumentCreating_Handler(object sender, DocumentCreatingEventArgs e)
{
MessageBox.Show("DocumentCreating");
}
public static void ApplicationEvent_DocumentOpened_Handler(object sender, DocumentOpenedEventArgs e)
{
MessageBox.Show("DocumentOpened");
}
public static void ApplicationEvent_DocumentOpening_Handler(object sender, DocumentOpeningEventArgs e)
{
MessageBox.Show("DocumentOpening");
}
public static void ApplicationEvent_DocumentPrinted_Handler(object sender, DocumentPrintedEventArgs e)
{
MessageBox.Show("DocumentPrinted");
}
public static void ApplicationEvent_DocumentPrinting_Handler(object sender, DocumentPrintingEventArgs e)
{
MessageBox.Show("DocumentPrinting");
}
public static void ApplicationEvent_DocumentSaved_Handler(object sender, DocumentSavedEventArgs e)
{
MessageBox.Show("DocumentSaved");
}
public static void ApplicationEvent_DocumentSavedAs_Handler(object sender, DocumentSavedAsEventArgs e)
{
MessageBox.Show("DocumentSavedAs");
}
public static void ApplicationEvent_DocumentSaving_Handler(object sender, DocumentSavingEventArgs e)
{
MessageBox.Show("DocumentSaving");
}
public static void ApplicationEvent_DocumentSavingAs_Handler(object sender, DocumentSavingAsEventArgs e)
{
MessageBox.Show("DocumentSavingAs");
}
public static void ApplicationEvent_DocumentSynchronizedWithCentral_Handler(object sender, DocumentSynchronizedWithCentralEventArgs e)
{
MessageBox.Show("DocumentSynchronizedWithCentral");
}
public static void ApplicationEvent_DocumentSynchronizingWithCentral_Handler(object sender, DocumentSynchronizingWithCentralEventArgs e)
{
MessageBox.Show("DocumentSynchronizingWithCentral");
}
public static void ApplicationEvent_FailuresProcessing_Handler(object sender, FailuresProcessingEventArgs e)
{
MessageBox.Show("FailuresProcessing");
}
public static void ApplicationEvent_FileExported_Handler(object sender, FileExportedEventArgs e)
{
MessageBox.Show("FileExported");
}
public static void ApplicationEvent_FileExporting_Handler(object sender, FileExportingEventArgs e)
{
MessageBox.Show("FileExporting");
}
public static void ApplicationEvent_FileImported_Handler(object sender, FileImportedEventArgs e)
{
MessageBox.Show("FileImported");
}
public static void ApplicationEvent_FileImporting_Handler(object sender, FileImportingEventArgs e)
{
MessageBox.Show("FileImporting");
}
public static void ApplicationEvent_ViewPrinted_Handler(object sender, ViewPrintedEventArgs e)
{
MessageBox.Show("ViewPrinted");
}
public static void ApplicationEvent_ViewPrinting_Handler(object sender, ViewPrintingEventArgs e)
{
MessageBox.Show("ViewPrinting");
}
}
Here is the VB.NET version:
Public Class AppEventHandler
Public Shared Sub ApplicationEvent_DocumentChanged_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentChangedEventArgs)
MessageBox.Show("DocumentChanged")
End Sub
Public Shared Sub ApplicationEvent_DocumentClosed_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentClosedEventArgs)
MessageBox.Show("DocumentClosed")
End Sub
Public Shared Sub ApplicationEvent_DocumentClosing_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentClosingEventArgs)
MessageBox.Show("DocumentClosing")
End Sub
Public Shared Sub ApplicationEvent_DocumentCreated_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentCreatedEventArgs)
MessageBox.Show("DocumentCreated")
End Sub
Public Shared Sub ApplicationEvent_DocumentCreating_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentCreatingEventArgs)
MessageBox.Show("DocumentCreating")
End Sub
Public Shared Sub ApplicationEvent_DocumentOpened_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentOpenedEventArgs)
MessageBox.Show("DocumentOpened")
End Sub
Public Shared Sub ApplicationEvent_DocumentOpening_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentOpeningEventArgs)
MessageBox.Show("DocumentOpening")
End Sub
Public Shared Sub ApplicationEvent_DocumentPrinted_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentPrintedEventArgs)
MessageBox.Show("DocumentPrinted")
End Sub
Public Shared Sub ApplicationEvent_DocumentPrinting_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentPrintingEventArgs)
MessageBox.Show("DocumentPrinting")
End Sub
Public Shared Sub ApplicationEvent_DocumentSaved_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSavedEventArgs)
MessageBox.Show("DocumentSaved")
End Sub
Public Shared Sub ApplicationEvent_DocumentSavedAs_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSavedAsEventArgs)
MessageBox.Show("DocumentSavedAs")
End Sub
Public Shared Sub ApplicationEvent_DocumentSaving_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSavingEventArgs)
MessageBox.Show("DocumentSaving")
End Sub
Public Shared Sub ApplicationEvent_DocumentSavingAs_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSavingAsEventArgs)
MessageBox.Show("DocumentSavingAs")
End Sub
Public Shared Sub ApplicationEvent_DocumentSynchronizedWithCentral_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs)
MessageBox.Show("DocumentSynchronizedWithCentral")
End Sub
Public Shared Sub ApplicationEvent_DocumentSynchronizingWithCentral_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs)
MessageBox.Show("DocumentSynchronizingWithCentral")
End Sub
Public Shared Sub ApplicationEvent_FailuresProcessing_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.FailuresProcessingEventArgs)
MessageBox.Show("FailuresProcessing")
End Sub
Public Shared Sub ApplicationEvent_FileExported_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.FileExportedEventArgs)
MessageBox.Show("FileExported")
End Sub
Public Shared Sub ApplicationEvent_FileExporting_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.FileExportingEventArgs)
MessageBox.Show("FileExporting")
End Sub
Public Shared Sub ApplicationEvent_FileImported_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.FileImportedEventArgs)
MessageBox.Show("FileImported")
End Sub
Public Shared Sub ApplicationEvent_FileImporting_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.FileImportingEventArgs)
MessageBox.Show("FileImporting")
End Sub
Public Shared Sub ApplicationEvent_ViewPrinted_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.ViewPrintedEventArgs)
MessageBox.Show("ViewPrinted")
End Sub
Public Shared Sub ApplicationEvent_ViewPrinting_Handler(ByVal sender As Object, ByVal e As Autodesk.Revit.DB.Events.ViewPrintingEventArgs)
MessageBox.Show("ViewPrinting")
End Sub
End Class
The nicer thing about the new RevitAddinWizard is that as can be noticed the VB.NET version of application event handlers has event argument types fully qualified now so that the common ‘Friend’ Is Not Accessible issue can be all avoided.
Recent Comments