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 ,

Sorry mr, I tried again, by create a rebar curve stirrup, showing an EOF error, but I tried to make the standard rebar successful, why?

check the end of your script, missing parenthesis or missing quotes

1 Like

@c.poupin Hi,

I’m using the script above you’ve fixed to model stirrupties. After running the script gives the following error: CreateRebarFromCurve TypeError: expected Curve, got CurveLoop.

Could the cause be that I’m feeding this script with rectangles (as curves). The startpoint is the same as the endpoint so that could be the CurveLoop.

Try to get the longest curve.

for curve_Loop, v in zip(curve_Loops, vector):
    max_curve = max(curve_Loop, key = lambda x : x.Length)
    curv = List[Curve]([max_curve ])

This triggered a new error:
Traceback (most recent call last):
File “”, line 58, in
File “”, line 40, in CreateRebarFromCurve
TypeError: Error in IEnumeratorOfTWrapper.Current. Could not cast: Autodesk.Revit.DB.Curve in Autodesk.DesignScript.Geometry.PolyCurve

I’m still trying to find out how to post my Python script as you guys do. Feel a bit stupid now…

@d.kelfkens
Paste your code, select it, then click this button.

1 Like
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]
vector = UnwrapElement(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):
        max_curve = max(curves, key = lambda x : x.Length)
        #curv = List[Curve]()
        curv = List[Curve]([max_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)

This means that you have an object of type Dynamo PolyCurve instead of a Revit CurveLoop object as you mentioned above.

you must convert Dynamo Geometry to a Revit GeometryObject, read this

Try this (untested):

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
polycurves = IN[0] # assume is a list of polycurve
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]
vectors = IN[8]

def CreateRebarFromCurve(polycurves, 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 polycurve, v in zip(polycurves, vectors):
        max_curve = max(polycurve.Curves(), key = lambda x : x.Length) # we get only the longuest curve
        curv = List[Curve]([max_curve.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(polycurves, rebar_style, rebar_bar_type, start_hook_type, end_hook_type, start_hook_orientation, end_hook_orientation, host_element, vector)

not there yet…

My input. Maybe I’m doing something wrong.

Yes, in your case, you need to pass arguments (all input lists) to the zip function.

1 Like