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
Содержание 24000-000000-9860 - Revit Architecture - PC
Страница 1: ...Revit Architecture 2009 User s Guide April 2008 240A1 050000 PM02A ...
Страница 4: ......
Страница 56: ...18 ...
Страница 116: ...78 Chapter 3 Revit Essentials ...
Страница 172: ...134 Chapter 4 ProjectViews ...
Страница 178: ...Schedule with Grid Lines Schedule with Grid Lines and an Outline 140 Chapter 4 ProjectViews ...
Страница 352: ...Defining the first scale vector Defining the second scale vector 314 Chapter 7 Editing Elements ...
Страница 554: ...516 ...
Страница 739: ...Curtain wall Curtain Grid Curtain Walls Curtain Grids and Mullions 701 ...
Страница 898: ...Pile caps with multiple piles Rectangular and triangular pile caps 860 Chapter 12 Working with Structural Components ...
Страница 1004: ...966 ...
Страница 1136: ...1098 ...
Страница 1226: ...1188 ...
Страница 1250: ...1212 ...
Страница 1276: ...1238 Chapter 20 Design Options ...
Страница 1310: ...1272 ...
Страница 1366: ...1328 ...
Страница 1406: ...1368 ...