Revit 2011 API exposed many new application events that API developer can use. The whole list of events is:
| Name | Description |
| DocumentChanged | Subscribe to the DocumentChanged event to be notified when Revit document has changed. |
| DocumentClosed | Subscribe to the DocumentClosed event to be notified immediately after Revit has finished closing a document. |
| DocumentClosing | Subscribe to the DocumentClosing event to be notified when Revit is just about to close a document. |
| DocumentCreated | Subscribe to the DocumentCreated event to be notified immediately after Revit has finished creating a new document. |
| DocumentCreating | Subscribe to the DocumentCreating event to be notified when Revit is just about to create a new document. |
| DocumentOpened | Subscribe to the DocumentOpened event to be notified immediately after Revit has finished opening a document. |
| DocumentOpening | Subscribe to the DocumentOpening event to be notified when Revit is just about to open a document. |
| DocumentPrinted | Subscribe to the DocumentPrinted event to be notified immediately after Revit has finished printing a view or ViewSet of the document. |
| DocumentPrinting | Subscribe to the DocumentPrinting event to be notified when Revit is just about to print a view or ViewSet of the document. |
| DocumentSaved | Subscribe to the DocumentSaved event to be notified immediately after Revit has finished saving a document. |
| DocumentSavedAs | Subscribe to the DocumentSavedAs event to be notified immediately after Revit has finished saving document with a new file name. |
| DocumentSaving | Subscribe to the DocumentSaving event to be notified when Revit is just about to save a document. |
| DocumentSavingAs | Subscribe to the DocumentSavingAs event to be notified when Revit is just about to save the document with a new file name. |
| DocumentSynchronizedWithCentral | Subscribe to the DocumentSynchronizedWithCentral event to be notified immediately after Revit has finished synchronizing a document with central file. |
| DocumentSynchronizingWithCentral | Subscribe to the DocumentSynchronizingWithCentral event to be notified when Revit is just about to synchronize a document with central file. |
| FailuresProcessing | Subscribe to the FailuresProcessing event to be notified when failures are being processed at the end of transaction. |
| FileExported | Subscribe to the FileExported event to be notified immediately after Revit has finished exporting files of formats supported by the API. |
| FileExporting | Subscribe to the FileExporting event to be notified when Revit is just about to export files of formats supported by the API. |
| FileImported | Subscribe to the FileImported event to be notified immediately after Revit has finished importing a file of format supported by the API. |
| FileImporting | Subscribe to the FileImporting event to be notified when Revit is just about to import a file of format supported by the API. |
| ViewPrinted | Subscribe to the ViewPrinted event to be notified immediately after Revit has finished printing a view of the document. |
| ViewPrinting | Subscribe to the ViewPrinting event to be notified when Revit is just about to print a view of the document. |
When the event will be post is decribed in the description part. Most cases, you could know what the event is through its name directly.
Use the event is very simple, let’s use the DocumentOpened event as example, the event handler should be added when revit startup, so we need an external application instead of external command to handle this. New a file and write code as:
using System;
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
namespace myRevitApp
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class MyApp : IExternalApplication
{
public Result OnStartup(UIControlledApplication uiControlledApplication)
{
uiControlledApplication.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(docOpen);
return Result.Succeeded;
}
private void docOpen(object sender, DocumentOpenedEventArgs e)
{
Autodesk.Revit.ApplicationServices.Application app = sender as Autodesk.Revit.ApplicationServices.Application;
UIApplication uiApp = new UIApplication(app);
Document doc = uiApp.ActiveUIDocument.Document;
// show document name
TaskDialog.Show(“Doc path name”, doc.PathName);
}
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
}
}
And the .addin file should be like:
<?xml version=”1.0″ encoding=”utf-16″ standalone=”no”?>
<RevitAddIns>
<AddIn Type=”Application”>
<Name>myRevitApp</Name>
<Assembly>C:\Project\myRevitApp.dll</Assembly>
<AddInId>7cbae747-78f9-4dd5-ac92-a94f6229f9b3</AddInId>
<FullClassName>myRevitApp.MyApp</FullClassName>
</AddIn>
</RevitAddIns>
Then build the solution and run, a task dialog will show up when you open a document.
Comments
Leave a comment