__title__ = "Split Pipe/Duct"
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType, ISelectionFilter
from Autodesk.Revit.DB.Mechanical import Duct, MechanicalUtils
from Autodesk.Revit.DB.Plumbing import Pipe, PlumbingUtils
uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document
class ISF_Pipe_Duct(ISelectionFilter):
def AllowElement(self, element):
if type(element) in [Pipe, Duct]:
return True
ref_pipe_ducts = uidoc.Selection.PickObjects(ObjectType.Element, ISF_Pipe_Duct())
pipe_ducts = [doc.GetElement(ref) for ref in ref_pipe_ducts]
for elem in pipe_ducts:
try:
crv = elem.Location.Curve
pt_start = crv.GetEndPoint(0)
pt_end = crv.GetEndPoint(1)
pt_middle = (pt_start + pt_end)/2
t = Transaction(doc,'Split Pipe/Duct')
t.Start()
if type(elem) == Pipe:
new_elem_id = PlumbingUtils.BreakCurve(doc, elem.Id, pt_middle)
elif type(elem) == Duct:
new_elem_id = MechanicalUtils.BreakCurve(doc, elem.Id, pt_middle)
if new_elem_id:
new_elem = doc.GetElement(new_elem_id)
print(new_elem)
t.Commit()
except Exception as e:
print('⚠️Error with ElementId({}): {}'.format(elem.Id,e))