In this lab, we create an external application to prevent the document from saving unless the user accepts to do so in a dialogue box. The Events API in Revit 2010 has been completely rewritten to be compliant to the .NET event standard. With events, now, we can do many interesting tasks. Events can now trap before and after various triggering actions (known as "pre" and "post" events). Many of the new pre-events are cancellable, offering the API application the ability to prevent the event from taking place.
First, we subscribe to the DocumentSaving event in the external application OnStartup() method, and remove the event handler in OnShutdown() method:
public IExternalApplication.Result OnStartup(ControlledApplication a) { try { // subscribe to the DocumentSaving event: a.DocumentSaving += new EventHandler<DocumentSavingEventArgs>( app_eventsHandlerMethod ); } catch ( Exception ex ) { LabUtils.InfoMsg( ex.Message ); return IExternalApplication.Result.Failed; } return IExternalApplication.Result.Succeeded; }
Public Function OnStartup(ByVal a As ControlledApplication) As IExternalApplication.Result _ Implements IExternalApplication.OnStartup Try AddHandler a.DocumentSaving, AddressOf app_eventsHandlerMethod Catch ex As Exception MsgBox(ex.Message) Return IExternalApplication.Result.Failed End Try Return IExternalApplication.Result.Succeeded End Function
When Revit shuts down, the event need to be removed in OnShutdown() method:
public IExternalApplication.Result OnShutdown(ControlledApplication a ) { // remove the event subscription: a.DocumentSaving -= new EventHandler<DocumentSavingEventArgs>( app_eventsHandlerMethod ); return IExternalApplication.Result.Succeeded; }
Public Function OnShutdown( ByVal a As ControlledApplication) As IExternalApplication.Result _ Implements IExternalApplication.OnShutdown RemoveHandler a.DocumentSaving, AddressOf app_eventsHandlerMethod Return IExternalApplication.Result.Succeeded End Function
Implement the event handler method app_eventsHandlerMethod. If the second argument's Cancel property is set to True, thesave action will be stopped. A message box is displayed to let the user make a decision to save the document or not. We set the choice of user to Cancel property of the Event argument accordingly.
// Show a message to decide whether to save the document. public void app_eventsHandlerMethod(object obj, DocumentSavingEventArgs args ) { if ( args.Cancellable ) { // Ask whether to prevent from saving. WinForms.DialogResult dr = WinForms.MessageBox.Show( "Saving event handler was triggered.\r\n" + "Using the pre-event mechanism, we can cancel the save.\r\n" + "Continue saving the document?", "Document Saving Event", WinForms.MessageBoxButtons.YesNo, WinForms.MessageBoxIcon.Question ); args.Cancel = (dr != WinForms.DialogResult.Yes ); } }
' Show a message to decide whether to save the document. Private Sub app_eventsHandlerMethod( _ ByVal obj As Object, _ ByVal args As Autodesk.Revit.Events.DocumentSavingEventArgs) If args.Cancellable Then ' Ask whether to prevent from saving. Dim dr As WinForms.DialogResult = WinForms.MessageBox.Show( _ "Saving event handler was triggered." + vbCrLf _ + "Using the pre-event mechanism, we can cancel the save." + vbCrLf _ + "Continue saving the document?", _ "Document Saving Event", _ WinForms.MessageBoxButtons.YesNo, _ WinForms.MessageBoxIcon.Question) args.Cancel = (dr <> WinForms.DialogResult.Yes) End If End Sub
Compile and build the application. The loading method is the same as Hello World External Application. Restart Revit to see the results. Open a document from disk, click the save icon or menu item, and note that this will display the dialog as shown below for user input.
Clicking "Yes" in the dialog box results in the document being saved, and clicking "No" prevent this.
next previous home copyright © 2007-2009 jeremy tammik, autodesk inc. all rights reserved.