Loop through ElementTypes and bind definitions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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