How to create Rebar From Curves using Revit API and Python

Hi everybody,

I’m using the Revit API method to create rebar free form curve as indicated here, but I’m stuck how to iterate at the same time in the Normal verctor list (Normal_vect) and its corresponding curve list (crvs) to be able to create the rebar doc…I thought to use zip function but I dont know how can I write the correct syntax to iterate in each sublists nested in my two lists (I want to keep the structure of my list)
here my graph:

Note: ignore the error until I haven’t correctly write the correct syntax of the function zip to iterate between curves list and vector list

Here my code:

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

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
geo = IN[0]
bar_type = UnwrapElement(IN[1])
host = UnwrapElement(IN[2])
vect = IN[3]

# converting dynamo curves to Revit curves
crvs = []
temp = []
for l in range(0, len(geo)):
    for c in geo[l]: 
        temp.append(c.ToRevitType())
    crvs.append(temp)
    temp = []
# get nornal vectors of each curve    
Normal_vect = []
for l in range(0, len(vect)):
    for v in vect[l]: 
        temp.append(v.ToRevitType())
    Normal_vect.append(temp)
    temp = []
rebars = []

TransactionManager.Instance.EnsureInTransaction(doc)
for i in range(0, len(crvs)):
    for j in crvs[i]:

rebars = [Rebar.CreateFromCurves(doc, RebarStyle.Standard, bar_type, None, None, host, j, i, RebarHookOrientation.Left, RebarHookOrientation.Right, True, False) for i, j in zip(crvs, Normal_vect)]

TransactionManager.Instance.TransactionTaskDone()
OUT = rebars

Thanks.

@christian.stan

I received your reply in my email but I can’t see it in the forum??

@c.poupin

I tried something using zip function but I dont know if the iteration over curves and vectors outcome, because I get an error message:
image

for more details, please find below my last code, dyn file and Revit file

Note: for surface selection you should select the model bottom surface

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

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
geo = IN[0]
bar_type = UnwrapElement(IN[1])
host = UnwrapElement(IN[2])
vect = IN[3]

# converting dynamo curves to Revit curves
crvs = []
temp = []
for l in range(0, len(geo)):
    for c in geo[l]: 
        temp.append(c.ToRevitType())
    crvs.append(temp)
    temp = []
# get nornal vectors of each curve    
Normal_vect = []
for l in range(0, len(vect)):
    for v in vect[l]: 
        temp.append(v.ToRevitType())
    Normal_vect.append(temp)
    temp = []

TransactionManager.Instance.EnsureInTransaction(doc)
# iterate over sublist in Normal_vect list and crvs list  
for i, j in zip(Normal_vect, crvs):
    rebars = []
    #iterate over each item in sublists to create rebar doc  (v = normal vector and c = curves ) 
    for v, c in zip(i, j):
        rebar = Rebar.CreateFromCurves(doc, RebarStyle.Standard, bar_type, None, None, host, v, c, RebarHookOrientation.Left, RebarHookOrientation.Right, True, False) 
        rebars.append(rebar)

TransactionManager.Instance.TransactionTaskDone()
OUT = rebars

fer_radier.dyn (22.5 KB)
Raft_rebar.rvt (2.2 MB)
Radier_chateau_eau_1000.rfa (440 KB)

Thanks.

