Create Rebar From Curves in Python


I’m trying to learn how to create nodes, from python, following the references to posts that were previously made, why the error?

import sys
import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import*

from System.Collections.Generic import IList, List

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

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

doc = DocumentManager.Instance.CurrentDBDocument

# dynamo curves as input
curves = UnwrapElement(IN[0])
rebar_style = UnwrapElement(IN[1])
rebar_bar_type = UnwrapElement(IN[2])
start_hook_type = UnwrapElement(IN[3])
end_hook_type = UnwrapElement(IN[4])
start_hook_orientation = IN[5]
end_hook_orientation = IN[6]
host_element = UnwrapElement(IN[7])
vector = IN[8]

def CreateRebarFromCurve(curves, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, start_hook_orientation, end_hook_orientation, host_element, vector):
    rebars = []
    TransactionManager.Instance.EnsureInTransaction(doc)
    for c, v in zip(curves, vector):
        curv = List[Curve]()
        curv.Add(c.ToRevitType())
        rebar = Rebar.CreateFromCurves(doc, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, host_element, v.ToRevitType(), curv, start_hook_orientation, end_hook_orientation, True, False)
        rebars.append(rebar)
    TransactionManager.Instance.TransactionTaskDone()
    return rebars

OUT = CreateRebarFromCurve(curves, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, start_hook_orientation, end_hook_orientation, host_element, vector)

Hi

the error indicates that you are trying to iterate on a non-iterable object

  • Check that curves and vector are input lists of the same size

or

  • just use a for-loop (instead zip() )on the curves object and use vector (type conversion to be done) directly in the Rebar.CreateFromCurves() method.
2 Likes

A few notes

  • You may find the curve elements do not need to be converted as they have been unwrapped
  • If you have single value for an element you can use them globally (just as you have used the doc variable) rather than pass them into the function so that your function only needs the variable to process
  • If possible do all your conversions in one place either in the function or as they are established
  • Convert a dynamo Vector with ToXyz() method
  • I’m pretty sure you can create the rebars using one list of curves if all the other items are static instead of one by one
  • As @c.poupin noted - is there a purpose for the zip
  • More information on the inputs (i.e. the whole graph) would assist understanding what the code is working with
  • You may have to unwrap the rebar hook orientation inputs
1 Like

I’ve checked the list curve, right

rebar Python.dyn.rvt (7.5 MB)



Rebar Python.dyn (27.0 KB)

Good morning
you are missing:
import System
in your code
Sincerely
christian.stan

Hi,

you must analyze the error message, in this example there is a problem with several object types passed as arguments that do not match with the Revit API.


image

here the fixed code

import sys
import clr
import System
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

from System.Collections.Generic import IList, List

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

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

doc = DocumentManager.Instance.CurrentDBDocument

# dynamo curves as input
curves = UnwrapElement(IN[0])
rebar_style = System.Enum.Parse(RebarStyle, IN[1])
rebar_bar_type = UnwrapElement(IN[2])
start_hook_type = UnwrapElement(IN[3])
end_hook_type = UnwrapElement(IN[4])
start_hook_orientation = System.Enum.Parse(RebarHookOrientation, IN[5])
end_hook_orientation = System.Enum.Parse(RebarHookOrientation, IN[6])
host_element = UnwrapElement(IN[7])
vector = IN[8]

def CreateRebarFromCurve(curves, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, start_hook_orientation, end_hook_orientation, host_element, vector):
    rebars = []
    TransactionManager.Instance.EnsureInTransaction(doc)
    for c, v in zip(curves, vector):
        curv = List[Curve]()
        curv.Add(c.ToRevitType())
        rebar = Rebar.CreateFromCurves(doc, 
                                        rebar_style, 
                                        rebar_bar_type, 
                                        start_hook_type, 
                                        end_hook_type, 
                                        host_element, 
                                        v.ToRevitType(), 
                                        curv, 
                                        start_hook_orientation, 
                                        end_hook_orientation, 
                                        True, 
                                        False)
        rebars.append(rebar)
    TransactionManager.Instance.TransactionTaskDone()
    return rebars

OUT = CreateRebarFromCurve(curves, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, start_hook_orientation, end_hook_orientation, host_element, vector)
2 Likes

Thank you very much work

It turns out that the string must be converted, yes, I think at the beginning he can read string data ,