Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Safe method to add sahred parameter to project elements for multipe categories

3 REPLIES 3
Reply
Message 1 of 4
edgars_miskins
393 Views, 3 Replies

Safe method to add sahred parameter to project elements for multipe categories

edgars_miskins
Advocate
Advocate

Hello,
I am trying to add a shared parameter to certain categories by using defined shared parameters in shared parameter file.
In GUI this would be done with Manage->Project Parameters -> Add -> Shared Parameter (select categories, and grouping info, ect) ...

My issue is, that I want to add an existing parameter to new categories without disturbing the parameter and it's values for elements of existing categories.

Example:
1) I add a parameter "PARAM1" to category "Lighting Fixtures"
2) I edit the "PARAM1" values for each Lighting Fixture element
3) I expand the category list of the same parameter "PARAM1" to "Lighting Fixtures" and "Cable Trays"
4) Values set in step 2 should not have changed

For my solution see below.
The confusing part is, that sometimes the parameter value (for previously set, existing categories) get reset, and sometimes they are retained (desired outcome).

"sometimes" - as in - it works in a small, isolated test project, but doesn't work in a large production file (parameter values are reset).

My solution so far:
I can create and add a new binding definition to Document.ParameterBindings.
The issue arises if I need to expand the selected category list. In that case, I do the following:
1) get the associated categories from existing binding definition,
2) recreate category set and add the new categories to it,
3) remove the old binding definition
4) add back the binding definition with updated categories

What else should I be taking into account? Am I missing something? Why does it work only sometimes?

Here is my method to add a specific shared parameter to selected categories:

public static void CreateProjectParam(Application app, Guid paramGUID, CategorySet newCats, BuiltInParameterGroup group, bool inst)
{
    // get shared parameter file
    DefinitionFile defFile = app.OpenSharedParameterFile();
    if (defFile == null) return;

    // get parameter defintion from the file
    var v = (from DefinitionGroup dg in defFile.Groups
                from ExternalDefinition d in dg.Definitions
                where d.GUID == paramGUID
                select d);
    if (v == null || v.Count() < 1) return;

    ExternalDefinition def = v.First();

    // get project parameter bindings
    BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;

    // Check if binding already exists and recreate the category list with new additions
    if (map.Contains(def))
    {
        ElementBinding existingDef = (ElementBinding)map.get_Item(def);
        var existingCats = existingDef.Categories;

        foreach (Category cat in existingCats)
        {
            if (!newCats.Contains(cat))
            {
                newCats.Insert(cat);
            }
        }

    }

    // create the new binding with updated category list
    Autodesk.Revit.DB.Binding binding = app.Create.NewTypeBinding(newCats);
    if (inst) binding = app.Create.NewInstanceBinding(newCats);

    // remove old binding definition and add it back again with updated category list
    map.Remove(def);
    map.Insert(def, binding, group);

    // other settings
    SetInstanceParamVaryBetweenGroupsBehaviour((new UIApplication(app)).ActiveUIDocument.Document, paramGUID, true);

}




Safe method to add sahred parameter to project elements for multipe categories

Hello,
I am trying to add a shared parameter to certain categories by using defined shared parameters in shared parameter file.
In GUI this would be done with Manage->Project Parameters -> Add -> Shared Parameter (select categories, and grouping info, ect) ...

My issue is, that I want to add an existing parameter to new categories without disturbing the parameter and it's values for elements of existing categories.

Example:
1) I add a parameter "PARAM1" to category "Lighting Fixtures"
2) I edit the "PARAM1" values for each Lighting Fixture element
3) I expand the category list of the same parameter "PARAM1" to "Lighting Fixtures" and "Cable Trays"
4) Values set in step 2 should not have changed

For my solution see below.
The confusing part is, that sometimes the parameter value (for previously set, existing categories) get reset, and sometimes they are retained (desired outcome).

"sometimes" - as in - it works in a small, isolated test project, but doesn't work in a large production file (parameter values are reset).

My solution so far:
I can create and add a new binding definition to Document.ParameterBindings.
The issue arises if I need to expand the selected category list. In that case, I do the following:
1) get the associated categories from existing binding definition,
2) recreate category set and add the new categories to it,
3) remove the old binding definition
4) add back the binding definition with updated categories

What else should I be taking into account? Am I missing something? Why does it work only sometimes?

Here is my method to add a specific shared parameter to selected categories:

public static void CreateProjectParam(Application app, Guid paramGUID, CategorySet newCats, BuiltInParameterGroup group, bool inst)
{
    // get shared parameter file
    DefinitionFile defFile = app.OpenSharedParameterFile();
    if (defFile == null) return;

    // get parameter defintion from the file
    var v = (from DefinitionGroup dg in defFile.Groups
                from ExternalDefinition d in dg.Definitions
                where d.GUID == paramGUID
                select d);
    if (v == null || v.Count() < 1) return;

    ExternalDefinition def = v.First();

    // get project parameter bindings
    BindingMap map = (new UIApplication(app)).ActiveUIDocument.Document.ParameterBindings;

    // Check if binding already exists and recreate the category list with new additions
    if (map.Contains(def))
    {
        ElementBinding existingDef = (ElementBinding)map.get_Item(def);
        var existingCats = existingDef.Categories;

        foreach (Category cat in existingCats)
        {
            if (!newCats.Contains(cat))
            {
                newCats.Insert(cat);
            }
        }

    }

    // create the new binding with updated category list
    Autodesk.Revit.DB.Binding binding = app.Create.NewTypeBinding(newCats);
    if (inst) binding = app.Create.NewInstanceBinding(newCats);

    // remove old binding definition and add it back again with updated category list
    map.Remove(def);
    map.Insert(def, binding, group);

    // other settings
    SetInstanceParamVaryBetweenGroupsBehaviour((new UIApplication(app)).ActiveUIDocument.Document, paramGUID, true);

}




Labels (3)
3 REPLIES 3
Message 2 of 4

RPTHOMAS108
Mentor
Mentor

I believe you are supposed to use ReInsert if the binding already exists.

 

However some aspects such as InternalDefinition.SetAllowVaryBetweenGroups don't seem to update regardless as to how you try (last I checked).

0 Likes

I believe you are supposed to use ReInsert if the binding already exists.

 

However some aspects such as InternalDefinition.SetAllowVaryBetweenGroups don't seem to update regardless as to how you try (last I checked).

Message 3 of 4

edgars_miskins
Advocate
Advocate

Thank you for your answer!
I've changed my solution as per your recommendation to this:

if (map.Contains(def))
{
    map.ReInsert(def, binding, group);
}
else
{
    map.Insert(def, binding, group);
}

 
Sadly, it didn't work and some parameter values are still getting erased.
Gonna have to dig around some more.


0 Likes

Thank you for your answer!
I've changed my solution as per your recommendation to this:

if (map.Contains(def))
{
    map.ReInsert(def, binding, group);
}
else
{
    map.Insert(def, binding, group);
}

 
Sadly, it didn't work and some parameter values are still getting erased.
Gonna have to dig around some more.


Message 4 of 4

prasadNELXB
Explorer
Explorer

 

I am having the exact same problem have you found out any solution?

 

Thank you in advance  

0 Likes

 

I am having the exact same problem have you found out any solution?

 

Thank you in advance  

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report