Copy All Filters Between Views

Hi everybody
I’m trying to write a Form that duplicates a filter for a view (Level 1). However, my copy command is executed for all views in the project… Is there any way to make my code apply to only one view that I select(Level1)? Help me

My Script :
Copy Filters Form.dyn (10.7 KB)

Hey,

Interesting code :slight_smile:

I think it might find you get more responses if you copy & paste your code in, rather than linking a dyn… you can use the <./> button to help format it :slight_smile:

I think part of the problem is in the question… There isn’t any commenting in your code, so both you and I are finding it a little hard to spot the errors… Personally I also don’t break out code into definitions until I’ve got it working, because i find it hard to de-bug. Once you’re ready though, it’s a really great practice :slight_smile:

From what I can see, at first glance, I think the problem is this line…

for i in nameviews:

I don’t think you want to iterate through several views, you just want the index of the one which was selected? Perhaps? :slight_smile:

Best of luck,

Mark

Just to help others, here’s the full code:


import clr
import System
from System.Collections.Generic import *

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

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import*

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

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

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *


doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
filter = view.GetFilters()
getviews = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()

nameviews = []
for i in getviews:
	nameviews.append(doc.GetElement(i.Id))
sourceviewFilters = []
for i in filter:
	sourceviewFilters.append(doc.GetElement(i))

filters = []
for i in sourceviewFilters:
	filters.append(i.Id)
class MainForm(Form):
    	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._groupBox1 = System.Windows.Forms.GroupBox()
		self._groupBox2 = System.Windows.Forms.GroupBox()
		self._ckListFilters = System.Windows.Forms.CheckedListBox()
		self._ckAllNone = System.Windows.Forms.CheckBox()
		self._comboBox1 = System.Windows.Forms.ComboBox()
		self._btnOk = System.Windows.Forms.Button()
		self._btnCancel = System.Windows.Forms.Button()
		self._groupBox1.SuspendLayout()
		self._groupBox2.SuspendLayout()
		self.SuspendLayout()
		# 
		# groupBox1
		# 
		self._groupBox1.Controls.Add(self._ckAllNone)
		self._groupBox1.Controls.Add(self._ckListFilters)
		self._groupBox1.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._groupBox1.Location = System.Drawing.Point(3, 3)
		self._groupBox1.Name = "groupBox1"
		self._groupBox1.Size = System.Drawing.Size(381, 356)
		self._groupBox1.TabIndex = 0
		self._groupBox1.TabStop = False
		self._groupBox1.Text = "All Filters In View"
		# 
		# groupBox2
		# 
		self._groupBox2.Controls.Add(self._comboBox1)
		self._groupBox2.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._groupBox2.Location = System.Drawing.Point(3, 360)
		self._groupBox2.Name = "groupBox2"
		self._groupBox2.Size = System.Drawing.Size(381, 58)
		self._groupBox2.TabIndex = 1
		self._groupBox2.TabStop = False
		self._groupBox2.Text = "View Paste"
		# 
		# ckListFilters
		# 
		self._ckListFilters.DisplayMember = "Name"
		self._ckListFilters.CheckOnClick = True
		self._ckListFilters.FormattingEnabled = True
		self._ckListFilters.Location = System.Drawing.Point(10, 24)
		self._ckListFilters.Name = "ckListFilters"
		self._ckListFilters.Size = System.Drawing.Size(364, 289)
		self._ckListFilters.Sorted = True
		self._ckListFilters.TabIndex = 0
		self._ckListFilters.TabStop = False
		self._ckListFilters.SelectedIndexChanged += self.CkListFiltersSelectedIndexChanged
		# ckAllNone
		# 
		self._ckAllNone.Location = System.Drawing.Point(10, 320)
		self._ckAllNone.Name = "ckAllNone"
		self._ckAllNone.Size = System.Drawing.Size(150, 24)
		self._ckAllNone.TabIndex = 1
		self._ckAllNone.Text = "Check All/None"
		self._ckAllNone.UseVisualStyleBackColor = True
		self._ckAllNone.CheckedChanged += self.CkAllNoneCheckedChanged
		# 
		# comboBox1
		# 
		self._comboBox1.DisplayMember = "Name"
		self._comboBox1.BackColor = System.Drawing.SystemColors.ControlLightLight
		self._comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
		self._comboBox1.FormattingEnabled = True
		self._comboBox1.Location = System.Drawing.Point(10, 22)
		self._comboBox1.Name = "comboBox1"
		self._comboBox1.Size = System.Drawing.Size(364, 26)
		self._comboBox1.Sorted = True
		self._comboBox1.TabIndex = 0
		self._comboBox1.Items.AddRange(System.Array[System.Object](nameviews))
		self._comboBox1.SelectedIndexChanged += self.ComboBox1SelectedIndexChanged
		# 
		# btnOk
		# 
		self._btnOk.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._btnOk.Location = System.Drawing.Point(221, 421)
		self._btnOk.Name = "btnOk"
		self._btnOk.Size = System.Drawing.Size(75, 23)
		self._btnOk.TabIndex = 2
		self._btnOk.Text = "OK"
		self._btnOk.UseVisualStyleBackColor = True
		self._btnOk.Click += self.BtnOkClick
		# 
		# btnCancel
		# 
		self._btnCancel.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._btnCancel.Location = System.Drawing.Point(302, 421)
		self._btnCancel.Name = "btnCancel"
		self._btnCancel.Size = System.Drawing.Size(75, 23)
		self._btnCancel.TabIndex = 3
		self._btnCancel.Text = "Cancel"
		self._btnCancel.UseVisualStyleBackColor = True
		self._btnCancel.Click += self.BtnCancelClick
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(389, 453)
		self.Controls.Add(self._btnCancel)
		self.Controls.Add(self._btnOk)
		self.Controls.Add(self._groupBox2)
		self.Controls.Add(self._groupBox1)
		self.Name = "MainForm"
		self.ShowIcon = False
		self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		self.Text = "Copy Filters - BIMTech"
		self._groupBox1.ResumeLayout(False)
		self._groupBox2.ResumeLayout(False)
		self.ResumeLayout(False)


	def CkListFiltersSelectedIndexChanged(self, sender, e):
		pass

	def CkAllNoneCheckedChanged(self, sender, e):
		var = self._ckListFilters.Items.Count
		for i in range(var):
			if self._ckAllNone.Checked == True:
				self._ckListFilters.SetItemChecked(i,True)
			else:
				self._ckListFilters.SetItemChecked(i,False)
		pass

	def ComboBox1SelectedIndexChanged(self, sender, e):
		for i in nameviews:
			self._comboBox1.Items.Add(i)
		if self._comboBox1.SelectedItem:
			for i in sourceviewFilters:
				self._ckListFilters.Items.Add(i)
		pass

	def BtnOkClick(self, sender, e):
		self.Select = []
		for i in self._ckListFilters.CheckedItems:
			self.Select.append(i)
		if self._comboBox1.SelectedItem:
			TransactionManager.Instance.EnsureInTransaction(doc)
			for i in nameviews:
				for j in self.Select:
					try:
						i.AddFilter(j.Id)
					except:
						pass
			TransactionManager.Instance.TransactionTaskDone()
		self.Close()
		pass

	def BtnCancelClick(self, sender, e):
		self.Close()
		pass

