Search code examples
apiautodeskrevit

How to set the value of a shared parameter with type binding in Autodesk Revit Architecture 2010?


I have a shared parameter UValue bound to the Wall type with TypeBinding in Autodesk Revit Architecture 2010.

I can easily access the parameter with:

Definition d = DefinitionFile.Groups.get_Item("groupname").Definitions.get_Item("UValue");
Parameter parameter = self.get_Parameter("UValue");

The value of this parameter can be looked at with

var u = parameter.AsDouble();

But when I do

parameter.Set(0.8);

I get an Error:

InvalidOperationException: Operation is not valid due to the current state of the object.

On inspection, the parameters ReadOnly property is set to false.


Solution

  • Ok, I have found the problem:

    When using TypeBinding, the parameter is not in the Wall object itself, but in its WallType property. If you are doing this in a polymorphic way (not just walls, but also floors, roofs etc.), then you can use the Element.ObjectType property.

    The code in the OP should thus have been:

    Definition d = DefinitionFile.Groups.get_Item("groupname").Definitions.get_Item("UValue");
    Parameter parameter = self.ObjectType.get_Parameter("UValue");
    

    This is being called from an extension method, a rather neat technique for adding parameters to Revit objects.

    Setting the parameter can thus be done like this:

    public static void SetUValue(this Wall self, double uvalue)
    { 
        Parameter parameter = self.ObjectType.get_Parameter("UValue");
        if (parameter != null)
        {
            parameter.Set(uvalue);
        }
        else
        {
            throw new InvalidOperationException(
                "Wall does not contain the parameter 'UValue'");
        }
    }