Revit document provides a few events for programs to monitor document related operations such as saving, saving as, closing, and printing.
Let’s create handlers for all the document events first:
public class DocEventHandlers1
{
public static void DocEvent_DocumentClosing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentClosing");
}
public static void DocEvent_DocumentPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinted");
}
public static void DocEvent_DocumentPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentPrinting");
}
public static void DocEvent_DocumentSaved_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaved");
}
public static void DocEvent_DocumentSavedAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavedAs");
}
public static void DocEvent_DocumentSaving_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSaving");
}
public static void DocEvent_DocumentSavingAs_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DocumentSavingAs");
}
public static void DocEvent_ViewPrinted_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinted");
}
public static void DocEvent_ViewPrinting_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewPrinting");
}
}
Next, we prepare a helper method which can subscribe all the events to a Document object:
public class DocEventSubscriber
{
// …
public static void SubscribeForDoc(RvtDocument doc)
{
doc.DocumentClosing += DocEventHandlers1.DocEvent_DocumentClosing_Handler;
doc.DocumentPrinted += DocEventHandlers1.DocEvent_DocumentPrinted_Handler;
doc.DocumentPrinting += DocEventHandlers1.DocEvent_DocumentPrinting_Handler;
doc.DocumentSaved += DocEventHandlers1.DocEvent_DocumentSaved_Handler;
doc.DocumentSavedAs += DocEventHandlers1.DocEvent_DocumentSavedAs_Handler;
doc.DocumentSaving += DocEventHandlers1.DocEvent_DocumentSaving_Handler;
doc.DocumentSavingAs += DocEventHandlers1.DocEvent_DocumentSavingAs_Handler;
doc.ViewPrinted += DocEventHandlers1.DocEvent_ViewPrinted_Handler;
doc.ViewPrinting += DocEventHandlers1.DocEvent_ViewPrinting_Handler;
}
// …
}
Last, we call the method against the active document in an external command:
DocEventSubscriber.SubscribeForDoc(CachedDoc);
All these can be done automatically by the RevitAddinWizard in a moment.
As can be seen above, it is straightforward to create document event handlers and subscribe them to the active document that a command is going to operate on. However, it will be very tricky to subscribe document events to all documents including existing ones and those that will be opened or created in the whole Revit session.
In the part 2, we will be addressing this issue. Please stay tuned…
Links to some related articles:
Manage Revit Application Events of Revit API
Use RevitAddinWizard to Add Revit Application Event Handlers of Revit API
Implement Revit FailuresProcessing Event Hanlders of Revit API
Use RevitAddinWizard to Implement Revit FailuresProcessing Event Hanlders of Revit API
Manage Revit UIApplication Events
Manage Revit Document Events of Revit API - 3 Document Event Handler of RevitAddinWizard
Recent Comments