Convert STRING to [elem] . SetParameterByName (force parameter TYPE) in Python

“ASSET_CONDITION” is a ‘number’ variable type in Revit- however, from excel - it reads as a ‘string’, how do I:

  1. Determine the target variable type I am about to set in Revit for the parameter data
  2. Force-cast that parameter data FROM string to that ‘target type’ … Preferably in a ‘pythonic’ manner - e.g converting all types across the list to the target variable type before pushing - or casting on the push back to Revit)

ParameterDataMap.py


## from http://dynamobim.org/forums/topic/element-setparameterbyname-node-through-python-script/
import clr
clr.AddReference("RevitNodes")
import Revit
import System  ## http://dynamobim.org/forums/topic/python-parametertype/

clr.ImportExtensions(Revit.Elements)
elements = IN[0]
params = IN[1]
vals = IN[2]
vi=[]
out=[]
pl=[]
plpr=[]
##Elements and Data move together - Params repeats
ie=0 ##sync elements in elements and values
for e in elements:
	vi=vals[ie]
	ip=0
	pl=[]
	pl.append(e)
	for p in params:
		plpr=[]
		try:
			##vtype=clr.GetClrType(e.GetParameterByName(params[ip]))
			##Need to assess target parameter type and push .string or .long or .int to the value
			##Erroring out on pushing strings to LONG and INT types.
			e.SetParameterByName(params[ip],vi[ip])
		except:
			plpr.append("ERROR")
		plpr.append(params[ip])
		plpr.append(vi[ip])
		pl.append(plpr)
		ip=ip+1
	ie=ie+1
	out.append(pl)
#Assign your output to the OUT variable.
OUT = out

@Ron_Allen
Just use a list comprehension.
Below is an example from interpreter for a list L:

L = [10.35, 11023.1455, 15.67, 0.33687, -20014.15872, 115]
LS = [str(i) for i in L] # change all elements to string type
LS
[‘10.35’, ‘11023.1455’, ‘15.67’, ‘0.33687’, ‘-20014.15872’, ‘115’]
LF = [float(i) for i in LS] # change all elements to float type
LF
[10.35, 11023.1455, 15.67, 0.33687, -20014.15872, 115.0]
TYPE_LS = [type(i) for i in LS] # verify type for strings
TYPE_LS
[type ‘str’, type ‘str’, type ‘str’, type ‘str’, type ‘str’, type ‘str’]
TYPE_LF = [type(i) for i in LF] # verify type for floats
TYPE_LF
[type ‘float’, type ‘float’, type ‘float’, type ‘float’, type ‘float’, type ‘float’]

Thanks!-

That looks good - for a list of lists would it be
L = (str(L[ ][1]) for i in L[ ][1]) without having to transpose, break out the lists, re-transpose and re-merge them?

I have about 100 or so lists of parameter data (about 12 deep in each of those sublists)

Do you know if a null throw it off or will it replace it with a zero? I realized later last night the nulls were part of what was throwing it and causing some of the errors.

@Ron_Allen
for a “list of lists” let’s call it L0 (two levels deep) use a list comprehension like this:

L1 = [[str(j) for j in i] for i in L0]

this will transform all the items from a second level list in strings and will flatten your list.

nb: list comprehensions are faster than for loops.

1 Like

Totally Sweet! Thnx!

@Ron_Allen

if you have “null” which are actually “None” in IronPython, you can replace those values like this:

for i in L:
if i == None:
i = "N/A"
else:
i = i
L.append(i)

in this example all the “None” values from a list of strings L, got replaced with “N/A”

That might be it
so if I get value from the Revit Parameter and then:

## from http://dynamobim.org/forums/topic/element-setparameterbyname-node-through-python-script/
import clr
clr.AddReference("RevitNodes")
import Revit
import System  ## http://dynamobim.org/forums/topic/python-parametertype/

clr.ImportExtensions(Revit.Elements)
elements = IN[0]
params = IN[1]
vals = IN[2]

vi=[]
out=[]
pl=[]
plpr=[]
for e in elements:
	vi=vals[ie]   ##Get values for current list element
	ip=0          ##Reset ip counter
	pl=[]         ##Reset pl Parameter list for sublist OUTPUT
	pl.append(e)  ##Add the element to the top of the sublist for reference

    ##pseudo code here: where 
    vtype= e.ParameterByName(p)               ##Probably a better pythonic way to wrap up prior to loop
    if(vtype Type.ToString() == "String"):
        vi = [[str(j) for j in i] for i in vi0]   # Set all values in that position of the data in the substrings to type STRING
    ##End pseudocods
	
    for p in params:
		plpr=[]
		try:
        ##vtype=clr.GetClrType(e.GetParameterByName(params[ip]))
			##Need to assess target parameter type and push .string or .long or .int to the value
			##Erroring out on pushing strings to LONG and INT types.
			e.SetParameterByName(params[ip],vi[ip])
		except:
			plpr.append("ERROR")              ##Something bombed out report is as "error" in the outlist
		plpr.append(params[ip])
		plpr.append(vi[ip])
		pl.append(plpr)
		ip=ip+1
	ie=ie+1
	out.append(pl)

@Ron_Allen Hi there. Did you manage to get this working as I am trying to do a similar thing at the moment. I thought it best to check in with this already constructed thread rather than starting a new one.
If you could help I would really appreciate that!