Hi everyone I have some problems trying to get the result with this dynamo python script all the inputs that I bring to the code seems to be fine, but when try to iterate all of them with an if statements prompts me an error “object cannot be interpreted as an index”. I am still learning with python and I do not know what should be the problem. If anyone knows I will really appreciate regards.
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
pol = []
#Geeting Voltage Drop by Num_Poles
for p in Poles:
if p == 1:
pol = 2 * (current * res * (len/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
result.append(pol)
elif p == 3:
pol = 1.732 * (current * res * (len/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
result.append(pol)
OUT = result
I believe the code is OK, so it might be your inputs. Make sure they are all of a numerical type (integer/double) and not in a list format. If you want to work with lists of values and poles in parallel you should use the zip() iteration method which looks a bit like this…
example1 = [1,2,3]
example2 = [10,20,30]
results = []
for e1,e2 in zip(example1,example2):
result = e1 + e2
results.append(result)
print(results)
would show...
[11,22,33]
I think you need to add either “len” as a variable or you need to call it “leng”?
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
pol = []
#Geeting Voltage Drop by Num_Poles
for p in Poles:
if p == 1:
pol = 2 * (current * res * (leng/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
result.append(pol)
elif p == 3:
pol = 1.732 * (current * res * (leng/1000 ) * power) + (current * reac * (leng/1000 ) * math.sin(math.acos(power)))
result.append(pol)
OUT = result
type or paste code here
Thank you everyone for your comments this what I got so far, I believe with the for in zip function gives me more results that I need. for instance all the input list got 187 items which is all the values that i got from the project. with my new code instead of that items that i need for the list i got 34969 items, seems to me is like kind of cross product with a node in Dynamo correct me if I wrong regards.
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
Poles = IN[0]
current = IN[1]
res = IN[2]
leng = IN[3]
power = IN[4]
reac = IN[5]
result = []
#Geeting Voltage Drop by Num_Poles
for p in Poles:
if p == 1:
for curr, res1, leng1, power1, reac1 in zip(current, res,leng, power,reac):
x = 2 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
result.append(x)
elif p == 3:
for curr, res1, leng1, power1, reac1 in zip(current, res,leng, power,reac):
x = 1.732 * (curr * res1 * (leng1/1000 ) * power1) + (curr * reac1 * (leng1/1000 ) * math.sin(math.acos(power1)))
result.append(x)
else:
result.append("No voltage Drop")
OUT = result
I usually use “cycle” function in my Python code to rotate for each item in my lists. So you can control the the list length.
I don’t have other values that feeds your code but I tried to mimic your code.
here is the first version which is the longest:
and the code you need to use below:
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
from itertools import cycle
data = IN[0]
list_one = IN[1]
list_two = IN[2]
output = []
for i in data:
if i == 1:
for lst1, lst2 in zip(list_one, cycle(list_two)):
total = lst1 + lst2
output.append(total)
elif i == 2:
for lst1, lst2 in zip(list_one, cycle(list_two)):
total = lst1 / lst2
output.append(total)
elif i == 3:
for lst1, lst2 in zip(list_one, cycle(list_two)):
total = lst1 * lst2
output.append(total)
else:
output.append("No Voltage")
OUT = output
here is the second version which is the shortest:
and the code you need to use below:
# Load the Python Standard and DesignScript Libraries
import clr
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
import math
from itertools import cycle
data = IN[0]
list_one = IN[1]
list_two = IN[2]
output = []
for i in data:
if i == 1:
for lst1, lst2 in zip(cycle(list_one), list_two):
total = lst1 + lst2
output.append(total)
elif i == 2:
for lst1, lst2 in zip(cycle(list_one), list_two):
total = lst1 / lst2
output.append(total)
elif i == 3:
for lst1, lst2 in zip(cycle(list_one), list_two):
total = lst1 * lst2
output.append(total)
else:
output.append("No Voltage")
OUT = output
Yes all the inputs has the same items, so probably that the reason why I got the cross product. See the image down below thank you for the help everyone.
I think you should explain more about your graph and What’s the result for.
And it may easier to understand with less items, like 3 or 4 items…not your real hundreds items.
for curr, res1, leng1, power1, reac1, p in zip(current, res,leng, power,reac, Poles):
if p == 1:
# rest of code for voltage drop "single phase" calculation
elif p == 3:
# rest of code for voltage drop "three phase" calculation
else:
# rest of code
Although I understand this code may be your resolution, it’s typically good practice to mark the post which provided the path or suggested the approach that resolved your issue as the Answer. This gives credit to those that help you find the solution.