Key Parameters

Key Parameters

As you know we can create Key Parameter schedule in Revit to control multiple parameter values.

And if you need to automate it with Revit API - it can be a bit tricky… You see, you won't be able to provide values with text, instead you actually need to get ElementId of all possible keys, and then choose the right one.

Get Key Parameter Values

So, firstly you need to get all possible key values, and using the schedule is the best way to do that.

Here is the code snippet:

  • 1. Get Schedule

  • 2. Match Schedule to Key-Parameter Name

  • 3. Sort Values in Dict

#📦 Variables
p_key_name   = 'Room Style' # Write Your Key-Param Name!
key_schedule = None

#1️⃣ Get all Schedules
all_schedules = FilteredElementCollector(doc)\
                .OfCategory(BuiltInCategory.OST_Schedules)\
                .ToElements()

#2️⃣ Get Key-Schedule            
for schedule in all_schedules:
    try:
        if schedule.KeyScheduleParameterName == p_key_name:
            key_schedule = schedule
            break
    except: 
        pass

#💡 Ensure you found matching schedule
if not key_schedule:
    forms.alert("Can't find matching Schedule.\nPlease Try Again.",
                                                    exitscript=True)
    
#3️⃣ Get Possible Key-Parameter Values
key_values = FilteredElementCollector(doc, key_schedule.Id).ToElements()
dict_key_values = {key.Name : key.Id for key in key_values}

#👀 Display Key-Parameter Values {Names: Id}
print('Key Parameter: {}'.format(p_key_name))
print('Matching Schedule: {}'.format(key_schedule.Name))
for k,v in dict_key_values.items():
    print(k,v)


Set Key Parameter Value

Once you have a dict of all possible values, you can select the value you need and provide its Elementid as a new value in your Key-Parameter.

Here is code:

t = Transaction(doc,'Change KeyParam')
t.Start()  # 🔓

random_id = dict_key_values.values()[0]
p_key     = room.LookupParameter(p_key_name )
p_key.Set(random_id)

t.Commit() # 🔒

⌨️ Happy Coding!
Erik Frits