Trying to convert Parameter to a String, in Python. Can I do it only through Python?

I’ve got a list of Type Id parameters. And I’m looking to try to convert those to a list of strings so that I can strip the beginning 10 characters from each list item. Is there a command to convert Parameter defined values to String(s)?

import clr
clr.AddReference('RevitAPI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
clr.AddReference('RevitAPIUI')
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import collections
from sys import *
import string
import math
#The inputs to this node


lst1=IN[0]	#Lis of Type Id parameters
Elements=IN[1]	#List of correlating elements
inx=0
Remove=[]
Output = []
Type =[]
while inx<(len(lst1)):
	if not(lst1[inx] == None):
		Type.append(lst1[inx]), Output.append(Elements[inx])
	inx=inx+1
#Val="huh"
rem="Type Id : "
Vals=[]
inx=0
while inx<(len(Type)):
	Val=Type[inx]
	inx=inx+1
ValV= Val.ToString
#	Val.TrimStart(10)

#Assign your output to the OUT variable
#OUT = [Output, Type]
OUT= ValV

Yes, this can be done through the Revit API doing something like this:

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

# Assuming you are inputting parameters from Dynamo in IN[0]
parameters = UnwrapElement(IN[0])
values = []
for parameter in parameters:
    values.append(parameter.AsString())
OUT = values

Here’s the specific part of the documentation related to this.

1 Like

Unfortunately it still sees them as Parameter values instead of Strings

import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
import Autodesk
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
import RevitServices
from Autodesk.Revit.DB import *
from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import collections
from sys import *
import string
import math
#The inputs to this node


lst=IN[0]	#List of Type Id parameters
lst1=UnwrapElement(IN[0])	#Unwrapped Params
Elements=IN[1]	#List of correlating elements
inx=0
Remove=[]
Output = []
Type =[]
while inx<(len(lst1)):
	if not(lst1[inx] == None):
		Type.append(lst1[inx]), Output.append(Elements[inx])
	inx=inx+1
TypeV = UnwrapElement(Type)
rem="Type Id : "
Vals=[]
inx=0
#while inx<(len(TypeV)):
#	Val=TypeV[inx]
#	Vals.append(Val())
#	inx=inx+1
for parameter in lst1:
	Vals.append(parameter.AsString())
#Vals.append(Val.AsString())
#Vals.TrimStart(10)

#Assign your output to the OUT variable
#OUT = [Output, Type]
OUT= Vals

Unless I have implemented that wrong?

Here is the error that it is giving me:

File "<string ", line 47, in <module
AttributeError: ‘Parameter’ object has no attribute ‘AsString’

Some parameters use AsValueString() instead of AsString()

for parameter in lst1:
	Vals.append(UnwrapElement(parameter).AsValueString())

you can try something like this, or try to use AsString() if the other one returns null.

Same for both of those solutions. It is still percieving the values as Parameter types and is telling me that Parameter Objects don’t have ‘AsValueString()’ or ‘AsString()’.

I’m not sure how to possibly predefine Vals as equating to a string (or list of string) value(s.)

Would it be a better solution to just get the type Id’s from the elements list? Like

Vals = []
for i in UnwrapElement([IN[1]]):
	Vals.append(i.LookupParameter("Type Id").AsValueString())
OUT = Vals

I’ll try going about it with your code in mind. Big picture my thinking behind my original logic was to use the any lack of Type Id value’s as a way to filter out any non-FabricationPart elements that may be selected. I’ve done more coding for the rest of the entire DYN and have determined that I don’t actually have to cut off the 'Type Id : ’ part of the Parameter list. But I believe I do in fact need them to be in strings so I can create a boolean list when filtering later on. Like I said though I’ll keep you’re words in mind and try to look at it a little differently, try it again and I’ll reply back tomorrow with any findings.

With that being said I just threw you’re code into my script and it says that UnwrapElements is undefined. Not sure if it means from a module/command sense or if it’s seeing “UnwrapElements” as a variable…

import sys
import clr
import System
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
from sys import *
from System import *
import Autodesk
import Revit
import RevitServices
clr.ImportExtensions(Revit.GeometryConversion)
from Autodesk import *	#Revit
#from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *
import collections
import string
import math

#The inputs to this node
lst=IN[0]	#List of Type Id parameters
lst1=UnwrapElement(IN[0])	#Unwrapped Params
Elements=IN[1]	#List of correlating elements
inx=0
Remove=[]
Output = []
Type =[]
while inx<(len(lst1)):
	if not(lst1[inx] == None):
		Type.append(lst1[inx]), Output.append(Elements[inx])
	inx=inx+1

TypeV = UnwrapElement(Type)
rem="Type Id : "
Vals=[]
inx=0

for i in UnwrapElements([IN[1]]):
	Vals.append(i.LookupParameter("Type Id").AsValueString())


#while inx<(len(TypeV)):
#	Val=TypeV[inx]
#	Vals.append(Val())
#	inx=inx+1



#for parameter in lst1: Vals.append(UnwrapElement(parameter).AsString())

#Vals.append(Val.AsString())
#Vals.TrimStart(10)
#Assign your output to the OUT variable
#OUT = [Output, Type]
OUT= Vals

And When I change for i in UnwrapElement([IN[1]]): to for i in (TypeV): ((TypeV=UnwrapElement([IN[1]]) is defined above the For loop)) it gives me the following Error message:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 46, in
AttributeError: ‘List[object]’ object has no attribute ‘LookupParameter’

for i in UnwrapElements(IN[1]):
	Vals.append(i.LookupParameter("Type Id").AsValueString())

That might work better. I had brackets because I was testing it on a single object and not a list.
I should have removed the brackets, also there is no 'UnwrapElement(s), I think you may have made a typo. It is just UnwrapElement(argument) with no ‘s’

So when I use that, it gives me the following error:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 46, in
AttributeError: ‘NoneType’ object has no attribute ‘AsValueString’

And when I change it to .AsString it gives me the same error, but for the attribute .AsString

So I’ve done some digging and was trying my best to just re-write the script using Dynamo nodes for a different perspective. And I’ve come across a node that is apart of the Bakery Add-On called “List to Single String” and after diving into that node I’ve found a python script that does what I’m looking for. I have my revised script below. Thank you so much for you’re help @martin.scholl. I’ve learned alot more about Dynamo/Python in us trying to work towards getting this working.

import sys
import clr
import System
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
clr.AddReference("RevitServices")
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
from sys import *
from System import *
import Autodesk
import Revit
import RevitServices
clr.ImportExtensions(Revit.GeometryConversion)
from Autodesk import *	#Revit
#from Autodesk.DesignScript.Geometry import *
from RevitServices.Persistence import *
from RevitServices.Transactions import *
clr.AddReference('DSCoreNodes') 
import DSCore as DS
from DSCore import *
import collections
import string
import math

#The inputs to this node
lst=IN[0]	#List of Type Id parameters
lst1=UnwrapElement(IN[0])	#Unwrapped Params
Elements=IN[1]	#List of correlating elements
inx=0
Remove=[]
Output = []
Type =[]


while inx<(len(lst1)):
	if not(lst1[inx] == None):
		Type.append(lst1[inx]), Output.append(Elements[inx])
	inx=inx+1

TypeV = UnwrapElement(Type)
rem="Type Id"
Vals=[]
inx=0

StringList = []
stringList = []
for x in lst:
	try:
		tempString = str(x)
	except:
		tempString = "stringConversionFailed"
	stringList.append(tempString)
for i in stringList:
	if not i == "None":
		StringList.append(i)



#for i in (IN[1]):
#	Vals.append(i.LookupParameter("Type Id").ToString())

#Element.

#while inx<(len(TypeV)):
#	Val=TypeV[inx]
#	Vals.append(Val())
#	inx=inx+1


#for parameter in lst1: Vals.append(UnwrapElement(parameter).AsString())

#Vals.append(Val.AsString())
#Vals.TrimStart(10)
#Assign your output to the OUT variable
#OUT = [Output, Type]
OUT= StringList

The LookupParameter() method attempts to find a parameter of the provided name. If there is no parameter by that name, it will return a null object. So the function is then calling None.AsString() which is invalid. The way to work around this would be to add a check before attempting the AsString() method. For example:

elements = UnwrapElement(IN[0])
values = []
for e in elements:
    parameter = e.LookupParameter('Type Id')
    if parameter:
        values.append(parameter.AsString())
    else:
        values.append('')
OUT = values
3 Likes