Revit API 2012 provides one more application type, IExternalDBApplication, which is supposed to help do some DB stuffs at Revit startup. In this post, we are going to see how to create a text at startup of such an External DB Application (IExternalDBApplication).
As with the OnStartup and OnShutdown methods of the IExternalApplication, we can also register or unregister some event callbacks at that moment in the pretty same methods of the IExternalDBApplication. We’d like to create a TEXT when any new document is created, so the event DocumentCreated is our concern.
After the DocumentCreated event is registered in the OnStartup point as follows:
_cachedCtrlApp.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(_cachedCtrlApp_DocumentCreated);
we can implement the event handler like:
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;
}
}
Then whenever a new document is created, a TEXT will be added to the center of the view.
Here is the whole implementation of the IExternalDBApplication for your 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);
return ExternalDBApplicationResult.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show( ex.ToString() );
return ExternalDBApplicationResult.Failed;
}
}
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.
Recent Comments