Need Help With My Python Script

my scripts returns error which I am having difficulties.

columns = UnwrapElement(IN[0])
Rebars = UnwrapElement(IN[1])

def element(revelements,mrebars):
return {revelement:mrebar for revelement,mrebar in zip(revelements,mrebars)}

mydict = element(columns,Rebars)

rebarnumber = []
for i in mydict[columns]:
for rebar in IEnumerable(i):
rebarnumber.append(rebar.BuiltInParameter.REBAR_NUMBER)
Total = rebarnumber.Count()

OUT = Total

What are you trying to obtain? the total number of rebars? When I try to copy your code I get many errors.
Is there a reason why you want to use dictionaries?

PS.could you also post a picture of the error you are getting please?

See below for the error message.

This is the script

import clr
clr.AddReference("mscorlib")
from System.Collections.Generic import *
from builtin import zip
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript import *
clr.AddReference("RevitServices")
import RevitServices

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitNodes")
import Revit

clr.AddReference("RevitAPI")

import Autodesk
from Autodesk.Revit.DB import *

columns = UnwrapElement(IN[0])
Rebars = UnwrapElement(IN[1])

def element(revelements,mrebars):
return {revelement:mrebar for revelement,mrebar in zip(revelements,mrebars)}

mydict = element(columns,Rebars)

rebarnumber =
for i in mydict[columns]:
for rebar in IEnumerable(i.value):#accessing element in the dictionary using key "column"
rebarnumber.append(rebar.BuiltInParameter.REBAR_NUMBER)

Total = rebarnumber.Count()

OUT = Total

Please excuse me but I still do not understand why you are using dictionaries, python and what your final goal is.

Do you want to count the total number of rebars in each column? Or for the whole project? Or the diffferent types of rebars? I think that the REBAR_NUMBER does not tell you how many rebars there are but simply the different types in a partition

can you please also paste the code as preformatted text… This makes it possible for people to actually copy/paste it and help you discover errors… This can be done using this icon:
image

The code is already preformatted, but I would like to know what the final goal is (:

In that case there is definitely some issues with missing indentations in loops etc… :wink:

This would be preformatted:

import clr
clr.AddReference("mscorlib")
from System.Collections.Generic import *
from  **builtin**  import zip
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript import *
clr.AddReference("RevitServices")
import RevitServices

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitNodes")
import Revit

clr.AddReference("RevitAPI")

import Autodesk
from Autodesk.Revit.DB import *

columns = UnwrapElement(IN[0])
Rebars = UnwrapElement(IN[1])

def element(revelements,mrebars):
    return {revelement:mrebar for revelement,mrebar in zip(revelements,mrebars)}

mydict = element(columns,Rebars)

rebarnumber = []
for i in mydict[columns]:
    for rebar in IEnumerable(i.value):#accessing element in the dictionary using key "column"
        rebarnumber.append(rebar.BuiltInParameter.REBAR_NUMBER)

Total = rebarnumber.Count()

OUT = Total

Assuming I have not missed any indentations (as they had to be added manually…)

Eh I too noticed that often the indentations (and also the quotes) get messed up when preformatting the text as code

for each column I want to count the total number of rebar of the same rebar_number. this is essentially what I want to do. There should some code to check the equality of the rebar numbers but that I will look later but want solution to the current issue

I have taken notice of that. Thank you very much.

Are you trying to get something like this? Please excuse me if I am doing it in dynamo and not in python, it’s just to understand.

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

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

rebars=UnwrapElement(IN[0])

OUT=[doc.GetElement(r.GetHostId()) for r in rebars]

Exactly as you have displayed in the image. to make it clearer I have attached an image of my dynamo graph and how I wanted it to relate to the python script

I’m not good with dictionaries in python but I think you aren’t really creating a correct dictionary,then I do not think you are actually reading the rebar number parameter, nor should the .Count() method work…

Anyways I cleaned up a little bit your script (you were grouping and then selecting the last list instead of filtering directly the rebars hosted on the columns with the List.FilterByBoolMask) and I also added both my old version plus a new (ugly) one in python:
rebar2.dyn (43.7 KB)

This is the python code of the “short” node. I used long variable names to explain better what I’m doing. I’m sure there is a better way to do this, but for better troubleshooting you could just use the dynamo nodes I showed in my previous post.

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument

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

columnRebars=UnwrapElement(IN[0])

uniqueNumbers=[]
uniqueQuantities=[]
for rebars in columnRebars:
	uniqueNumbersForEachColumn=[]
	uniqueQuantitiesForEachColumn=[]
	for r in rebars:
		n=r.LookupParameter("Rebar Number").AsString()
		q=r.LookupParameter("Quantity").AsInteger()
		if n in uniqueNumbersForEachColumn:
			ind=uniqueNumbersForEachColumn.index(n)
			uniqueQuantitiesForEachColumn[ind]+=q
		else:
			uniqueNumbersForEachColumn.append(n)
			uniqueQuantitiesForEachColumn.append(q)
	uniqueNumbers.append(uniqueNumbersForEachColumn)
	uniqueQuantities.append(uniqueQuantitiesForEachColumn)

OUT=uniqueNumbers,uniqueQuantities

The file you attached is corrupt. It cannot open

It is written in Dynamo 2.0 but you can simply copy all the content of my previous messages…

Just as an addendum, native Python Dictionaries do not translate outside of nodes direclty into Dynamo :frowning: They Dynamo implementation is a .NET variant.

You can output a dictionary.items, dictionary.keys or dictionary.values however as a list :slight_smile:

1 Like

I had googled some examples and found some here on the forum with the dict.items but couldnt find how to create dictionaries from scratch. Furthermore i Never tested to see (but Will surely) if dictionaries are Quicker than normal lists or arrays, especially when searching and indexing.
Thanks anyways!

You can create dictionaries in a couple of ways, the one I know off the top of my head (Not near Dynamo/Revit to test right now) is by using a Dictionary Comprehension and is as follows (In Python):

myDictionary = {key:value for key, value in zip(keysList, valuesList)}

On the provision you have a singular key for every value, this will result in a Dictionary :slight_smile:

Then you simply call:

OUT = myDictionary.keys(), myDictionary.values()
5 Likes