Set Parameter Values with Revit APi

You know how to Get and Read your parameters, let's look at how to change these values and a few possible errors you might encounter.

Set Parameter Values with Revit APi

You know how to Get and Read your parameters, let's look at how to change these values and a few possible errors you might encounter.

Summary

Set Parameter Values

In the previous video, you learned how to access parameters using the Revit API and read their values based on their storage type. But now let's look at how to set new values for these parameters?

Revit API Docs

Let's have a look at .Set method of Parameter Class.

There are 4 different options, depending on the StorageType of our Parameters:

So you should either know what type of parameter you are dealing with, or make a conditional statement to check its StorageType.


Prepare a button

Create a button for this lesson.

We will use code from the previous lesson, where we already got different parameters when we select a Wall.

#⬇️ 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 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")
Set Parmeter Values

Setting parameters is very simple, we just use .Set method and then we provide a value as an argument with the correct data type.

💡Make sure you provide value to match parameter's StorageType!

If you provide wrong data type of a value, then it won't make any changes, but it also won't show you any errors. So pay attention to that!

# Set Parameter values
with Transaction(doc, __title__) as t:
    t.Start()

    comments.Set(str(50))
    mark.Set(str(picked_object.Id))
    el_type.Set(ElementId(9427))
    b_offset.Set(float("-1.5"))
    p_text.Set("some text")
    s_bool.Set(int(1))
    s_material.Set(ElementId(26))
    s_number.Set(15.55)
    s_area.Set(15.55)

    t.Commit()

💡 Be sure that your parameters are not .ReadOnly!

Transaction

Also notice that I use Transaction class.

💡Transaction are used to make any changes in Revit projects. This protects our project from unintentional changes.

💡 It's also great for beginners to know, we can't mess up anything unless we use them, so get comfortable using Revit API!

In a nutshell: We need to
1) Define a Transaction
2) .Start() transaction
3) .Commit() transaction.
and all the changes have to be between Start and Commit statements.

Here are 2 syntax examples of how to use them:

# Syntax A
t = Transaction(doc, 'Change Name')
t.Start()  #🔓
# 👉 Changes Here...
t.Commit() #🔒

# Syntax B
with Transaction(doc, 'Change Name') as t:
    t.Start()  #🔓
    # 👉 Changes Here...
    t.Commit() #🔒
Results Overview

If you going to inspect parameters where you changed values you might notice different values.

Remember that Revit API uses feet as internal units. So when your parameters have units associated with them, you need to provide your values in feet!

That's why you will see different values in AsDouble() and AsValueString() for s_area parameter. AsValueString shows values converted to your project units!

However, if parameter doesn't have any units associated like s_nubmer, then AsDouble and AsValueString() will hold the same value. That's because Revit doesn't know what units is this value supposed to hold.

Bonus: Write ElementId in Mark parameter for Walls

Here is a bonus example of how to write ElementId in Mark parameter for all walls.

I get all walls in the project with FilteredElementCollector. Don't worry too much about it yet, you will learn everything about getting your elements in Module 06.

all_walls = FilteredElementCollector(doc).OfClass(Wall).ToElements()

t = Transaction(doc, 'Write ElementId in Mark')
t.Start()

for wall in all_walls:
    p_mark = wall.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
    p_mark.Set(str(wall.Id))
    
t.Commit()

💡 Try this snippet!

HomeWork

Use the code snippet provided in the Bonus section to write ElementIds for all your Walls.

Then try to make similar snippet, but for Rooms. Choose a property that you can get out of rooms, and write them in one of Room's parameters.

👇 Here is a snippet to help you get all Rooms.

all_rooms = FilteredElementCollector(doc)\
            .OfCategory(BuiltInCategory.OST_Rooms)\
            .ToElements()

t = Transaction(doc, 'Write Parameter for Rooms.')
t.Start()

for room in all_rooms:
    #👉 Do Something
    
t.Commit()

⌨️ Happy Coding!

Questions:

What to do if Parameter Value doesn't change?

What to do if Parameter Value doesn't change?

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API