Search code examples
c#revit-api

Only shared parameters can be created with Revit API?


After some research, I've arrived to the conclusion that only shared parameters can be created with the Revit API.

It would be useful for me to create project parameters (I need to add them to built-in families), but I can't find a good resource explaining how to do it.

Could someone tell me if my conclusion is correct? It is not possible to create project parameters by using the Revit API? Only shared parameters can be created with the Revit API?

Using:

  • Revit 2022
  • C#

What I tried:

  • I've tried with these methods from the docs.
  • This old post mentions an approach, but I haven't been able to make the code work.
  • There is more information in this post, but didn't help me either.

Solution

  • Yes, I believe your conclusion is correct, that you have to create a Shared Parameter to do this.

    Here is a link to the Revit API forum where the topic has been discussed in depth and updated over several years of people wanting to do exactly what you are asking without success: https://forums.autodesk.com/t5/revit-api-forum/create-project-parameter-not-shared-parameter/m-p/5152374#M6777

    If you read the whole discussion on this thread from the API forum, you can see some back and forth, but ultimately the answer is that you have to use a Shared Parameter. Here's the quote from the forum to that regard:

    I took a quick glance at the code for creating a project parameter. It turns out that to create a project parameter, they just create a temporary new shared parameters file, create a shared parameter in that file, import the shared parameter, then delete the temporary shared parameter file.

    The good news is that using shared parameters is much faster since you can use the Element.get_Parameter() method and supply the guid for your shared parameter, whereas non-shared parameters have to use Element.LookUpParameter() which is considerably slower.

    Here is a sample command to add shared parameters from a file to the project:

        [Transaction(TransactionMode.Manual)]
        [Regeneration(RegenerationOption.Manual)]
    
        internal class AddSharedParametersToProject : IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                var UiApp = commandData.Application;
                var Doc = commandData.Application.ActiveUIDocument.Document;
    
                // specify the shared parameter file you want to load
                UiApp.Application.SharedParametersFilename = @"C:\MyLocation\MySharedParams.txt";
                var sharedParameterFile = UiApp.Application.OpenSharedParameterFile();
    
                // create the category set that you want to bind to
                var catSet = UiApp.Application.Create.NewCategorySet();
                var cat = Doc.Settings.Categories.get_Item(BuiltInCategory.OST_Walls);
                catSet.Insert(cat);
    
    
                using (var t = new Transaction(Doc))
                {
                    t.Start("Add Shared Parameters");
    
                    // loop through the groups/definitions and add the parameters
                    foreach (var definitionGroup in sharedParameterFile.Groups)
                    {
                        foreach (var sharedPramDef in definitionGroup.Definitions)
                        {
                            var newIB = UiApp.Application.Create.NewInstanceBinding(catSet);
                            Doc.ParameterBindings.Insert(sharedPramDef, newIB, BuiltInParameterGroup.PG_TEXT);
                        }
                    }
    
                    t.Commit();
                }
                return Result.Succeeded;
    
            }
        }
    

    Here is the Shared Parameter file I used:

    # This is a Revit shared parameter file.
    # Do not edit manually.
    *META   VERSION MINVERSION
    META    2   1
    *GROUP  ID  NAME
    GROUP   1   Component
    *PARAM  GUID    NAME    DATATYPE    DATACATEGORY    GROUP   VISIBLE DESCRIPTION USERMODIFIABLE  HIDEWHENNOVALUE
    PARAM   c395a003-5291-473b-bff4-495623e11f41    MyParam_1   YESNO       1   1       1   0
    PARAM   c2188a20-947f-4869-a2b9-5e3f179453d2    MyParam_2   MULTILINETEXT       1   1       0   0
    PARAM   81eb0c23-beac-4fe5-9553-30ebaa2ca343    MyParam_3   TEXT        1   1       0   0
    
    
    

    And here are the resulting parameters now associated with walls: Resulting Parameters