I am in infant stages of a script. Below is a working piece of it, for device selection in Revit. But I wanted to make sure my manner of importing the OperationCanceledException is correct.
Is the manner in which I have done that correct? Or is there a cleaner way to perform the same goal? Yes, ChatGPT helped me. I am learning, slowly.
from pyrevit import revit, DB, forms
from Autodesk.Revit.UI.Selection import ObjectType
import Autodesk.Revit.Exceptions.OperationCanceledException
selected_elements = [ ]
# Prompt the user to select elements individually
while True:
try:
selection = revit.uidoc.Selection
selected_object = selection.PickObject(ObjectType.Element,\
"Select Fire Alarm Device (Press Esc to finish selection)")
if selected_object:
element = revit.doc.GetElement(selected_object.ElementId)
# Check if the selected element is of the allowable family name
if element.Symbol.FamilyName == "Allowed Fam Name":
selected_elements.append(element)
else:
forms.alert("Please select an element of the 'Allowed Fam Name' family.")
else:
break
except Autodesk.Revit.Exceptions.OperationCanceledException:
# User canceled the selection, exit the loop
break
print(selected_elements)