@c.poupin Had you on the right track. I took the time to add in the ability to input different FilterRule types - As it stands they will always be case sensitive if the parameter value is a string.
I also set it up to expect input how I’m assuming you’ll be handing it the information, based on your image.
I tested it out a bit, but not extensively, seemed to work just fine when I input different rule types, and values.
It does expect a string that matches one of these for the rule type, just FYI.
import clr
import sys
import System
#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB
#import net library
from System import Array
from System.Collections.Generic import List, IList, Dictionary
#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)
def get_ParameterId_byName(lst_paraIds, para_name):
dictBip = {ElementId(bip) : bip for bip in System.Enum.GetValues(BuiltInParameter)}
for pId in lst_paraIds:
if doc.GetElement(pId) is not None and doc.GetElement(pId).Name == para_name:
return pId
elif pId in dictBip and DB.LabelUtils.GetLabelFor(dictBip.get(pId)) == para_name:
return pId
else: pass
return ElementId.InvalidElementId
def create_rule(type_name, para_id, value):
if type_name == 'BeginsWith':
return DB.ParameterFilterRuleFactory.CreateBeginsWithRule(para_id, value, True)
elif type_name == 'Contains':
return DB.ParameterFilterRuleFactory.CreateContainsRule(para_id, value, True)
elif type_name == 'EndsWith':
return DB.ParameterFilterRuleFactory.CreateEndsWithRule(para_id, value, True)
elif type_name == 'Equals':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateEqualsRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateEqualsRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateEqualsRule(para_id, value)
elif type_name == 'Greater':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateGreaterRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateGreaterRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateGreaterRule(para_id, value)
elif type_name == 'GreaterOrEqual':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateGreaterOrEqualRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateGreaterOrEqualRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateGreaterOrEqualRule(para_id, value)
elif type_name == 'Less':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateLessRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateLessRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateLessRule(para_id, value)
elif type_name == 'LessOrEqual':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateLessOrEqualRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateLessOrEqualRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateLessOrEqualRule(para_id, value)
elif type_name == 'NotBeginsWith':
return DB.ParameterFilterRuleFactory.CreateNotBeginsWithRule(para_id, value, True)
elif type_name == 'NotContains':
return DB.ParameterFilterRuleFactory.CreateNotContainsRule(para_id, value, True)
elif type_name == 'NotEndsWith':
return DB.ParameterFilterRuleFactory.CreateNotEndsWithRule(para_id, value, True)
elif type_name == 'NotEquals':
if isinstance(value, str):
return DB.ParameterFilterRuleFactory.CreateNotEqualsRule(para_id, value, True)
elif isinstance(value, float):
return DB.ParameterFilterRuleFactory.CreateNotEqualsRule(para_id, value, 1e-10)
else:
return DB.ParameterFilterRuleFactory.CreateNotEqualsRule(para_id, value)
toList = lambda x : x if hasattr(x, '__iter__') else [x]
def create_filter(grouped_inputs):
ot = []
for f_name, cats, f_type, p_value, p_name in grouped_inputs:
cats = toList(cats)
cats = [UnwrapElement(x) for x in cats]
lstCatIds = List[ElementId]([c.Id for c in cats])
lstParameterIds = DB.ParameterFilterUtilities.GetFilterableParametersInCommon(doc,lstCatIds)
paraId = get_ParameterId_byName(lstParameterIds, p_name)
if paraId != ElementId.InvalidElementId:
rule = create_rule(f_type, paraId, p_value)
if sdkNumber > 2018:
elementFilter = ElementParameterFilter(rule)
filter = DB.ParameterFilterElement.Create(doc, f_name, lstCatIds, elementFilter)
ot.append(filter)
else:
lstRule = List[FilterRule]([rule])
filter = DB.ParameterFilterElement.Create(doc, f_name, lstCatIds, lstRule)
ot.append(filter)
return ot
#Preparing input from dynamo to revit
filter_name = IN[0]
categories = IN[1]
filter_type = IN[2]
parameter_value = IN[3]
parameter_name = IN[4]
input = zip(filter_name, categories, filter_type, parameter_value, parameter_name)
TransactionManager.Instance.EnsureInTransaction(doc)
out = create_filter(input)
TransactionManager.Instance.TransactionTaskDone()
OUT = out