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.
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
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
💡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.
💡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.
🅱️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.
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:
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
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