Getting Started With The Revit API
Getting Started With The Revit API
Table of Contents
Overview.......................................................................................................................................................................................2
What Can I Do With the Autodesk Revit API?.............................................................................................................................2
What You Will Need to Get Started..............................................................................................................................................2
Understanding Autodesk Revit......................................................................................................................................................2
Installation of the Autodesk Revit API..........................................................................................................................................3
Development Requirements..........................................................................................................................................................3
Using the Autodesk Revit API......................................................................................................................................................3
Autodesk Revit SDK and Online Help.....................................................................................................................................3
Documentation Conventions.................................................................................................................................................4
What can you do with the Autodesk Revit API?.......................................................................................................................4
Deployment Options.............................................................................................................................................................4
1. External commands.....................................................................................................................................................4
2. External applications...................................................................................................................................................4
3. REX addins..................................................................................................................................................................5
Registration of add-ins..............................................................................................................................................................5
External Commands......................................................................................................................................................................7
The IExternalCommand Interface.............................................................................................................................................7
Parameters.............................................................................................................................................................................7
Return Value..........................................................................................................................................................................7
Remarks.................................................................................................................................................................................8
Using an Autodesk Revit API External Command...................................................................................................................8
External Command Object Lifetime.........................................................................................................................................8
External Applications....................................................................................................................................................................8
The IExternalApplication Interface...........................................................................................................................................9
Parameters.............................................................................................................................................................................9
Return Value..........................................................................................................................................................................9
External Application Object Lifetime.......................................................................................................................................9
Compatibility of API applications with 64-bit Revit....................................................................................................................9
Debugging Your Application in MS Visual Studio 2010..............................................................................................................9
The Revit Unit System................................................................................................................................................................10
Storing and accessing Custom Data for Applications.................................................................................................................11
Appendix 1 Glossary of Autodesk Revit terms....................................................................................................................12
Appendix 2 - FAQ...................................................................................................................................................................12
General:...............................................................................................................................................................................12
Autodesk Revit Structure:...................................................................................................................................................14
Autodesk Revit 2012 API Getting Started
Overview
Autodesk Revit Architecture 2012, Autodesk Revit Structure 2012 and Autodesk Revit MEP 2012
offer an API designed to allow power users and external application developers to integrate their
applications with Autodesk Revit. The APIs for each of these three products are very similar and
will be jointly referred to as the Autodesk Revit 2012 API or Autodesk Revit API. It is strongly
recommended that you become familiar with Autodesk Revit Architecture, Structure or MEP and its
features before attempting to use the Autodesk Revit API. Training can be found through the
Autodesk Developer Network (ADN).
The Autodesk Revit API is designed to reflect the same user interaction paradigms as the programs
Graphical User Interface. Therefore, the first step to understanding the API is to learn how to use
the program. If you are an Autodesk Revit novice, we suggest you first start by going through the
Tutorials which you can access through the programs Help menu. You may also find it helpful to
take a Training class from your local Autodesk reseller. This will help you quickly get up to speed
with the program
Autodesk Resources:
http://www.autodesk.com/revitbuilding/
2
Autodesk Revit 2012 API Getting Started
http://www.autodesk.com/revitstructure/
http://www.autodesk.com/revitsystems/
http://www.autodesk.com/bim/
http://discussion.autodesk.com/
Select = Autodesk Revit Building
Then Select = Autodesk Revit API
External Resources:
http://www.revitcity.com/
http://www.augi.com/revit/
http://www.revitinside.com/
Development Requirements
The Autodesk Revit API requires the Microsoft .NET Framework v3.5. To edit and debug your API
applications, you need an interactive development environment such as Microsoft Visual Studio
2008 or one of the MS Visual Studio Express Editions for C# or Visual Basic.NET. When developing
with the Autodesk Revit API, ensure that your project references two DLLs: RevitAPI.dll and
RevitAPIUI.dll contained in the Autodesk Revit Program directory.
Some programming skills are required to effectively use the API. If you are a beginner in
programming, we strongly advise you to learn Microsoft Visual Studio 2008 and one of the
compatible languages like C# or Visual Basic.NET. There are many good books and classes to get
you started.
Resources:
Online resources
Free Visual Studio http://msdn.microsoft.com/VStudio/Express/
http://www.codeguru.com/
http://devx.com/
http://www.msdn.microsoft.com/
Books:
Code Complete, Second Edition, by Steve McConnell
Software Project Survival Guide, by Steve McConnell
3
Autodesk Revit 2012 API Getting Started
Documentation Conventions
This document will contain names in namespace format, such as Autodesk.Revit.DB.Element
What can you do with the Autodesk Revit API?
Deployment Options
The Autodesk Revit API supports in-process DLLs only. This means that your API application will
be compiled as a DLL loaded into the Autodesk Revit process.
The Autodesk Revit API supports single threaded access only. This means that your API application
must perform all Autodesk Revit API calls in the main thread (which is called by the Autodesk Revit
process at various API entry points), and your API application cannot maintain operations in other
threads and expect them to be able to make calls to Autodesk Revit at any time.
There are two types of DLLs that you can create with the Autodesk Revit API:
1. External commands
The Autodesk Revit API enables you to add new commands to the user interface of Autodesk Revit.
These commands will appear in the Add-ins tab under the External Tools pulldown, as seen in
Figure 1. Through the API, external tool commands have access to the Autodesk Revit database, as
well as the currently selected elements.
2. External applications
The Autodesk Revit API enables you to also add external applications. These applications are
invoked during Autodesk Revit startup and shutdown. They can create new panels in the Add-ins
tab, as seen in Figure 2. They can also register handlers that can react to events occurring in the
Autodesk Revit user interface.
4
Autodesk Revit 2012 API Getting Started
3. REX addins
REX is an API framework that lets you build applications for Revit in .NET similar to classes that
implement IExternalCommand. REX is meant to give you a more high-level development
environment through built-in resources such as:
Please see the \Revit 2012 SDK\REX SDK\ folder for more details.
Registration of add-ins
The Revit API offers the ability to register API applications via a .addin manifest file.
Manifest files will be read automatically by Revit when they are places in one of two locations on a
user's system:
In a non-user specific location in "application data"
o For Windows XP - C:\Documents and Settings\All Users\Application
Data\Autodesk\Revit\Addins\2012\
o For Vista/Windows 7 - C:\ProgramData\Autodesk\Revit\Addins\2012\
In a user specific location in "application data"
o For Windows XP - C:\Documents and Settings\<user>\Application
Data\Autodesk\Revit\Addins\2012\
o For Vista/Windows 7 -
C:\Users\<user>\AppData\Roaming\Autodesk\Revit\Addins\2012\
All files named .addin in these locations will be read and processed by Revit during startup.
A basic file adding one ExternalCommand looks like this:
<?xml version="1.0" encoding="utf-16" standalone="no"?>
<RevitAddIns>
<AddIn Type="Command">
<Assembly>c:\MyProgram\MyProgram.dll</Assembly>
<AddInId>76eb700a-2c85-4888-a78d-31429ecae9ed</AddInId>
5
Autodesk Revit 2012 API Getting Started
<FullClassName>Revit.Samples.SampleCommand</FullClassName>
<Text>Sample command</Text>
<VisibilityMode>NotVisibleInFamily</VisibilityMode>
<VisibilityMode>NotVisibleInMEP</VisibilityMode>
<AvailabilityClassName>Revit.Samples.SampleAccessibilityCheck
</AvailabilityClassName>
</AddIn>
</RevitAddIns>
6
Autodesk Revit 2012 API Getting Started
AvailabilityClassN The full name of the class in the assembly file which implemented
ame IExternalCommandAvailability. This class allows the command button
to be selectively grayed out depending on context. Optional; use this
tag for ExternalCommands only. The default is a command that is
available whenever it is visible.
LargeImage The path to the icon to use for the button in the External Tools pulldown
menu. The icon should be 32 x 32 pixels for best results. Optional; use
this tag for ExternalCommands only. The default is to show a button
without an icon.
LongDescription Long description of the command, will be used as part of the button's
extended tooltip. This tooltip is shown when the mouse hovers over the
command for a long amount of time. You can split the text of this option
into multiple paragraphs by placing <p> tags around each paragraph.
Optional; use this tag for ExternalCommands only. If neither of this
property and TooltipImage are supplied, the button will not have an
extended tooltip.
TooltipImage The path to an image file to show as a part of the button extended
tooltip, shown when the mouse hovers over the command for a longer
amount of time. Optional; use this tag for ExternalCommands only. If
neither of this property and TooltipImage are supplied, the button will
not have an extended tooltip.
LanguageType Localization setting for Text, Description, LargeImage, LongDescription,
and TooltipImage of external tools buttons. Revit will load the resource
values from the specified language resource dll. The value can be one
of the eleven languages supported by Revit. If no LanguageType is
specified, the language resource which the current session of Revit is
using will be automatically loaded.
External Commands
Implement support for a specific interface, in this case Autodesk.Revit.UI.IExternalCommand.
Adds one or more entries to the Revit.ini file describing the external command.
7
Autodesk Revit 2012 API Getting Started
Parameters
commandData : The object passed in this parameter contains information important to the
command that is being executed. This data includes the Autodesk Revit Application object
as well as the currently active view.
message : The message string can be set to supply a specific message to the user when the
command terminates. How this message is displayed is dependent upon the return value of
the function. See the remarks section for more details.
elements : Initially this is an empty set that can contain Autodesk Revit elements. When
the command terminates, the elements within this set may be displayed, based on the
return value. See the remarks section for more details.
Return Value
result : The return value can be one of the following:
Success : Is returned if the command succeeded as expected without any unhandled error
conditions. The external command will appear as an undoable operation in the Autodesk
Revit user interface.
Cancelled : This value specifies that the user requested that the command be cancelled.
Any changes that were made to Autodesk Revit objects during the external commands
execution will be undone. A message may be posted, see the Remarks section.
Failure : Failure signifies that the external command failed in some manner from which it
cannot recover. Any changes made to Autodesk Revit objects during the execution of the
external command will be undone. A message will be posted, see the Remarks section.
Remarks
The message and elements parameters are used if the command was cancelled or failed.
Cancelled : If the external command was cancelled and the message parameter was set by
the external command then the message is displayed when execution is returned back to
Autodesk Revit. If the message parameter was not set then no message is displayed and
the command will exit silently.
Failed : If the external command failed then the contents of the message parameter will be
displayed. If the element set contains Autodesk Revit elements then these elements will be
highlighted when the error message is displayed thus giving the developer the ability to
show the user the problem elements.
3. The user had the option to select a number of Autodesk Revit elements before invoking the
External Tools program. If they did, the program can decide to only perform its function on
the selected members.
4. The API program takes focus from Autodesk Revit and performs the required task. Often a
dialog box may be required to obtain user input before the application can complete its
work.
8
Autodesk Revit 2012 API Getting Started
5. Once the add-on tool has completed its function or has been dismissed by the user the
program will update the Autodesk Revit model as required and return from the external
command, giving focus back to Autodesk Revit.
External Applications
Implement support for a specific interface, in this case
Autodesk.Revit.UI.IExternalApplication.
Adds one or more entries to the Revit.ini file describing the external application.
Technically, an external application is an exposed .NET object that supports the
Autodesk.Revit.IExternalApplication interface. Furthermore, in the Revit.ini file there must be one
entry for each such object in order for Autodesk Revit to be able to load these applications when
Autodesk Revit starts.
The IExternalApplication Interface
The declaration (C#) of the interface is as follows:
Autodesk.Revit.UI.IExternalApplication.Result
OnStartup(Autodesk.Revit.ControlledApplication application)
Autodesk.Revit.UI.IExternalApplication.Result
OnShutdown(Autodesk.Revit.ControlledApplication application)
Parameters
application : The object passed in this parameter contains information important to the
commands OnStartup and OnShutdown that are being called. This object provides limited
access methods of Autodesk Revit Application, such as VersionName, VersionNumber; and
delegates for some events, such as OnDocumentOpened, OnDocumentSaved.
Return Value
result : The return value can be one of the following:
Success : Is returned if the external application succeeded as expected without any
unhandled error conditions.
Failure : Failure signifies that the external application failed in some manner from which it
cannot recover.
Cancelled : This value specifies that the external application be cancelled.
9
Autodesk Revit 2012 API Getting Started
application object will be held during the entire Autodesk Revit session. The OnShutdown method
will be called when Autodesk Revit shuts down.
10
Autodesk Revit 2012 API Getting Started
11
Autodesk Revit 2012 API Getting Started
Family
A Family is a collection of objects called types. A family groups elements with a common set of
parameters, identical use, and similar graphical representation. Different types within a family may
have different values of some or all parameters, but the set of parameters - their names and their
meaning - are the same.
Parameter
There are a number of Autodesk Revit parameter types. Shared Parameters can be thought of as
user defined variables. System Parameters are variables that are hard coded in Autodesk Revit.
Family parameters are variables that are defined when a family is created or modified.
Type
A Type is a member of a Family. Each Type has specific parameters that are constant for all
instance of the Type that exist in your model. These are called Type Properties. Types have other
parameters called Instance parameters, which may vary over the various instances of this type in
your model.
Appendix 2 - FAQ
General:
Q: How do I reference an element in Autodesk Revit?
A: Each element has Id and UniqueId properties that can be used to reference the element.
Q: Are element IDs universally unique and can they ever change?
A: At any given time, element IDs in a Revit model are unique. However, multi-user interactions in
Revit can result in an elements ID changing. Also, another element in a different model may have
an element with the same ID. If you need to store an ID (especially an ID of an element that was
newly created) in order to find the element later, store UniqueId.
Autodesk Revit can work with multiple shared parameter files but can only read parameters from
one file at a time. It is then up to you to choose the same shared parameter file for all models or a
different one for each.
An API application should avoid interfering with the users own parameter file. Your application
should ship with a parameter file containing the parameters it requires. To load the parameter(s)
into an Autodesk Revit file, an application should:
Store the name of the users parameter file,
Make the applications parameter file current,
Load the required parameter(s) and,
12
Autodesk Revit 2012 API Getting Started
Q: Do I need to distribute the shared parameters file with the model so other programs can use the
shared parameters?
A: No, you do not need to distribute shared parameter files. The shared parameters file is only
used to load shared parameters. After they are loaded the file is no longer needed for that model.
Q: Do shared parameter values get copied when the corresponding element is copied?
A: Yes they do. Therefore if you have a shared parameter that holds a unique ID for an item in
your database it is recommended that you make this ID include the Autodesk Revit elements
UniqueId property. This will allow you to identify elements that were copied from existing
elements in your application.
Q: Autodesk Revit takes a long time to update when my application sends data back to the model.
What do I need to do to speed it up?
A: You should use the Manual regeneration mode to prevent the API framework from regenerating
after every model level change. Then, you will use the regeneration APIs to force update of the
document after a group of changes. Performance of multiple modifications of the Revit document
should be faster than RegenerationOption.Automatic. Because this mode suspends all updates to
the document, your application should not read data from the document after it has been modified
until the document has been regenerated, or it runs the risk of accessing stale data.
Q: What do I do if I want to add shared parameters to elements that do not have the ability to
have shared parameters bound to them? For example: Grids or Materials.
A: If an element type does not have the ability to contain shared parameters, you will need to add
a project parameter. This does make it a bit more complicated as you will need different code to
access the value of the parameter on these elements.
Q: When exporting a model containing groups to an external program, the user receives an error
at the end of the export:
"Changes to group "Group 1" are allowed only in group edit mode. Use the Edit Group command
to make the change to all instances of the group. You may use the "Ungroup" option to proceed
with this change by ungrouping the changed group instances."
A: Currently the API does not permit changes to members of groups. You can programmatically
ungroup, make the change, regroup and then swap the other instances of the old group to the new
group to get the same effect.
Q: Why do two different FamilySymbols have the same name in my Autodesk Revit document?
A: In the Autodesk Revit API, Symbols represent geometry.
Consider that two window instances of the same type are inserted into walls of different types.
Now, these two window instances refer to the same FamilySymbol and count as being of the same
type, but it is clear that they cannot possibly use the same symbols geometry, since inserting
them into walls of different thickness changes each windows geometry to match. To track
individual instances of FamilySymbols, Autodesk Revit employs a master and slave functionality
with slave FamilySymbols localizing their geometry to surrounding conditions according to the
master FamilySymbol template.
13
Autodesk Revit 2012 API Getting Started
If there is no door instance in a model, there is only one master FamilySymbol for a 36x84
door, but if a door is inserted, Autodesk Revit will create a slave FamilySymbol for the new
36x84 Door.
Subsequent doors placed in the model will cause Autodesk Revit to generate additional slave
FamilySymbols.
Q: Is the order of vertices or the triangles in an Autodesk Revit geometry mesh object reliably
sorted in counter-clockwise direction?
A: Yes.
Q: Why after flipping a wall or door (both external and internal) do its properties FromRoom and
ToRoom do not change?
A: External Doors & External Windows have only either a FromRoom or a ToRoom. Users can
decide which property exists when locating doors or windows. If external doors or windows are
flipped their FromRoom or ToRoom properties do not change automatically, but can be manually
changed.
Q: I'm rotating the orientation of the beam so it is rotated in the weak direction, i.e. the I of a
W14X30 is rotated to look like an H by a 90 degree rotation. How is that rotation angle accessed in
the API?
Because the location is a LocationCurve not a LocationPoint I do not have access to the Rotation
value so what is it I need to check? I've got a FamilyInstance element to check so what do I do
with it?
A: Take a look at the RotateFramingObject example in the SDK. It has examples of how to get and
change the rotation angle of beams, braces and columns.
14
Autodesk Revit 2012 API Getting Started
15