Filter Elements by value and Name for Worksets Python

Dear Colleagues, I am having problems again with my coding so I hope you can give some feedback regarding this issue.

I will explain what is the goal: I want to develop a script to place the elements in the right workset, but in my case we have some conditionals. My inputs are:

Workset Name = [w1,w2…wn] List with Unique worksets
Conditional = List[ c1,c2,…cn] List with Parameter or Names
Tipe of Conditional = List[cond1,cond2,…condn] List with True or False
Contains= List[prhase1,[phrase2.1,phrase2.2,phrase2.n],…phrasen] List that can contain sublist with phrases
Elements = [[e1.1,e1.2,…e1.n],[e2.1,e2.2,…e2.n]…[en.1,en.2,…en.n] List with sublist of elements already filter for the main category of the workset conditional

So for this phase, the only thing I need is for the evaluation to be carried out to know if the element should be in the workset or not. It is omitted if it should not be, or the list is empty. I want a structured list with worksets and elements that meet the condition.

At the moment, I have this, but it is not working for me; I am new to the definitions, so I would be looking for your support to see where I am making my mistake.

Thank you in advance for all the support you offer to the community.

This is the code and I left a dummy script to make easy the evaluation that they script works

import clr
from Autodesk.DesignScript.Geometry import *

import Autodesk
from Autodesk.Revit.DB import *

import DSCore
from DSCore import *

import Revit

import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
# Place your code below this line
WNames = IN[0][0]

#Contains Parameter
def ContainsPar(find, element, condition, values):
	if find == "Family Name" and condition:
		#val = element.Name
		val = element
		for value in values:
			if val.Contains(value):
				el = element
				return val,el
			#Family Name does not contain
	elif find == "Family Name" and condition != True:
		#val = element.Name
		val = element
		for value in values:
			if val.Contains(value) == False:
				el = element
				return val,el
			#By parameter contains
	elif find != "Family Name" and condition:
		val = element.LookupParameter(find).AsString()
		for value in values:
			if val.Contains(value):
				el = element
				return val,el
		#By parameter does not contains
	elif find != "Family Name" and condition is False:
		val = element.LookupParameter(find).AsString()
		for value in values:
			if val.Contains(value) == False:
				el = element
				return val,el
		return None

newworks, newlist=[] ,[]

#trial = ContainsPar(CondN, LCatE, CondC, CondL)
for CatE,wnam,cdn,cdc,cdl in zip(LCatE,WNames,CondN,CondC,CondL):
	for elem in CatE:
		ev =ContainsPar(cdn, CatE, cdc, cdl)
		if ev != None:

# Assign your output to the OUT variable.
OUT = newworks, newlist

Test-Filter-Evaluation.dyn (14.5 KB)

I think the issue could be here. If I’m following correctly, element is a list of pre-filtered elements and values is another list of pre-filtered search strings. At this point you have your element list and then you begin to loop through each search value to see if that value exists within the list of elements. The problem is that the search value is a sub-string, not an item in the list, so I think you really want to be searching each item within element for the sub-string value.

"Senzor" does not exist within the list of elements ["Senzor-Example"] but it does exist within the item "Senzor-Example".


Now I am using val = element only to make the dummy code, val will be equal to the family name or the parameter name that will be typed by the form, so let me try with some elements and check if that the issue, thanks very much for your suggestion