Parameter Bindings

Parameter Bindings

Anonymous
Not applicable
3,102 Views
8 Replies
Message 1 of 9

Parameter Bindings

Anonymous
Not applicable

Hello all,

 

I am using this code to add categories to existing parameters. Aim is to attach parameters defined in rooms automaticly to the generating walls of the room. I can do this manually via the userinterface, but ReInsert allways fails via API, any Idea?

 

 

            Document doc = target.Document;

            // Get the BingdingMap of current document.
            BindingMap bindingMap = doc.ParameterBindings;
            DefinitionBindingMapIterator it = bindingMap.ForwardIterator();
            it.Reset();
            while (it.MoveNext())
            {
                if (it.Key.Name.Contains(param)) 
                {
                    if (it.Current is InstanceBinding EB)
                    {
                        EB.Categories.Insert(target.Category);
                        bool success = bindingMap.ReInsert(it.Key, EB);
                        return success; // success;
                    }
                    else
                        return false;
                }
            }
0 Likes
3,103 Views
8 Replies
Replies (8)
Message 3 of 9

Anonymous
Not applicable

Hello Jeremy,

 

thx for the link I was aware of this, and I was trying to adapt the code provided. The parameter I want to bind to the new category (wall) is part of the project and currently only bound to rooms. I simply want to assign a new category to an existing binding. 
The provided code works well with parameters I insert via the api as shared parameters but it always fails when I want to update the categories of parameters that where added manually via the UI.

Message 4 of 9

jeremytammik
Autodesk
Autodesk

I thought this kind of binding of parameters to categories only works for shared parameters anyway.

 

Can you achieve the desired result manually in the user interface?

 

If not, you probably cannot do it in the API either.

 

If yes, then the code provided should achieve exactly that.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 9

joshua.lumley
Advocate
Advocate

This code works, it detects if parameter is already there, you need catSet and myDefinition coming into it.

Revit crashes if "break;" isn't there (even inside a try-catch).

 

 

 

                        BindingMap bindingMap = doc.ParameterBindings;
                        DefinitionBindingMapIterator it = bindingMap.ForwardIterator();
                        it.Reset();
                        while (it.MoveNext())
                        {
                            if (it.Key.Name == myDefinition.Name)
                            {
                                if(it.Current is InstanceBinding)
                                {
                                    foreach (Category cat in catSet)
                                    {
                                        if (!((InstanceBinding)it.Current).Categories.Contains(cat))
                                        {
                                            ((InstanceBinding)it.Current).Categories.Insert(cat);
                                        }
                                    }
                                    bool success = bindingMap.ReInsert(myDefinition, (InstanceBinding)it.Current, BuiltInParameterGroup.PG_IDENTITY_DATA);
                                    bool_Reinserted = true;
                                }
                                if (it.Current is TypeBinding)
                                {
                                    foreach (Category cat in catSet)
                                    {
                                        if (!((TypeBinding)it.Current).Categories.Contains(cat))
                                        {
                                            ((TypeBinding)it.Current).Categories.Insert(cat);
                                        }
                                    }
                                    bool success = bindingMap.ReInsert(myDefinition, (TypeBinding)it.Current, BuiltInParameterGroup.PG_IDENTITY_DATA);
                                    bool_Reinserted = true;
                                }

                                break;
                            }
                        }

                        if (!bool_Reinserted)
                        {
                            Binding binding = IsTypeParameter ? uidoc.Application.Application.Create.NewTypeBinding(catSet) as Binding : uidoc.Application.Application.Create.NewInstanceBinding(catSet) as Binding;

                            doc.ParameterBindings.Insert(myDefinition, binding, BuiltInParameterGroup.PG_IDENTITY_DATA);
                        }

 

 

 

0 Likes
Message 6 of 9

JesikaDG
Contributor
Contributor

A note on this code sample. The reason Revit crashes without a "break" is the code is walks through the binding list at the same time it is asking Revit to modify the binding list. It would not exception if the data was collected separately than the calls to Insert and Reinsert.
One way this can be accomplished is putting the contents of the binding map into a list first and then looping through the list instead of using the binding map iterator directly.

0 Likes
Message 7 of 9

joshua.lumley
Advocate
Advocate
    BindingMap All_Binding = doc.ParameterBindings;
    DefinitionBindingMapIterator Iterator = All_Binding.ForwardIterator();
    Iterator.Reset(); List<string> BindingList = new List<string>();
    while (Iterator.MoveNext())
    {
        Definition def = Iterator.Key;
        ElementBinding Binders = (ElementBinding)Iterator.Current;
        BindingList.Add(def.Name);
    }
    BindingList.Sort();
0 Likes
Message 8 of 9

prasadNELXB
Explorer
Explorer

Hello Richard,

 

 I am facing the exact same problem. Not able to update the categories of project parameters that were added manually via the UI(even with ReInsert) 

 

have you found out any solution or work around for this?

 

0 Likes
Message 9 of 9

gdavis479JP
Contributor
Contributor

Please explain how to set MyDefinition

 

0 Likes