Group Filtered Elements in different list from uidoc selection : pyrevit forms


I am using ui.doc method to pick Elements from Revit and have a class customISelectionFilter
which selects the Elements by MultiCategory. I am looking to group them in a list based on their categories after selection. Can anyone help me with this

This line does the selection from pyrevit forms.
sel_filteredElements = uidoc.Selection.PickObjects(ObjectType.Element,sel_filters,“Select your objects”)

You’ll need to do the sorting after the selection.
You can use an if elif elif else to parse the list into different groups (lists) and then put them all in one list or as a list of tuples.

You can’t do the sorting during.g the selection. Revit pretty much adds them in the order picked.

1 Like

I also find it nicer for the end use if they follow the standard selection path of Revit
If there are already elements selected - use that.
I’d no objects are selected - wait for a user selection (as you are doing.)
The same grouping and filtering works for either method the user chooses.

For instance, if the user has text, dimensions and walls selected, and my app oy wants walls and dimensions, I’ll group the walls and dimensions and skip the text as I sort. Saves a bunch of time for the user in case they pick poorly.

1 Like

Can you please help me solve this? @GavinCrump I was following your tutorial on CustomISelection and I want to print the Element Categories and also generate a Schedule based on selection from uidoc in pyrevit forms . Let me know if you can help

My code is below:

	user_filters = CustomISelectionFilter(chosen)
        sel_filteredElements = uidoc.Selection.PickObjects(ObjectType.Element,user_filters,"Select your objects")
        forms.alert('Done with Selection?',ok = False, yes = True, no = True, exitscript = True)

        print("This prints the List of the PickedObjects from user UI selection ")
        print("This prints the Category of the PickedObjects ")
		#Print the Category of the PickedObjects from the UI selection
        for elements in sel_filteredElements:

        # Sort the selected objects by their categories
        sort_sel_filteredElements = sorted(sel_filteredElements, key=lambda obj: obj.Element.Category.Name)


        #print("Quantities of the Selected Objects is :{}".format(len(sel_filteredElements)))

        # Iterate over the sorted objects and count the quantities for each category
        for category_name in tochoose:
            category_count = sum(1 for obj in sorted_objects if obj.Element.Category.Name == category_name)
            quantities[category_name] = category_count

        # Print the quantities specific to categories
        for category_name, quantity in quantities.items():
            print("{}: {}".format(category_name, quantity))

and for ISelection the code is:

tochoose = ["Walls","Structural Foundations","Structural Framing","Roofs","Structural Columns"]
		chosen =,title = "Choose Categories for Schedule", width= 300,
                                         height = 500,button_name = "Make a Selection",multiselect = True)

@AshindeExd to get you started: Erik Frits on LinkedIn: #learnrevitapi #revitapi #revit #pyrevit
by @ErikFrits

1 Like