Wednesday, February 11, 2015

Revit Macro for Mass Importing Shared Parameters

The following was a quick attempt to see if I could mass import a bunch of shared parameters into my projects. Its part of a tool I am building that needed some unique parameters for schedules and tags.

public void ImportSharedParameters()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
  
    // location of shared parameters file
    string spfile = @"\\corp.ktgy.com\bim\DT\API\Unit Coin\Shared Parameter-KTGY.txt";
  
    using (Transaction t = new Transaction(doc, "Import Shared Parameters"))
    {
        t.Start();
        

        // create a new line for each parameter like this:
       // doc, SharedParametersFile, ParameterName, Category, ParameterGroup, VaryByGroupInstance         CreateProjParameter(doc, spfile, "Building", BuiltInCategory.OST_Rooms, BuiltInParameterGroup.PG_IDENTITY_DATA, true);
        CreateProjParameter(doc, spfile, "Unit Area", BuiltInCategory.OST_Rooms, BuiltInParameterGroup.PG_GENERAL, true);
                      
        t.Commit();
    }
}


public void CreateProjParameter( Document doc, string sharedParameterFile, string parameterName,
                                BuiltInCategory bicategory, BuiltInParameterGroup bipgroup, Boolean varyByGroup)
{
    // load the shared parameters file
    Application.SharedParametersFilename = sharedParameterFile;
  
    // select the category
    Category cat = doc.Settings.Categories.get_Item(bicategory);
    // create a new category set
    CategorySet catset = Application.Create.NewCategorySet();
    // add category to category set
    catset.Insert(cat);
  
    // loaded shared parameter file
    DefinitionFile deffile = Application.OpenSharedParameterFile();
  
    try
    {
        // find the parameter in the shared parameters file
        // by searching each parameter group
        var v = (from DefinitionGroup dg in deffile.Groups
                 from ExternalDefinition d in dg.Definitions
                 where d.Name == parameterName
                 select d);
      
        ExternalDefinition def = v.First();
      
        // bind the parameter to the category and parameter group
        Binding binding = Application.Create.NewInstanceBinding(catset);
        BindingMap map = doc.ParameterBindings;
        map.Insert(def, binding, bipgroup);
      
        // change parameter to be "Values can vary by group instance"
        if(varyByGroup)
        {
            BindingMap grpmap = doc.ParameterBindings;
            DefinitionBindingMapIterator it = grpmap.ForwardIterator();
          
            while (it.MoveNext())
            {
                Definition d = it.Key as Definition;
                if (d.Name == parameterName)
                {
                    InternalDefinition idef = it.Key as InternalDefinition;
                    idef.SetAllowVaryBetweenGroups(doc, true);
                }
            }
        }
                      
    }
    catch (Exception ex)
    {
               TaskDialog.Show("Error", ex.Message);
    }
}

4 comments:

  1. Hi Troy, Thanks for the info.

    But we are facing an issue(Following exception) while setting the SetAllowVaryBetweenGroups parameter. Can you please let me know what could be the issue?

    Autodesk.Revit.Exceptions.ArgumentException: This parameter does not support the specified value of allowVaryBetweenGroups.
    Parameter name: allowVaryBetweenGroups
    at Autodesk.Revit.DB.InternalDefinition.SetAllowVaryBetweenGroups(Document document, Boolean allowVaryBetweenGroups)
    at CTC.SpecExchange.Plugin.RevitHelper.GetProject(UIApplication UIApp)
    at CTC.SpecExchange.Plugin.RevitHelper.GetProjectDataFromRevit(UIApplication UIApp, String& errorMessage)

    We have a requirement where we need to create few shared parameters and set this property. but when we try to set this parameter for the second time we are facing this issue.

    ReplyDelete
    Replies
    1. Most likely you are using a parameter type that doesn't support Vary by Group. What is the parameter type of the parameter that is giving you the error?

      Delete
    2. it is either ParameterType.Integer/ParameterType.Text or I'm using it for Doors.

      Delete
  2. It is ParameterType.Integer / ParameterType.Text

    ReplyDelete