Can't add filter rules with FilterRule.ByRuleType

Well, @Ben_Osborne posted solution on the other thread is the same approach as mine.

here is the python code is you ever want to reference it:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

# Import RevitAPI
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import ToProtoType, ToRevitType geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

# Import DSCore nodes in Dynamo
clr.AddReference('DSCoreNodes')
import DSCore
from DSCore import *

# Import python library
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import os
import shutil
import math
# Import math library
from math import *

#Import Data and Time
from datetime import datetime
now = datetime.now()

#Import System Library
import System
from System.Collections.Generic import *
from System.IO import Directory, Path

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
def ToList(x):
	if isinstance(x,list): return UnwrapElement(x)
	else: return [UnwrapElement(x)]
	
#Preparing input from dynamo to revit
names = ToList(IN[0])
cats = ToList(IN[1])
catids = List[ElementId]()
catids.Clear()
for cc in cats:
	catids.Add(cc.Id)
Parafilele = []

#get parameter id via name
iterator = doc.ParameterBindings.ForwardIterator()
while iterator.MoveNext():
	if iterator.Key.Name == "123": 
		pvp = ParameterValueProvider(iterator.Key.Id)
		break
		
#def subsubset of element filters
ssfilters = List[ElementFilter]()
ssfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringBeginsWith(),"test1",False)))
ssfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringEndsWith(),"test2",False)))
ssfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringLessOrEqual(),"test3",False)))
ssfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringGreaterOrEqual(),"test4",False)))
#define subset of element filters
sfilters = List[ElementFilter]()
sfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringEquals(),"hello1",False)))
sfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringContains(),"hello2",False)))
sfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringContains(),"hello3",False)))
sfilters.Add(ElementParameterFilter(FilterStringRule(pvp,FilterStringContains(),"hello4",False)))
sfilters.Add(LogicalOrFilter(ssfilters))
#define final set of element filters
fRule = LogicalAndFilter(sfilters)

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
for name,cat in zip(names,cats):
	paraele = ParameterFilterElement.Create(doc,name,catids)
	paraele.SetElementFilter(fRule)
	Parafilele.append(paraele)
TransactionManager.Instance.TransactionTaskDone()

#Final output
OUT = Parafilele

And i believe there are lots of custom package out there that can and will extract shared parameter id.

2 Likes

and also, i dont get what you meant by this?

Do you mind elaborating it?

I actually can’t set either of my values right now. I’ve tried both the string “Yes” and a boolean. I don’t know why the workset one isn’t working because I’ve been able to get the value to work when making another filter for demo.

I haven’t had a chance to look at your code in detail yet, I’m probably going to do this after this post. I noticed your loading a lot in. Is it all needed or is it just your standard module loading in Dynamo?

Is the parameter you are trying to set a Yes/No parameter?

If so, use Yes = 1, No = 0.

Yes, but when I use 1, FilterRule.ByRuleType doesn’t work.

Not sure if this is the problem, but might the problem be the Rule Type inputs? I don’t know because I have never used the nodes for filter creation but it’s worth a shot.

any, sorry for the late help. But if you wanna filter boolean type of params, here the code below to do it.

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

iterator = doc.ParameterBindings.ForwardIterator()
while iterator.MoveNext():
	if iterator.Key.Name == "boolean": 
		pvp = ParameterValueProvider(iterator.Key.Id)
		break

#Preparing input from dynamo to revit

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

#Empty list of Element Filter
sRule1 = ElementParameterFilter(FilterIntegerRule(pvp,FilterNumericEquals(), 1))

#Get a list to cat ids to be used by the filter
catids = List[ElementId]()
catids.Add(Category.GetCategory(doc,BuiltInCategory.OST_Floors).Id)

#Create the ParameterFilterElement
pfe1 = ParameterFilterElement.Create(doc,"Example view filter1",catids,sRule1)

TransactionManager.Instance.TransactionTaskDone()
#Final output
OUT = pfe1
1 Like