Get and Read Parameters
Description
Parameters Basics
You need to understand how to
- Get Parameters
- Read Parameters
- Write Parameters
1. Getting Parameters:
Built-in parameters - get_Parameter(bip)
Shared parameters - LookupParmaeter(p_name)
2. Reading Parameters
It's important to know the .StorageType to pick correct method
There are 4 available:
String -> .AsString()
Double -> .AsDouble()
Integer or Yes/No -> .AsInteger()
Element reference -> .AsElementId()
3. Setting Parameters
To set new value it's easy. Just use p.Set()
But there are a few things to keep in mind:
- Provide value with correct Type
- Don't forget Transaction to allow changes
- Values with units should be in feet
Happy Coding!
Code
pyRevit# Get Elem/ElemType
#------------------------------
elem = ... # Get an Element Here...
elem_type = doc.GetElement(elem.GetTypeId())
# 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)
# You can check BuilltInParameter with RevitLookup under:
# Parameters -> Definition -> BuiltInParameter (p.Definition.BuiltInParame
#Get Project/Shared Parameters
#------------------------------
custom_p1 = elem.LookupParameter("P_NAME_1")
custom_p2 = elem.LookupParameter("P_NAME_2")
# It's good to check if you got the parameter when you use LookupParameter 😉
if custom_p1:
print(custom_p1.AsValueString())
elem = ...
param = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
#🟠 Read Parameter Properties
#------------------------------
# Choose correct method based on StorageType.
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()
#Real Use
#------------------------------
# You'd normally use method without StorageType check bcause you'd do it mentally.
# So it would be like
elem = ...
param = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
value = param.AsString() #Because comments are always text parameter...
elem = ...
param = elem.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)
new_value = "Test"
# Allow Model Changes with Transaction Start-Commit
t = Transaction(doc)
t.Start()
param.Set(new_value)
t.Commit()
# Ensure correct Type because Revit won't show error message and just supress
# Ensure correct Units because Revit need Feet internally