Primary Fitting Connectors Python Workaround

Hi forum, I found this post for rotating the fitting and its connected elements, but found that maybe our families don’t specify the primary connector in the order the script anticipates and it ends up rotating the selected pipe that is in alignment with the axis of the primary connector.

So I modified the script to rotate based on a connector I feed it through an input and it works except i can’t figure out how to get the connected element (pipe) to rotate with it as the dyn from the link does. Here’s my code, any suggestions? Note: still learning python and am not an engineer either, so could be missing something obvious:

import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit

clr.ImportExtensions(Revit.Elements)

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

from System.Collections.Generic import *
import math

doc = DocumentManager.Instance.CurrentDBDocument

clr.ImportExtensions(Revit.GeometryConversion)

inn = [UnwrapElement(IN[0]), UnwrapElement(IN[1]), UnwrapElement(IN[2])]
conns = [0, 1]

def disconnect(curve):
    connectors = curve.ConnectorManager.Connectors
    for conn in connectors:
        if conn.IsConnected:
            for c in conn.AllRefs:
                if conn.IsConnectedTo(c):
                    conns[0] = conn
                    conns[1] = c
                    conn.DisconnectFrom(c)

angle = (IN[3] * math.pi) / 180
TransactionManager.Instance.EnsureInTransaction(doc)

# Get the connectors from the supplied element
connectors = inn[0].MEPModel.ConnectorManager.Connectors
connector = None

# Find the desired connector based on your criteria
for conn in connectors:
    # Add your logic to select the desired connector
    if conn.Id == inn[2].Id:
        connector = conn
        break

if connector is not None:
    loc = connector.Owner.Location
    ductloc = inn[1].Location

    # Extract the rotation axis from the supplied connector
    rotation_axis = XYZ(connector.CoordinateSystem.BasisZ.X, connector.CoordinateSystem.BasisZ.Y, connector.CoordinateSystem.BasisZ.Z)

    line = Autodesk.Revit.DB.Line.CreateUnbound(connector.Origin, rotation_axis)
    disconnect(inn[1])
    ductloc.Rotate(line, angle)
    loc.Rotate(line, angle)
    conns[0].ConnectTo(conns[1])

TransactionManager.Instance.TransactionTaskDone()

OUT = IN[0]

here’s the error:

here’s the result, the fitting rotates but connected pipe doesn’t:


Rotate PipeFitting and pipe.dyn (60.4 KB)