How to buy
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © 2025 Autodesk Inc. All rights reserved
Hello everyone,
I’m trying to set the routing preferences for a new cable tray type in Revit 2022 using the Revit API. By "new," I mean that the type has just been created and does not yet have any routing preferences set — no elbows, tees, etc. I’m attempting to do this by following the same approach used for creating routing preferences for pipes, which works perfectly fine in that case, but the same approach does not seem to work with cable trays.
Specifically, it seems that the RoutingPreferenceManager object for the cable tray is null. Below is a code snippet and a screenshot from RevitLookup for reference.
Has anyone encountered this issue when trying to set routing preferences for cable trays? Or is this limitation related to the Revit API itself, meaning it’s impossible to set cable tray routing preferences via API without using the UI?
Any help or insight would be greatly appreciated!
Thank you in advance!
var trayType = (trayList.FirstOrDefault(x => (x as CableTrayType).FamilyName == "Кабельный лоток с соединительными деталями")) as CableTrayType;
var newCableTrayType = elementType.Duplicate(_userSettings.TrayTypeName);
using (Transaction transaction = new Transaction(doc, "Routing Preference"))
{
transaction.Start();
RoutingPreferenceManager routeManager = newCableTrayType.RoutingPreferenceManager;// in this line routeManager == null
routeManager.PreferredJunctionType = PreferredJunctionType.Tee;
int initRuleCount = routeManager.GetNumberOfRules(RoutingPreferenceRuleGroupType.Junctions);
for (int i = 0; i != initRuleCount; ++i)
{
routeManager.RemoveRule(RoutingPreferenceRuleGroupType.Junctions, 0);
}
symbol = tapFam.Document.GetElement(id) as FamilySymbol;
if ((!symbol.IsActive) && (symbol != null))
{
symbol.Activate();
doc.Regenerate();
}
RoutingPreferenceRule newRule = new RoutingPreferenceRule(symbol.Id, "Round Takeoff");
routeManager.AddRule(RoutingPreferenceRuleGroupType.Junctions, newRule);
transaction.Commit();
}
Solved! Go to Solution.
Of course, right after posting my question on the forum, I found a solution (probably the rubber duck effect at play) 🙂 I'm sharing the solution code here in case it might be useful to someone in the future, especially since there isn't much information available on this theme online.
Thanks for all )
/// <summary>
/// Dictionary for routing elements and their family names: <RoutingPreferenceRuleGroupType, Family Names>
/// </summary>
internal Dictionary<RoutingPreferenceRuleGroupType, string> FittingNames = new Dictionary<RoutingPreferenceRuleGroupType, string>(5)
{
{ RoutingPreferenceRuleGroupType.Elbows, "MP_Угол_Горизонтальный_Листовой_Лоток систем канализации" },
{ RoutingPreferenceRuleGroupType.Crosses, "MP_Ответвитель Х-образный_Листовой_Лоток систем канализации" },
{ RoutingPreferenceRuleGroupType.MechanicalJoints, "MP_Ответвитель Т-образный_Листовой_Лоток систем канализации" },
{ RoutingPreferenceRuleGroupType.Transitions, "MP_Переходник_Листовой_Лоток систем канализации" },
{ RoutingPreferenceRuleGroupType.Unions, "MP_Пластина_Cоединительная_Лоток систем канализации" }
};
internal void SetNewTrayType(List<Element> trayList)
{
//get cable tray type for cable tray with fittings, where trayList - is List of all elements of BuiltInCategory.OST_CableTray
var elementType = (trayList.FirstOrDefault(x => (x as CableTrayType).FamilyName == "Кабельный лоток с соединительными деталями")) as CableTrayType;
//create new cable tray type for cable tray with fittings, where _userSettings.TrayTypeName - new type name (string)
var newCableTrayType = elementType.Duplicate(_userSettings.TrayTypeName);
if (newCableTrayType != null)
{
using (Transaction transaction = new Transaction(_doc, "Set Tray Routing Preferences"))
{
var failOpts = transaction.GetFailureHandlingOptions();
failOpts.SetClearAfterRollback(true);
transaction.SetFailureHandlingOptions(failOpts);
transaction.Start();
/*Verify whether the families are loaded into the project; if
not, load them. The method parameters include the family names
and their paths.*/
EnsureCableTrayFittingsLoaded(_doc,
_userSettings.FamilyTrayFittingsNamesAndPath);
//get list tray fittings families in this project
var familiesList = GetFamilySymbolsByFamilyNames(_doc, FittingNames);
var trace = newCableTrayType as MEPCurveType;
//Set routing references for cable tray. You do not need to configure vertical inner/outer bends for proper tracing
trace.Elbow = familiesList.FirstOrDefault(x => x.FamilyName.Equals(FittingNames[RoutingPreferenceRuleGroupType.Elbows]));
trace.Cross = familiesList.FirstOrDefault(x => x.FamilyName.Equals(FittingNames[RoutingPreferenceRuleGroupType.Crosses]));
trace.Tee = familiesList.FirstOrDefault(x => x.FamilyName.Equals(FittingNames[RoutingPreferenceRuleGroupType.MechanicalJoints]));
trace.Transition = familiesList.FirstOrDefault(x => x.FamilyName.Equals(FittingNames[RoutingPreferenceRuleGroupType.Transitions]));
trace.Union = familiesList.FirstOrDefault(x => x.FamilyName.Equals(FittingNames[RoutingPreferenceRuleGroupType.Unions]));
transaction.Commit();
}
}
_doc.Regenerate();
}
How to buy
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © 2025 Autodesk Inc. All rights reserved
Type a product name