Managing shared parameters is a pain. The standard Revit GUI makes hard to create/manage a lot of parameters. The first version of the shared parameter manager is pretty old but was not as handy to use. Making an easy to use one was a tough job.
In this script is used a WPF DataGrid which nicely displays objects data and is able to auto generate a column for each attribute of an object including drop-down menu (aka combo-box) for enumeration, checkbox for boolean, textbox for sub-objects which can be represented by a string. But for some reason you have to click once to select the row and a second time to edit (check a box, activate drop-down or text edition) which makes it pretty unusable for edition. To solve this you need manually generate all columns with a data template, add handler to make bidirectional communication between displayed data and data in the background really used, sorting etc…
Some subjects are more complex that expected like actually sorting. As often I found answers on stack overflow : https://stackoverflow.com/questions/16956251/sort-a-wpf-datagrid-programmatically
As always source code is available. You’ll find it on github : https://github.com/CyrilWaechter/pyRevitMEP
My first objective with this tool and other related tools is to be able to easily add IFC parameters to objects (families) and project in order to produce better IFC files thereby improving interoperability.
Someone told me that the common add-in they were using to make transition between 2 elements was discontinued in Revit 2018. So I made mine I’ve been surprised about how easy and short it is :
with rpw.db.Transaction("Create transition"):
doc.Create.NewTransitionFitting(connector1, connector2)
So the only thing you need is to prompt user to pick 2 elements (targeting desired connectors) and it is exactly what I described in my previous article.
Even when you master it. Connecting MEP elements in order to get a fully functional system with flow and pressure loss calculation is a pain. So learn Revit API ways to connect things is a good way help you manage this.
Connecting 2 elements and possible funny possible results
Get connector manager
Connector manager is the way to retrieve connectors and all info about it. When you talk about MEP elements there is 2 main kind of elements. MEPCurve (duct, pipe, cable tray, wire etc…) and FamilyInstance (fittings, mechanical equipments, plug, lights etc…) and ConnectorManager is retrieved in 2 different ways :
MEPCurve -> MEPCurve.ConnectorManager
FamilyInstance -> FamilyInstance.MEPModel.ConnectorManager
So it is handy to have a function to retrieve it on any object :
def get_connector_manager(element):
# type: (Element) -> ConnectorManager
"""Return element connector manager"""
try:
# Return ConnectorManager for pipes, ducts etc…
return element.ConnectorManager
except AttributeError:
pass
try:
# Return ConnectorManager for family instances etc…
return element.MEPModel.ConnectorManager
except AttributeError:
raise AttributeError("Cannot find connector manager in given element")
Get a connector
As much as I know there is no way to ask user to select a connector. So you need to figure out what connector user want to designate. A good way is to check which connector is the closest (e.g. closest to user selection point or to an other object). A function to check this is then also handy :
def get_connector_closest_to(connectors, xyz):
# type: (ConnectorSet, XYZ) -> Connector
"""Get connector from connector set iterable closest to an XYZ point"""
min_distance = float("inf")
closest_connector = None
for connector in connectors:
distance = connector.Origin.DistanceTo(xyz)
if distance < min_distance:
min_distance = distance
closest_connector = connector
return closest_connector
Connecting 2 connectors
This is actually really easy. Every connector has a Connector.ConnectTo(<other_connector>) method. This method is very permissive both connectors do not need to be at the same place which is a good thing. That’t how you can connect lights, plugs etc… to their panel without any physical connection. And it can lead to funny things :
How many architect have dreamed of wireless piping and ventilation ?
Physical connection
Logical connection only for duct and pipes is funny but also unstable and it is not what we want to achieve right ? So we also want them to be physically connected.
Each connectors has it’s own coordinate system. If you do families you have probably noticed that if you don’t set it correctly it leads to strange behaviour. Basis X and Y define the plane to land the other connector. Basis Z show in which direction connection is made. When 2 connectors are connected their origin has same coordinates, XY planes are coplanar and their Z basis are in opposite direction.
Rotate
We need to handle 3 cases :
connectors z basis are collinear and :
have opposite direction -> good, nothing to do.
have same direction -> not good, we need to rotate it of 180° (π) on their X or Y axis.
connectors coordinate systems are not collinear -> not good, we need to determine the angle and axis to make them collinear and in opposite direction. To determine the angle XYZ has an handy method xyz1.AngleTo(xyz2) which return the angle between 2 vectors, we then just need add or subtract π. To determine the axis we use xyz1.CrossProduct(xyz2) which return an XYZ (vector) perpendicular to the plane defined by the 2 XYZ.
# If connector direction is same, rotate it
angle = moved_direction.AngleTo(target_direction)
if angle != pi:
if angle == 0:
vector = moved_connector.CoordinateSystem.BasisY
else:
vector = moved_direction.CrossProduct(target_direction)
try:
line = Line.CreateBound(moved_point, moved_point+vector)
moved_element.Location.Rotate(line, angle - pi)
# Revit don't like angle and distance too close to 0
except Exceptions.ArgumentsInconsistentException:
logger.debug("Vector : {} ; Angle : {}".format(vector, angle))
Move
Easy part. We just need to translate one of the object by the difference of both connectors origin.
We can now ask user to select the object+connector he wants to connect and the target object+connector.
PickObject and awesome pyRevit WarningBar
PickObject from Autodesk.Revit.UI.Selection namespace do not return an element as we could expect but a Reference. In our case, it is pretty useful as a Reference has a GlobalPoint property which stores where (XYZ) the user clicked on the object. Then we just use our get_connector_closest_to function.
I also wanted to point out one of the pyRevit feature introduced in 4.5 : the WarningBar which is a much more visible way to show user what to do. It looks like this :
It can be used very easily with in a with statement. It shows up when you enter and vanishes when you exit :
with forms.WarningBar(title="Pick element to move and connect"):
reference = uidoc.Selection.PickObject(ObjectType.Element, NoInsulation(), "Pick element to move")
Facilitate selection
You maybe noticed the NoInsulation ISelectionFilter I introduced as a parameter in PickObject prompt. It just prevent user from selecting insulation and objects that don’t have a connector manager.
Some very simple script can help you achieve tasks of missing features in Revit. As much as I know there is no way to accurately place a label to origin manually. A way to place it as well as possible is to put 0 as sample value and try to align it as well as possible by zooming. Sometimes when you activate a leader your line isn’t straight even if your annotation is perfectly perpendicular to your object just because your annotation family is not perfectly aligned on 0,0. The following very simple script will solve it your annotation family will be perfectly aligned on 0,0 :
# coding: utf8
import rpw
from rpw import DB
__doc__ = "Designed for annotation families. It moves selected annotation to center (set 0,0,0 coordinates)"
__title__ = "Center Text"
__author__ = "Cyril Waechter"
__context__ = 'Selection'
with rpw.db.Transaction():
for text_element in rpw.ui.Selection():
text_element.Coord = DB.XYZ()
The tool is available in pyRevitMEP. Just update it from pyRevit tab.
This batch renaming utility elaboration has been a long journey. I did a simple one long time ago which was supporting only 1 renaming pattern : http://pythoncvc.net/?p=27
Principle
We use parameters available in our views to rename them in a programmatic and logic way. My old script was using an home parameter for discipline and sub-discipline which is set by view template, level and scope box name to make it unique as much as possible.
Objectives
Flexibility : clients, contractors or someone else may ask a specific naming pattern for views. So pattern need to be easily adaptable and savable.
Multilingual : as always, to avoid language dependent behaviour, when it is possible built in parameter name or shared parameter Guid shall be used to retrieve values.
Human readable : parameters storage type may be text, number, id etc… and have a better human readable AsValueString (it is the case for scope boxe).
Free separators choice : we should be able to freely choose and change separator, prefix, suffix between parameters
Flexible selection of view to rename : be able to rename X views then select Y other views and rename it and so on.
Code behind in short
Renaming part
I was thinking about common file re-namers like Ant Renamer. User is able to type parameters as a simple string and add anything he wants between them. The string looks like the old string formatting style with %s %d etc…
So my first attempt was to use python string format like "string{Guid.<Guid>}_{bip.<BuiltInParameterName>_{<parameter_name>}".format(parameter_by_Guid.value, parameter_by_builtinparameter.value, parameter_by_name.value). That was working but somehow it felt weird and slow.
I finally used regular expressions which are very much used for this kind of task in a very efficient way (spelling correction, retrieve a phone number, verify that an email entered by a user looks like one etc…).
So we needed a regular expression to retrieve parameters by Guid :
# Create a regular expression to retrieve Guid (including constructor) in a string
parameterGuidRegex = re.compile(r'''
Guid\( # Guid constructor. Example : Guid("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4")
[\" | \'] # Single or double quote required in Guid constructor
([0-9a-f]{8} # First 8 hexa
(-?[0-9a-f]{4}){3} # Optional separator + 4 hexa 3 times
-?[0-9a-f]{12}) # Optional separator + 12 hexa
[\" | \'] # Single or double quote required in Guid constructor
\) # Close parenthesis necessary to build a Guid
''', re.IGNORECASE | re.VERBOSE)
… by BuiltInParameter (way far simpler) :
# Create a regular expression to retrieve BuiltInParameter in a string
parameterBipRegex = re.compile(r"bip\((\w+)\)")
… and finally by name :
# Create a regular expression to retrieve named parameters in a string
parameterNameRegex = re.compile(r"name\(([\w\s]+)\)") # Retrieve group(1)
There is mainly 3 class of view (excluding schedules, legends etc…) : ViewPlan, View3D, ViewSection. Each kind don’t have the same available parameter. For example there is no level parameter in 3D and section views. So 3 pattern definition was required to batch rename views. To show available parameters to user a sample view of each class is retrieved and parameters Guid, BuiltInParameterName, and Name are stored as a dictionary.
To help user add parameter to pattern the best way. A function try to retrieve Guid else BuiltInParameter name else name.
Another function replace pattern by their associated parameter name and value to show user a preview.
A last function is in charge of actually renaming views.
GUI part
A WPF GUI splitted in 4 part, 3 for each view classes, 1 for saving and validation options.
Saving/loading part
Thanks again to Ehsan, there is a very easy way to save script parameter to pyRevit config file. Don’t forget to encode and decode your string !
my_config = this_script.config
# Then in WPF class we have following method :
def save_config_click(self, sender, e):
for view_class in self.view_class_dict.keys():
view_class_checkbox = eval('self.cb_{}'.format(view_class))
if view_class_checkbox.IsChecked:
pattern_textbox = eval('self.{}_pattern'.format(view_class))
setattr(my_config, view_class, pattern_textbox.Text.encode('utf8'))
this_script.save_config()
But I wanted also to be able to save to a parameter for project specific naming rules. Unfortunately with Revit API it is still currently not possible to create a project parameter which is not shared. So based on Spiderinnet samples I wrote a function to create a shared parameter and store configuration in it as a string which is formatted in a dictionary/json style.
When script is launched. It tries to load configuration from project parameter (as project specific case should be use first) else from pyRevit config file (the way you name it on your office) else from default values (which are for most people just samples).
Modeless / ExternalEvent part
To be able to rename successively multiple selections of views it was mandatory to use a modelless WPF window and so to use ExternalEvent for any Transaction to Revit : create shared parameter, save to parameter, rename views. I described how to do so in a previous article but as there was 3 different action to make it was a good idea to enhance it. I made 2 class to be able to execute any function or method through the external event using *args and *kwargs :
class CustomizableEvent:
def __init__(self):
self.function_or_method = None
self.args = ()
self.kwargs = {}
def raised_method(self):
self.function_or_method(*self.args, **self.kwargs)
def raise_event(self, function_or_method, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.function_or_method = function_or_method
custom_event.Raise()
customizable_event = CustomizableEvent()
# Create a subclass of IExternalEventHandler
class CustomHandler(IExternalEventHandler):
"""Input : function or method. Execute input in a IExternalEventHandler"""
# Execute method run in Revit API environment.
# noinspection PyPep8Naming, PyUnusedLocal
def Execute(self, application):
try:
customizable_event.raised_method()
except InvalidOperationException:
# If you don't catch this exeption Revit may crash.
print "InvalidOperationException catched"
# noinspection PyMethodMayBeStatic, PyPep8Naming
def GetName(self):
return "Execute an function or method in a IExternalHandler"
# Create an handler instance and his associated ExternalEvent
custom_handler = CustomHandler()
custom_event = ExternalEvent.Create(custom_handler)
Thanks to his creator Ehsan Iran-Nejad and all credits we have a great tool named pyRevit. This tool allows us to easily make python scripts for Revit and distribute them via the extensions manager.
And it is exactly what I did by creating pyRevitMEP extension which has been added by Ehsan to pyRevit extensions manager last week-end :
pyRevitMEP philosophy : encourage MEP people to group-up and develop common tools. You want to contribute by coding : send a pull request on github or more (do not hesitate to contact me). If you prefer to manage your own extension, add your tools in the /pyRevitMEP.tab/ and follow instructions on pyRevit blog to add it to extensions manager. You want to contribute but don’t know how, check FAQ page. License is and will remain open source. It is currently under GNU GPL v3.0 License.
More than 2 years ago I made an article to show a way to rotate element using Revit API. Using external events in a modeless form as described in my previous article you can for example make a GUI to get axis and angle from user inputs. It is also using ISelectionFilter as described in this previous article. The new thing is that I use a standard class to store rotation parameters. This way parameters are dynamically feed to methods which are run in external events.
Let’s see this in action :
Full source code with comments (designed to be used in pyRevit) :
from revitutils import doc, uidoc
from scriptutils.userinput import WPFWindow
# noinspection PyUnresolvedReferences
from Autodesk.Revit.DB import Transaction, ElementTransformUtils, Line, XYZ, Location, UnitType, UnitUtils
# noinspection PyUnresolvedReferences
from Autodesk.Revit.UI.Selection import ObjectType, ISelectionFilter
# noinspection PyUnresolvedReferences
from Autodesk.Revit.UI import IExternalEventHandler, IExternalApplication, Result, ExternalEvent, IExternalCommand
# noinspection PyUnresolvedReferences
from Autodesk.Revit.Exceptions import InvalidOperationException, OperationCanceledException
__doc__ = "Rotate object in any direction"
__title__ = "3D Rotate"
__author__ = "Cyril Waechter"
# Get current project units for angles
angle_unit = doc.GetUnits().GetFormatOptions(UnitType.UT_Angle).DisplayUnits
def xyz_axis(element_id):
"""Input : Element, Output : xyz axis of the element"""
origin = doc.GetElement(element_id).Location.Point
xyz_direction = [XYZ(origin.X + 1, origin.Y, origin.Z),
XYZ(origin.X, origin.Y + 1, origin.Z),
XYZ(origin.X, origin.Y, origin.Z + 1)]
axis = []
for direction in xyz_direction:
axis.append(Line.CreateBound(origin, direction))
return axis
class AxisISelectionFilter(ISelectionFilter):
"""ISelectionFilter that allow only which have an axis (Line)"""
# noinspection PyMethodMayBeStatic, PyPep8Naming
def AllowElement(self, element):
if isinstance(element.Location.Curve, Line):
return True
else:
return False
def axis_selection():
"""Ask user to select an element, return the axis of the element"""
try:
reference = uidoc.Selection.PickObject(ObjectType.Element, AxisISelectionFilter(), "Select an axis")
except OperationCanceledException:
pass
else:
axis = doc.GetElement(reference).Location.Curve
return axis
class RotateElement(object):
"""class used to store rotation parameters. Methods then rotate elements."""
def __init__(self):
self.selection = uidoc.Selection.GetElementIds()
self.angles = [0]
def around_itself(self):
"""Method used to rotate elements on themselves"""
try:
t = Transaction(doc, "Rotate around itself")
t.Start()
for elid in self.selection:
el_axis = xyz_axis(elid)
for i in range(3):
if self.angles[i] == 0:
pass
else:
ElementTransformUtils.RotateElement(doc, elid, el_axis[i], self.angles[i])
t.Commit()
except InvalidOperationException:
import traceback
traceback.print_exc()
except:
import traceback
traceback.print_exc()
def around_axis(self):
"""Method used to rotate elements around selected axis"""
try:
axis = axis_selection()
t = Transaction(doc, "Rotate around axis")
t.Start()
ElementTransformUtils.RotateElements(doc, self.selection, axis, self.angles)
t.Commit()
except InvalidOperationException:
import traceback
traceback.print_exc()
except:
import traceback
traceback.print_exc()
finally:
uidoc.Selection.SetElementIds(rotate_elements.selection)
rotate_elements = RotateElement()
# Create a subclass of IExternalEventHandler
class RotateElementHandler(IExternalEventHandler):
"""Input : function or method. Execute input in a IExternalEventHandler"""
# __init__ is used to make function from outside of the class to be executed by the handler. \
# Instructions could be simply written under Execute method only
def __init__(self, do_this):
self.do_this = do_this
# Execute method run in Revit API environment.
# noinspection PyPep8Naming, PyUnusedLocal
def Execute(self, application):
try:
self.do_this()
except InvalidOperationException:
# If you don't catch this exeption Revit may crash.
print "InvalidOperationException catched"
# noinspection PyMethodMayBeStatic, PyPep8Naming
def GetName(self):
return "Execute an function or method in a IExternalHandler"
# Create handler instances. Same class (2 instance) is used to call 2 different method.
around_itself_handler = RotateElementHandler(rotate_elements.around_itself)
around_axis_handler = RotateElementHandler(rotate_elements.around_axis)
# Create ExternalEvent instance which pass these handlers
around_itself_event = ExternalEvent.Create(around_itself_handler)
around_axis_event = ExternalEvent.Create(around_axis_handler)
class RotateOptions(WPFWindow):
"""
Modeless WPF form used for rotation angle input
"""
def __init__(self, xaml_file_name):
WPFWindow.__init__(self, xaml_file_name)
self.set_image_source("xyz_img", "XYZ.png")
self.set_image_source("plusminus_img", "PlusMinusRotation.png")
# noinspection PyUnusedLocal
def around_itself_click(self, sender, e):
try:
rotate_elements.selection = uidoc.Selection.GetElementIds()
angles = [self.x_axis.Text, self.y_axis.Text, self.z_axis.Text]
for i in range(3):
angles[i] = UnitUtils.ConvertToInternalUnits(float(angles[i]), angle_unit)
rotate_elements.angles = angles
except ValueError:
self.warning.Text = "Incorrect angles, input format required '0.0'"
else:
self.warning.Text = ""
around_itself_event.Raise()
# noinspection PyUnusedLocal
def around_axis_click(self, sender, e):
try:
rotate_elements.angles = UnitUtils.ConvertToInternalUnits(float(self.rotation_angle.Text), angle_unit)
rotate_elements.selection = uidoc.Selection.GetElementIds()
except ValueError:
self.warning.Text = "Incorrect angles, input format required '0.0'"
else:
around_axis_event.Raise()
RotateOptions('RotateOptions.xaml').Show()
Warning : Some change in Revit API and Revit behaviour makes this article obsolete with newer version of Revit. I advise you to install pyRevitMEP extension and study updated code : FormExternalEventHandler.pushbutton. For usage in your script I recommend to use my CustomizableEvent from pyRevitMEP library.
I struggled for a while to make a modeless form. Why did I need it ? Because each time I was trying to get user to select object after WPF appear I was going out of Revit API thread and got this very common exception «Autodesk.Revit.Exceptions.InvalidOperationException: Attempting to create an ExternalEvent outside of a standard API execution». As Jeremy Tammik says :
One of the most frequently raised questions around the Revit API is how to drive Revit from outside, e.g., from a separate thread, a modeless dialogue, or a stand-alone executable.
I have read many examples on the subject. Most on them were in C#.
Some post pointed out the ModelessDialog sample available in Revit SDK : it helps me to understand how it should behave but it was still a bit too complex for my C# knowledge.
This AEC DevBlog article make me wonder «Is my code really working or I missed something and I was only lucky in situation I tested ?»
So I made a very simple form to make a very simple ExternalEventHandler sample as pyRevit script. It will help me and I hope it will help some hackers to struggle less than I did.
Let’s start with common import statement using built-in pyRevit utils :
# noinspection PyUnresolvedReferences
from Autodesk.Revit.UI import IExternalEventHandler, ExternalEvent
# noinspection PyUnresolvedReferences
from Autodesk.Revit.DB import Transaction
# noinspection PyUnresolvedReferences
from Autodesk.Revit.Exceptions import InvalidOperationException
from revitutils import selection, uidoc, doc
from scriptutils.userinput import WPFWindow
__doc__ = "A simple modeless form sample"
__title__ = "Modeless Form"
__author__ = "Cyril Waechter"
Then let’s write a simple function we want to execute modeless (here it just delete selected elements) :
# Simple function we want to run
def delete_elements():
t = Transaction(doc, "Failing script")
t.Start()
for elid in uidoc.Selection.GetElementIds():
print elid
doc.Delete(elid)
t.Commit()
And now come the new magic thing that let you enter in a valid Revit API context. The «ExternalEvent» class with his «IExternalEventHandler» class :
# Create a subclass of IExternalEventHandler
class SimpleEventHandler(IExternalEventHandler):
"""
Simple IExternalEventHandler sample
"""
# __init__ is used to make function from outside of the class to be executed by the handler. \
# Instructions could be simply written under Execute method only
def __init__(self, do_this):
self.do_this = do_this
# Execute method run in Revit API environment.
def Execute(self, uiapp):
try:
self.do_this()
except InvalidOperationException:
# If you don't catch this exeption Revit may crash.
print "InvalidOperationException catched"
def GetName(self):
return "simple function executed by an IExternalEventHandler in a Form"
# Now we need to make an instance of this handler. Moreover, it shows that the same class could be used to for
# different functions using different handler class instances
simple_event_handler = SimpleEventHandler(delete_elements)
# We now need to create the ExternalEvent
ext_event = ExternalEvent.Create(simple_event_handler)
Let’s do a simple form so easily created thanks to pyRevit in order to use our new toy :
# A simple WPF form used to call the ExternalEvent
class ModelessForm(WPFWindow):
"""
Simple modeless form sample
"""
def __init__(self, xaml_file_name):
WPFWindow.__init__(self, xaml_file_name)
self.simple_text.Text = "Hello World"
self.Show()
def delete_click(self, sender, e):
# This Raise() method launch a signal to Revit to tell him you want to do something in the API context
ext_event.Raise()
# Let's launch our beautiful and useful form !
modeless_form = ModelessForm("ModelessForm.xaml")
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Plumbing import PipeSegment
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
#Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
dataEnteringNode = IN
elem = doc.GetElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)
for dn, di, de in zip(IN[1], IN[2], IN[3]):
DN = UnitUtils.ConvertToInternalUnits(dn , DisplayUnitType.DUT_MILLIMETERS)
Di = UnitUtils.ConvertToInternalUnits(di , DisplayUnitType.DUT_MILLIMETERS)
De = UnitUtils.ConvertToInternalUnits(de , DisplayUnitType.DUT_MILLIMETERS)
try:
elem.AddSize(MEPSize(DN,Di,De,True,True))
except:
elem.RemoveSize(DN)
elem.AddSize(MEPSize(DN,Di,De,True,True))
#Affectez la sortie à la variable OUT.
OUT = IN