Announcement

Collapse
No announcement yet.

Adding a family parameter via the api

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Adding a family parameter via the api

    Is it possible to add an Instance Parameter to a family in a project via the api if that Instance Parameter does not exist?
    CG Visions
    Revit Coding Blog

    #2
    Should be possible. The basic steps would be:

    - - - - - - - - -

    Iterate over the elements of type Family until you find the one with the desired Name

    familyDoc = projectDoc.EditFamily(theFamily);

    begin a transaction on familyDoc

    BuiltInParameterGroup addToGroup == BuiltInParameterGroup.INVALID; // INVALID= "Other" group, or pick your fave from the enum
    ParameterType parameterType = ParameterType.Text; // Or pick your fave from the enum

    familyDoc.FamilyManager.AddParameter("My Parameter Name", addToGroup, parameterType, true); // true = instance parameter

    close the transaction on the familyDoc

    projectDoc.LoadFamily(familyDoc);

    familyDoc.Close(false)

    - - - - - - - -

    To avoid the annoying Overwrite dialog on reload, you can create a class that implements IFamilyLoadOptions and pass it as an additional argument to the projectDoc.LoadFamily call.

    Comment


      #3
      Great- thank you very much!
      CG Visions
      Revit Coding Blog

      Comment


        #4
        Hi DavidVS,

        I was looking at your post and it's amazingly helpful, although I'm getting an error when I try to load the family back into the document. The line that is causing the error is:

        P_doc.LoadFamily(familyDoc, iFamOptions)

        P_doc is a valid document I'm working on
        familyDoc is the family that I am editing
        iFamOptions is a class where I'm implementing the IFamilyLoadOptions

        The exception is: InvalidOperationException that says "This method can only be used with family documents"

        I'm using Revit 2014. Would you have any insight as to how I may be able to solve this problem. Any help is greatly appreciated.
        Cheers,
        [C]

        Originally posted by DavidVS View Post
        Should be possible. The basic steps would be:

        - - - - - - - - -

        Iterate over the elements of type Family until you find the one with the desired Name

        familyDoc = projectDoc.EditFamily(theFamily);

        begin a transaction on familyDoc

        BuiltInParameterGroup addToGroup == BuiltInParameterGroup.INVALID; // INVALID= "Other" group, or pick your fave from the enum
        ParameterType parameterType = ParameterType.Text; // Or pick your fave from the enum

        familyDoc.FamilyManager.AddParameter("My Parameter Name", addToGroup, parameterType, true); // true = instance parameter

        close the transaction on the familyDoc

        projectDoc.LoadFamily(familyDoc);

        familyDoc.Close(false)

        - - - - - - - -

        To avoid the annoying Overwrite dialog on reload, you can create a class that implements IFamilyLoadOptions and pass it as an additional argument to the projectDoc.LoadFamily call.

        Comment


          #5
          should reverse it? familyDoc.LoadFamily(P_doc, iFamOptions) instead of P_doc.LoadFamily(familyDoc, iFamOptions)

          Comment


            #6
            I have some code that makes adding shared parameters to a project easy. It could be editted to add Family parameters:

            Code:
            private static bool createSharedParameterFile(Document doc, string paramFileDir, string paramFileName)
                    {
                        string paramFile  = paramFileDir + paramFileName;
            
                        if (File.Exists(paramFile)) {
                            doc.Application.SharedParametersFilename = paramFile;
                            return true;
                        }
            
                        if (!Directory.Exists(paramFileDir))
                            Directory.CreateDirectory(paramFileDir);
            
                        FileStream fs = File.Create(paramFile);
                        fs.Close();
                        doc.Application.SharedParametersFilename = paramFile;
                        return true;
                    }
            
                    public static bool containsDefinition(DefinitionGroup grp, string paramName)
                    {
                        paramName = paramName.ToLower();
            
                        foreach (Definition def in grp.Definitions)
                            if (string.Equals(def.Name.ToLower(), paramName))
                                return true;
            
                        return false;
                    }
            
                    public static bool addSharedParameter(Document doc, BuiltInCategory paramCategory, string paramName, ParameterType paramType, string definitionGroup="APIGroup", bool isParamVisible = true)
                    {
                        try
                        {
                            if (!createSharedParameterFile(doc, "PARAM_FILE_DIR", "PARAM_FILE_NAME"))
                                return false;
            
                            DefinitionFile parafile = doc.Application.OpenSharedParameterFile();
                            CategorySet categories  = doc.Application.Create.NewCategorySet();
                            categories.Insert(doc.Settings.Categories.get_Item(paramCategory)); // pipeSymbol.category
                            
                            InstanceBinding binding  = doc.Application.Create.NewInstanceBinding( categories );
                            DefinitionGroup apiGroup = parafile.Groups.get_Item(definitionGroup);
                            
                            if (apiGroup == null)
                                apiGroup = parafile.Groups.Create(definitionGroup);
                            // Check that param is not already in file
                            if (containsDefinition(apiGroup, paramName))
                                return true;
            
                            BindingMap bindingMap = doc.ParameterBindings;
                            Definition visibleParamDef = apiGroup.Definitions.Create(paramName, paramType, isParamVisible);
                            bindingMap.Insert(visibleParamDef, binding);
                            return true;
                        }
                        catch (Exception e)
                        {
                            /*var st = new StackTrace(e, true);
                            var frame = st.GetFrame(0);
                            var line = frame.GetFileLineNumber();
                            TaskDialog.Show("","Line: " + line.ToString() + ", Error: " + e.Message);*/
                            return false;
                        }
                    }
            
                    public static bool setupSharedParameters(Document doc)
                    {
                        try
                        {
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Name", ParameterType.Text);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Type", ParameterType.Text);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Style", ParameterType.Text);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Breakline", ParameterType.Text);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Colour", ParameterType.Text);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Chainage", ParameterType.Number);
                            addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Weight", ParameterType.Integer);*/
            
                            return true;
                        }
                        catch (Exception e)
                        {
                            TaskDialog.Show("","Error: " + e.Message);
                            return false;
                        }
                    }

            Comment


              #7
              Thanks for the notes. Much appreciated!
              Cheers,

              Originally posted by samz1 View Post
              I have some code that makes adding shared parameters to a project easy. It could be editted to add Family parameters:

              Code:
              private static bool createSharedParameterFile(Document doc, string paramFileDir, string paramFileName)
                      {
                          string paramFile  = paramFileDir + paramFileName;
              
                          if (File.Exists(paramFile)) {
                              doc.Application.SharedParametersFilename = paramFile;
                              return true;
                          }
              
                          if (!Directory.Exists(paramFileDir))
                              Directory.CreateDirectory(paramFileDir);
              
                          FileStream fs = File.Create(paramFile);
                          fs.Close();
                          doc.Application.SharedParametersFilename = paramFile;
                          return true;
                      }
              
                      public static bool containsDefinition(DefinitionGroup grp, string paramName)
                      {
                          paramName = paramName.ToLower();
              
                          foreach (Definition def in grp.Definitions)
                              if (string.Equals(def.Name.ToLower(), paramName))
                                  return true;
              
                          return false;
                      }
              
                      public static bool addSharedParameter(Document doc, BuiltInCategory paramCategory, string paramName, ParameterType paramType, string definitionGroup="APIGroup", bool isParamVisible = true)
                      {
                          try
                          {
                              if (!createSharedParameterFile(doc, "PARAM_FILE_DIR", "PARAM_FILE_NAME"))
                                  return false;
              
                              DefinitionFile parafile = doc.Application.OpenSharedParameterFile();
                              CategorySet categories  = doc.Application.Create.NewCategorySet();
                              categories.Insert(doc.Settings.Categories.get_Item(paramCategory)); // pipeSymbol.category
                              
                              InstanceBinding binding  = doc.Application.Create.NewInstanceBinding( categories );
                              DefinitionGroup apiGroup = parafile.Groups.get_Item(definitionGroup);
                              
                              if (apiGroup == null)
                                  apiGroup = parafile.Groups.Create(definitionGroup);
                              // Check that param is not already in file
                              if (containsDefinition(apiGroup, paramName))
                                  return true;
              
                              BindingMap bindingMap = doc.ParameterBindings;
                              Definition visibleParamDef = apiGroup.Definitions.Create(paramName, paramType, isParamVisible);
                              bindingMap.Insert(visibleParamDef, binding);
                              return true;
                          }
                          catch (Exception e)
                          {
                              /*var st = new StackTrace(e, true);
                              var frame = st.GetFrame(0);
                              var line = frame.GetFileLineNumber();
                              TaskDialog.Show("","Line: " + line.ToString() + ", Error: " + e.Message);*/
                              return false;
                          }
                      }
              
                      public static bool setupSharedParameters(Document doc)
                      {
                          try
                          {
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Name", ParameterType.Text);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Type", ParameterType.Text);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Style", ParameterType.Text);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Breakline", ParameterType.Text);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Colour", ParameterType.Text);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Chainage", ParameterType.Number);
                              addSharedParameter(doc, BuiltInCategory.OST_IOSModelGroups, "Weight", ParameterType.Integer);*/
              
                              return true;
                          }
                          catch (Exception e)
                          {
                              TaskDialog.Show("","Error: " + e.Message);
                              return false;
                          }
                      }

              Comment

              Related Topics

              Collapse

              Working...
              X