and
ThisDocument_Shutdown
is called when the project document closes. You can add your some initializing
code in the
*_Startup
methods and do the cleanup work in
*_Shutdown
methods. For example, you can
register event handlers in
*_Startup
methods and unregister them in
*_Shutdown
methods (this actually is
the recommended way).
Q:
How and why should I register and unregister my Revit event handler?
A:
As noted previously, the recommended way to do this in Revit VSTA is to register event handlers in the
*_Startup
method and unregister them in
*_Shutdown
method. Every VSTA macro will be loaded and
unloaded dynamically. When you debug a macro, if the event handler is not properly unregistered, Revit
may call into a wrong method (maybe an invalid memory address). Although Revit VSTA may prevent Revit
from crashing in this scenario, any event handlers that are not properly unregistered may cause performance
issues during your current Revit session.
Q:
I want to experiment with the Startup and Shutdown methods and an event handler. Can you show me
an example?
A:
The following sample code shows how to register an
OnDocumentNewed
event handler, which will
automatically launch a message box when a new Revit project is created. Note: One of the VSTA samples
provided on the Revit SDK may show an example of a Document-level event handler's startup and shutdown.
This FAQ shows Application-level event handler examples.
VB.NET example, Application-level:
Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Startup
AddHandler Me.OnDocumentNewed, AddressOf Me.ThisApplication_OnDocumentNewed
End Sub
Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Shutdown
RemoveHandler Me.OnDocumentNewed, AddressOf Me.ThisApplication_OnDocumentNewed
End Sub
Private Sub ThisApplication_OnDocumentNewed(ByVal document As Autodesk.Revit.Document)
System.Windows.Forms.MessageBox.Show("VB.NET Application event OnDocumentNewed")
End Sub
C# Example, Application-level:
private void ThisApplication_Startup(object sender, EventArgs e)
{
this.OnDocumen= new Autodesk.Revit.Events.DocumentNewedEventHandler(ThisApplica
tion_OnDocumentNewed);
}
private void ThisApplication_Shutdown(object sender, EventArgs e)
{
this.OnDocumentNewed -= new Autodesk.Revit.Events.DocumentNewedEventHandler(ThisApplica
tion_OnDocumentNewed);
}
void ThisApplication_OnDocumentNewed(Document document)
{
System.Windows.Forms.MessageBox.Show("C# Application event OnDocumentNewed");
}
1366 | Chapter 24 Creating Macros with Revit VSTA
Summary of Contents for 24000-000000-9860 - Revit Architecture - PC
Page 1: ...Revit Architecture 2009 User s Guide April 2008 240A1 050000 PM02A ...
Page 4: ......
Page 56: ...18 ...
Page 116: ...78 Chapter 3 Revit Essentials ...
Page 172: ...134 Chapter 4 ProjectViews ...
Page 178: ...Schedule with Grid Lines Schedule with Grid Lines and an Outline 140 Chapter 4 ProjectViews ...
Page 554: ...516 ...
Page 739: ...Curtain wall Curtain Grid Curtain Walls Curtain Grids and Mullions 701 ...
Page 1004: ...966 ...
Page 1136: ...1098 ...
Page 1226: ...1188 ...
Page 1250: ...1212 ...
Page 1276: ...1238 Chapter 20 Design Options ...
Page 1310: ...1272 ...
Page 1366: ...1328 ...
Page 1406: ...1368 ...