Hello,
You must add the Structure namespace to have access to the Rebar class (otherwise you must specify the Structure domain in your code Structure.Rebar.CreateFromCurves(doc, Structure.RebarStyle.Standard.....)
from Autodesk.Revit.DB.Structure import *

After there must also be the .Net class import (List and / or Ilist) (really not sure)

import System
from System.Collections.Generic import List,IList

too obscure for me with my current level, that’s why I deleted the message not being sure, I didn’t want to waste your time in a direction that may not be necessary
Cordially
christian.stan

2 Likes

Hello, with great difficulty I succeeded, (but hey I think there must be much faster)

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

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

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

import System 
from System.Collections.Generic import List

doc = DocumentManager.Instance.CurrentDBDocument

curv_dyn=IN[0]
reb_typ=UnwrapElement(IN[1])
col=UnwrapElement(IN[2])
vec_dyn=IN[3]

rc=[]
temp=[]
#vec=vec_dyn.ToRevitType()

for k,l in zip(range(0, len(curv_dyn)),range(0, len(vec_dyn))):
    curv_l=List[Curve]()
    for i,j in zip(curv_dyn[k],vec_dyn[l]):
        curv_l.Add(i.ToRevitType())
        TransactionManager.Instance.EnsureInTransaction(doc)
        r=Rebar.CreateFromCurves(doc,RebarStyle.Standard,reb_typ,None,None,col,j.ToRevitType(),curv_l,RebarHookOrientation.Right,RebarHookOrientation.Left,True,False)
        TransactionManager.Instance.TransactionTaskDone()
        curv_l=List[Curve]()
        temp.append(r)
    rc.append(temp)
    temp=[]

OUT=rc

cordially
christian.stan

3 Likes

Hi @christian.stan

Thanks for your Job

I tried your code but I get an error which saying me:

error

Avertissement:IronPythonEvaluator.EvaluateIronPythonScript l’opération a échoué.
Traceback (most recent call last):
File “”, line 35, in
Exception: The input argument “barType” of function Autodesk::Revit::Proxy::DB::Structure::RebarProxy::CreateFromCurves or one item in the collection is null at line 558 of file E:\Ship\2022_px64\Source\Rebar\RebarDBAPI\gensrc\APIRebarProxy.cpp.
Parameter name: barType

here my code:

rebar
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
geo = IN[0]
bar_type = UnwrapElement(IN[1])
host = UnwrapElement(IN[2])
vect = IN[3]


rc = []
temp = []
for i, j in zip((0, len(geo)), (0, len(vect))):
    curv = List[Curve]()
    for c, v in zip(geo[i], vect[j]):
        curv.Add(c.ToRevitType())
        TransactionManager.Instance.EnsureInTransaction(doc)
        rebar = Rebar.CreateFromCurves(doc, RebarStyle.Standard, bar_type, None, None, host, v.ToRevitType(), curv, RebarHookOrientation.Left, RebarHookOrientation.Right, True, False)
        TransactionManager.Instance.TransactionTaskDone()
        curv = List[Curve]()        
        temp.append(rebar)
    rc.append(temp)
    temp = []

OUT = rc

Can you please try your code in my Revit model above?

Another question: since the Structure.DB is part of Revit.DB why I should import it a second time??

Thanks.

1 Like

I used a custom node from Mr. Fudala
image

you ‘‘may need’’ to install this one since it comes from bartype

during the 1st import we scan the namespaces to Autodesk.Revit.DB
during the 2nd import we add the possibility of having access to the classes of Structure more directly
But then I’m a beginner, don’t forget that
Cordially
christian.stan

1 Like

@christian.stan
I already select rebar bar type from Structural Design Package!!..(Have you tried your code in my model to check if you got the same error?)

We use * to import everything from Autodesk.Revit.DB so why we call Structure.DB for a second time??

Thanks

you have forgot range in first for

edit: you have forgot too import System

edit:2
and concerning:

from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

I had tried to explain with my words and ‘‘understanding’’ in the previous post
someone more alert than me may take over on this aspect
cordially
christian.stan

2 Likes

because namespaces are different, need to import both (although there is an inheritance system between)

it would be useful if the models you share were not purged, the structural families are missing (or you have copy and paste the model on a non-structural template project)

2 Likes

@c.poupin

Thanks for your explanation about the Revit API namespaces and their uses domain!

I corrected my script and the rebars are created correctely.
Now, I want to group the rebars with the sames length in a set, so how to do that?

Thanks.

Please start a new topic as that is a different issue.

2 Likes