Merge connected pipes

I have a script that moves and connects piping. It works great with accessories, fittings and pipes, but when I connect pipe to pipe it does not merge them. The two pipes connect but stay as two pipes. I’ve looked around for a while but can’t find anything that will merge these into a single pipe.

I know I could probably create a new pipe in their location and delete them but that sounds like it might be overly complicated. Is there some method I’m missing?

@Crapai

can you share your code and screenshots ?

Is it possible in the Revit UI?

# Import libraries
from pyrevit import revit, forms
from Autodesk.Revit.DB import (Transaction)
from rpw.ui.forms import (FlexForm, Label, CheckBox)
# Current document
doc = __revit__.ActiveUIDocument.Document
cView = doc.ActiveView

from rpw.ui.selection import Pick

################################################################################

pipePickOne = Pick.pick_element(msg = "Pick One element", multiple=False)
pipePickTwo = Pick.pick_element(msg = "Pick One element", multiple=False)

# print(pipePickOne.id)
# print(pipePickTwo.id)

elemOne = doc.GetElement(pipePickOne.id)
elemTwo = doc.GetElement(pipePickTwo.id)
connSetOne = 0
connSetTwo = 0
connOne = 0
connTwo = 0
connTwoDirection = 0

if elemOne.ToString() == "Autodesk.Revit.DB.FamilyInstance":
    connSetOne = elemOne.MEPModel.ConnectorManager.Connectors
elif elemOne.ToString() == "Autodesk.Revit.DB.Plumbing.Pipe":
    connSetOne = elemOne.ConnectorManager.Connectors

if elemTwo.ToString() == "Autodesk.Revit.DB.FamilyInstance":
    connSetTwo = elemTwo.MEPModel.ConnectorManager.Connectors
elif elemTwo.ToString() == "Autodesk.Revit.DB.Plumbing.Pipe":
    connSetTwo = elemTwo.ConnectorManager.Connectors


for cT in connSetTwo:
    if cT.IsConnected:
        pass
    else:
        connTwo = cT
        connTwoZ = cT.CoordinateSystem.BasisZ

if connTwo == 0:
    forms.alert("Destination element has no open connector",title="Script Cancelled",exitscript=True)
else:
    pass


# print(connTwoZ)

################################################################################

t = Transaction(doc, "pyRevit - Connect Pipe")
t.Start()

for cO in connSetOne:
    if cO.IsConnected:
        pass
    else:
        if cO.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connTwoZ):
            # print("Opposite and parallel")
            trans = connTwo.Origin - cO.Origin
            elemLocation = elemOne.Location
            elemLocation.Move(trans)
            cO.ConnectTo(connTwo)
        else:
            pass
            # print("Shrug?")

t.Commit()
1 Like

Just tested it out and it looks like Trim/Extend to Corner works to join the pipes into one. I didn’t know that works for parallel elements. I try checking out if there is a way I can use that command in the API.

the closest thing I could get:

from pyrevit import revit, DB, script

op=script.get_output().close_others()
doc = revit.DOCS.doc

selection_of_two_pipes = revit.get_selection()
end_points = []
connectors = []
with revit.Transaction('Connectors'):
    for i in selection_of_two_pipes:
        p1 = i.Location.Curve.GetEndPoint(0)
        p2 = i.Location.Curve.GetEndPoint(1)
        for connector in i.ConnectorManager.Connectors:
            connectors.append(connector)
        end_points.append(p1)
        end_points.append(p2)
    pipe1_point1 = end_points[0]
    pipe1_point2 = end_points[1]
    pipe2_point1 = end_points[2]
    pipe2_point2 = end_points[3]
    pipe1_connector1 = connectors[0]
    pipe1_connector2 = connectors[1]
    pipe2_connector1 = connectors[2]
    pipe2_connector2 = connectors[3]
    selection_of_two_pipes[0].Location.Curve = DB.Line.CreateBound(pipe1_point1, pipe2_point1)
with revit.Transaction('Connectors Connect To'):
    pipe1_connector2.ConnectTo(pipe2_connector1)
with revit.Transaction('Connectors regen'):
    doc.Regenerate()

but I cannot get it to behave like in the UI and physically merging the two pipes

and you are not the only one Combine Pipe Elements Seamlessly - Autodesk Community

better to recreate a new one from the endpoints of pipe 1 and 2

also tried the Join Geometry Utils method to join to no avail

DB.JoinGeometryUtils.JoinGeometry(doc,selection_of_two_pipes[0], selection_of_two_pipes[1])

If you do end up recreating the pipe I did a similar exercise in Dynamo. It would work for multiple pipe segments. I know Dynamo is different but the logic is sound.

For this, you would want to get the curve of all connected pipes. Then get the start and end point (eliminate duplicates). You will need to have the points sorted along the finished segment. You can then use vectors to check if the pipes are straight and eliminate all mid-section points. You will be left with a single start and end point for creating a new pipe.

If you know the two pipes then there are simpler ways of doing this. This method would be more for the whole model all at once.

So, I’ve decided to create a new pipe. I first tried using postable command to start the trim command but I don’t think I can actually send any info to it so the user would have to trim the pipes after running the command which is a no go for me.

I ended up getting the connected connectors of the each element and after doing the same trans elemLocation.Move with the existing pipes, I deleted them, and created a new one with the Pipe.Create method which uses connector to connector as the endpoint. I needed to add something in my script for checking to make sure each element had a open connector anyway so it wasn’t that much extra work.

Here’s a snippet of the transaction when there are two pipes:

        if pipeCount == 2:
            if cO.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connTwoZ):
                trans = connTwo.Origin - cO.Origin
                elemLocation = elemOne.Location
                elemLocation.Move(trans)
                doc.Delete(elemOne.Id)
                doc.Delete(elemTwo.Id)
                newPipe = Pipe.Create(doc,pipeType.Id,pipeLevel,connSetOneConnectedEndConnector,
                                      connSetTwoConnectedEndConnector)
1 Like