Revit API 2012 provides one more application type, IExternalDBApplication, which is supposed to help do some DB stuffs at Revit startup, monitor some document events, and so on. In this post, we are going to see how to monitor the Document Open and Close events and create a log file to record these in an External DB Application (IExternalDBApplication).
It is obvious that the DocumentOpened and the DocumentClosed document events need to be lisented to in the OnStartup point as follows:
_cachedCtrlApp.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(_cachedCtrlApp_DocumentOpened);
_cachedCtrlApp.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(_cachedCtrlApp_DocumentClosed);
Then we can implement the event handlers like:
void _cachedCtrlApp_DocumentClosed(object sender, DocumentClosedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"c:\temp\RevitLogOpenClose.txt", true))
{
sw.WriteLine(DateTime.Now.ToString() + " : The document with #" + e.DocumentId.ToString() + " was closed.");
}
}
void _cachedCtrlApp_DocumentOpened(object sender, DocumentOpenedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"c:\temp\RevitLogOpenClose.txt", true))
{
sw.WriteLine(DateTime.Now.ToString() + " : The document " + e.Document.PathName + " was opened.");
}
}
Then whenever a new document is opened or closed, a record will be added to the log file. Here is an example of such a log file:
2012-03-01 10:09:07 PM : The document C:\Temp\rac_basic_sample_project.rvt was opened.
2012-03-01 10:12:14 PM : The document C:\Temp\TEST.rvt was opened.
2012-03-01 10:13:11 PM : The document C:\Temp\Walls.rvt was opened.
2012-03-01 10:13:15 PM : The document with #1001 was closed.
2012-03-01 10:13:17 PM : The document with #1002 was closed.
2012-03-01 10:13:20 PM : The document with #1003 was closed.
Here is the whole implementation of the IExternalDBApplication for convenience:
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
#endregion
namespace RevitAddinCS1
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtDbApp : IExternalDBApplication
{
#region Cached Variables
public static ControlledApplication _cachedCtrlApp;
#endregion
#region IExternalApplication Members
public ExternalDBApplicationResult OnStartup(ControlledApplication ctrlApp)
{
try
{
_cachedCtrlApp = ctrlApp;
//TODO: add you code below.
//MessageBox.Show("ExtDbApp");
_cachedCtrlApp.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(_cachedCtrlApp_DocumentCreated);
_cachedCtrlApp.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(_cachedCtrlApp_DocumentOpened);
_cachedCtrlApp.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(_cachedCtrlApp_DocumentClosed);
return ExternalDBApplicationResult.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show( ex.ToString() );
return ExternalDBApplicationResult.Failed;
}
}
void _cachedCtrlApp_DocumentClosed(object sender, DocumentClosedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"c:\temp\RevitLogOpenClose.txt", true))
{
sw.WriteLine(DateTime.Now.ToString() + " : The document with #" + e.DocumentId.ToString() + " was closed.");
}
}
void _cachedCtrlApp_DocumentOpened(object sender, DocumentOpenedEventArgs e)
{
using (StreamWriter sw = new StreamWriter(@"c:\temp\RevitLogOpenClose.txt", true))
{
sw.WriteLine(DateTime.Now.ToString() + " : The document " + e.Document.PathName + " was opened.");
}
}
void _cachedCtrlApp_DocumentCreated(object sender, DocumentCreatedEventArgs e)
{
Document doc = e.Document;
ElementId id = ElementId.InvalidElementId;
using (Transaction tr = new Transaction(doc, "Hello from RevitAddinWizard."))
{
tr.Start();
TextNote text = doc.Create.NewTextNote(
doc.ActiveView,
new XYZ(0, 0, 0),
new XYZ(1, 0, 0),
new XYZ(0, 0, 1),
2,
TextAlignFlags.TEF_ALIGN_CENTER,
"Hello from RevitAddinWizard.");
text.Width = 20;
tr.Commit();
id = text.Id;
}
}
public ExternalDBApplicationResult OnShutdown(ControlledApplication ctlApp)
{
try
{
return ExternalDBApplicationResult.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return ExternalDBApplicationResult.Failed;
}
}
#endregion
}
}
Revit Addin Wizard (RevitAddinWizard) is going to provide an External DB Application wizard to help implement the IExternalDBApplication interface automatically and reliably. With the assistance of the External DB Application wizard and some event wizards, the above code can be created conveniently in a minute.
Recent Comments