i have a script that allows me to select 2 elements and return some parameters. The idea with the script is to push the parameter of the first selected element into the second selected element. However, the script is somehow picking up on the order the elements were placed in the Revit model.
This is my script so far. any suggestion?
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType
# Function to select exactly two elements and return their ID Instance parameters
def select_two_elements_and_get_id_instances():
uidoc = __revit__.ActiveUIDocument
# Prompt the user to select elements
references = uidoc.Selection.PickObjects(ObjectType.Element, "Select exactly two elements (press Enter when done)")
# Get the Element instances and their corresponding "ID Instance" parameter values
id_instances = []
elements = []
for ref in references:
element = uidoc.Document.GetElement(ref.ElementId)
elements.append(element)
id_instance_param = element.LookupParameter("ID Instance")
id_instance_value = id_instance_param.AsString() if id_instance_param else "Parameter not found"
id_instances.append(id_instance_value)
# Print ID Instance parameters before returning the value of the second selected element
print("ID Instance Parameters:", id_instances)
return elements, id_instances # Return elements and their ID Instance values
# Main function
def main():
# Get the elements and ID Instance parameters of the selected elements
selected_elements, id_instances = select_two_elements_and_get_id_instances()
# Ensure the first selected element is first in the list
first_selected_id_instance = id_instances[0]
second_selected_id_instance = id_instances[1]
# Output ID Instance parameters
print("ID Instance Parameter of the first selected element:", first_selected_id_instance)
print("ID Instance Parameter of the second selected element:", second_selected_id_instance)
# Call the main function
if __name__ == "__main__":
main()
Here’s the rewritten version of the post above that fixed this problem. Essentially, instead of storing the values in a list, I stored them as a variable.
And thank you for the tip of the triple backticks.
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitServices')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType
# Function to select a single element and return its ID Instance parameter
def select_element_and_get_id_instance(prompt):
uidoc = __revit__.ActiveUIDocument
reference = uidoc.Selection.PickObject(ObjectType.Element, prompt)
element = uidoc.Document.GetElement(reference.ElementId)
id_instance_param = element.LookupParameter("ID Instance")
id_instance_value = id_instance_param.AsString() if id_instance_param else "Parameter not found"
return element, id_instance_value
# Function to set the ID Instance parameter of an element
def set_id_instance_parameter(element, value):
uidoc = __revit__.ActiveUIDocument
t = Transaction(uidoc.Document, "Set ID Instance Parameter")
t.Start()
element.LookupParameter("ID Instance").Set(value)
t.Commit()
# Main function
def main():
# Select the first element
first_element, first_id_instance = select_element_and_get_id_instance("Select the first element")
print("ID Instance Parameter of the first selected element:", first_id_instance)
# Select the second element
second_element, second_id_instance = select_element_and_get_id_instance("Select the second element")
print("ID Instance Parameter of the second selected element before replacement:", second_id_instance)
# Replace the ID Instance parameter value of the second element with the value of the first element
set_id_instance_parameter(second_element, first_id_instance)
print("ID Instance Parameter of the second selected element after replacement:", first_id_instance)
# Call the main function
if __name__ == "__main__":
main()
Since you got rid of the loop, you can avoid reading the value of the second item by taking it out of the first function.
And since all you care is the parameters, you can just return it instead of the element and the value.
Also, as I mentioned in my previuos post, the picking (and most of the other things in the code) can be simplified by using the pyrevit library:
from pyrevit.revit.selection import pick_element
from pyrevit.revit.db import query, Transaction
def pick_and_get_id_instance_parameter():
element = pick_element("select an item")
return query.get_param(element, "ID Instance")
def main():
first_parameter = pick_and_get_id_instance_parameter()
if first_parameter is None:
# here I use print, but an alert would be better... look at the effective output page of the pyrevit notion site
print("Parameter ID Instance not found!")
return
id_instance = query.get_param_value(first_parameter)
second_parameter = pick_and_get_id_instance_parameter()
if second_parameter is not None:
with Transaction("Set ID Instance Parameter"):
second_parameter.Set(id_instance)