1 Revit API Intro
1 Revit API Intro
Introduction
Products, SDK, documentation and samples
Database Fundamentals
Understanding the representation of Revit elements
Element iteration, filtering and queries
Element modification
Model creation
Overview
Products, SDK and assembly dlls
Revit Products Introduction to Revit Programming
Read once
Read Me First.doc
Getting Started with the Revit API.docx
Revit Platform API Changes and Additions.docx
Familiarize yourself with
Revit API Developer’s Guide (http://www.autodesk.com/revitapi-help )
RevitAPI.chm
What's New section is similar to Changes and Additions doc
Read if needed
RevitAddInUtility.chm – installer
Autodesk Icon Guidelines.pdf – user interface
Macro Samples – Revit Macros
Revit Server SDK – file access on server
Revit Structure – section definitions and material properties
REX SDK – Revit extensions framework
Structural Analysis SDK – Analysis and code checking
Important utilities
Add-In Manager
Samples
Documentation
ReadMeFirst.doc
Main samples solution
SDKSamples.sln
And the samples themselves!
1. External command
Implement IExternalCommand; install an add-in manifest
Commands are added to the External Tools pulldown in the ribbon Add-Ins tab
Tools > External Tools
2. External application
Implement IExternalApplication; install an add-in manifest
Applications can create new panels in the ribbon Add-Ins tab
External applications can invoke external commands
.NET API
.NET Framework 4.8
Microsoft Visual Studio 2017
C# or VB.NET, managed C++, any .NET compliant language
Class library
References
<revit install folder>\Program\RevitAPI.dll
<revit install folder>\Program\RevitAPIUI.dll
Remember to set 'Copy Local' to False
Getting Started
First Steps to Hello World: External command and add-ins manifest
Steps to Hello World External Command Introduction to Revit Programming
<VB.NET>
'' Hello World #1 - A minimum Revit external command.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class HelloWorld
Implements IExternalCommand
Public Function Execute( _
ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, _
ByVal elements As Autodesk.Revit.DB.ElementSet) _
As Autodesk.Revit.UI.Result _
Implements Autodesk.Revit.UI.IExternalCommand.Execute
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!")
Return Result.Succeeded
End Function
End Class
</VB.NET>
Minimum Code in C#
<C#>
// Hello World #1 - A minimum Revit external command.
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class HelloWorld : IExternalCommand
{
public Autodesk.Revit.UI.Result Execute(
Autodesk.Revit.UI.ExternalCommandData commandData,
ref string message,
Autodesk.Revit.DB.ElementSet elements)
{
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!");
return Result.Succeeded;
}
}
</C#>
IExternalCommand Class
<VB.NET>
'' Hello World #1 - A minimum Revit external command.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class HelloWorld
Implements IExternalCommand
1.Derive a class from IExternalCommand
Public Function Execute( _
ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, _
ByVal elements As Autodesk.Revit.DB.ElementSet) _
As Autodesk.Revit.UI.Result _
Implements Autodesk.Revit.UI.IExternalCommand.Execute
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!")
Return Result.Succeeded
End Function
End Class
</VB.NET>
Execute() Method
<VB.NET>
'' Hello World #1 - A minimum Revit external command.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class HelloWorld
Implements IExternalCommand
2.Implement Execute() method
Public Function Execute( _
ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, _
ByVal elements As Autodesk.Revit.DB.ElementSet) _
As Autodesk.Revit.UI.Result _
Arguments:
Implements Autodesk.Revit.UI.IExternalCommand.Execute
1st Access to the Revit object model
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!")
2nd Message to the user when a
Return Result.Succeeded command fails
3rd A set of elements to be highlighted
End Function
when a command fails
Return value:
End Class
</VB.NET>
• Succeeded
• Failed
• Cancelled
Attributes
<VB.NET>
'' Hello World #1 - A minimum Revit external command.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class HelloWorld
Implements IExternalCommand
Public Function Execute( _ 3. Set attributes
ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef messageAAs
Transaction
String, _mode: controls the transaction behavior
ByVal elements As Autodesk.Revit.DB.ElementSet) _
Manual
As ReadOnly
Autodesk.Revit.UI.Result
_
Implements Autodesk.Revit.UI.IExternalCommand.Execute
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!")
Return Result.Succeeded
End Function
End Class
</VB.NET>
<VB.NET>
'' Hello World #1 - A minimum Revit external command.
<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)> _
Public Class HelloWorld
Implements IExternalCommand
Public Function Execute( _ 4. Show a dialog with a message
ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, _
ByVal elements As Autodesk.Revit.DB.ElementSet) _
As Autodesk.Revit.UI.Result _
Implements Autodesk.Revit.UI.IExternalCommand.Execute
Autodesk.Revit.UI.TaskDialog.Show("My Dialog Title", "Hello World!")
Return Result.Succeeded
End Function
Task Dialog:
End Class
</VB.NET> Revit-style message box
to say “Hello World”
Registration Mechanism
Automatically read by Revit at startup
Windows 7,8,10
C:\ProgramData\Autodesk\Revit\Addins\202x
C:\Users\<user>\AppData\Roaming\Autodesk\Revit\Addins\202x
.addin File
Once .addin manifest is in place, you will see [Add-Ins] tab and
[External Tools] panel. (not visible with no add-ins)
Run your command from the pull down menu
Carrying On …
External application and external command data
External Application Introduction to Revit Programming
<VB.NET>
'' Hello World App - minimum external application
Public Class HelloWorldApp
Implements IExternalApplication
Implement IExternalApplication
'' OnShutdown() - called when Revit ends.
Public Function OnShutdown(ByVal application As UIControlledApplication) _
As Result _
Implements IExternalApplication.OnShutdown
Return Result.Succeeded
End Function OnShutdown is called when Revit ends
'' OnStartup() - called when Revit starts.
Public Function OnStartup(ByVal application As UIControlledApplication) _
As Result _
Implements IExternalApplication.OnStartup
TaskDialog.Show("My Dialog Title", "Hello World from App!")
Return Result.Succeeded
End Function
End Class
</VB.NET>
OnStartup is called when Revit starts
<C#>
// Hello World #3 - minimum external application
public class HelloWorldApp : IExternalApplication
{
// OnStartup() - called when Revit starts.
public Result OnStartup(UIControlledApplication application)
{
TaskDialog.Show("My Dialog Title", "Hello World from App!");
return Result.Succeeded;
}
.addin Manifest
2022
Examples:
'' access to the version of Revit and the title of the document currently in use
Dim versionName As String = _
commandData.Application.Application.VersionName
Dim documentTitle As String = _
commandData.Application.ActiveUIDocument.Document.Title
'' print out wall types available in the current rvt project
Dim collector As New FilteredElementCollector(rvtDoc)
Collector.OfClass(GetType(WallType))
Dim s As String = ""
For Each wType As WallType In wallTypes
s = s + wType.Name + vbCr
Next
<VB.NET>
Public Class DBElement
Implements IExternalCommand
'' member variables
Dim m_rvtApp As Application
Dim m_rvtDoc As Document
Public Function Execute(ByVal commandData As ExternalCommandData, _
...
'' Get the access to the top most objects.
Dim rvtUIApp As UIApplication = commandData.Application
Dim rvtUIDoc As UIDocument = rvtUIApp.ActiveUIDocument
m_rvtApp = rvtUIApp.Application
m_rvtDoc = rvtUIDoc.Document
'' ...
</VB.NET>
Tools
Revit Lookup, Add-In Manager, SDKSamples202x.sln, and RvtSamples
Tools Introduction to Revit Programming
Must Know
Add-In Manager – allows you to load your dll while running Revit
without registering an addin and to rebuild dll without restarting
Revit
DB Element
Understanding the Representation of Revit Element
DB Element Introduction to Revit Programming
Element Basics
Class Derivations
API Object
Element
Cont. Footing
Wall Type Floor Type Cont. Footing
Type
RoofBase
Identifying Element
Element.Parameters
Element.Parameters
<VB.NET>
'' show all the parameter values of the element
Public Sub ShowParameters(ByVal elem As Element, _
Optional ByVal header As String = "")
Dim s As String = header + vbCr + vbCr
Dim params As ParameterSet = elem.Parameters
For Each param As Parameter In params
Dim name As String = param.Definition.Name
'' see the helper function below
Dim val As String = ParameterToString(param)
s = s + name + " = " + val + vbCr
Next
TaskDialog.Show("Revit Intro Lab", s)
End Sub
</VB.NET>
Parameters
If param Is Nothing Then
Return val
End If
ABC '' to get to the parameter value, we need to pause it depending on
'' its strage type
Select Case param.StorageType
Case StorageType.Double
Dim dVal As Double = param.AsDouble
val = dVal.ToString
Case StorageType.Integer
Dim iVal As Integer = param.AsInteger
val = iVal.ToString()
Case StorageType.String
Dim sVal As String = param.AsString
val = sVal
Case StorageType.ElementId
Dim idVal As ElementId = param.AsElementId
val = idVal.IntegerValue.ToString
Case StorageType.None
Case Else
End Select
Return val
End Function
</VB.NET>
<VB.NET>
'' examples of retrieving a specific parameter individlly.
Public Sub RetrieveParameter(ByVal elem As Element, _
Optional ByVal header As String = "")
Dim s As String = header + vbCr + vbCr
'' comments - most of instance has this parameter
'' (1) by name. (Mark - most of instance has this parameter.)
param = elem.Parameter("Mark")
...
'' (2) by BuiltInParameter.
Dim param As Parameter = _
elem.Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
...
'' using the BuiltInParameter, you can sometimes access one
'' that is not in the parameters set.
param = elem.Parameter(BuiltInParameter.SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM)
...
param = elem.Parameter(BuiltInParameter.SYMBOL_FAMILY_NAME_PARAM)
...
</VB.NET>
Location
Location property
Location is further derived in two forms:
LocationPoint - point-based location (e.g., furniture)
LocationCurve – line-based location (e.g., wall)
You will need to cast to LocationPoint or LocationCurve in order
to access more properties.
Element Filtering
Element Iterations, Filtering and Queries
Element Filtering Introduction to Revit Programming
Retrieving an Element
LogicalOrFilter Where elements must pass at least UnionWith() - joins two sets
one of 2 or more filters of independent filters
CurveElementFilter Finds specific types of curve elements (model curves, symbolic curves, none
detail curves, etc)
Collect all the wall types (2nd and 3rd using shortcuts)
<VB.NET>
Dim wallTypeCollector1 = new FilteredElementCollector(m_rvtDoc)
wallTypeCollector1.WherePasses(New ElementClassFilter(GetType(WallType)))
Dim wallTypes1 As IList(Of Element) = wallTypeCollector1.ToElements
</VB.NET>
<VB.NET>
Dim wallTypeCollector2 = new FilteredElementCollector(m_rvtDoc)
wallTypeCollector2.OfClass(GetType(WallType))
</VB.NET>
<VB.NET>
Dim wallTypeCollector3 = _
new FilteredElementCollector(m_rvtDoc).OfClass(GetType(WallType))
</VB.NET>
<VB.NET>
Dim doorTypeCollector = new FilteredElementCollector(m_rvtDoc)
doorTypeCollector.OfClass(GetType(FamilySymbol))
doorTypeCollector.OfCategory(BuiltInCategory.OST_Doors)
Dim doorTypes As IList(Of Element) = doorTypeCollector.ToElements
</VB.NET>
<VB.NET>
Dim doorTypes As IList(Of Element) _
= new FilteredElementCollector(m_rvtDoc) _
.OfClass(GetType(FamilySymbol)) _
.OfCategory(BuiltInCategory.OST_Doors) _
.ToElements
</VB.NET>
<VB.NET>
Dim wallCollector = New
FilteredElementCollector(m_rvtDoc).OfClass(GetType(Wall))
Dim wallList As IList(Of Element) = wallCollector.ToElements
</VB.NET>
<VB.NET>
Dim doorCollector = New FilteredElementCollector(m_rvtDoc). _
OfClass(GetType(FamilyInstance))
doorCollector.OfCategory(BuiltInCategory.OST_Doors)
Dim doorList As IList(Of Element) = doorCollector.ToElements
</VB.NET>
More Options
Element Modification
How to modify an element
Element Modification Introduction to Revit Programming
By Document methods:
Move, Rotate, Mirror, Array, Array without
associate (this will not create a group)
<VB.NET>
'' e.g., an element we are given is a door.
Dim aDoor As FamilyInstance = elem
'' find a door family type with the given name.
Dim newDoorType As Element = ElementFiltering.FindFamilyType( _
GetType(FamilySymbol), "M_Single-Flush", "0762 x 2032mm", _
BuiltInCategory.OST_Doors)
'' assign a new family type.
aDoor.Symbol = newDoorType
</VB.NET>
<VB.NET>
'' or use a general way: ChangeTypeId
aDoor.ChangeTypeId(newDoorType.Id)
</VB.NET>
<VB.NET>
'' rotate by 15 degree around z-axis.
Dim pt1 = XYZ.Zero
Dim pt2 = XYZ.BasisZ
Dim axis As Line = Line.CreateBound(pt1, pt2)
ElementTransformUtils.RotateElement(doc, elem.Id, axis, Math.PI / 12.0)
</VB.NET>
Regeneration of Graphics
m_rvtDoc.Regenerate()
Model Creation
How to create instances of Revit elements
Model Creation Introduction to Revit Programming
Create a Wall
'' get the door type to use.
Dim doorType As FamilySymbol = _
ElementFiltering.FindFamilyType(m_rvtDoc, GetType(FamilySymbol), _
"M_Single-Flush", "0915 x 2134mm", BuiltInCategory.OST_Doors)
'' get the start and end points of the wall.
Dim locCurve As LocationCurve = hostWall.Location
Dim pt1 As XYZ = locCurve.Curve.GetEndPoint(0)
Dim pt2 As XYZ = locCurve.Curve.GetEndPoint(1)
'' calculate the mid point.
Dim pt As XYZ = (pt1 + pt2) / 2.0
'' we want to set the reference as a bottom of the wall or level1.
Dim idLevel1 As ElementId = _
hostWall.Parameter(BuiltInParameter.WALL_BASE_CONSTRAINT).AsElementId
Dim level1 As Level = m_rvtDoc.Element(idLevel1)
'' finally, create a door.
Dim aDoor As FamilyInstance = m_rvtDoc.Create.NewFamilyInstance( _
pt, doorType, hostWall, level1, StructuralType.NonStructural)
End Sub
</VB.NET>
Thank you!
Autodesk is a registered trademark of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice,
and is not responsible for typographical or graphical errors that may appear in this document.