Pipe creation and routing preferences

Hello, everyone!

I have a difficult task and I don’t know if I have enough knowledge to solve it. Could someone help me?

In this case, I would like to know if it is possible to change the routing preferences of existing pipe types in a Revit project directly through Dynamo? Or even create new pipe types and modify them according to the necessary routing preferences?

I am working to automatically create pipes in my project, change their properties and routing preferences so that I can do this in a Dynamo routine without the user having to change these properties manually.

From what I have studied here on the forum, it is possible through the REVIT API. Is this the only possible solution?

I hope you can understand my objective and I appreciate any help, I’m having a lot of difficulty with this.

1 Like

Hello! I had the same problem a while ago and I made a python script to try to solve it, because apparently I had the same impression as you and you can only achieve this change through the Revit API, but some error always appeared in my code and I’m still trying to solve this part. I’ll leave my code here and maybe someone else can help you!


import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Plumbing import PipeType
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager


doc = DocumentManager.Instance.CurrentDBDocument


nome_novo_tipo = "Tipo de Cano Automatizado"


def get_pipe_type_by_name(name):
    for pt in FilteredElementCollector(doc).OfClass(PipeType):
        param = pt.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM)
        if param and param.AsString() == name:
            return pt
    return None


base_type = FilteredElementCollector(doc).OfClass(PipeType).FirstElement()
existing = get_pipe_type_by_name(nome_novo_tipo)


TransactionManager.Instance.EnsureInTransaction(doc)

if existing:
    new_pipe_type = existing
else:
    new_type_id = base_type.Duplicate(nome_novo_tipo)
    new_pipe_type = doc.GetElement(new_type_id)

TransactionManager.Instance.TransactionTaskDone()


fittings = FilteredElementCollector(doc)\
    .OfCategory(BuiltInCategory.OST_PipeFitting)\
    .OfClass(FamilySymbol)\
    .ToElements()


TransactionManager.Instance.EnsureInTransaction(doc)
for f in fittings:
    if not f.IsActive:
        f.Activate()
TransactionManager.Instance.TransactionTaskDone()


def get_fitting(keyword):
    for f in fittings:
        try:
            name = f.LookupParameter("Name").AsString()
            if keyword.lower() in name.lower():
                return f
        except:
            continue
    return None


elbow = get_fitting("joelho") or get_fitting("elbow")
tee = get_fitting("tê") or get_fitting("tee")
reducer = get_fitting("redução") or get_fitting("reducer")
union = get_fitting("união") or get_fitting("union")


TransactionManager.Instance.EnsureInTransaction(doc)

prefs = new_pipe_type.RoutingPreferences

if elbow: prefs.PreferredElbow = elbow
if tee: prefs.PreferredTee = tee
if reducer: prefs.PreferredReducer = reducer
if union: prefs.PreferredUnion = union

TransactionManager.Instance.TransactionTaskDone()

OUT = new_pipe_type

He manages to create a duplicate of the pipes, but he cannot access the routing preferences.

1 Like

yeah probably im wrong but we cant set the routing preference, at least i cant :wink: but you can get it, genius loci have a great node for that in python

Hi,

it’s only possible to add new rules (it’s obviously not possible to modify existing ones)

Here is a simple example

set_pipe_rules

import clr
import sys
import System
#
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import Autodesk.DesignScript.Geometry as DS

#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument


pipe_type = UnwrapElement(IN[0]) 
mep_part = UnwrapElement(IN[1]) 
# type where we add rule
group_type = RoutingPreferenceRuleGroupType.Elbows  

TransactionManager.Instance.EnsureInTransaction(doc)

# create a routing preference rule with all Size criterion
criter = PrimarySizeCriterion.All()
new_rule = RoutingPreferenceRule(mep_part.Id, "NewRule")
new_rule.AddCriterion(criter)

# add rule at the top
pipe_type.RoutingPreferenceManager.AddRule(group_type, new_rule, 0)

TransactionManager.Instance.TransactionTaskDone()

OUT = "Rule added."
3 Likes

awesome :wink: :wink:

1 Like

This is already a huge help, thank you very much!!!

1 Like