String to Spec Type

Buongiorno a tutti,

nel creare dei parametri condivisi da un file excel non riesco a convertire una lista di stringhe in Spec Type.

Avete qualche suggerimento in merito?
Grazie


EDIT : Translated by Moderation (English forum)

Good morning, everyone,

in creating shared parameters from an excel file I cannot convert a list of strings to Spec Type.

Do you have any suggestions on this?
Thanks

This code was made for GroupTypes but there wouldn’t be much to change for Spectype, Give it a go :slight_smile:

Hint for you*

# Collect the parameter groups and convert them to a string list
parameter_groups_enum = System.Enum.GetValues(BuiltInParameterGroup)
parameter_groups = [System.Enum.GetName(BuiltInParameterGroup, group) for group in parameter_groups_enum]

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]
3 Likes