f = MainForm()
Application.Run(f)
OUT = f.Select
3 Likes

Thanks for your feedback. I think like you about the line of code: (for i in nameview). I’m trying to fix this line of code. Have a nice day

Hello,
here an alternative using python dictionaries

import clr
import System
from System.Collections.Generic import *

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

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import*

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

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

doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.viewPaste = None
		self.curview = doc.ActiveView
		filters = [doc.GetElement(i) for i in self.curview.GetFilters()]
		self._dictfilter = {x.Name : x for x in filters}
		getviews = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
		self._dictView = {Element.Name.GetValue(x) : x for x in getviews}
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._groupBox1 = System.Windows.Forms.GroupBox()
		self._groupBox2 = System.Windows.Forms.GroupBox()
		self._ckListFilters = System.Windows.Forms.CheckedListBox()
		self._ckAllNone = System.Windows.Forms.CheckBox()
		self._comboBox1 = System.Windows.Forms.ComboBox()
		self._btnOk = System.Windows.Forms.Button()
		self._btnCancel = System.Windows.Forms.Button()
		self._groupBox1.SuspendLayout()
		self._groupBox2.SuspendLayout()
		self.SuspendLayout()
		# 
		# groupBox1
		# 
		self._groupBox1.Controls.Add(self._ckAllNone)
		self._groupBox1.Controls.Add(self._ckListFilters)
		self._groupBox1.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._groupBox1.Location = System.Drawing.Point(3, 3)
		self._groupBox1.Name = "groupBox1"
		self._groupBox1.Size = System.Drawing.Size(381, 356)
		self._groupBox1.TabIndex = 0
		self._groupBox1.TabStop = False
		self._groupBox1.Text = "All Filters In View" if len(self._dictfilter) > 0 else "NO FILTER IN CURRENT VIEW"
		# 
		# groupBox2
		# 
		self._groupBox2.Controls.Add(self._comboBox1)
		self._groupBox2.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._groupBox2.Location = System.Drawing.Point(3, 360)
		self._groupBox2.Name = "groupBox2"
		self._groupBox2.Size = System.Drawing.Size(381, 58)
		self._groupBox2.TabIndex = 1
		self._groupBox2.TabStop = False
		self._groupBox2.Text = "View Paste"
		# 
		# ckListFilters
		# 
		self._ckListFilters.DisplayMember = "Name"
		self._ckListFilters.CheckOnClick = True
		self._ckListFilters.FormattingEnabled = True
		self._ckListFilters.Location = System.Drawing.Point(10, 24)
		self._ckListFilters.Name = "ckListFilters"
		self._ckListFilters.Size = System.Drawing.Size(364, 289)
		self._ckListFilters.Sorted = True
		self._ckListFilters.TabIndex = 0
		self._ckListFilters.TabStop = False
		self._ckListFilters.Items.AddRange(System.Array[System.Object](self._dictfilter.keys()))
		self._ckListFilters.SelectedIndexChanged += self.CkListFiltersSelectedIndexChanged
		# ckAllNone
		# 
		self._ckAllNone.Location = System.Drawing.Point(10, 320)
		self._ckAllNone.Name = "ckAllNone"
		self._ckAllNone.Size = System.Drawing.Size(150, 24)
		self._ckAllNone.TabIndex = 1
		self._ckAllNone.Text = "Check All/None"
		self._ckAllNone.UseVisualStyleBackColor = True
		self._ckAllNone.CheckedChanged += self.CkAllNoneCheckedChanged
		# 
		# comboBox1
		# 
		self._comboBox1.DisplayMember = "Name"
		self._comboBox1.BackColor = System.Drawing.SystemColors.ControlLightLight
		self._comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
		self._comboBox1.FormattingEnabled = True
		self._comboBox1.Location = System.Drawing.Point(10, 22)
		self._comboBox1.Name = "comboBox1"
		self._comboBox1.Size = System.Drawing.Size(364, 26)
		self._comboBox1.Sorted = True
		self._comboBox1.TabIndex = 0
		self._comboBox1.Enabled = True if len(self._dictfilter) > 0 else False
		self._comboBox1.Items.AddRange(System.Array[System.Object](self._dictView.keys()))
		self._comboBox1.SelectedIndexChanged += self.ComboBox1SelectedIndexChanged
		# 
		# btnOk
		# 
		self._btnOk.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._btnOk.Location = System.Drawing.Point(221, 421)
		self._btnOk.Name = "btnOk"
		self._btnOk.Size = System.Drawing.Size(75, 23)
		self._btnOk.TabIndex = 2
		self._btnOk.Text = "OK"
		self._btnOk.Enabled = True if len(self._dictfilter) > 0 else False
		self._btnOk.UseVisualStyleBackColor = True
		self._btnOk.Click += self.BtnOkClick
		# 
		# btnCancel
		# 
		self._btnCancel.Font = System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0)
		self._btnCancel.Location = System.Drawing.Point(302, 421)
		self._btnCancel.Name = "btnCancel"
		self._btnCancel.Size = System.Drawing.Size(75, 23)
		self._btnCancel.TabIndex = 3
		self._btnCancel.Text = "Cancel"
		self._btnCancel.UseVisualStyleBackColor = True
		self._btnCancel.Click += self.BtnCancelClick
		# 
		# MainForm
		# 
		self.ClientSize = System.Drawing.Size(389, 453)
		self.Controls.Add(self._btnCancel)
		self.Controls.Add(self._btnOk)
		self.Controls.Add(self._groupBox2)
		self.Controls.Add(self._groupBox1)
		self.Name = "MainForm"
		self.ShowIcon = False
		self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
		self.Text = "Copy Filters - BIMTech"
		self._groupBox1.ResumeLayout(False)
		self._groupBox2.ResumeLayout(False)
		self.ResumeLayout(False)

	def CkListFiltersSelectedIndexChanged(self, sender, e):
		pass

	def CkAllNoneCheckedChanged(self, sender, e):
		var = self._ckListFilters.Items.Count
		for i in range(var):
			if self._ckAllNone.Checked == True:
				self._ckListFilters.SetItemChecked(i,True)
			else:
				self._ckListFilters.SetItemChecked(i,False)

	def ComboBox1SelectedIndexChanged(self, sender, e):
		self.viewPaste = self._dictView.get(sender.Text)
		#print(sender.Text)

	def BtnOkClick(self, sender, e):
		if self.viewPaste is not None:
			TransactionManager.Instance.EnsureInTransaction(doc)
			for i in self._ckListFilters.CheckedItems:
				#print(i)
				filterV = self._dictfilter.get(i)
				overGraph = self.curview.GetFilterOverrides(filterV.Id)
				self.viewPaste.AddFilter(filterV.Id)
				self.viewPaste.SetFilterOverrides(filterV.Id, overGraph)
			TransactionManager.Instance.TransactionTaskDone()
		self.Close()

	def BtnCancelClick(self, sender, e):
		self.Close()


f = MainForm()
Application.Run(f)
OUT = f.viewPaste
6 Likes

Thank you. I fixed it to my liking. I will leave my code below for anyone to want use. Wish you happy weekend
Copy AllFilters Form - BIMTech.py (6.7 KB)

2 Likes