Create and apply list of filters to single view using Python

Hi everyone,

I’m trying to create a list of filters and apply to the view. I’m able to create one filter but when I put a loop in to create multiple filters. I got it solved with code below.

    import clr
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    from RevitServices.Transactions import TransactionManager
    doc = DocumentManager.Instance.CurrentDBDocument
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *
    import System
    from System.Collections.Generic import *


    dataEnteringNode = IN




    cats = IN[0]
    rules = IN[1]
    view = UnwrapElement(IN[2])
    filterName = IN[3]

    TransactionManager.Instance.EnsureInTransaction(doc)

    catList = []
    for i in cats:
    	catList.append(ElementId(i.Id))

    typedCatList = List[ElementId](catList)






    filterlist = []
    for i in filterName:
    	filterlist.append(i)

    try:
    	
    	for name in filterlist:
    		filter = ParameterFilterElement.Create(doc, name, typedCatList,rules)
    		
    		view.AddFilter(filter.Id)	
    except:
    	pass

    TransactionManager.Instance.TransactionTaskDone()


    OUT = filter

filtername = IN[3]

filtername =
Sets variable filtername to an empty list

len(filtername)
This evaluates to Zero, since list is empty.

range(len(filtername)):
Given the length of the list is zero, this doesn’t make sense.
It create a list that starts at 0 (default), and ends at 0 (len of list).
Python will skip this since it cannot iterate over a range of zero length.

Dude, you can just download a Archi-lab_ThinkParametric package from www.ThinkParametric.com website. There is also a course that explains just how to do it. :slight_smile:

1 Like

Also, no need to use range. Just iterate over filtername:

for name in filtername:
      filter = .....

and make sure you move the view.AddFilter and filterlist.append to inside the for loop.

This website has very good ability for posting code. Please remove the screenshot and repost with code. I would not expect people to retype your code just so they can help you debug it.

1 Like

Thank you so much Gui, I got it, I just started learning Python.

Will do. Thanks Konrad

@letuandinh Refer clip on this link Making rebar solid on how to paste code shown by @Einar_Raknes

Thank you !!