# Split Duct Node Custom

**URL:** <https://forum.dynamobim.com/t/split-duct-node-custom/106665>\
**Category:** Revit\
**Tags:** dynamo, api\
**Created:** [December 18, 2024, 7:33am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665 "2024-12-18T07:33:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cazorlas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cazorlas/32/190160_2.png) [@cazorlas](https://forum.dynamobim.com/u/cazorlas)\
**Post date:** [December 18, 2024, 7:33am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/1 "2024-12-18T07:33:31Z")

</div>

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:

```auto
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?

 ![png 1](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/7/9760317d870db0fa4f7447667df10af12afd3b3e.png)  
[Divide Ducts.dyn](https://forum.dynamobim.com/uploads/short-url/88jry1etpUh4MGZGEA2fQaoFdPU.dyn) (73.2 KB)

---

<div class="post-metadata">

**Author:** ![BimAmbit](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bimambit/32/192629_2.png) [@BimAmbit](https://forum.dynamobim.com/u/BimAmbit)\
**Post date:** [December 18, 2024, 8:10am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/2 "2024-12-18T08:10:49Z")

</div>

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

```

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

---

<div class="post-metadata">

**Author:** ![cazorlas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cazorlas/32/190160_2.png) [@cazorlas](https://forum.dynamobim.com/u/cazorlas)\
**Post date:** [December 18, 2024, 9:11am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/3 "2024-12-18T09:11:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BimAmbit](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bimambit/32/192629_2.png) [@BimAmbit](https://forum.dynamobim.com/u/BimAmbit)\
**Post date:** [December 18, 2024, 9:28am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/4 "2024-12-18T09:28:25Z")

</div>

> [@cazorlas](#):
>
> ```auto
> newele = DB.Mechanical.MechanicalUtils.BreakCurve(doc, duct.Id, pt)
> ele.append(doc.GetElement(newele))
> subList.append(newele)
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![cazorlas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cazorlas/32/190160_2.png) [@cazorlas](https://forum.dynamobim.com/u/cazorlas)\
**Post date:** [December 18, 2024, 9:39am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/5 "2024-12-18T09:39:51Z")

</div>

> [@BimAmbit](#):
>
> `newele`

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

---

<div class="post-metadata">

**Author:** ![cazorlas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cazorlas/32/190160_2.png) [@cazorlas](https://forum.dynamobim.com/u/cazorlas)\
**Post date:** [December 18, 2024, 9:43am UTC](https://forum.dynamobim.com/t/split-duct-node-custom/106665/6 "2024-12-18T09:43:05Z")

</div>

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

```auto
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

```
