Cheat Sheet¶
This is a collection of some useful little snippets and examples that can help you getting started quickly.
Imports¶
To make use of Revitron and the _() function the following items have to be imported:
import revitron
from revitron import _
The revitron
module also exposes some useful properties such as the active document and gives you access
to the Autodesk.Revit.DB
class as well as follows:
doc = revitron.DOC
db = revitron.DB
Selection¶
Getting all selected elements:
selection = revitron.Selection.get()
Getting the first element in the selection:
elements = revitron.Selection.first()
Elements¶
Printing the category and class names for of all selected elements:
for el in revitron.Selection.get():
name = _(el).getClassName()
cat = _(el).getCategoryName()
print(name, cat)
Dependent Elements¶
You can get a list of all dependent elements of a given element as follows:
dependents = _(el).getDependent()
For example the following loop prints the ElementIds
of all dependent elements of the selected elements:
for el in revitron.Selection.get():
for d in _(el).getDependent():
print(d.Id)
Parameters¶
Getting a parameter value of all Revit elements in the selection:
for el in revitron.Selection.get():
name = _(el).get('Name')
print(name)
Getting the Family and Type
parameter as id
and string
of elements in the selection.
Note that here we want actually the value string instead of an ElementId
.
So the first function will return the ElementId
and the second one will give us the actual string.
for el in revitron.Selection.get():
famTypeId = _(el).get('Family and Type')
famType = _(el).getParameter('Family and Type').getValueString()
print(famTypeId)
print(famType)
Setting a parameter value. Note that a shared parameter will be created and bound to the element’s
category in case the parameter doesn’t exist yet. The el
variable contains a Revit element.
t = revitron.Transaction()
_(el).set('Cool Parameter', 'Hello there!')
t.commit()
By default the parameter type will be text
.
It is possible to pass a third argument to the function to set a parameter type:
t = revitron.Transaction()
_(el).set('Cool Integer Parameter', 10, 'Integer')
t.commit()
Filters¶
Getting all elements of a certain category, specified by a string, for example “Room”:
rooms = revitron.Filter().byCategory('Rooms').getElements()
for room in rooms:
print(_(room).get('Name'))
Instead of the natural category name, it is also valid to use the string representation of a built-in category as filter argument:
rooms = revitron.Filter().byCategory('OST_Rooms').getElements()
Note
A full list of natural category names and their corresponding built-in categories can be found here.
Filtering those rooms by filtering the Name
“beginning with the word Room” can be done as follows.
Note the flexible way of breaking down the filtering into multiple line for better readability:
fltr = revitron.Filter()
fltr = fltr.byCategory('Rooms')
fltr = fltr.byStringBeginsWith('Name', 'Room')
for room in fltr.getElements():
print(_(room).get('Name'))
The filter can be inverted to get only rooms with a Name
value “not beginning with Room”:
fltr = revitron.Filter()
fltr = fltr.byCategory('Rooms')
fltr = fltr.byStringBeginsWith('Name', 'Room', True)
for room in fltr.getElements():
print(_(room).get('Name'))
Getting intersecting elements of one selected element and for example printing their category name:
el = revitron.Selection.first()
for intEl in revitron.Filter().byIntersection(el).getElements():
print(_(intEl).getCategoryName())
Room Geometry¶
Printing or getting the boundary points of one (first) selected room:
el = revitron.Selection.first()
points = _(el).getBoundaryPoints()
for p in points:
print(p)
Or with an inset:
el = revitron.Selection.first()
points = _(el).getBoundaryInsetPoints(0.2)
for p in points:
print(p)
Get list of all boundary segments of the first selected room:
el = revitron.Selection.first()
boundary = _(el).getBoundary()
for segment in boundary:
print(segment)
Bounding Boxes¶
Getting the Revitron bounding box object of the first element in the selection:
el = revitron.Selection.first()
bbox = _(el).getBbox()
print(bbox)
Document Context¶
In order to filter elements and using revitron.Filter
with any other model than the active one,
it is possible to temporarily change the document context as follows:
with revitron.Document(anyOtherDoc):
fltr = revitron.Filter().noTypes()
elements = fltr.getElements()
Storing Configurations¶
You can store any kind of easily serializable data such as string
, number
, list
or dict
in a config storage container as follows:
data = {'some': 'content'}
revitron.DocumentConfigStorage().set('my.namespace', data)
To get data out of that storage simply do the following. Note that you can pass a default value as a second argument to be returned in case there is no data stored yet and you don’t want to check for existence first:
config = revitron.DocumentConfigStorage().get('my.namespace', dict())
print(config.get('some'))