Basics: Revit API Parameters

Revit is all about working with parameters. That's where we store our data and control everything else.

So, it's crucial to understand how to Get, Read and Set new values in your parameters.

So, Let's have an overlook of parameters in Revit API

Intro

Firstly, keep in mind there are different parameters in Revit API:

🔹Built-In Parameter
🔹Project Parameter
🔹Shared Parameter
🔸Global Parameter
🔸Family Parameter

​I won't go into the last ones, let's focus on the main project parameters (Built-In, Shared, Project)

Script Preparation

I'm going to share the most useful code snippets related to Revit API parameters for beginners.
But before we begin, we need to get an element to work with.

You can get an element any way you want, but here is a simple example to prompt PickObject to select an element.

#⬇️ Imports
from Autodesk.Revit.UI.Seleciton import ObjectType

#📦 Variables
uidoc     = __revit__.ActiveUIDocument

#👉 PickObject
ref_picked_object = uidoc.Selection.PickObject(ObjectType.Element)
elem              = doc.GetElement(ref_picked_object)

Get All Instance/Type Parameters

Firstly, you need to understand that Instance and Type are 2 different objects in Revit API. They are related to each other, but they have their own classes.

And if you want to get Instance or Type parameters, make sure you get it from appropriate objects.
For example, here is how to get a list of all available Instance/Type Parameters in Revit API

#1️⃣ Get Instance Parameters
instance_params = elem.Parameters

#2️⃣ Get Type + Type Parameters
el_type_id    = elem.GetTypeId()
el_type       = doc.GetElement(el_type_id)
type_params   = el_type.Parameters


Parameter Properties

Parameters have many properties that can be really useful during development or even during execution.
You can explore Parameter Class in Revit API in more detail or have a look inside of Revit Lookup.

Here is an example of the most useful properties. Notice that some of them are inside of Parameter.Definition

# Read All Instance Parameters of an Element
for p in elem.Parameters:
    print("Name: {}".format(p.Definition.Name))
    print("ParameterGroup: {}".format(p.Definition.ParameterGroup))
    print("BuiltInParameter: {}".format(p.Definition.BuiltInParameter))
    print("IsReadOnly: {}".format(p.IsReadOnly))
    print("HasValue: {}".format(p.HasValue))
    print("IsShared: {}".format(p.IsShared))
    print("StorageType: {}".format(p.StorageType))
    print("AsValueString(): {}".format(p.AsValueString()))
    print('-'*100)

💡You can execute this code snippet as an example, as it will go through all parameters of selected elements and write its information.


Get parameters

When dealing with parameters, firstly you need to get them.

There are multiple ways of getting you parameter:

  • LookupParameter('p_name') Method

  • get_Parameter(BuiltInParameter) Method

  • GetParameter(ForgeTypeId) Method

  • GetParameters('p_name') Method

  • Iterate through Parameters IList

And maybe even a few more...

Personally, I prefer to use get_Parameter and LookupParameter methods, so that's what I will teach you.

🅰️Get Built-In Parameter

To get a Built-In Parameter, we need to use BuiltInParameter Enumeration. If you don't know, enumeration is a class that holds all possible values. Think of it as a pulldown from which you can select something.

To find the right bip (BuiltInParameter), you can either try to guess the name with Revit API autocomplete, or go straight to Revit Lookup and have a look at the Parameter.Definition.BuiltInParameter property of your parameter.

Here is an example how to get Comments and Mark parameter.

# Get Elem/ElemType
elem = # Get an Element Here...
elem_type = doc.GetElement(elem.GetTypeId())

# You can check BuilltInParameter with RevitLookup under: 
# Parameters -> Definition -> BuiltInParameter (p.Definition.BuiltInParameter)

# Get Instance Parameters
p_comments = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
p_mark     = elem.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)

# Get Type Parameters
p_type_comments = elem_type.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS)
p_type_mark     = elem_type.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_MARK)

