Adding categories to existing parameter binding

Adding categories to existing parameter binding

j.jansmaG7C9C
Contributor Contributor
1,011 Views
3 Replies
Message 1 of 4

Adding categories to existing parameter binding

j.jansmaG7C9C
Contributor
Contributor

Hi all,

 

I have this piece of code that (succesfully) adds a category to an existing parameter binding:

 

Categories.Add(BuiltInCategory.OST_Walls);
Categories.Add(BuiltInCategory.OST_Roofs);
Categories.Add(BuiltInCategory.OST_Floors);
Categories.Add(BuiltInCategory.OST_Assemblies);

foreach (BuiltInCategory BuiltInCategory in Categories)
{
Category Category = doc.Settings.Categories.get_Item(BuiltInCategory);
if (elemBind.Categories.Contains(Category) == false)
{ elemBind.Categories.Insert(Category); }
}

 

This code seems to work like a charm. In an example in which 3 of the 4 categories were already added, the fourt one is (in this case, Assemblies) added succesfully and when I lookup the actual variable values of the parameterbinding in Visual Studio, I see that all four categories are added:

jjansmaG7C9C_0-1686917760684.png

 

 

However, back in the UI, the changes are neglected:

jjansmaG7C9C_1-1686917903898.png

 

 

I also tried adding the following line, but unfortunetaly, to no avail.

doc.ParameterBindings.ReInsert(definition, rlemBind, Group);

 

I've seen multiple snippets from Jeremy that seem to work this way, like this simple one here:

The Building Coder: Adding a Category to a Parameter Binding (typepad.com)

 

Can anyone provide a clue?

0 Likes
1,012 Views
3 Replies
Replies (3)
Message 2 of 4

Mohamed_Arshad
Advisor
Advisor

HI @j.jansmaG7C9C 

 

I create a script that will add the New Category to Existing Binding. Kindly Check the Below Code and GIF for additional Reference.

 

Script

            //Global Variables
            InstanceBinding instanceBinding = null;
            Definition def = null;

            //Category Need to Add to the Existing Prameter Bindings
            Category cat = Category.GetCategory(doc, BuiltInCategory.OST_Assemblies);

            //Get Instance Binding From Document
            var map = doc.ParameterBindings.ForwardIterator();

            //Iterate using Iterator
            while (map.MoveNext())
            {
                //Get the Parameter need to add
                def = map.Key as Definition;

                if (def.Name == "Demo")
                {
                    instanceBinding = map.Current as InstanceBinding;
                }

            }

            //Get Existing Category Set From the Binding
            CategorySet existingSet = instanceBinding.Categories;

            if(!existingSet.Contains(cat))
            {
                existingSet.Insert(cat);
            }

            //Adding Category to Exsting Instance Binding (Without changing any parameter)
            using (Transaction addCategory = new Transaction(doc,"Add Category"))
            {
                addCategory.Start();

                //Re-Insert Categories to Exsting Instance Binding
                instanceBinding.Categories = existingSet;

                doc.ParameterBindings.ReInsert(def, instanceBinding);

                addCategory.Commit();
            }

 

Reference GIF

Adding Category to Existing Instance Parameter Binding.gif

 

Hope this Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 4

cig_ad
Enthusiast
Enthusiast

-

0 Likes
Message 4 of 4

phantbinh99
Enthusiast
Enthusiast
```
doc.ParameterBindings.ReInsert(def, instanceBinding);
and
doc.ParameterBindings.Remove(def);
```

both functions return false, do you know why? I checked with contains function before Remove or ReInsert so def definitely exists

they work for project parameters created from shared parameters, but for newly created project parameters they both return false
0 Likes