Filtering View List by String - Python

Hi all,

Attempting to filter through views in a project based on the view name containing a specific string. I have been able to achieve this using dynamo nodes (see screenshots) “Edit could only upload one screenshot” however I am attempting to achieve the same result using python script (more so for my own development than necessity). The code will work fine with the view names as strings but obviously when the views are as elements the search string will not iterate. My question is the following:

Is it possible to go backwards (from string to element) i.e. if I have a list of views as strings can I get the same list as elements.

Failing the above is there a method for searching views (as elements) for string within python.
Screenshots attached any help greatly appreciated.

Using the Element.Name property you can do this quite easily. The following expects IN[0] to be a list of elements. You can also greatly reduce the redundancy in your script by defining a custom function:

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

def filter_list(elements, search_str):
	"""Given a list of Views, returns only those which
	contain a given string in their name"""
	passed = []
	for element in elements:
		name = element.Name
		if search_str in name:
			passed.append(element)
	return passed

views = [UnwrapElement(view) for view in IN[0] if view is not None]
nwc = filter_list(views, 'NWC')
elec = filter_list(views, 'ELEC')

OUT = nwc, elec

views.dyn (5.1 KB)

This could also be easily modified to accept a list of strings to test (e.g. ["NWC", "ARCH", "STRC"...]) since you’re combining all of the lists anyway.

4 Likes

That worked perfectly, thank you very much. I wanted a manual toggle for the different services so slightly modified as per the attached.