Get Parameters with Revit API

Let's learn how to Get Project/Shared/Built-In Parameters with Revit API and read their values.

Get Parameters with Revit API

Let's learn how to Get Project/Shared/Built-In Parameters with Revit API and read their values.

Summary

Get Parameters with Revit API

Revit is all about working with parameters. So understanding how to Get, Read and Set new values in your parameters is a crucial part when we work with Revit API.

👀 Let's explore how to get parameters with Revit API.

Explore Revit API Docs

Let's start by looking at LookupParameter methods in Revit API Docs.

It's very straight-forwards. We have to call this method on our elements and provide a Name of a parameter.

💡 This method is used for getting Shared and Project parameters.

It's also possible to get Built-In parameters, but as we saw in remarks: we should use get_Parameter method for getting Built-In Parameters.

💡 I would also recommend you to read Remarks of LookupParameter method, to understand the nuances.

Prepare Button

Let's go into code and learn how to Get Parameters and their values.

First of all copy code from the previous lesson, so we can easily select an element to get its parameters.

👇 Here is the snippet we will start with.

# IMPORTS
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType, Selection

# VARIABLES
uidoc     = __revit__.ActiveUIDocument
doc       = __revit__.ActiveUIDocument.Document
selection = uidoc.Selection # type: Selection

#🔷 Pick Object
ref_picked_object = selection.PickObject(ObjectType.Element)
picked_object     = doc.GetElement(ref_picked_object)
Get Built-In Parameters

Let's begin by getting Built-In Parameters.

As I mentioned, we should use elem.get_Parameter(BuiltInParameter) method.

❗Unfortunately, this method is not documented in Revit API Docs. So you just need to understand how to use it, and it's quite simple.

This method takes single argument - BuiltInParameter, which is an Enumeration.

💡 Enumeration is a class that contains all possible values to choose from, so if we look in Revit API Docs, you will see all available built-in parameters in Revit API.

This list is incredibly long, so don't even try to read all of it. Instead, we can have a look inside of a Parameter that we want to get with Revit Lookup plugin.

👉 Select an Element -> Revit Lookup -> Snoop Selection

This will open up a form with overview of properties and methods of your selected elements.

And one of these properties is going to be Parameters.



Then you can select one of the parameters in the list.

Let's look at Comments parameter.

To find the name of BuiltInParameter, we should look inside of Definition property.

You can see BuiltInParameter name of Comments selected in this screenshot.

They are always all upper case like:
ALL_MODEL_INSTANCE_COMMENTS.

You will get used to these names and their logic, but sometimes they don't make any sense!

Let's get different Built-In Parameters using this method.

# ✅ Get Built-In Parameters
comments = picked_object.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
mark     = picked_object.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
el_type  = picked_object.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM)
area     = picked_object.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED)
b_offset = picked_object.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET)

💡 Try to guess BuiltInParameter name or look inside of RevitLookup!

Type Parameters

❌ Also some of you will try to get Type parameters from instance elements, and that won't work!

Element and ElementType are 2 different objects in Revit API ! Surely, they are related to one another, but you need to be aware what you are working with.

So if you want to get a type parameter, you would need to get Type object first, and then use the same methods to get a type parameter.

👇 Here is an example of getting Comments and Type Comments

#👉 Get an Element!
elem = ... # 

#1️⃣ Get Instance Parameter
comments = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)

#2️⃣ Get Type Parameter
el_type_id    = elem.GetTypeId()
el_type       = doc.GetElement(el_type_id)
type_comments = el_type.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS)
Get Project/Shared Parameters

Now let's look at how to get Project or Shared Parameters.

As we've seen before we need to use LookupParameter method and provide name of the parameter, so it's quite simple!

# ✅ Get Shared/Project Parametetrs
p_text     = picked_object.LookupParameter("p_text")
s_bool     = picked_object.LookupParameter("s_bool")
s_material = picked_object.LookupParameter("s_material")
s_number   = picked_object.LookupParameter("s_number")
s_area     = picked_object.LookupParameter("s_area")

Also it's a good practice to ensure that you got a parameter out of your element.

Because if you will misspell the name or parameter is not in the project, this method will return you None. And then you can get an error when you try to get a parameter value from None object.

You can make a simple conditional statement to ensure you get the parameter.

p_text     = picked_object.LookupParameter("p_text")

if p_text:
    #do something...
Get Parameter Value

Once you get your parameter we can read the value that it holds.

I strongly recommend you to explore your parameters with Revit Lookup to see what values you can get out of them. Have a look at values returned by methods:

  • AsDouble()

  • AsElementId()

  • AsInteger()

  • AsString()

  • AsValueString()

You can find out which one to use by looking at StorageType property.

StorageType is also an Enumeration, so you can make a conditional statements to determine which one you are dealing with.

if p.StorageType == StorageType.String:
    value = p.AsString()

Let's create a function to get the right value from our parameters for this lesson.

💡 Usually you will know what parameter you are working with, and therefore you will choose appropriate method to get its value.

# FUNCTIONS

