The IExternalApplication interface can be implemented in addins to provide external applications for Revit to load. Two methods in the IExternalApplication interface are OnStartup() and OnShutdown().
Here is an example implementation of IExternalApplication in VB.NET:
#Region "Namespaces"
Imports System.Text
Imports System.Linq
Imports System.Xml
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.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
#End Region
<Transaction(TransactionMode.Manual)> _
<Regeneration(RegenerationOption.Manual)> _
Public Class ExtApp1
Implements IExternalApplication
#Region "Cached Variables"
Public Shared _cachedUiCtrApp As UIControlledApplication
#End Region
#Region "IExternalApplication Members"
Public Function OnStartup(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnStartup
_cachedUiCtrApp = uiApp
Try
'TODO: add you code below.
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
Public Function OnShutdown(ByVal uiApp As UIControlledApplication) As Result Implements IExternalApplication.OnShutdown
Try
'TODO: add you code below.
Return Result.Succeeded
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return Result.Failed
End Try
End Function
#End Region
#Region "Local Methods"
#End Region
End Class
Som commonly used namespaces of Revit API have been imported at the top of the source. It is good that we do not have to prefix any types in the above VB.NET code, but still have to do so for many types due to a problem of the VB.NET IDE and compiler as we talked about before.
Another looking smart but actually stupid thing regarding VB.NET is that it will append the ‘Root namespace’ as specified in the Project Properties to all types defined in the project no matter whether they have some other namespaces specified or not in declarations.
To avoid such confusion, as a common practise in our Revit API wizards, coders, and widgets, newly created VB.NET types such as classes, interfaces, and enums will not have any extra namespaces specified in their source besides the hidden ‘Root namespace’ one.
The TransactionMode and RegenerationOption have to be specified for external applications. We use Manual for both in this case. Automatic mode/option can be used for convenience but is not recommended.
The OnStartup() method will be called when Revit starts up in case the IExternalApplication implementation has been registered properly and loaded into the process of Revit. Generally we create ribbon panels and items, register application level events, register custom failure definitions, and similar in the OnStartup() method implementation.
The OnShutdown() will be called when Revit shuts down and the external application is being unloaded. Usually we unregister event handlers, unregister custom failure definitions, or do some cleanups in the OnShutdown() method implementation if necessary.
The UIControlledApplication instance passed by the OnStartup() method is cached into the public shared/static variable, _cachedUiCtrApp, which can be used anywhere anytime.
All these VB.NET code can be created through a few clicks by the RevitAddinWizard automatically.
Recent Comments