self revit base element selection transaction collector geometry builtins forms console utils dynamo revitpythonshell exceptions tests
known_issues
Version:
modindex
| genindex
Revit Python Wrapper was created to help Python programmers write Revit API code.
Wrapper classes make the interaction with API objects less repetitive, and more consistent with Python's conventions.
It also provides a few convenient shortcuts:
- Initializes a
doc
anduidoc
document handling variables, so you can reuse code across platforms with no change to your import code.- Imports
clr
, and addsRevitAPI.dll
+RevitAPIUI.dll
assemblies.- Adds Reference the IronPython Standard Library to your
sys.path
(fordynamo
).- Easy to use WPF
forms
so you can request additional user input with little effort.
Caution
| This library should not be used in complex applications or mission-critical work. | API changes are expected.
There are several ways to use RevitPythonWrapper:
revitpythonshell
: import the library directly into the interactive interpreterdynamo
: Install through Dynamo Package Manager- pyRevit : import it into your scripts
- Macros: import library into your Python Macros (untested).
- Normalizes Document and Application handlers for Revit + Dynamo
- Increase code re-use across platforms (ie.
revit
)- Implements patterns to reduce repetitive tasks (ie.
rpw.db.transaction
)- Handles some data-type casting for speed and flexibility (ie.
rpw.db.Parameter
)- Normalizes API calls for different Revit Versions (not yet implemented)
RevitPythonWrapper has been tested on the following platforms:
- RevitPythonShell + Revit: 2015, 2016, 2017
- pyRevit: 2015, 2016
- Dynamo: 1.2
The examples below give a basic overview of how the library is used, paired with an example sans-rpw.
>>> # Handles Document Manager and namespace imports for RevitPythonShell and Dynamo >>> from rpw import doc, uidoc, DB, UI >>> uidoc.ActiveView
Without RPW
>>> # Dynamo Example >>> import clr >>> clr.AddReference('RevitAPI') >>> clr.AddReference('RevitAPIUI') >>> from Autodesk.Revit.DB import * >>> from Autodesk.Revit.UI import * >>> >>> clr.AddReference("RevitServices") >>> import RevitServices >>> from RevitServices.Persistence import DocumentManager >>> from RevitServices.Transactions import TransactionManager >>> >>> doc = DocumentManager.Instance.CurrentDBDocument >>> uiapp = DocumentManager.Instance.CurrentUIApplication >>> app = uiapp.Application >>> uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
>>> # Using Wrapper - Same code for RevitPythonShell, and Dynamo >>> import rpw >>> from rpw import doc >>> with rpw.db.transaction('Delete Object'): >>> doc.Remove(SomeElementId)
Without RPW
>>> # Typical Transaction In Dynamo >>> import clr >>> clr.AddReference("RevitServices") >>> import RevitServices >>> from RevitServices.Persistence import DocumentManager >>> from RevitServices.Transactions import TransactionManager >>> doc = DocumentManager.Instance.CurrentDBDocument >>> TransactionManager.Instance.EnsureInTransaction(doc) >>> doc.Remove(SomeElementId) >>> TransactionManager.Instance.TransactionTaskDone()
>>> # Typical Transaction in Revit Python Shell / pyRevit >>> import clr >>> clr.AddReference('RevitAPI') >>> from Autodesk.Revit.DB import Transaction >>> doc = __revit__.ActiveUIDocument.Document >>> >>> transaction = Transaction(doc, 'Delete Object') >>> transaction.Start() >>> try: >>> doc.Remove(SomeElementId) >>> except: >>> transaction.RollBack() >>> else: >>> transaction.Commit()
>>> import rpw >>> selection = rpw.ui.selection() >>> selection[0] < Autodesk.Revit.DB.Element > >>> selection.elements [< Autodesk.Revit.DB.Element >]
>>> # Other Features >>> selection.clear() >>> selection.add(DB.Element or DB.ElementId)
Without RPW
>>> # In Revit Python Shell >>> uidoc = __revit__.ActiveUIDocument # Different for Dynamo >>> selection_ids = uidoc.Selection.GetElementIds() >>> selected_elements = [doc.GetElemend(eid) for eid in selection_ids]
>>> import rpw >>> element = rpw.db.element(SomeRevitElement) >>> with rpw.db.transaction('Set Comment Parameter'): >>> element.parameters['Comments'].value = 'Some String' >>> >>> # Parameters >>> element.parameters['some value'].type <type: string> >>> element.parameters['some value'].value 'Some String' >>> element.parameters.builtins['WALL_LOCATION_LINE'].value 1
Access to original attributes, and parameters are provided by the
Element
wrapper. More Specialized Wrappers also provide additional features based on its type:DB.FamilyInstace
(Instance
),DB.FamilySymbol
(Symbol
),DB.Family
(Family
), andDB.Category
(Category
).>>> instance = rpw.Instance(SomeFamilyInstance) >>> instance.parameters['Comments'] 'Comment' >>> instance.parameters['Comments'].value = 'Your Comment' # Comment set >>> instance.parameters.builtins['SOME_BUILT_IN'].value 'Parameter Value' >>> instance.symbol <RPW_Symbol:72" x 36"> >>> instance.symbol.name '72" x 36"' >>> instance.family <RPW_Family:desk> >>> instance.family.name 'desk' >>> instance.category <RPW_Category:Furniture> >>> instance.symbol.instances [<RPW_Instance:72" x 36">, <RPW_Instance:72" x 36">, ... ]
>>> import rpw >>> walls = rpw.db.collector(of_class='Wall').elements [< instance DB.Wall>, < instance DB.Wall>, < instance DB.Wall>, etc]
>>> aview = rpw.db.collector(of_category='OST_Views', is_element_type=True).first < instance DB.View>
Without RPW
>>> # Typical API Example: >>> from Autodesk.Revit.DB import FilteredElementCollector, WallType >>> collector = FilteredElementCollector() >>> walls = FilteredElementCollector.OfClass(WallType).ToElements()
>>> import rpw >>> filter_rule = rpw.db.parameterFilter(some_param_id, greater=3) >>> collector = rpw.db.collector(of_class='WallType', paramter_filter=filter_rule)
Without RPW
>>> # Typical API Example: >>> from Autodesk.Revit.DB import FilteredElementCollector, WallType >>> from Autodesk.Revit.DB import ParameterFilterRuleFactory, ElementParameterFilter >>> >>> rule = ParameterFilterRuleFactory.CreateEqualsRule(param_id, some_string, False) >>> filter_rule = ElementParameterFilter(rule) >>> collector = FilteredElementCollector.OfClass(WallType).WherePasses(filter_rule)
>>> options = ['Option 1','Option 2','Option 3'] >>> form = SelectFromList('Window Title', options) >>> # Form shows on screen >>> form_ok = form.show() >>> if not form_ok: >>> sys.exit() # User Canceld >>> selected_item = form.selected
>>> # Handy Batch Converters to and from Element / ElementIds >>> rpw.utils.to_elements(DB.ElementId) [ DB.Element ] >>> rpw.utils.to_elements(20001) [ DB.Element ] >>> rpw.utils.to_elements([20001, 20003]) [ DB.Element, DB.Element ]
>>> rpw.utils.to_element_ids(DB.Element) [ DB.ElementId ] >>> rpw.utils.to_element_ids(20001) [ DB.ElementId ] >>> rpw.utils.to_element_ids([20001, 20003]) [ DB.ElementId, DB.ElementId ]