I’ll paste my modified code here in its entirety (just in case anyone wants to look through it), but I’m pretty sure the only changes I made were here:

```
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
if isinstance(IN[0], list):
pipe1 = UnwrapElement(IN[0])
else:
pipe1 = [UnwrapElement(IN[0])]
if isinstance(IN[1], list):
pipe2 = UnwrapElement(IN[1])
else:
pipe2 = [UnwrapElement(IN[1])]
offset = IN[2]
collector = FilteredElementCollector(doc)
pipetypes = collector.OfClass(Plumbing.PipeType).ToElements()
pipetypeIds = [x.Id for x in pipetypes]
collector2 = FilteredElementCollector(doc)
ducttypes = collector2.OfClass(Mechanical.DuctType).ToElements()
ducttypeIds = [x.Id for x in ducttypes]
def createPipe(point1,point2,systype,pipetype,level,radius):
pipe = Plumbing.Pipe.Create(doc,systype,pipetype,level,point1,point2)
param = pipe.get_Parameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM)
diameter = radius * 2
param.Set(diameter)
return pipe
def createDuctRound(point1,point2,systype,pipetype,level,radius):
duct = Mechanical.Duct.Create(doc,systype,pipetype,level,point1,point2)
param = duct.get_Parameter(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM)
diameter = radius * 2
param.Set(diameter)
return duct
def closest_point(point1,pipe):
p2line = pipe.Location.Curve
start = p2line.GetEndPoint(0)
end = p2line.GetEndPoint(1)
if point1.DistanceTo(start) < point1.DistanceTo(end):
return end
else:
return start
def closest_connectors(pipe1, pipe2):
try:
conn1 = pipe1.ConnectorManager.UnusedConnectors
except:
conn1 = pipe1.MEPModel.ConnectorManager.UnusedConnectors
try:
conn2 = pipe2.ConnectorManager.UnusedConnectors
except:
conn2 = pipe2.MEPModel.ConnectorManager.UnusedConnectors
dist = 100000000
connset = None
for c in conn1:
for d in conn2:
conndist = c.Origin.DistanceTo(d.Origin)
if conndist < dist:
dist = conndist
connset = [c,d]
return connset
def closest_connectors_base(pipe1, pipe2):
try:
conn1 = pipe1.ConnectorManager.UnusedConnectors
except:
conn1 = pipe1.MEPModel.ConnectorManager.UnusedConnectors
try:
conn2 = pipe2.ConnectorManager.UnusedConnectors
except:
conn2 = pipe2.MEPModel.ConnectorManager.UnusedConnectors
dist = 100000000
connset = None
for c in conn1:
try:
sys1 = c.PipeSystemType
except:
sys1 = c.DuctSystemType
for d in conn2:
try:
sys2 = d.PipeSystemType
except:
sys2 = d.DuctSystemType
if sys1 != sys2:
continue
conndist = c.Origin.DistanceTo(d.Origin)
if conndist < dist:
dist = conndist
connset = [c,d]
return connset
def getBasis(element,element2):
try:
connMan = element.ConnectorManager
except:
connMan = element.MEPModel.ConnectorManager
if connMan.UnusedConnectors.Size > 1:
connset = closest_connectors_base(element,element2)
try:
return connset[0].CoordinateSystem.BasisZ,connset[0].Origin,connset[0].Radius
except:
return connset[0].CoordinateSystem.BasisZ,connset[0].Origin,connset[0].Width
else:
connectors = connMan.Connectors
for conn in connectors:
if conn.IsConnected == False:
openconn = conn
break
try:
return openconn.CoordinateSystem.BasisZ,openconn.Origin,openconn.Radius
except:
return openconn.CoordinateSystem.BasisZ,openconn.Origin,openconn.Width
listout = []
TransactionManager.Instance.EnsureInTransaction(doc)
for p1,p2 in zip(pipe1,pipe2):
if p1.GetType() == Plumbing.Pipe or p1.GetType() == Mechanical.Duct:
ptemp = p1
p1 = p2
p2 = ptemp
basis1 = getBasis(p1,p2)
basis2 = getBasis(p2,p1)
radius1 = basis1[2]
radius2 = basis2[2]
#create points for duct/pipe creation
line1 = Line.CreateUnbound(basis1[1],basis1[0])
if abs(basis1[0].Z) == 1 and abs(basis2[0].Z) == 0:
point1 = XYZ(basis1[1].X,basis1[1].Y,basis2[1].Z)
else:
if offset == 0:
point1 = line1.Evaluate(radius1*5,False)
else:
point1 = line1.Evaluate(offset/304.8,False)
line2 = Line.CreateUnbound(basis2[1],basis2[0])
point3 = None
projection = line2.Project(basis1[1])
if basis1[0].Z == 0 and basis2[0].Z >= 0:
point2 = XYZ(point1.X,point1.Y,basis2[1].Z)
project2 = line2.Project(point2)
if project2.Distance > 1/304.8:
point3 = project2.XYZPoint
else:
point2 = projection.XYZPoint
#determine pipe or duct as input and get pipe and system type
level = p2.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM)
if level == None:
level = p1.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId()
else:
level = level.AsElementId()
if p1.GetType() == Plumbing.Pipe or p2.GetType() == Plumbing.Pipe:
try:
pipetype = p2.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsElementId()
if pipetype not in pipetypeIds:
pipetype = p1.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsElementId()
systype = p2.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM)
if systype == None or systype.AsValueString() == "Undefined":
systype = p1.get_Parameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsElementId()
else:
systype = systype.AsElementId()
#p2 can connect to p1 directly
if projection.Distance < 1/304.8:
endpoint = closest_point(basis1[1],p2)
p2.Location.Curve = Line.CreateBound(endpoint,basis1[1])
if radius1 != radius2:
conns = closest_connectors(p2,p1)
doc.Create.NewUnionFitting(conns[0],conns[1])
else:
conns = closest_connectors(p2,p1)
conns[0].ConnectTo(conns[1])
elif basis1[0].Z == 1:
point1 = XYZ(basis1[1].X,basis1[1].Y,basis2[1].Z)
pipe1 = createPipe(basis1[1],point1,systype,pipetype,level,radius1)
if line2.Project(point1).Distance < 1/304.8:
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
conns = closest_connectors(pipe1,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
else:
point2 = line2.Project(point1).XYZPoint
pipe2 = createPipe(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
elif basis1[0].Z == 0 and basis2[0].Z >= 0:
if point3 == None:
pipe1 = createPipe(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createPipe(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
else:
pipe1 = createPipe(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createPipe(point1,point2,systype,pipetype,level,radius1)
pipe3 = createPipe(point2, point3, systype, pipetype, level, radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,pipe3)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe3,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
#create pipes and elbows and then connect to p2 with an elbow
else:
pipe1 = createPipe(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createPipe(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
listout.append("succes")
except:
listout.append("failed")
else:
try:
pipetype = p2.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsElementId()
if pipetype not in ducttypeIds:
pipetype = p1.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM).AsElementId()
systype = p2.get_Parameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM)
if systype == None or systype.AsValueString() == "Undefined":
systype = p1.get_Parameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM).AsElementId()
else:
systype = systype.AsElementId()
#p2 can connect to p1 directly
if projection.Distance < 1/304.8:
endpoint = closest_point(basis1[1],p2)
p2.Location.Curve = Line.CreateBound(endpoint,basis1[1])
if radius1 != radius2:
conns = closest_connectors(p2,p1)
doc.Create.NewUnionFitting(conns[0],conns[1])
else:
conns = closest_connectors(p2,p1)
conns[0].ConnectTo(conns[1])
elif basis1[0].Z == 1:
point1 = XYZ(basis1[1].X,basis1[1].Y,basis2[1].Z)
pipe1 = createDuctRound(basis1[1],point1,systype,pipetype,level,radius1)
if line2.Project(point1).Distance < 1/304.8:
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
conns = closest_connectors(pipe1,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
else:
point2 = line2.Project(point1).XYZPoint
pipe2 = createDuctRound(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
elif basis1[0].Z == 0 and basis2[0].Z >= 0:
if point3 == None:
pipe1 = createDuctRound(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createDuctRound(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
else:
pipe1 = createDuctRound(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createDuctRound(point1,point2,systype,pipetype,level,radius1)
pipe3 = createDuctRound(point2, point3, systype, pipetype, level, radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,pipe3)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe3,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
#create pipes and elbows and then connect to p2 with an elbow
else:
pipe1 = createDuctRound(basis1[1],point1,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,p1)
conns[0].ConnectTo(conns[1])
pipe2 = createDuctRound(point1,point2,systype,pipetype,level,radius1)
conns = closest_connectors(pipe1,pipe2)
doc.Create.NewElbowFitting(conns[0],conns[1])
conns = closest_connectors(pipe2,p2)
doc.Create.NewElbowFitting(conns[0],conns[1])
listout.append("succes")
except:
listout.append("failed")
TransactionManager.Instance.TransactionTaskDone()
OUT = listout
```