How to hide elements in view?

Did you fixed your error? By just looking at the images above maybe adding a flatten node after your elements list will help? Also is your active/current view on 3D…? If not try it after adding a flatten node. Tnx

I think there is an error in the code i posted - should be while not if.
But still couldn’t get it to work, so I tried a simpler bit of code. But it doesn’t work either, just returns null so I assume the code falls over somewhere in python.

2 lists are fed to it, one a list of views, the other a list of lists of elements.

The fact my counting variable cnt comes back null suggests it fails early on. Although i don’t know because python makes no sense to me.

Python code is:

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

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

doc = DocumentManager.Instance.CurrentDBDocument

view = UnwrapElement(IN[0])
elements = UnwrapElement(IN[1])

TransactionManager.Instance.EnsureInTransaction(doc)

	cnt = 0
	while cnt < len(view):
	    v = view[cnt]
   		ids = list()
    	helements = list()
 		for i in elements[cnt]:
			if not i.IsHidden(v) and i.CanBeHidden(v):
			ids.append(i.Id)
			helements = List[ElementId](ids)
		
		v.HideElements(helements)	
	cnt = cnt+1

TransactionManager.Instance.TransactionTaskDone()

OUT = (view,elements,cnt)

I can’t see anything wrong with the logic, I can only surmise is has something to do with the way python works. Is UnwrapElement wrong because it is applied to a list and/or a list of lists?
Am I missing an import?

After much forensic effort finally cracked it.
Python is a bitch to code in, one tab or space in the wrong place and caboom.
Also, by trial and error, I discovered List (with a capital) is not the same as list, and Add (also with a capital) is not the same as append. They must be defined in one of the imports.

Anyway here it is:

## 2017/07/20 Antony McPhee
## Hide list of elements per view. Accepts list of views and list of list of elements.
## for view[n] hide things listed in elements[n]

import clr
# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
import System
from System.Collections.Generic import *

doc = DocumentManager.Instance.CurrentDBDocument

views = UnwrapElement(IN[0])
elements = UnwrapElement(IN[1])

TransactionManager.Instance.EnsureInTransaction(doc)

v = list()
cnt = 0

while cnt < len(views):
	v = views[cnt]
	ids = List[ElementId]()
	for i in elements[cnt]:
		if not i.IsHidden(v) and i.CanBeHidden(v):
			ids.Add(i.Id)
	v.HideElements(ids)	
	cnt = cnt+1

TransactionManager.Instance.TransactionTaskDone()

OUT = views,elements
3 Likes

Nice work!

A bit more to clarify the difference btw List and list in current context.

List is available after import System and from System.Collections.Generic import *. Essentially it is a ironPython .NET integration. Check this out. Why we need it? Because v.HideElements() needs an ICollection input argument. See documentation here.

list is a built-in ironPython data type. list(x) function constructs a list based on an iterable x. There are other ways of use list too. I personally like list literal and list comprehension.

Correspondingly Add() is in .NET List land here, append() is in ironPython land here. They work in a similar fashion though.

I could use a little help with this one as well. I am trying to create a script that will hide groups on a drafting view/sheet. My intention is to filter groups on a drafting view by the key word “Pm notes” within the group type name. I seem to have the filtering for the groups and the drafting views coordinated however I am getting an error within the archi-lab view.hide elements node. Can you guys help. One post mentioned something about feeding it a list of names so I tried feeding it elementId’s and that didn’t work. This latest iteration is intended to combine element Id with type name prior to filtering but no luck. I also just tried feeding it 1 element Id via the element select node and one view also. It always gives me this same error.

This used to do the job, but doesn’t seem to work in Revit 2019. Any thoughts why?
Update:
This has been solved with updated code
https://forum.dynamobim.com/t/hide-and-unhide-elements-in-view/29825

Please start a new topic, link back to this one, and post some images showing what the errors are. This topic is over a year old and as a solution which has to ‘live on’ for others to refer to.