Get parameter with Revit API

Get parameter with Revit API

brudWK2W5
Contributor Contributor
3,696 Views
5 Replies
Message 1 of 6

Get parameter with Revit API

brudWK2W5
Contributor
Contributor

Hi guys i am new in Revit API and i have one list with Pipes, Pipes Acessory and Pipe Fittings on python. after it i would like to get one share parameter from this elements but i dont know how. Below you can se my script

 

#IMPORTS

from Autodesk.Revit.DB import*                 #Import DB Classes
from Autodesk.Revit.UI.Selection import*       #Import UI Selection Classes


#.NET imports
import clr
clr.AddReference('System')
from System.Collections.Generic import List

"----------------------------------------------------------------------------------"
#VARIABLES

doc   = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app   = __revit__.Application

Active_View = doc.ActiveView
 
# Global Variables

"----------------------------------------------------------------------------------"
 #MAIN
                                                         

PF = FilteredElementCollector(doc,Active_View.Id).OfCategory(BuiltInCategory.OST_PipeFitting)\
    .WhereElementIsNotElementType().ToElements()

PP = FilteredElementCollector(doc,Active_View.Id).OfCategory(BuiltInCategory.OST_PipeCurves)\
    .WhereElementIsNotElementType().ToElements()

PA = FilteredElementCollector(doc,Active_View.Id).OfCategory(BuiltInCategory.OST_PipeAccessory)\
    .WhereElementIsNotElementType().ToElements()

NK_CATEGORIES = list(PF) + list(PP) + list(PA)
 
some one know to get the value of one share parameter called EZ_LINE NO in the NK_CATEGORIES? It is so easy filter the elements and than get one parameter with Dynamo but in Revit API it has been a big issue for me 
0 Likes
Accepted solutions (1)
3,697 Views
5 Replies
Replies (5)
Message 2 of 6

ctm_mka
Collaborator
Collaborator

there are two methods, look in the API documentation for "LookupParameter" and get_Parameter methods. Also to consider, is your parameter instance or type based? for example, to get at a type parameter using lookupparameter, you do it like so:

 

element.Symbol.LookupParameter("theparametername")

 

 to get an instance, same call, but with Symbol. Lastly, i recommend installing Revit Lookup if you have not already done so, extremely useful in figuring how to get to various things, as well a what return type to use. By return type i mean, is the value your looking for a double, string, or other. It will affect your parameter call. In the example above if its a string type paramater, i would tack onto the end ".AsString()" or ".AsValueString()"

Message 3 of 6

NURIATIRED
Contributor
Contributor

Hi!

To retrieve a shared parameter value called EZ_LINE NO for your list of elements (NK_CATEGORIES), you can use the Parameter property of an element to access its parameters and then get the value of your shared parameter. Since you’re dealing with shared parameters, you may need to use the GetParameter method with the parameter name as a string.

Here’s how you can iterate through your list of elements and try to retrieve the value of the EZ_LINE NO shared parameter for each element. Note that this code checks if the parameter exists for an element before trying to access its value to avoid errors:

# Function to get parameter value by name
def get_param_value_by_name(element, param_name):
    # Attempt to get the parameter value
    param = element.LookupParameter(param_name)
    if param and param.HasValue:
        # Check the storage type to ensure correct value retrieval
        if param.StorageType == StorageType.String:
            return param.AsString()
        elif param.StorageType == StorageType.Integer:
            return param.AsInteger()
        elif param.StorageType == StorageType.Double:
            # Optionally, format the Double value as needed
            return param.AsDouble()
        elif param.StorageType == StorageType.ElementId:
            # Optionally, handle the ElementId type as needed
            return param.AsElementId()
    return None

# Parameter name to look for
param_name = "EZ_LINE NO"

# Iterate through each element in the NK_CATEGORIES list
for element in NK_CATEGORIES:
    # Get the parameter value
    param_value = get_param_value_by_name(element, param_name)
    # Do something with the parameter value, e.g., print it
    if param_value is not None:
        print("Element ID: {0}, {1}: {2}".format(element.Id, param_name, param_value))
    else:
        print("Element ID: {0}, {1} not found".format(element.Id, param_name))

 This script defines a function get_param_value_by_name that attempts to retrieve a parameter by its name and return its value depending on the parameter’s storage type. It then iterates through each element in your NK_CATEGORIES list, retrieves the value of the EZ_LINE NO parameter, and prints it out. Note that the print statements are just placeholders, and you should replace them with your own logic, such as storing these values or further processing them.

If you found my answer useful, mark it as a solution

NuriaTired
0 Likes
Message 4 of 6

brudWK2W5
Contributor
Contributor

Thanks a lot @NURIATIRED . 

 

i found one way using it and works as well.

 

for element in NK_CATEGORIES:
    atributo = element.LookupParameter("LINHA")
    print(atributo.AsString())
 
I will use and see your methods to study deeper.
0 Likes
Message 5 of 6

brudWK2W5
Contributor
Contributor

Thanks a lot!

0 Likes
Message 6 of 6

MarryTookMyCoffe
Collaborator
Collaborator
Accepted solution

don't get shared parameter base on name it is possible for document to have two parameters named the same. For shared parameters use GUID

Guid guid = Guid.Parse("{C4337539-D59E-49A2-AB53-C939DBC05F01}");
var parameter = element.get_Parameter(guid);

or use it like this

//get this on the start 
Guid guid = Guid.Parse("{C4337539-D59E-49A2-AB53-C939DBC05F01}");
SharedParameterElement sharedParameterElement = SharedParameterElement.Lookup(document, guid);
var definition = sharedParameterElement.GetDefinition();
//use definition to get parameter from elements
var parameter = element.get_Parameter(definition);

 If you need to use it one time only first way can do, but if you what to change many parameters then second way is better. But if you just need to filter elements on parameter value than it need a completely different approach.

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug