LearnRevitAPI
Nov 26, 2024
Parameters in Revit API
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.
Parameter Types
Firstly, keep in mind that there are 5 types of parameters in Revit API:
🔹Built-In Parameter
🔹Project Parameter
🔹Shared Parameter
🔸Global Parameter
🔸Family Parameter
Let's look into how to work with the first 3 types of parameters.
How to 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 Shared/Project Parameters
Shared and project parameters will always have the same names (as long as they are added to the project...)
You can get these parameters by using LookupParameter('p_name') method and providing parameter name as an argument. Very straight-forward.
When you use this method, make sure you also check if you found the parameter, because it's very likely that parameters from one project, might not be present in another project.
Here is a simple example:
💡NB! Do not use this method for Built-In parameters because they are language dependent.
Get Built-In Parameters
To get built-in parameters, it's best if you use get_Parameter(BuiltInParameter) method.
For that you need to use BuiltInParameter Enumeration, that contains a list of all possible values. It's really big list and it's not always obvious names...
So you have 2 options to find the right bip (BuiltInParameter):
Guess it with Autocomplete.
Sometimes it's similar to how parameter is named, so you might guess it.
Check with RevitLookup.
Go to Revit -> Select an element -> Snoop with Revit Lookup -> Open Parameters -> Find your parameter -> Look into Definition.BuiltInParameter.
That's where you will see the exact bip you need to use.
e.g. Comments have bip:
BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS
Here is a code sample to get Built-In Parameter:
💡It's possible to get Built-In Param by name, but you might break it in other Revit languages. So better stick to the method I showed you above.
Instance vs Type Parameters
Also, very important: How to get Instance vs Type parameters. That's very simple, but beginners often stumble over this.
Let me be clear.
element.Parameters -> List of Instance Parameters
elem_type.Parameters -> List of Type Paramters
Element and element type are related in Revit API, they are not the same object! So you have to get the right object to get the right parameter.
But it's not hard, here is an example of getting Instance and Type parameters:
How to Get Paramter Value?
Now you have a parameter, how do you read it?
To read parameters, you need to know its StorageType. Usually it's very clear. If you work with text parameters like comments, mark and other, it will have StorageType.String.
If it's a numeric parameter, it can be StorageType.Double or StorageType.Integer.
If it refers to other objects like Types, Materials, Levels... Then it will have StorageType.ElementId
You need to know the StorageType, because there are separate methods for getting values with the right type. If in doubt, you can also reference Parameter.StorageType property.
Once you know it, you can use one of the following methods:
param.AsDouble()
param.AsElementId()
param.AsInteger()
param.AsString()
Here is screenshot from RevitLookup:
Usually you would look into your elements with Revit Lookup and it will be very clear what you get with each of these methods.
In the example, you can see that StorageType is String and AsString will return you 'Special Wall' value.
So you would write:
💡 Same applies to both Built-In or Shared Parameters.
How to Set Parameters?
Lastly, how do you override parameter values?
We need to use .Set() method to change value, and you need to provide a value with the correct type.
💡If you provide wrong type, it won't show you errors! It will silently ignore what you tried to do, and you might spend hours debugging because you don't see error message but also nothing changes.
🔓 Another thing to keep in mind is that you need to use Transaction.
In Revit API we can't make any changes to our projects, unless we use Transaction.Start and Transaction.Commit. This is like a guardian of your Revit projects to avoid unwanted changes and make it easier for us beginners to explore API.
Here is an example of how to do that:
Bonus Example
💪 Give it a try yourself!
Open RevitPythonShell and write this,
it will get more clear what is going on.
✅ Try Getting a few BuiltInParameters. Read and Set values.
✅ Do the same with Project/Shared Parameters
This code snippet will teach you a lot about Parameters in Revit API. Give it a try.
Want to learn more?
Click here to check my video about Revit API Parameters on YouTube. This will help you learn more and see some examples in action.
P.S.
Have any questions about Parameters in Revit API?
Hit 'Reply' and let me know.
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 7400+ people!