Binding

Binding

In the add-in code, complete the following steps to bind a specific parameter:

  1. Use an InstanceBinding or a TypeBinding object to create a new Binding object that includes the categories to which the parameter is bound.
  2. Add the binding and definition to the document using the Document.ParameterBindings object.

The following list provides more information about the classes and methods in the previous diagram.

Type Binding

The Autodesk.Revit.Parameters.TypeBinding objects are used to bind a property to a Revit type, such as a wall type. It differs from Instance bindings in that the property is shared by all instances identified in type binding. Changing the parameter for one type affects all instances of the same type.

Figure 132: Parameter Properties dialog box Type Binding

The following code segment demonstrates how to add parameter definitions using a shared parameter file. The following code performs the same actions as using the dialog box in the previous picture. Parameter definitions are created in the following order:

  1. A shared parameter file is created.
  2. A definition group and a parameter definition are created for the Walls type.
  3. The definition is bound to the wall type parameter in the current document based on the wall category.

Code Region 22-7: Adding type parameter definitions using a shared parameter file

public bool SetNewParameterToTypeWall(UIApplication app, DefinitionFile myDefinitionFile)
{
        // Create a new group in the shared parameters file
        DefinitionGroups myGroups = myDefinitionFile.Groups;
        DefinitionGroup myGroup = myGroups.Create("MyParameters");

        // Create a type definition
        Definition myDefinition_CompanyName = 
           myGroup.Definitions.Create("CompanyName", ParameterType.Text);

        // Create a category set and insert category of wall to it
        CategorySet myCategories = app.Application.Create.NewCategorySet();
                // Use BuiltInCategory to get category of wall
        Category myCategory = app.ActiveUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
        myCategories.Insert(myCategory);

        //Create an object of TypeBinding according to the Categories
        TypeBinding typeBinding = app.Application.Create.NewTypeBinding(myCategories);

        // Get the BingdingMap of current document.
        BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings;

        // Bind the definitions to the document
        bool typeBindOK = bindingMap.Insert(myDefinition_CompanyName, typeBinding, 
     BuiltInParameterGroup.PG_TEXT);
        return typeBindOK;
}

Instance Binding

The Autodesk.Revit.Parameters.InstanceBinding object indicates binding between a parameter definition and a parameter in certain category instances. The following diagram illustrates Instance Binding in the Walls category.

Once bound, the parameter appears in all property dialog boxes for the instance. Changing the parameter in any one instance does not change the value in any other instance.

Figure 133: Parameter Properties dialog box Instance Binding

The following code sample demonstrates how to add parameter definitions using a shared parameter file. Parameter definitions are added in the following order:

  1. A shared parameter file is created
  2. A definition group and a definition for all Walls instances is created
  3. Definitions are bound to each wall instance parameter in the current document based on the wall category.

    Code Region 22-8: Adding instance parameter definitions using a shared parameter file

    public bool SetNewParameterToInsanceWall(UIApplication app, DefinitionFile myDefinitionFile)
    {
            // create a new group in the shared parameters file
            DefinitionGroups myGroups = myDefinitionFile.Groups;
            DefinitionGroup myGroup = myGroups.Create("MyParameters1");
    
            // create an instance definition in definition group MyParameters
            Definition myDefinition_ProductDate = 
                    myGroup.Definitions.Create("Instance_ProductDate", ParameterType.Text);
    
            // create a category set and insert category of wall to it
            CategorySet myCategories = app.Application.Create.NewCategorySet();
            // use BuiltInCategory to get category of wall
            Category myCategory = app.ActiveUIDocument.Document.Settings.Categories.get_Item(
                                            BuiltInCategory.OST_Walls);
            myCategories.Insert(myCategory);
    
            //Create an instance of InstanceBinding
            InstanceBinding instanceBinding = 
                    app.Application.Create.NewInstanceBinding(myCategories);
    
            // Get the BingdingMap of current document.
            BindingMap bindingMap = app.ActiveUIDocument.Document.ParameterBindings;
    
            // Bind the definitions to the document
            bool instanceBindOK = bindingMap.Insert(myDefinition_ProductDate, 
                                            instanceBinding, BuiltInParameterGroup.PG_TEXT);
            return instanceBindOK;
    }