How to add a new category to an existing project parameter

How to add a new category to an existing project parameter

434977268
Participant Participant
443 Views
2 Replies
Message 1 of 3

How to add a new category to an existing project parameter

434977268
Participant
Participant

 

 string targetParamName = "测试";
BuiltInCategory newCategory = BuiltInCategory.OST_Ceilings;

DefinitionBindingMapIterator iter = document.ParameterBindings.ForwardIterator();
                while (iter.MoveNext())
                {
                    Definition def = iter.Key;
                    if (def.Name == targetParamName)
                    {
                        ElementBinding binding = iter.Current as ElementBinding;
                        CategorySet categories = binding.Categories;
                        using (Transaction t = new Transaction(document, "Insert Category"))
                        {
                            t.Start();
                            categories.Insert(document.Settings.Categories.get_Item(newCategory));
                            t.Commit();
                        }
                        break;
                    }
                }

 

This method does not add a new category to the specified project parameter. Although the members of binding.Categories increase every time the method is executed, there is no Test parameter in the Ceiling properties in the software.

Please tell me where the problem is, thank you

0 Likes
444 Views
2 Replies
Replies (2)
Message 2 of 3

Mohamed_Arshad
Advisor
Advisor

@HI @434977268 

You can't able to add new Category to existing one, But you can Create a New Category Set and then Re-Insert the in the Binding Map.

01. Create a New Category Set with Existing Categories.

02. Create a New Binding using New Category Set.

03. using https://www.revitapidocs.com/2021.1/7b613771-310d-6d89-4b69-475a68033f73.htm Add the Category.

Hope this Helps 🙂

 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 3

434977268
Participant
Participant
 string targetParamName = "Test";
                BuiltInCategory newCategory = BuiltInCategory.OST_Ceilings;

                DefinitionBindingMapIterator iter = document.ParameterBindings.ForwardIterator();
                BindingMap bdm = document.ParameterBindings;
                Category category = document.Settings.Categories.get_Item(newCategory);
                while (iter.MoveNext())
                {
                    Definition def = iter.Key;
                    if (def.Name == targetParamName)
                    {
                        ElementBinding binding = iter.Current as ElementBinding;
                        CategorySet categories = binding.Categories;
                        categories.Insert(category);
                        using (Transaction t = new Transaction(document, "Insert Category"))
                        {
                            t.Start();
                            CategorySet categorySet = new CategorySet();
                            foreach (Category item in categories)
                            {
                                categorySet.Insert(item);
                            }
                            InstanceBinding instanceBinding = document.Application.Create.NewInstanceBinding(categorySet);
                            bdm.ReInsert(def, instanceBinding);
                            t.Commit();
                        }
                        break;
                    }
                }

I changed the code according to what you said, but I found that it still doesn’t work. Can you help me see where the problem is? Thank you

0 Likes