Create project parameters

from Autodesk.Revit.DB import*
from pyrevit import DB, forms, script, revit

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 




# get ui doc and open views
app = __revit__.Application
doc = revit.doc
uidoc = revit.uidoc



def add_parameter_to_project(parameter_name):
    # Check if the parameter already exists
    existing_parameter = doc.ParameterBindings.get_Item(parameter_name)
    if existing_parameter is not None:
        print("The parameter '{parameter_name}' already exists.")
        return

    # Create a new shared parameter
    shared_parameter = ParameterFilterElement.CreateSharedParameterElement(doc, parameter_name, BuiltInParameter.INVALID, True)

    # Bind the parameter to the project
    category = Category.GetCategory(doc, BuiltInCategory.OST_GenericModel)  # You can change the category as needed
    binding = DefinitionBindingMap.Create(doc)
    binding.Insert(category.Id, shared_parameter.GetDefinition(), BuiltInParameterGroup.PG_DATA)

    # Apply the binding to the project
    doc.ParameterBindings.Insert(shared_parameter.GetDefinition(), binding, BuiltInParameterGroup.PG_DATA)

    print("The parameter '{parameter_name}' has been added to the project.")

# Specify the parameter name you want to add
parameter_name_to_add = "height"

# Call the function to add the parameter
add_parameter_to_project(parameter_name_to_add)

" I just created this script for project parameters, not solved . what is the problem this script and what is the solution?

@shuvro.arc ,

at the first view you have no transaction. So you need it, because it is a change in your current document.

f.e.

# 🔓 create project parameter
t = Transaction(doc, "create project parameter")
t.Start()
# try to create the parameter
t.Commit()
# 🔒 done

or leveraging the pyrevit revit module for transaction

from pyrevit import revit
# ---
with revit.Transaction(doc, "create param"):
    # do stuff

Thanks, but not solved.

from Autodesk.Revit.DB import*
from pyrevit import DB, forms, script, revit

import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 




# get ui doc and open views
app = __revit__.Application
doc = revit.doc
uidoc = revit.uidoc

# Specify the parameter name you want to add
parameter_name_to_add = "height"


with Transaction(doc, "P") as t:
    t.Start()
    # Check if the parameter already exists
    existing_parameter = doc.ParameterBindings.get_Item(parameter_name_to_add)
    if existing_parameter is not None:
        print("The parameter '{parameter_name}' already exists.")



    # Create a new shared parameter
    shared_parameter = ParameterFilterElement.CreateSharedParameterElement(doc, parameter_name_to_add, BuiltInParameter.INVALID, True)

    # Bind the parameter to the project
    category = Category.GetCategory(doc, BuiltInCategory.OST_GenericModel)  # You can change the category as needed
    binding = DefinitionBindingMap.Create(doc)
    binding.Insert(category.Id, shared_parameter.GetDefinition(), BuiltInParameterGroup.PG_DATA)

    # Apply the binding to the project
    doc.ParameterBindings.Insert(shared_parameter.GetDefinition(), binding, BuiltInParameterGroup.PG_DATA)

    print("The parameter '{parameter_name}' has been added to the project.")

    t.Commit()

" already aplied, but not solved. error message is (
image

ok. thanks. i will try it.

  • ParameterFilterElement does not have a ‘CreateSharedParameterElement’ method. Is this a function defined elsewhere in the code? See class here: ParameterFilterElement Methods

  • If you use the ‘with’ transaction syntax you don’t need to start/commit it. It will be freed from memory once it is finished with (commited) by pyRevit’s implementation of this class

I’m wondering if you’re trying to do this instead?

The code will likely only work if you actually have an existing shared parameter to get the external definition of to apply to that method if so.

Can’t help but feel the code might be GPT generated as some of it doesn’t seem to make sense or use valid API methods.

1 Like

Hi @shuvro.arc

You’re mixing the two suggestion, you should pick one or the other.

Since you import everything from Autodesk.Revit.DB, and you don’t fully qualify the pyrevit transaction with with revit.Transaction..., the transaction object used is the revit api one, which doesnt support the usage as context manager (the with keyword).

Use the with revit.Transaction as instructed by @Jean-Marc, and drop the t.Start() and t.Commit() since they are taken care by the pyrevit transaction itself (thats why I always suggest to use that, but nobody listen to me :sweat_smile:)

2 Likes

The only real benefit to using start/commit is when dealing with transaction groups in my xp or if you write your code for use elsewhere than pyRevit also (e.g. Dynamo). I’m pretty sure the ‘with’ transactions might not be able to assimilate with transaction groups as they might be disposed by the custom class. Haven’t checked, but otherwise the ‘with’ approach is far superior syntactically for sure.

Thanks for suggestions.