def get_param_value(param):
    """Get a value from a Parameter based on its StorageType."""
    value = None
    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()
    return value

This function will allow us to get the right value based on StorageType.

Let's also create a function to read different Parameter properties, so we can practice getting Parameter information.

def read_param(p):
    """Read properties of a Parameter"""
    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("Value: {}".format(get_param_value(p)))
    print("AsValueString(): {}".format(p.AsValueString()))
    print('-'*100)

# 👀 READ PARAMETERS
read_param(comments)
read_param(mark)
read_param(el_type)
read_param(area)
read_param(b_offset)
read_param(p_text)
read_param(s_bool)
read_param(s_material)
read_param(s_number)
read_param(s_area)

Final Code

I hope by now you understand how to Get and Read parameters with Revit API.

Here is final code from this lesson:

# -*- coding: utf-8 -*-
__title__ = "04.02 - Get Parameters"
__doc__ = """Date    = 02.01.2023
_____________________________________________________________________
Description:
Code from lesson 04.02 - Get Parameters 
Example on how to Get Parameters with Revit API.
- Built-In Parameters
- Shared/Project Parameters
_____________________________________________________________________
Author: Erik Frits"""

# ╦╔╦╗╔═╗╔═╗╦═╗╔╦╗╔═╗
# ║║║║╠═╝║ ║╠╦╝ ║ ╚═╗
# ╩╩ ╩╩  ╚═╝╩╚═ ╩ ╚═╝ IMPORTS
#==================================================
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType, Selection

# ╦  ╦╔═╗╦═╗╦╔═╗╔╗ ╦  ╔═╗╔═╗
# ╚╗╔╝╠═╣╠╦╝║╠═╣╠╩╗║  ║╣ ╚═╗
#  ╚╝ ╩ ╩╩╚═╩╩ ╩╚═╝╩═╝╚═╝╚═╝ VARIABLES
#==================================================
uidoc = __revit__.ActiveUIDocument
doc   = __revit__.ActiveUIDocument.Document

selection = uidoc.Selection # type: Selection

# ╔═╗╦ ╦╔╗╔╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
# ╠╣ ║ ║║║║║   ║ ║║ ║║║║╚═╗
# ╚  ╚═╝╝╚╝╚═╝ ╩ ╩╚═╝╝╚╝╚═╝ FUNCTIONS
#==================================================
def get_param_value(param):
    """Get a value from a Parameter based on its StorageType."""
    value = None
    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()
    return value

def read_param(p):
    """Read properties of a Parameter"""
    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("Value: {}".format(get_param_value(p)))
    print("AsValueString(): {}".format(p.AsValueString()))
    print('-'*100)


# ╔╦╗╔═╗╦╔╗╔
# ║║║╠═╣║║║║
# ╩ ╩╩ ╩╩╝╚╝
#==================================================

# 🔷 Pick an Object
ref_picked_object = selection.PickObject(ObjectType.Element)
picked_object     = doc.GetElement(ref_picked_object)


# ✅ Get Built-In Parameters
comments = picked_object.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
mark     = picked_object.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
el_type  = picked_object.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM)
area     = picked_object.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED)
b_offset = picked_object.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET)

# ✅ Get Shared/Project Parametetrs
p_text     = picked_object.LookupParameter("p_text")
s_bool     = picked_object.LookupParameter("s_bool")
s_material = picked_object.LookupParameter("s_material")
s_number   = picked_object.LookupParameter("s_number")
s_area     = picked_object.LookupParameter("s_area")

# ❌ Get Built-In parameter with LookupParmaeter()
# comments = picked_object.LookupParameter("Comments")  # English
# comments = picked_object.LookupParameter("Kommentar") # German

# Read Parameters
read_param(comments)
read_param(mark)
read_param(el_type)
read_param(area)
read_param(b_offset)
read_param(p_text)
read_param(s_bool)
read_param(s_material)
read_param(s_number)
read_param(s_area)

HomeWork

❗Try to Get and Read parameters of other elements.

💡For example Pick a Room and try to read parameters related to your rooms.

👀 Feel free to share your code in the Discord community, and have a look what other people have made.

💟 Sharing is Caring! And together you will learn far more!

⌨️ Happy Coding!

Questions:

Do I always need to check StorageType?

Do I always need to check StorageType?

Why I can't see Type Parameters.

Why I can't see Type Parameters.

Are there different classes for Built-In and Shared Parameters?

Are there different classes for Built-In and Shared Parameters?

How to get parameters in a Family?

How to get parameters in a Family?

Discuss the lesson:

P.S. This discord window is controlled with a bot and sometimes you might experience connection issues. If that happened, open Discord server with an app and look for this lesson's channel. Alternatively you could give it a try later in the browser.

Sorry for that, I am still figuring it out.
- Erik

P.S. This discord window is controlled with a bot and sometimes you might experience connection issues. If that happened, open Discord server with an app and look for this lesson's channel. Alternatively you could give it a try later in the browser.

Sorry for that, I am still figuring it out.
- Erik

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API