Changing Order of View Filters in a view

Hello, is there a way to change the order of view filters with api after you have added them in a view. For example, there are options of moving view filters up and down in Revit but can you access these functions from api somehow?

Thank you.

Welcome Dolly,
No, I doubt it.
The closest thing you can get through the API is view.GetOrderedFilters() which will give you the actual ordered list of filters. From there:

  • get ids of these filters,
  • reorder them as expected
  • remove them from view
  • add them in the right order (order of creation = order of application (or the other way around)

duh operator error. now its working

from pyrevit import script, revit, forms
from Autodesk.Revit.DB import FilteredElementCollector, Transaction, ViewType
import Autodesk.Revit.DB as DB


# Setting up the script environment
output = script.get_output()
output.close_others()
output.set_width(1100)

# Accessing the current document
doc = revit.doc

# Start a transaction for making changes to the document
t = Transaction(doc, 'Sort Filters in View Templates')
t.Start()

try:
    # Collecting view templates and filtering out Schedule view templates
    view_templates = FilteredElementCollector(doc)\
                    .OfClass(DB.View)\
                    .WhereElementIsNotElementType()\
                    .ToElements()

    filtered_view_templates = [vt for vt in view_templates if vt.IsTemplate and vt.ViewType != ViewType.Schedule]

    # Sorting and displaying filtered view templates in a UI for selection
    selected_viewtemplates = forms.SelectFromList.show(
        sorted(filtered_view_templates, key=lambda vt: vt.Name),
        button_name='Select View Template',
        multiselect=True,
        name_attr='Name'
    )

    # Exit if no view template is selected
    if not selected_viewtemplates:
        t.RollBack()
        script.exit()

    # Sorting filters alphabetically and applying to selected view templates
    for view_template in selected_viewtemplates:
        filter_ids = view_template.GetFilters()

        # Store settings for each filter
        filter_settings = {}
        for fid in filter_ids:
            ogs = view_template.GetFilterOverrides(fid)
            visibility = view_template.GetFilterVisibility(fid)
            filter_settings[fid] = (ogs, visibility)

        # Retrieve filter names
        filters = [(fid, doc.GetElement(fid).Name) for fid in filter_ids]
        
        # Sort filters by name
        sorted_filters = sorted(filters, key=lambda x: x[1])

        # Remove existing filters
        for fid in filter_ids:
            view_template.RemoveFilter(fid)

        # Reapply filters in sorted order with stored settings
        for fid, _ in sorted_filters:
            view_template.AddFilter(fid)
            if fid in filter_settings:
                ogs, visibility = filter_settings[fid]
                view_template.SetFilterOverrides(fid, ogs)
                view_template.SetFilterVisibility(fid, visibility)

    # Commit the transaction
    t.Commit()

except Exception as e:
    # If an error occurred, roll back the transaction
    t.RollBack()
    # Log or display the error
    forms.alert('An error occurred: {}'.format(str(e)), title='Error')
2 Likes

Hello JP, Thank you for your response. Is there a way to sort them out for example, if I want to add filter B right below filter C.
I have filter A and C in my template and I want to add filter B between them.

define the list in the preferred order instead of sorting alphabetically.

sorted_filters = [item4, item2, item1, item3]

how would you like to order this? set list every time, or allow for adjusting order during execution?

1 Like