Select Legend in Browser and remove from Sheet

A solution is to recreate the SheetViews Tree on a treeview with DataShape or Python (Wpf or Winform) and set the selection in this UI

example
delete viewports by view

Python code (compatible to both engine)
import sys
import clr

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

from System.Collections.Generic import List, IList, Dictionary
clr.AddReference("System.Core")
from System.Linq import Enumerable

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

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

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

class FormSheet(Form):
	def __init__(self):
		self.allSheet = FilteredElementCollector(doc).OfClass(ViewSheet).WhereElementIsNotElementType().ToElements()
		self.selectVP = []
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._treeView1 = System.Windows.Forms.TreeView()
		self._label1 = System.Windows.Forms.Label()
		self._buttonOK = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# treeView1
		self._treeView1.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right
		self._treeView1.CheckBoxes = True
		self._treeView1.Location = System.Drawing.Point(13, 32)
		self._treeView1.Name = "treeView1"
		lstParentNode = []
		for sheet in self.allSheet:
			filterFunc = System.Func[DB.Element, System.Boolean](lambda x : doc.GetElement(x.ViewId).ViewType == ViewType.Legend)
			lstVP = list(Enumerable.Where[DB.Element](FilteredElementCollector(doc, sheet.Id).OfCategory(BuiltInCategory.OST_Viewports), filterFunc))
			if len(lstVP) > 0:
				lstchildNode = []
				for vp in lstVP:
					legend =  doc.GetElement(vp.ViewId)
					treeNodeChild = System.Windows.Forms.TreeNode(legend.Name)
					treeNodeChild.Name = legend.Name
					treeNodeChild.Text = 'Legend : ' + legend.Name
					treeNodeChild.Tag = vp
					lstchildNode.append(treeNodeChild)
				treeNodeParent = System.Windows.Forms.TreeNode(sheet.SheetNumber + ' : ' + sheet.Name, System.Array[System.Windows.Forms.TreeNode](lstchildNode))
				lstParentNode.append(treeNodeParent)
		self._treeView1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](lstParentNode))
		self._treeView1.Size = System.Drawing.Size(319, 447)
		self._treeView1.TabIndex = 0
		self._treeView1.NodeMouseClick += self.TreeView1Click
		# 
		# label1
		self._label1.Location = System.Drawing.Point(13, 3)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(319, 23)
		self._label1.Text = "Select Legends"
		# 
		# buttonOK
		self._buttonOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right
		self._buttonOK.Location = System.Drawing.Point(247, 490)
		self._buttonOK.Name = "buttonOK"
		self._buttonOK.Size = System.Drawing.Size(85, 30)
		self._buttonOK.Text = "OK"
		self._buttonOK.UseVisualStyleBackColor = True
		self._buttonOK.Click += self.ButtonOKClick
		# 
		# FormSheet
		self.ClientSize = System.Drawing.Size(344, 532)
		self.Controls.Add(self._buttonOK)
		self.Controls.Add(self._label1)
		self.Controls.Add(self._treeView1)
		self.Name = "FormSheet"
		self.Text = "FormSheet"
		self.ResumeLayout(False)
		
	def TreeView1Click(self, sender, e):
		for cNode in e.Node.Nodes: 
			cNode.Checked = e.Node.Checked
	
	def ButtonOKClick(self, sender, e):
		for pNode in self._treeView1.Nodes:
			for cNode in pNode.Nodes:
				if cNode.Checked:
					self.selectVP.append(cNode.Tag)
		self.Close()
		
objForm = FormSheet()
objForm.ShowDialog()
OUT = objForm.selectVP
3 Likes