Let’s take the DocumentClosing application event for an example.
Firstly, we define a handler for the DocumentClosing application event. It can be anywhere, e.g. in an application class, a command class, any existing class, or in a separate new class; it can be local (member method) or global (static method); its name can be long or short. All depend on your need. In the code example we are talking about here, a new class and some static event handler methods are created, and a naming convention like AppEvent_DelegateName_Handler is applied.
Here is a sample implementation of the DocumentClosing event handler:
Public Shared Sub ApplicationEvent_DocumentClosing_Handler(ByVal sender As Object, ByVal args As EventArgs)
Dim specificArgs As Autodesk.Revit.DB.Events.DocumentClosingEventArgs = TryCast(args, Autodesk.Revit.DB.Events.DocumentClosingEventArgs)
Dim msg As String = "Cancellable: " & specificArgs.Cancellable & "\n" & "Document Title: " & specificArgs.Document.Title & "\n"
MessageBox.Show(msg, "DocumentClosing Event")
End Sub
It may be worth of mentioning the EventArgs casting a little bit. If we’d like to access some specific information for the event, the generic EventArgs needs to be casted to the specific DocumentClosingEventArgs. Then we can access to some information like whether the document is cancellable or what its title is, and even determine whether to cancel the document closing when necessary.
Of course, the EventArgs parameter type can be replaced directly by the specific type in the handler signature and it seems a common practice, but we choose to keep all event handlers the same signature and do the type casting inside each individual handler when necessary.
Secondly, the handler needs to be hooked up into the system. This is done in the OnStartup implementation of an IExternalApplication interface:
Public Function OnStartup(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnStartup
_cachedUiCtrApp = uiApp
Try
AddHandler uiApp.ControlledApplication.DocumentClosing, AddressOf AppEventHandlers1.ApplicationEvent_DocumentClosing_Handler
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
Thirdly, we’d better remove the handler from the application event system when the Revit is shutting down. It is not absolutely necessary as the addin and all its affiliates and even the whole Revit application will be destroyed at that moment, but it does not harm at all and is always a good practice to do so.
Public Function OnShutdown(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnShutdown
Try
RemoveHandler uiApp.ControlledApplication.DocumentCreating, AddressOf AppEventHandlers1.ApplicationEvent_DocumentCreating_Handler
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
That is about it.
The code examples regarding handling, registration and un-registration of all application events have been appended below for reference:
Application event handlers:
Imports System
Imports System.Text
Imports System.Xml
Imports System.Linq
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.DB.Events
Imports Autodesk.Revit.DB.Architecture
Imports Autodesk.Revit.DB.Structure
Imports Autodesk.Revit.DB.Mechanical
Imports Autodesk.Revit.DB.Electrical
Imports Autodesk.Revit.DB.Plumbing
Imports Autodesk.Revit.DB.Analysis
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
Imports Autodesk.Revit.UI.Events
Imports Autodesk.Revit.Collections
Imports Autodesk.Revit.Exceptions
Imports Autodesk.Revit.Utility
Imports RvtApplication = Autodesk.Revit.ApplicationServices.Application
Imports RvtDocument = Autodesk.Revit.DB.Document
Public Class AppEventHandlers1
Public Shared Sub ApplicationEvent_DocumentChanged_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentChanged")
End Sub
Public Shared Sub ApplicationEvent_DocumentClosed_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentClosed")
End Sub
Public Shared Sub ApplicationEvent_DocumentClosing_Handler(ByVal sender As Object, ByVal args As EventArgs)
Dim specificArgs As Autodesk.Revit.DB.Events.DocumentClosingEventArgs = TryCast(args, Autodesk.Revit.DB.Events.DocumentClosingEventArgs)
Dim msg As String = "Cancellable: " & specificArgs.Cancellable & "\n" & "Document Title: " & specificArgs.Document.Title & "\n"
MessageBox.Show(msg, "DocumentClosing Event")
End Sub
Public Shared Sub ApplicationEvent_DocumentCreated_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentCreated")
End Sub
Public Shared Sub ApplicationEvent_DocumentCreating_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentCreating")
End Sub
Public Shared Sub ApplicationEvent_DocumentOpened_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentOpened")
End Sub
Public Shared Sub ApplicationEvent_DocumentOpening_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentOpening")
End Sub
Public Shared Sub ApplicationEvent_DocumentPrinted_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentPrinted")
End Sub
Public Shared Sub ApplicationEvent_DocumentPrinting_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentPrinting")
End Sub
Public Shared Sub ApplicationEvent_DocumentSaved_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSaved")
End Sub
Public Shared Sub ApplicationEvent_DocumentSavedAs_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSavedAs")
End Sub
Public Shared Sub ApplicationEvent_DocumentSaving_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSaving")
End Sub
Public Shared Sub ApplicationEvent_DocumentSavingAs_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSavingAs")
End Sub
Public Shared Sub ApplicationEvent_DocumentSynchronizedWithCentral_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSynchronizedWithCentral")
End Sub
Public Shared Sub ApplicationEvent_DocumentSynchronizingWithCentral_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("DocumentSynchronizingWithCentral")
End Sub
Public Shared Sub ApplicationEvent_FailuresProcessing_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("FailuresProcessing")
End Sub
Public Shared Sub ApplicationEvent_FileExported_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("FileExported")
End Sub
Public Shared Sub ApplicationEvent_FileExporting_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("FileExporting")
End Sub
Public Shared Sub ApplicationEvent_FileImported_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("FileImported")
End Sub
Public Shared Sub ApplicationEvent_FileImporting_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("FileImporting")
End Sub
Public Shared Sub ApplicationEvent_ViewPrinted_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("ViewPrinted")
End Sub
Public Shared Sub ApplicationEvent_ViewPrinting_Handler(ByVal sender As Object, ByVal args As EventArgs)
MessageBox.Show("ViewPrinting")
End Sub
End Class
Application event registration in the OnStartup implementation of an IExternalApplication interface:
Public Function OnStartup(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnStartup
_cachedUiCtrApp = uiApp
Try
AddHandler uiApp.ControlledApplication.DocumentChanged, AddressOf AppEventHandlers1.ApplicationEvent_DocumentChanged_Handler
AddHandler uiApp.ControlledApplication.DocumentClosed, AddressOf AppEventHandlers1.ApplicationEvent_DocumentClosed_Handler
AddHandler uiApp.ControlledApplication.DocumentClosing, AddressOf AppEventHandlers1.ApplicationEvent_DocumentClosing_Handler
AddHandler uiApp.ControlledApplication.DocumentCreated, AddressOf AppEventHandlers1.ApplicationEvent_DocumentCreated_Handler
AddHandler uiApp.ControlledApplication.DocumentCreating, AddressOf AppEventHandlers1.ApplicationEvent_DocumentCreating_Handler
AddHandler uiApp.ControlledApplication.DocumentOpened, AddressOf AppEventHandlers1.ApplicationEvent_DocumentOpened_Handler
AddHandler uiApp.ControlledApplication.DocumentOpening, AddressOf AppEventHandlers1.ApplicationEvent_DocumentOpening_Handler
AddHandler uiApp.ControlledApplication.DocumentPrinted, AddressOf AppEventHandlers1.ApplicationEvent_DocumentPrinted_Handler
AddHandler uiApp.ControlledApplication.DocumentPrinting, AddressOf AppEventHandlers1.ApplicationEvent_DocumentPrinting_Handler
AddHandler uiApp.ControlledApplication.DocumentSaved, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSaved_Handler
AddHandler uiApp.ControlledApplication.DocumentSavedAs, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSavedAs_Handler
AddHandler uiApp.ControlledApplication.DocumentSaving, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSaving_Handler
AddHandler uiApp.ControlledApplication.DocumentSavingAs, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSavingAs_Handler
AddHandler uiApp.ControlledApplication.DocumentSynchronizedWithCentral, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSynchronizedWithCentral_Handler
AddHandler uiApp.ControlledApplication.DocumentSynchronizingWithCentral, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSynchronizingWithCentral_Handler
AddHandler uiApp.ControlledApplication.FailuresProcessing, AddressOf AppEventHandlers1.ApplicationEvent_FailuresProcessing_Handler
AddHandler uiApp.ControlledApplication.FileExported, AddressOf AppEventHandlers1.ApplicationEvent_FileExported_Handler
AddHandler uiApp.ControlledApplication.FileExporting, AddressOf AppEventHandlers1.ApplicationEvent_FileExporting_Handler
AddHandler uiApp.ControlledApplication.FileImported, AddressOf AppEventHandlers1.ApplicationEvent_FileImported_Handler
AddHandler uiApp.ControlledApplication.FileImporting, AddressOf AppEventHandlers1.ApplicationEvent_FileImporting_Handler
AddHandler uiApp.ControlledApplication.ViewPrinted, AddressOf AppEventHandlers1.ApplicationEvent_ViewPrinted_Handler
AddHandler uiApp.ControlledApplication.ViewPrinting, AddressOf AppEventHandlers1.ApplicationEvent_ViewPrinting_Handler
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
Application event un-registration in the OnShutdown implementation of an IExternalApplication interface:
Public Function OnShutdown(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnShutdown
Try
RemoveHandler uiApp.ApplicationClosing, AddressOf UIAppEventHandlers1.UIAppEvent_ApplicationClosing_Handler
RemoveHandler uiApp.DialogBoxShowing, AddressOf UIAppEventHandlers1.UIAppEvent_DialogBoxShowing_Handler
RemoveHandler uiApp.Idling, AddressOf UIAppEventHandlers1.UIAppEvent_Idling_Handler
RemoveHandler uiApp.ViewActivated, AddressOf UIAppEventHandlers1.UIAppEvent_ViewActivated_Handler
RemoveHandler uiApp.ViewActivating, AddressOf UIAppEventHandlers1.UIAppEvent_ViewActivating_Handler
RemoveHandler uiApp.ControlledApplication.DocumentChanged, AddressOf AppEventHandlers1.ApplicationEvent_DocumentChanged_Handler
RemoveHandler uiApp.ControlledApplication.DocumentClosed, AddressOf AppEventHandlers1.ApplicationEvent_DocumentClosed_Handler
RemoveHandler uiApp.ControlledApplication.DocumentClosing, AddressOf AppEventHandlers1.ApplicationEvent_DocumentClosing_Handler
RemoveHandler uiApp.ControlledApplication.DocumentCreated, AddressOf AppEventHandlers1.ApplicationEvent_DocumentCreated_Handler
RemoveHandler uiApp.ControlledApplication.DocumentCreating, AddressOf AppEventHandlers1.ApplicationEvent_DocumentCreating_Handler
RemoveHandler uiApp.ControlledApplication.DocumentOpened, AddressOf AppEventHandlers1.ApplicationEvent_DocumentOpened_Handler
RemoveHandler uiApp.ControlledApplication.DocumentOpening, AddressOf AppEventHandlers1.ApplicationEvent_DocumentOpening_Handler
RemoveHandler uiApp.ControlledApplication.DocumentPrinted, AddressOf AppEventHandlers1.ApplicationEvent_DocumentPrinted_Handler
RemoveHandler uiApp.ControlledApplication.DocumentPrinting, AddressOf AppEventHandlers1.ApplicationEvent_DocumentPrinting_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSaved, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSaved_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSavedAs, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSavedAs_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSaving, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSaving_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSavingAs, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSavingAs_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSynchronizedWithCentral, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSynchronizedWithCentral_Handler
RemoveHandler uiApp.ControlledApplication.DocumentSynchronizingWithCentral, AddressOf AppEventHandlers1.ApplicationEvent_DocumentSynchronizingWithCentral_Handler
RemoveHandler uiApp.ControlledApplication.FailuresProcessing, AddressOf AppEventHandlers1.ApplicationEvent_FailuresProcessing_Handler
RemoveHandler uiApp.ControlledApplication.FileExported, AddressOf AppEventHandlers1.ApplicationEvent_FileExported_Handler
RemoveHandler uiApp.ControlledApplication.FileExporting, AddressOf AppEventHandlers1.ApplicationEvent_FileExporting_Handler
RemoveHandler uiApp.ControlledApplication.FileImported, AddressOf AppEventHandlers1.ApplicationEvent_FileImported_Handler
RemoveHandler uiApp.ControlledApplication.FileImporting, AddressOf AppEventHandlers1.ApplicationEvent_FileImporting_Handler
RemoveHandler uiApp.ControlledApplication.ViewPrinted, AddressOf AppEventHandlers1.ApplicationEvent_ViewPrinted_Handler
RemoveHandler uiApp.ControlledApplication.ViewPrinting, AddressOf AppEventHandlers1.ApplicationEvent_ViewPrinting_Handler
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
All the application event handler prototype code here in VB.NET was created automatically by the RevitAddinWizard.
Recent Comments