String to Spec Type

Hi,

here is a solution with Python

the input is a flat list of the Name of Parameter Type (Spec Type) :

  • in English
    or
  • in Revit Current language

Python code (compatible to all engines)

import clr
import sys
import System
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
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

def toList(x):
    if isinstance(x, list):
        return x
    elif hasattr(x, "GetType") and x.GetType().GetInterface("IEnumerable") is not None:
        return x
    else :
        return [x]

lst_specType_Name = toList(IN[0])
invalid_forgeTypeId = ForgeTypeId()

dict_forgeTypeId = {}
lst_specTypeId_class = [clr.GetClrType(SpecTypeId), 
                        clr.GetClrType(SpecTypeId.Boolean), 
                        clr.GetClrType(SpecTypeId.Int), 
                        clr.GetClrType(SpecTypeId.String)]
#
for specTypeId_class in lst_specTypeId_class :
    prop_infos = specTypeId_class.GetProperties()
    # get the Name of SpecType and put it in a dictionary {SpecType Name : SpecType }
    dict_tempA_forgeTypeId = {p.Name : p.GetGetMethod().Invoke(None, None) for p in prop_infos}
    # get the LabelForSpec and put it in a dictionary {label : SpecType }
    dict_tempB_forgeTypeId = {}
    for key_, value_ in dict_tempA_forgeTypeId.items() :
        try:
            dict_tempB_forgeTypeId[LabelUtils.GetLabelForSpec(value_)] = value_ 
        except Exception as ex:
            print(ex)
    # merge dicts in  main dict
    dict_forgeTypeId = dict(dict_forgeTypeId, **dict_tempA_forgeTypeId)
    dict_forgeTypeId = dict(dict_forgeTypeId, **dict_tempB_forgeTypeId)
    
# get DB.SpecTypeId by input Name
lst_db_specTypeId = [dict_forgeTypeId.get(txt_value, invalid_forgeTypeId) for txt_value in lst_specType_Name]
# convert to Dynamo Revit.Elements.SpecType
OUT = [Revit.Elements.SpecType.ByTypeId(v.TypeId) for v in lst_db_specTypeId]
2 Likes