Select Legend in Browser and remove from Sheet

Hello Dynamo Friends!

I´m selecting Legends in the bowser.

image

I want to remove them from the sheet they are placed on. The Problem is, that I´m getting viewports of all legend instances in the project and I have no chance to get the sheet that my desired legend are placed on. I just can get all plans where these legends are placed, that does`nt help^^

Any ideas how i can delete this legends from the sheet?

Kind regards!

Similar thread from the past:

Get Viewport and Sheet from Legend - Revit - Dynamo (dynamobim.com)

Hello, hope this can help

Cordially
christian.stan

1 Like

nope, because I can´t get the sheet, sheetnumber or sheet ID from the desired legends!

If i can get the sheet I win :slight_smile:

Is there any chance the View.Viewport node has a list 5 items deep? If so these aren’t ALL legend viewports but the viewports associated to this legend.

If that isn’t the case, run the loop the other direction, starting with sheets first.

  • Start with selecting the legends as you have. Pull the ID from there.
  • Then get all the sheets, pull the viewports on each sheet, and get the view ID for each viewport.
  • For the sublists of viewports, compare the viewIDs, and if they are the same you now have the sheet the viewports are on, and you can delete the viewport.
1 Like

Hello @jacob.small

I can´t see how that can work out.

Let´s start with only one selected Legend:

It is placed on 28 sheets and so has 28 viewports and so 28 different sheets.
I can compare whatever i want, i can not find out which viewport is the one corresponding the Legend I have selected.

So the main problem is: The Legend selected in the browser gives me always the same view, no matter on which plan it is placed. So i have no chance to get a specific sheet from that view.

If you selected a different legend, you’d get different viewports and therefore different sheets.

It’s a one > many > one relationship.

It´s so easy doing it manually :smiley:

ezgif.com-gif-maker (2)

Can it really be true i can´t do that with dynamo…

Unfortunately that element there is the legend type used by the viewport, not the viewport itself. Unfortunately you can’t ask the sheet that is active for the viewport with that legend either, as the project browser registers as the active view whilst selecting from it.

You’d be best off getting a user to select the legend viewport on the sheet, then asking the viewport for it’s view, then getting all instances of that legend view instead.

2 Likes

@gerhard.p Like pointed out by others on the thread, I don’t think there is a straightforward way to remove those legends.

A workaround would be to select one or more views on the sheet along with legends and wait for the magic to happen.(No schedules allowed in the selection)
Remove Legends

Try this: Delete Legends Workaround.dyn (34.8 KB)

What if the sheet doesn’t have a view? You will have to resort to your trusty old manual method!

2 Likes

Hi Amol i have tried something and it just end up in the same as the projectbrowser…just with datashapes…not sure how smart it is :wink:

2 Likes

Thanks amol.

I have a graph that let´s you select Views on the sheet, in the browser or the section lines and then you can:

  • Duplicate to sheet
  • Duplicate with detailing to sheet
  • Duplicate multiple times to sheet
  • Duplicate as dependent to multiple sheets
  • Move to sheet

and so on. So thats a big graph where I had to consider a lot of things that are different when a view is selected in the browser, on the sheet, or as the section line. And Legends made a lot of trouble, but nearly everything works now.

The only thing that does not work is moving legends when selected in the browser. As you showed it works if another view has been selected, right! So my final solution will be to filter legends out if selected in the browser, thats a little disappointing :smiley:

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

You can try this.

1 Like

Hello Robert,

You are deleting all instances of the legend on all sheets, thats easy but not what i want to do.

And to your reply in the other thread, you are getting all the sheets where the viewports are placed, that doesn´t help me, the problem is i can`t get the right sheet from there because I´m in an active view (like in the other thread) or i selected the legend in the browser (like in this thread).

So the problem is always the same, i can´t get the specific sheet i need from the legend.

Very creative! :smiley:
For my purposes too complicated, i will for now stick to filtering legends out and add a user-message.

1 Like

if my understanding is correct your problem can be sorted as below.

Active legend on sheet then you can get the sheet number.