# How to set a series Filter Rules using "OR (Any rule may be ture)" instead of "AND (All rules must be true)"?

**URL:** <https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656>\
**Category:** Revit\
**Created:** [February 5, 2019, 1:36am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656 "2019-02-05T01:36:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![evoLM](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@evoLM](https://forum.dynamobim.com/u/evoLM)\
**Post date:** [February 5, 2019, 1:36am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/1 "2019-02-05T01:36:47Z")

</div>

Dear All, I am new to the forum and trying to set up Filter Rules using FilterRule.ByRuleType and ParameterFilterElement.ByRules. However, the list of the rules I created is using “AND” by default. How to change to “OR”? Please see the attached images.

 ![Capture_2](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/5/e5662e50b23cb5fc935e186f1cb3f54fcc6b411b.png)

---

<div class="post-metadata">

**Author:** ![rdeardorff](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rdeardorff](https://forum.dynamobim.com/u/rdeardorff)\
**Post date:** [July 26, 2019, 3:42pm UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/2 "2019-07-26T15:42:18Z")

</div>

I am also looking for a way to do this. Anyone know a way?

---

<div class="post-metadata">

**Author:** ![Matt\_Fleming](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/matt_fleming/32/8868_2.png) [@Matt\_Fleming](https://forum.dynamobim.com/u/Matt_Fleming)\
**Post date:** [July 27, 2019, 5:02am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/3 "2019-07-27T05:02:51Z")

</div>

Maybe take a look at:  
ParameterFilterElement.SetRules() → ParameterFilterElement.SetElementFilter() – The new function sets the ParameterFilterElement to contain an ElementFilter representing the combination of filter rules.

I don’t think any packages have this yet, so you may need python?

> **[What's New in the Revit 2019 API](https://thebuildingcoder.typepad.com/blog/2018/04/whats-new-in-the-revit-2019-api.html)**
>
> The Revit 2019 API includes numerous exciting enhancements for add-in developers. Many relate directly to explicit developer wishes and requests
> – Information sources
> – Installation and migration from previous versions
> – What's new in previous...

---

<div class="post-metadata">

**Author:** ![Matt\_Fleming](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/matt_fleming/32/8868_2.png) [@Matt\_Fleming](https://forum.dynamobim.com/u/Matt_Fleming)\
**Post date:** [July 27, 2019, 5:26am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/4 "2019-07-27T05:26:43Z")

</div>

There is also this. (I’m currently testing this out)

- ParameterFilterElement.Create(Document, String, ICollection, IList) → ParameterFilterElement.Create(Document, String, ICollection, ElementFilter) – The ElementFilter input now specifies the filtering rules. This allows combinations of filter rules using logical AND and OR operations. The ElementFilter must be either an ElementParameterFilter or an ElementLogicalFilter containing only ElementParameterFilters and other ElementLogicalFilters.

---

<div class="post-metadata">

**Author:** ![rdeardorff](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rdeardorff](https://forum.dynamobim.com/u/rdeardorff)\
**Post date:** [July 29, 2019, 12:39pm UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/5 "2019-07-29T12:39:48Z")

</div>

@Matt_Fleming Thanks, I will look into this as well.

@erfajo I wonder if this could possibly be something that you could roll into Orchid if you have time. 🙂

---

<div class="post-metadata">

**Author:** ![Ben\_Osborne](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ben_osborne/32/17366_2.png) [@Ben\_Osborne](https://forum.dynamobim.com/u/Ben_Osborne)\
**Post date:** [July 30, 2019, 12:33pm UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/6 "2019-07-30T12:33:02Z")

</div>

I’ve done this using Python in Dynamo.

To create a Revit Filter (Parameter Filter Element) you need an ICollection of the categories the filter will apply to and an ElementParameterFilter. The Construction of the Element Parameter Filter is the most confusing part because you can construct ElementParameterFilters our of other previously constructed ElementParameterFilters.

To try to show this I’ll make a filter that works on Framing and Columns and test for either _Comments_ **Equals** “Magnum” **OR** (_Family Name_ **Begins With** “Blue” **AND** _Comments_ **Equals** “Steel”)

```
import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
import System
from System.Collections.Generic import *
from System.Collections.Generic import List

doc=DocumentManager.Instance.CurrentDBDocument
app=DocumentManager.Instance.CurrentUIApplication.Application

```

First construct an ICollection of StructuralFraming and Structural Column Categories:

```
beamcolumncatlist=[]
beamcolumncatlist.Add(ElementId(BuiltInCategory.OST_StructuralFraming))
beamcolumncatlist.Add(ElementId(BuiltInCategory.OST_StructuralColumns))
iCollBeamColumnCats=List[ElementId](beamcolumncatlist)

```

Next get the parameters you will be testing on (in this case the Comments parameter and the Family Name):

```
pComment=ParameterValueProvider(ElementId(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS))
pFamName=ParameterValueProvider(ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME))

```

Next construct the Filter Rules/Tests on those parameters:

```
xFamName=FilterStringRule(pFamName,FilterStringBeginsWith(),"Blue",False)
xCommSteel=FilterStringRule(pComment,FilterStringEquals(),"Steel",False)
xCommMagnum=FilterStringRule(pComment,FilterStringEquals(),"Magnum",False)

```

Now construct the actual filter (ParameterFilterElement) - note that this must occur inside a transaction:

```
TransactionManager.Instance.EnsureInTransaction(doc)

```

##Make an ICollection of the “Blue” and “Steel” filter tests, then use that ICollection to construct an **AND** filter set.

```
beamcolumnfilt1=[]
beamcolumnfilt1.Add(ElementParameterFilter(xFamName))
beamcolumnfilt1.Add(ElementParameterFilter(xCommSteel))
icolbeamcolumnfilt1=List[ElementFilter](beamcolumnfilt1)
andbeamcolumnfilt1=LogicalAndFilter(icolbeamcolumnfilt1)

```

##Now make an ICollection of the “Magnum” tests and the previous **AND** of “Blue”+“Steel”, then use that ICollection to construct an **OR** filter set.

```
beamcolumnfilt2=[]
beamcolumnfilt2.Add(ElementParameterFilter(xCommMagnum))
beamcolumnfilt2.Add(andbeamcolumnfilt1)
icolbeamcolumnfilt2=List[ElementFilter](beamcolumnfilt2)
orbeamcolumnfilt2=LogicalOrFilter(icolbeamcolumnfilt2)

```

##Now construct a ParameterFilterElement called “ZoolanderBeamsColumns” using the previously defined beam and column category ICollection and the **OR** ElementParameterFilter.

```
pfebeamcolelem=ParameterFilterElement.Create(doc,"ZoolanderBeamsColumns",iCollBeamColumnCats,orbeamcolumnfilt2)
TransactionManager.Instance.TransactionTaskDone()

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![BIM4CLT](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@BIM4CLT](https://forum.dynamobim.com/u/BIM4CLT)\
**Post date:** [September 22, 2020, 10:48am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/7 "2020-09-22T10:48:22Z")

</div>

Hi all, Read the below, unfortunately am not yet at Python stage of my learning.  
I have the opposite issue which is strange, When I use “FilterFule.ByRuleType” in Revit 2020 all Filters Default to “OR (Any rule may be true)” instead of “AND(All rules must be true)”

Of course the below does not work, (Put it in as a joke 😊 ) but I’m just hoping the “FilterRule.ByRuleType” gets updated as this is a super handy Node. We use it to auto-generate loads filters based on Parameters in the model.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/6/f69a74ae9c42fd23e9c93df0dd4723556688a370.jpeg)

---

<div class="post-metadata">

**Author:** ![BIM4CLT](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@BIM4CLT](https://forum.dynamobim.com/u/BIM4CLT)\
**Post date:** [September 22, 2020, 2:00pm UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/8 "2020-09-22T14:00:24Z")

</div>

This is already asked here:

> [@Filter Rules](https://forum.dynamobim.com/t/filter-rules/55379/3):
>
> Also posted a question about this today, you’ve worded it better. Pre-Revit 2019 the script works fine, but now that we have the AND / OR option it always defaults to OR…

---

<div class="post-metadata">

**Author:** ![ssw9UZNL](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@ssw9UZNL](https://forum.dynamobim.com/u/ssw9UZNL)\
**Post date:** [February 18, 2021, 3:06pm UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/9 "2021-02-18T15:06:49Z")

</div>

Hello @Ben_Osborne

i have tried to follow your outline for 2020 and have run into one issues.

I cant really work out how to set the rules, I have attached the script, but basically I try and create a rule that sets a typename- but it just doesnt want to work.

> Blockquoteimport clr  
> clr.AddReference(‘System.Core’)  
> clr.AddReference(‘RhinoInside.Revit’)  
> clr.AddReference(‘RevitAPI’)  
> clr.AddReference(‘RevitAPIUI’)

from System import Enum

clr.AddReference(‘System’)  
from System.Collections.Generic import List

clr.AddReference(‘RevitNodes’)  
import Revit  
clr.ImportExtensions(Revit.Elements)

import rhinoscriptsyntax as rs  
import Rhino  
import RhinoInside  
import Grasshopper  
from Grasshopper.Kernel import GH\_RuntimeMessageLevel as RML  
from RhinoInside.Revit import Revit, Convert  
from Autodesk.Revit import DB  
#from Autodesk.Revit.DB import \*

# DB is the data base module

clr.AddReference(‘RevitServices’)  
import RevitServices  
from RevitServices.Persistence import DocumentManager

clr.ImportExtensions(RhinoInside.Revit.Convert.Geometry)  
from RhinoInside.Revit.Convert.Geometry import UnitConverter

# active Revit verison

REVIT\_VERSION = Revit.ActiveUIApplication.Application.VersionNumber

# access the active document object

doc = Revit.ActiveDBDocument

def show\_warning(msg):  
ghenv.Component.AddRuntimeMessage(RML.Warning, msg)

def show\_error(msg):  
ghenv.Component.AddRuntimeMessage(RML.Error, msg)

def show\_remark(msg):  
ghenv.Component.AddRuntimeMessage(RML.Remark, msg)

# We want to get the element ID of the category

# Get the built in type name paramter from curtain panels category

# FINDNG CATEGR TYPES

CategoryFilter = DB.FilteredElementCollector(doc).OfCategory(DB.BuiltInCategory.OST\_CurtainWallPanels)

# [BuiltInCategory Enumeration](https://www.revitapidocs.com/2019/ba1c5b30-242f-5fdc-8ea9-ec3b61e6e722.htm)

# Create a list with one element of the category

Category\_Elements =   
for count, category in enumerate(CategoryFilter):  
if count == 1:  
break  
Category\_Elements.append(category)

# Create a list

Category\_ElementID\_List2 =

# get data from the category element

CategoryElement = Category\_Elements[0]

# Get the Element Id of the category

Category\_ID = (CategoryElement.Id)  
print (Category\_ID)

# Get tge element from the Category ID

Element = doc.GetElement(Category\_ID)  
Parameter\_ElementID2= Element.get\_Parameter(DB.BuiltInParameter.ALL\_MODEL\_TYPE\_NAME).AsElementId()  
print (Parameter\_ElementID2)  
Category\_ElementID\_List2.append(Parameter\_ElementID2)  
print (Category\_ElementID\_List2)

# LOOKING ONLY AT CATEGORIES

# Get element ID from curtain wall panels for the category and add it to a .net list

Category\_ElementID\_List =   
Parameter\_ElementID = DB.ElementId(DB.BuiltInCategory.OST\_CurtainWallPanels)  
print (Parameter\_ElementID)  
Category\_ElementID\_List.append(Parameter\_ElementID)  
print (Parameter\_ElementID)

#cat\_ids = [cat.Id for cat in category\_set]

# Convert Python list to .NET List

# [Stuck with List[ElementId](ids) - #4 by Yien](https://forum.dynamobim.com/t/stuck-with-list-elementid-ids/26007/4)

# [Solved: view filter by elementIds - Autodesk Community](https://forums.autodesk.com/t5/revit-api-forum/view-filter-by-elementids/td-p/5531477)

# [FilterRule.ByRuleType input issues - #12 by crapai](https://forum.dynamobim.com/t/filterrule-byruletype-input-issues/42207/12)

col1 = ListDB.ElementId  
cat\_ids\_valid = DB.ParameterFilterUtilities.RemoveUnfilterableCategories(col1)

print (cat\_ids\_valid)

# Find type name (just to add)

Type\_Name = CategoryElement.get\_Parameter(DB.BuiltInParameter.ALL\_MODEL\_TYPE\_NAME).AsString()

# Set the rules

Rules\_List =

# [ParameterFilterRuleFactory Class](https://www.revitapidocs.com/2016/317755a4-24ba-9f36-7639-f6fb2aa5a1a7.htm)

rule\_01 = DB.ParameterFilterRuleFactory.CreateContainsRule(Parameter\_ElementID, “Yes”, False)  
Rules\_List.append(rule\_01)

# Convert single FitlerRule to List

rules = ListDB.FilterRule

# HOW DO I MAKE THE RULES?

# [FilterRule.ByRuleType input issues - #17 by crapai](https://forum.dynamobim.com/t/filterrule-byruletype-input-issues/42207/17)

# [How to set a series Filter Rules using "OR (Any rule may be ture)" instead of "AND (All rules must be true)"? - #6 by Ben\_Osborne](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/6)

# [How to set a series Filter Rules using "OR (Any rule may be ture)" instead of "AND (All rules must be true)"? - #6 by Ben\_Osborne](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/6)

xFamName=DB.FilterStringRule(Parameter\_ElementID2,DB.FilterStringBeginsWith(),“Blue”,False)

test=  
test.Add(ElementParameterFilter(xFamName))  
testlist=ListElementFilter  
logical=LogicalAndFilter(testlist)

# Redo this bit

t = DB.Transaction(doc, ‘update top constraint’)  
t.Start()

#try:

# [ParameterFilterElement Class](https://www.revitapidocs.com/2016/b231dc85-516a-5e75-c634-c6cd81b43fc5.htm)

fitler = DB.ParameterFilterElement.Create(doc, “Test Filter”, cat\_ids\_valid, logical)  
#except:  
#pass

t.Commit()

---

<div class="post-metadata">

**Author:** ![Joaquim\_Agostinho1](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@Joaquim\_Agostinho1](https://forum.dynamobim.com/u/Joaquim_Agostinho1)\
**Post date:** [April 23, 2024, 8:39am UTC](https://forum.dynamobim.com/t/how-to-set-a-series-filter-rules-using-or-any-rule-may-be-ture-instead-of-and-all-rules-must-be-true/31656/10 "2024-04-23T08:39:17Z")

</div>

an excellent detailed description, @Ben_Osborne. Thank you for taking the time for that.

One quick question, why is “False” added to the end of each rule (below in bold, your code)  
The [FilterStringRule constructor](https://www.revitapidocs.com/2024/5bd5286a-612f-e12f-e200-6a8763bb2aee.htm) does not seem to have a boolean in its syntax.

_xFamName=FilterStringRule(pFamName,FilterStringBeginsWith(),“Blue”, **False** )_  
_xCommSteel=FilterStringRule(pComment,FilterStringEquals(),“Steel”, **False** )_  
_xCommMagnum=FilterStringRule(pComment,FilterStringEquals(),“Magnum”, **False** )_

Thanks in advance
