Copy View Filters between Views


#1️⃣ Get Views From/To
def get_view_by_name(view_name):
    """Get Views by exact Name."""
    views = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
    for view in views:
        if view.Name == view_name:
            return view
    raise("Couldn't find view by name: {}".format(view_name))

view_from  = get_view_by_name('HG_UG 1_OK FFB')         #type: View
view_to    = get_view_by_name('HG_UG 1_OK FFB Copy 1')  #type: View

#2️⃣ Get and Select View Filters
filter_ids = view_from.GetOrderedFilters()
filters    = [doc.GetElement(f_id) for f_id in filter_ids]

sel_filters = forms.SelectFromList.show(filters,
                                 multiselect=True,
                                 name_attr='Name',
                                 button_name='Select View Filters')

#✅ Ensure Selected Filters
if not sel_filters:
    forms.alert("No View Filters were Selected.\n"
                "Please Try Again.", exitscript=True)

#3️⃣ Copy View Filters
with Transaction(doc, 'Copy ViewFilters') as t:
    t.Start()

    for view_filter in filters:

        #🅰️ Copy Filter Overrides
        overrides = view_from.GetFilterOverrides(view_filter.Id)
        view_to.SetFilterOverrides(view_filter.Id, overrides)

        #🅱️ Copy Enable/Visibility Controls
        visibility = view_from.GetFilterVisibility(view_filter.Id)
        enable     = view_from.GetIsFilterEnabled(view_filter.Id)

        view_to.SetFilterVisibility(view_filter.Id, visibility)
        view_to.SetIsFilterEnabled(view_filter.Id, enable)

    t.Commit()

⌨️ Happy Coding!
Erik Frits