How to output the trays selected by the frame

“”“Calculates total volume of all cable tray in the model.”“”
from pyrevit import revit, DB

doc = revit.doc

cable_tray_collector = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST_CableTray).WhereElementIsNotElementType()

total_length = 0

for tray in cable_tray_collector:

length=tray.get_Parameter(DB.BuiltInParameter.CURVE_ELEM_LENGTH).AsValueString()

id=tray.Id

total_length = total_length + int(length) # forgot to convert to int earlier

print("Cable Tray {}: Length = {} mm".format(id,length))

print(“Total length for Cable Trays : {} mm”.format(total_length))

I need to display not the entire collector, but only the selected elements. let’s say 2 trays

@Andrey-SPGR,
If your question does not receive an answer, it usually means it does not make sense for anyone around.the forum.
Remember, not everyone is an MEP specialist, or knows what a tray/frame is.
Try to reformulate, illustrate with screen capture or code what you are trying to achieve.

Well documented question = Great answer

Agree with Jean-Marc that the question is a little vague. If “selected by frame” means “elements in the view”, then you can simple add the view id to your filtered element collector, like this:

cable_tray_collector = DB.FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(DB.BuiltInCategory.OST_CableTray).WhereElementIsNotElementType()

if you want to collect only the selected cable tray elements, you can do it this way (non cable tray elements will be filtered out if they are selected as well):

uidoc = revit.uidoc
selection = uidoc.Selection.GetElementIds()
categoryid = DB.ElementId(DB.BuiltInCategory.OST_CableTray)
cable_tray_collector = [doc.GetElement(id) for id in selection if doc.GetElement(id).Category.Id.Equals(categoryid)]

Many thanks Archibum! Everything is working!
Still wanted to know how can I resize the window?