Getting Started With The Revit API
Getting Started With The Revit API
Table of Content
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.........................................................................................................................................................................8
Remarks.................................................................................................................................................................................8
Using an Autodesk Revit API External Command...................................................................................................................8
External Command Object Lifetime.........................................................................................................................................8
External Applications....................................................................................................................................................................9
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 2012...........................................................................................................10
The Revit Unit System................................................................................................................................................................11
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 Kepler API – Getting Started
Overview
Autodesk Revit offers an API designed to allow power users and external application developers to
integrate their applications with Autodesk Revit, Revit Structure, Revit Architecture, and Revit MEP.
The APIs for each of these products are very similar and will be jointly referred to as the Autodesk
Revit Copernicus 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 program’s
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 program’s 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/products/revit-family/overview
http://www.autodesk.com/solutions/building-information-modeling/overview
http://forums.autodesk.com/
Select = Autodesk Revit
2
Autodesk Revit Kepler API – Getting Started
Then Select = Autodesk Revit API
External Resources:
http://www.augi.com/community/autodesk-revit
http://www.revitcity.com
http://revitinside.com
Development Requirements
The Autodesk Revit API requires the Microsoft .NET Framework v4.5. To edit and debug your API
applications, you need an interactive development environment such as Microsoft Visual Studio
2012 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 2012 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 Kepler API – Getting Started
Resources:
www.autodesk.com/adn - Autodesk Developer Network home (ADN)
www.autodesk.com/developrevit - Autodesk Revit development resources
http://discussion.autodesk.com – Autodesk product discussion groups
Select = Autodesk Revit Architecture
Then Select = Autodesk Revit API
http://thebuildingcoder.typepad.com/blog/ - The Building Coder, an AND blog dedicated to Revit
coding
http://bimapps.typepad.com/ - Bim Apps, a blog dedicated to BIM applications
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 Kepler API – Getting Started
3. REX addins
REX (Revit Extensions) 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 Kepler 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 C:\ProgramData\Autodesk\Revit\Addins\2017\
In a user specific location in "application data"
o C:\Users\<user>\AppData\Roaming\Autodesk\Revit\Addins\2017\
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>
<FullClassName>Revit.Samples.SampleCommand</FullClassName>
<Text>Sample command</Text>
<VisibilityMode>NotVisibleInFamily</VisibilityMode>
<VisibilityMode>NotVisibleInMEP</VisibilityMode>
<AvailabilityClassName>Revit.Samples.SampleAccessibilityCheck
</AvailabilityClassName>
</AddIn>
</RevitAddIns>
5
Autodesk Revit Kepler API – Getting Started
AvailabilityClassName The full name of the class in the assembly file which implemented
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
6
Autodesk Revit Kepler API – Getting Started
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.
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.
7
Autodesk Revit Kepler API – Getting Started
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.
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
8
Autodesk Revit Kepler API – Getting Started
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.
10
Autodesk Revit Kepler API – Getting Started
11
Autodesk Revit Kepler API – Getting Started
Appendix 1 – Glossary of Autodesk Revit terms
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 element’s 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 user’s 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 user’s parameter file,
Make the applications parameter file current,
Load the required parameter(s) and,
Make the user’s parameter file current.
12
Autodesk Revit Kepler 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 element’s
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 symbol’s geometry, since inserting
them into walls of different thickness changes each window’s 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.
If there is no door instance in a model, there is only one “master” FamilySymbol for a 36”x84”
door, but if a door is inserted, Autodesk Revit will create a “slave” FamilySymbol for the new
36”x84” Door.
13
Autodesk Revit Kepler API – Getting Started
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 Kepler API – Getting Started
A: There is no direct way in the Autodesk Revit database to tell if a beam has a cantilever.
However one or more of the options below can give you a good guess at whether a beam is a
cantilever:
Options
1. There are two parameters called Moment Connection Start and Moment Connection End. If
the value that is set for these two is not None then you should look and see if there is a
beam that is co-linear and also has the value set to something other than None. You could
also ask the user to make sure to select the Cantilever Moment option rather than Moment
Frame option.
2. Trace the connectivity back beyond the element say one or two elements.
3. Look at element release conditions.
15