Copy Selection filter to all view template

Hi,
I’m trying to copy a selection filter from a view template I named “Desactiver_Maquette” who have just the filter I need to copy to all the view template of my project.

Maybe Copy and pasting isn’t the right method to use…
I work on Dynamo but I can also understand C# macro.

Let me know your issues.

Salut Thomas
Try this (in Python)

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

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#Get Important vars
doc = DocumentManager.Instance.CurrentDBDocument


def getfilterByName(nameFilter):
	collfilter = FilteredElementCollector(doc).OfClass(FilterElement).ToElements()
	if collfilter:
		filterSelect = [x for x in collfilter if x.Name == nameFilter]
		if 	filterSelect:
			return filterSelect[0]
		else: return None	

nameFilter = IN[0]
outViews = []
filter = getfilterByName(nameFilter)
if filter:
	collViews = FilteredElementCollector(doc).OfClass(View)
	viewsTemplate = [x for x in collViews if x.IsTemplate]
	#Processing
	TransactionManager.Instance.ForceCloseTransaction()
	TransactionManager.Instance.EnsureInTransaction(doc)
	for vTemp in viewsTemplate:
		if vTemp.AreGraphicsOverridesAllowed():
			if filter.Id not in vTemp.GetFilters():
				vTemp.AddFilter(filter.Id)
				outViews.append(vTemp)
	TransactionManager.Instance.TransactionTaskDone()
	
	OUT = outViews
else:
	OUT = "Filtre introuvable"

AddFilter

2 Likes

Merci beaucoup !
Petit plus, savez-vous me dire quoi rajouter après vTemp.AddFilter(filter.Id) pour faire en sorte que le filtre sois désactivé par défaut ?

Merci d’avance.

Thank you so much !

For more, can you tell me what I have to put after vTemp.AddFilter(filter.Id) to deactivate filter when it is in the view template.

@Thomas_LiB

using the method SetFilterVisibility

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

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#Get Important vars
doc = DocumentManager.Instance.CurrentDBDocument


def getfilterByName(nameFilter):
	collfilter = FilteredElementCollector(doc).OfClass(FilterElement).ToElements()
	if collfilter:
		filterSelect = [x for x in collfilter if x.Name == nameFilter]
		if 	filterSelect:
			return filterSelect[0]
		else: return None	

nameFilter = IN[0]
outViews = []
filter = getfilterByName(nameFilter)
if filter:
	collViews = FilteredElementCollector(doc).OfClass(View)
	viewsTemplate = [x for x in collViews if x.IsTemplate]
	#Processing
	TransactionManager.Instance.ForceCloseTransaction()
	TransactionManager.Instance.EnsureInTransaction(doc)
	for vTemp in viewsTemplate:
		if vTemp.AreGraphicsOverridesAllowed():
			if filter.Id not in vTemp.GetFilters():
				vTemp.AddFilter(filter.Id)
			vTemp.SetFilterVisibility(filter.Id, False)	
			outViews.append(vTemp)
	TransactionManager.Instance.TransactionTaskDone()
	
	OUT = outViews
else:
	OUT = "Filter not found"
2 Likes