Loop through ElementTypes and bind definitions

Loop through ElementTypes and bind definitions

Anonymous
Not applicable
707 Views
4 Replies
Message 1 of 5

Loop through ElementTypes and bind definitions

Anonymous
Not applicable

Hello,

 

I'm trying to bind some definitions to different ElementTypes.

I want to loop through all ElementTypes "elementTypes" and add the definitions "myDefs" to them. I'm using the loop because i want to add a check later, which checks if the definitions are already bound to the elementtypes. When I try it with a wall, window and door it somehow only binds to the wall and skips the other elementtypes. The bool "bindOk" stays false in this case. I know that i can add definitions to all elementtypes easily, but i want to add them only to special types.

foreach (ElementType eT in elementTypes)
                {
                    string eTypeCategory = eT.Category.Name;

                    CategorySet myCategories = app.Application.Create.NewCategorySet();

                    Category myCategory = app.ActiveUIDocument.Document.Settings.Categories.get_Item(eTypeCategory);
                    myCategories.Insert(myCategory);

                    BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings;

                    TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories);

                    foreach (Definition d in myDefs)
                    {

                        using (Transaction tr = new Transaction(doc))
                        {
                            tr.Start("addingParameters");
                            bool bindOK = bindingMap.Insert(d, typeBinding, BuiltInParameterGroup.PG_DATA);

                            tr.Commit();
                        }
                    }                    
                }

 

 

I'm quite confused, because if I try to bind the definitions all at once, like in the following code, it works.

foreach (ElementType eT in elementTypes)
                {
                    string eTypeCategory = eT.Category.Name;                    
                    Category myCategory = app.ActiveUIDocument.Document.Settings.Categories.get_Item(eTypeCategory);
                    myCategories.Insert(myCategory);      
                }
                BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings;
                TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories);

                foreach (Definition d in myDefs)
                {
                    foreach (ElementType e in elementTypes)
                    {
                        if (null == e.get_Parameter(d))
                        {
                            using (Transaction tr = new Transaction(doc))
                            {
                                tr.Start("addingParameters");
                                bool bindOK = bindingMap.Insert(d, typeBinding, BuiltInParameterGroup.PG_DATA);

                                tr.Commit();
                            }
                        }
                        else 
                        { 
                            continue;  
                        }
                    }                  
                }

 

Does somebody have an idea where my mistake is or can provide another way to solve this problem?

Thanks in advance! 🙂

Jonas

 

0 Likes
708 Views
4 Replies
Replies (4)
Message 2 of 5

cwaluga
Advocate
Advocate

In your first code you create the binding map with only one category. Every subsequent call to insert does not add to the map but should throw an exception (ReInsert should be used for later modifications for whatever reason). The right way would be to set up the category set for all categories and create only one binding for all relevant categories, then add parameters. 

Message 3 of 5

Anonymous
Not applicable

Hello,

thanks for your answer!

 

Do you have an idea how i can check, if the binding already exist for categories before i do the actual binding?

 

 

0 Likes
Message 4 of 5

cwaluga
Advocate
Advocate

You can ask a binding for its Categories. Thus you can implement a loop in which you collect all bindings for categories.

 

I would however do it the other way round. Whenever you try to bind a definition to categories, use Document.ParameterBindings.Contains to check whether there is already a Binding. Then retrieve the Binding using Document.ParameterBindings.get_Item. Insert all the categories already bound to into your category set for binding and use Document.ParameterBindings.ReInsert.

 

Message 5 of 5

Anonymous
Not applicable

So i managed to check if there is already a binding for the categories by looking up the parameters of the elements.

Now i devide them into two seperate categorysets and insert them into the typebinding/mapbinding.

 

The problem now is, that when I run the code, add an element afterwards, run the code again, it doesn't add the bindings to the new element, altought i can look up the category of the element in "myCategories". How can i add new bindings to the already existing BindingMap?

 

I'm new to coding, sorry for my slow understanding and thanks in advance!

 

foreach (ElementType eT in elementTypes)
                    {
                        Parameter para;
                        if (null != (para = eT.get_Parameter(d)))
                        {
                            Category eTypeCategory = eT.Category;
                            boundCategories.Insert(eTypeCategory);
                        }
                        else
                        {
                            Category eTypeCategory = eT.Category;
                            string eTypeCategoryName = eT.Category.Name;
                            myCategories.Insert(eTypeCategory);
                        }
                    }
                    Binding e = bindingMap.get_Item(d);

                    if (true != myCategories.IsEmpty)
                    {
                        TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories);
                        using (Transaction tr = new Transaction(doc))
                        {
                            tr.Start("addingParameters");
                            bool bindOk = bindingMap.Insert(d, typeBinding, BuiltInParameterGroup.PG_DATA);
                            tr.Commit();
                        }
                    }
                    
                    if (true != boundCategories.IsEmpty)
                    {
                        //TypeBinding typeBinding = app.Application.Create.NewTypeBinding(boundCategories);                        
                        using (Transaction tr = new Transaction(doc))
                        {
                            tr.Start("addingParameterz");
                            bool bindOk = bindingMap.ReInsert(d, e, BuiltInParameterGroup.PG_DATA);
                            tr.Commit();
                        }
                    }

 

0 Likes