Split Duct Node Custom

Hi everyone, I need some help on this script. I am writting a python script to work as the same as DynaTools.SplitDuctsByPoints. Here is my script:

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Mechanical import Duct, MechanicalUtils

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

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

# Access the current document
doc = DocumentManager.Instance.CurrentDBDocument
DB = Autodesk.Revit.DB

# Unwrap the input elements (Ducts) and convert points to XYZ
elements = UnwrapElement(IN[0]) if isinstance(IN[0], list) else [UnwrapElement(IN[0])]

# Ensure all points are converted to XYZ
points = []
for sublst in IN[1]:
    nested_lst = []
    for point in sublst:
        nested_lst.append(point.ToXyz())
    points.append(nested_lst)

# Initialize lists to store results and errors
ele = []
result = []

# Start a transaction to modify the Revit document
TransactionManager.Instance.EnsureInTransaction(doc)

# Loop through each duct element and try to split it at each point
for duct, point in zip(elements, points):
    subList = []
    for pt in point:
        try:
            newele = DB.Mechanical.MechanicalUtils.BreakCurve(doc, duct.Id, pt)
            ele.append(doc.GetElement(newele))
            subList.append(newele)
        except Exception as er:
            result.append(er)
    result.append(subList)

# Complete the transaction
TransactionManager.Instance.TransactionTaskDone()

# Output the result and errors (if any)
OUT = result

I expected that it will return list of new duct elements, but it only return ID for me. Can you help me to fix it to return new elements?


Divide Ducts.dyn (73.2 KB)

ele.append(doc.GetElement(newele))
subList.append(newele)

@cazorlas I suppose you want to subList.append(ele)?

2 Likes

No @BimAmbit I am accessing list level 2 so I do that

Regardless of what you are trying to do here, newele is ElementId alright. Then you use sublist to collect a newele (ElementId), then after the inner loop, you fill list “result” with a bunch of sublists (again, all ElementId) and some exceptions.

1 Like

I see my mistake here, I was wrong when I add another list. Thank you for your help

1 Like

Btw, here is my final code for who need it:

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Mechanical import Duct, MechanicalUtils

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

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)

# Access the current document
doc = DocumentManager.Instance.CurrentDBDocument
DB = Autodesk.Revit.DB

# Unwrap the input elements (Ducts) and convert points to XYZ
elements = UnwrapElement(IN[0]) if isinstance(IN[0], list) else [UnwrapElement(IN[0])]

# Ensure all points are converted to XYZ
points = []
for sublst in IN[1]:
    nested_lst = []
    for point in sublst:
        nested_lst.append(point.ToXyz())
    points.append(nested_lst)

# Initialize lists to store results and errors
#ele = []
result = []

# Start a transaction to modify the Revit document
TransactionManager.Instance.EnsureInTransaction(doc)

# Loop through each duct element and try to split it at each point
for duct, point in zip(elements, points):
    subList = []
    for pt in point:
        try:
            newele = DB.Mechanical.MechanicalUtils.BreakCurve(doc, duct.Id, pt)
            #ele.append(newele)
            subList.append(doc.GetElement(newele))
        except Exception as er:
            result.append(er)
    result.append(subList)

# Complete the transaction
TransactionManager.Instance.TransactionTaskDone()

# Output the result and errors (if any)
OUT = result
1 Like