💡We need to use bip, because Built-In Parameter can have different names in different Revit languages.

Also, keep in mind that you don't always have to use Parameters. Often time you can get the same information from Properties and methods of an element. For example, here is how you could get ElementId of the type.

# P.S. You don't always need to use Parameters. SOmetimes you can use Methods and Properties too

#🅰️ Get Type with Parameter
p_el_type  = elem.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM)
el_type_id = p_el_type.AsElementId() # Get ElementId Value of Parameter

#🅱️ Get With Method
el_type_id = elem.GetTypeId()
el_type    = doc.GetElement(el_type_id)


🅱️Get Project/Shared Parameter

In case of Project or Shared parameters, they will always have the same name. But you need to make sure that you actually found it, in case the parameter you are looking for wasn't loaded.

#🟠 Get Project/Shared Parameters
custom_p1 = picked_object.LookupParameter("P_NAME_1")
custom_p2 = picked_object.LookupParameter("P_NAME_1")

# It's good to check if you got the parameter when you use LookupParameter 😉
if custom_p1:
    print(custom_p1.AsValueString())

Also, keep in mind that Revit can have an issue with duplicate shared or project parameter. I didn't have much issues with that, but it depends on what kind of tools you create. If you encounter issues use Unique parameter IDs instead.


Read Parameters

If you want to read Parameter values, you need to know Parameter.StorageType, and it can be one of these 4:

  • String - Text value

  • Double - Numeric values with a point (float in python)

  • Integer - Integer numbers

  • ElementId - Reference to other elements like Level, Type, Material…

The reason why you need to know this is because there are 5 different methods to get the right value. You could check StorageType to select the right method, but usually you will know what to use…

If you work with text parameters like Comments, Mark, you would naturally use AsString()
If you work with some numberic parameters like Area, you would use AsDouble()
If parameter references other objects like Level, Type, Material… You would use AsElementId

Here is an example of all:

param = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)


#🟠 Read Parameter Properties
if param.StorageType == StorageType.Double:      
    value = param.AsDouble()
elif param.StorageType == StorageType.ElementId: 
    value = param.AsElementId()
elif param.StorageType == StorageType.Integer:   
    value = param.AsInteger()
elif param.StorageType == StorageType.String:    
    value = param.AsString()

There is also AsValueString which can be useful sometimes and it can display readable name of the value. In case of Levels for example it will hold level.Name value and so on…

It's best you look inside of Revit Lookup to see actual values parameter holds to decide which one you need to use. That's the most practical approach to decide what methods to use.


Set Parameter Values

Lastly, to make any changes in Parameters, you need to use .Set method and provide value with the correct type. So if parameter has a StorageType.String, then make sure you provide string value.

💡If you provide wrong type - you won't get any errors, but you would also won't change anything

This can be really confusing and some people spend hours debugging this because there are no error messages. And by some people I mean myself when I started, I just forgot to verify if I provide integers or strings to my parameter…

Here is how to Change Parameter Values

p_comments.Set('EF-Comment')   #Make Sure you provide correct Type!
p_numberic.Set(69)             #Make Sure you provide correct Type!


with Transaction(doc, __title__) as t:
    t.Start()

    p_comments.Set('EF-Comment')  
    p_numberic.Set(69)           

    t.Commit()

Want More?

I hope it was really helpful to understand Revit API parameters.

If you want to learn more about parameters and get more examples, check under Parameter group in the sidebar menu.

# ╦ ╦╔═╗╔═╗╔═╗╦ ╦  ╔═╗╔═╗╔╦╗╦╔╗╔╔═╗  ||
# ╠═╣╠═╣╠═╝╠═╝╚╦╝  ║  ║ ║ ║║║║║║║ ╦  ||
# ╩ ╩╩ ╩╩  ╩   ╩   ╚═╝╚═╝═╩╝╩╝╚╝╚═╝  .. 

⌨️ Happy Coding!
Erik Frits