SharedParameters: Modify CategorySet

I was asked how to modify CategorySet of existing SharedParameters in the project.

So let me share how to do that.

Change CategorySet of Shared Parameters:

Here is a simple code example to iterate through all SharedParameters in the project and ensure that they have extra_categories ticked in the CategorySet.

The Script Breakdown:
- Define extra_categories
- Get SharedParameters from ParameterBindings
- Modify Existing CategorySet
- ReInert definition with updated binding rules

Here is the Code:

#⬇️ Imports
from Autodesk.Revit.DB import *             # Revit API Import
import os

#----------------------------------------------------------------------------------------------------
#📦 Variables
uidoc    = __revit__.ActiveUIDocument                               # Revit UI
doc      = __revit__.ActiveUIDocument.Document #type:Document       # Revit Project

#----------------------------------------------------------------------------------------------------
#1️⃣ Define List of Categories
extra_categories = [BuiltInCategory.OST_Doors, BuiltInCategory.OST_Walls, BuiltInCategory.OST_Windows]

#🔓 Start Transaction for Changes
t = Transaction(doc, "Add Cats to Shared Parameters")
t.Start()

#2️⃣ Get Shared Parameters from ParameterBindings.
parameters_to_modify = []
binding_map          = doc.ParameterBindings
iterator             = binding_map.ForwardIterator() #
iterator.Reset()

while iterator.MoveNext():
    parameters_to_modify.append((iterator.Key, iterator.Current))


#3️⃣ Modify Existing SharedParameters (Add more Categories)
for definition, binding in parameters_to_modify:
    new_cats = []
    if isinstance(binding, InstanceBinding):
        cat_set = binding.Categories

        for cat_id in extra_categories:
            try:
                category = doc.Settings.Categories.get_Item(cat_id)
                if not cat_set.Contains(category):
                    cat_set.Insert(category)
                    new_cats.append(category.Name)
            except Exception as e:
                print("Error with category {}: {}".format(cat_id, str(e)))

        if new_cats:
            binding_map.ReInsert(definition, binding)
            print('Updated Definition: {}'.format(definition.Name))
            for cat_name in new_cats:
                print('Added Category: {}'.format(cat_name))
            print('-'*50)

t.Commit()

⌨️ Happy Coding!
Erik Frits