Compare lists from type name with parameter value if not equal, change parameter value

Translated by moderator. See below the line for the original post.


Hello,

I work in the family editor. I have a list of the type names and a list of the parameters.
The type names should be made up of the parameter values ​​(width x depth x height).
I converted the type name strings to Number and compare the two lists.
I’ve come this far.

If the values ​​of the lists are different I would like to replace the parameter value with the value from the type name. But I can’t get any further here. Can someone help me please? Unfortunately, as a new user, I cannot upload the .dyn and .rfa.

Thanks Stefan


Original post below.


Hallo,
ich arbeite im Familieneditor. Ich habe eine Liste mit den Typnamen und eine Liste mit den Parametern.
Die Typnamen sollen sich aus den Parameterwerten zusammensetzen (Breite x Tiefe x Höhe).
Ich habe die Typnamenstrings in Number umgewandelt und vergleiche die beiden Listen.
Bis hier hin bin ich gekommen.

Wenn die Werte der Listen unterschiedlich sind möchte ich den Parameterwert ersetzen durch den Wert aus dem Typename. Hier komme ich aber nicht weiter. Kann mir bitte jemand helfen? Leider kann ich als neuer User die .dyn und die .rfa nicht hochladen.

Danke Stefan

Reminder that the forum language is English. Sadly this is a requirement in order to make search work, and as the APIs are English based, annars skulle jag träna på min svenska i varje svar.

Please translate posts as I did in the edit above, or post only in English going forward.

Thanks!

You could try this?
Its a bit sloppy but it might work for you.

Just change the name of the paramets here and at the end of the script where it sets them

    _Parameter_Width_ = _Dims.GetParameters("AP Width") #Change for "SP_Width"
    for _Width in _Parameter_Width_:
        width = _Width.AsValueString()
        
    _Parameter_Length_ = _Dims.GetParameters("AP Length") #Change for "SP_Depth"
    for _Length in _Parameter_Length_:
        length = _Length.AsValueString()

    _Parameter_Height_ = _Dims.GetParameters("AP Height") #Change for "SP_Height"
    for _Height in _Parameter_Height_:
        height = _Height.AsValueString()

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

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

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#Definition Esure the input is a list
def tolist(obj1):
	if hasattr(obj1,'__iter__') : return obj1
	else : return [obj1]
		
#Definition to get the name of the element
#This Def is from Clockwork Package
def GetName(item):
	unwrapped = UnwrapElement(item)
	if not unwrapped: return None
	elif unwrapped.GetType().ToString() in ("Autodesk.Revit.DB.Area", "Autodesk.Revit.DB.Architecture.TopographySurface"):
		try: return unwrapped.get_Parameter(BuiltInParameter.ROOM_NAME).AsString()
		except: return unwrapped.Name
	elif unwrapped.GetType().ToString() in ("Autodesk.Revit.DB.BuiltInParameter", "Autodesk.Revit.DB.BuiltInParameterGroup", "Autodesk.Revit.DB.DisplayUnitType", "Autodesk.Revit.DB.ParameterType", "Autodesk.Revit.DB.UnitSymbolType", "Autodesk.Revit.DB.UnitType"): 
		try: return LabelUtils.GetLabelFor(unwrapped)
		except: return unwrapped.ToString()
	elif unwrapped.GetType().ToString() in ("Autodesk.Revit.DB.Parameter", "Autodesk.Revit.DB.FamilyParameter"): return unwrapped.Definition.Name
	elif unwrapped.GetType().ToString() == ("Autodesk.Revit.DB.ForgeTypeId"): 
		try: return LabelUtils.GetLabelForSpec(unwrapped)
		except: 
			try: return LabelUtils.GetLabelForSymbol(unwrapped)
			except: 
				try: return LabelUtils.GetLabelForUnit(unwrapped)
				except: 
					try: return LabelUtils.GetLabelForGroup(unwrapped)
					except: 
						try: return LabelUtils.GetLabelForBuiltinParameter(unwrapped)
						except: 
							try: return LabelUtils.GetLabelForDiscipline(unwrapped)
							except: return unwrapped.TypeId			
	elif hasattr(unwrapped, "GetName"): return unwrapped.GetName()
	elif hasattr(unwrapped, "Name"): return unwrapped.Name
	elif hasattr(item, "Name"): return item.Name
	else: return None
	
#Preparing input from dynamo to revit
_Element = UnwrapElement(tolist(IN[0]))

#Variables
doc =           DocumentManager.Instance.CurrentDBDocument
_Dimensions =   [] #Length x Width x Height of element
_Element_Name = [] #Current Name of the Element
_Name_Test =    []

for _Dims in _Element:
    _Parameter_Width_ = _Dims.GetParameters("AP Width") #Change for "SP_Width"
    for _Width in _Parameter_Width_:
        width = _Width.AsValueString()
        
    _Parameter_Length_ = _Dims.GetParameters("AP Length") #Change for "SP_Depth"
    for _Length in _Parameter_Length_:
        length = _Length.AsValueString()

    _Parameter_Height_ = _Dims.GetParameters("AP Height") #Change for "SP_Height"
    for _Height in _Parameter_Height_:
        height = _Height.AsValueString()

    # Create a string for each element with its dimensions
    element_dimensions = f"{width}x{length}x{height}mm"
    _Dimensions.append(element_dimensions)

#Get the element's Current Name		
if isinstance(_Element, list): 
    _Element_Name = [GetName(x) for x in _Element]
else: 
    _Element_Name = [GetName(_Element)]
    
# Compare the lists to test if the names match

for i in range(len(_Dimensions)):
    # Ensure the items exist before trying to access them
    if i < len(_Dimensions) and i < len(_Element_Name):
        # Compare the string values of the items
        if _Dimensions[i] == _Element_Name[i]:
            _Name_Test.append("Element Un-Changed")
        else:
            _Name_Test.append("Element Changed")
            # Start a transaction to modify the parameters
            TransactionManager.Instance.EnsureInTransaction(doc)
            for _Dims in _Element:
                _Dims.LookupParameter("AP Width").Set(int(_Dimensions[i].split('x')[0]) / 304.8) #Change for "SP_Width"
                _Dims.LookupParameter("AP Length").Set(int(_Dimensions[i].split('x')[1]) / 304.8) #Change for "SP_Depth"
                _Dims.LookupParameter("AP Height").Set(int(_Dimensions[i].split('x')[2].replace("mm", "")) / 304.8) #Change for "SP_Height"
            # End the transaction
            TransactionManager.Instance.TransactionTaskDone()

OUT =_Name_Test