Hide and Unhide elements in view

Hello all,

I am trying to hide or unhide all annotation symbol (of a particular family) in all views. The sorting works fine but View.HideElements and View.UnhideElements does not seem to be working correctly. Both nodes are from Archilab.

Waring from View.UnhideElements:
Traceback (most recent call last):
File “”, line 63, in
File “”, line 36, in ProcessListArg
File “”, line 36, in <lambda$1020>
File “”, line 46, in UnhideElements
Exception: The set of elements to be unhidden is empty.
Parameter name: elementIdSet

Waring from View.HideElements:
Traceback (most recent call last):
File “”, line 63, in
File “”, line 36, in ProcessListArg
File “”, line 36, in <lambda$1071>
File “”, line 46, in HideElements
Exception: The set of elements to be hidden is empty.
Parameter name: elementIdSet

This is the first run:
As you can see in the Flatten Node all elements are hidden and should work in View.UnhideElements but it tosses an error.
image

I hit run again (Second run): Now about half, the elements are listed as hidden. The Flatten node shows what elements were hidden last time since it has been fed new data. View.UnhideElements is still showing warnings.
image

Ren three: This time View.UnhideElements does not show a warning and it has finally unhidden all the elements.
image

Run four: Just to show that all elements have been unhidden.
image

When I change it to Hide elements I get the same thing in reverse order. Does anyone know what’s going on?
Here is a copy of the script if you want to test it.

Project Comments - No Plot Lines_online help upload.dyn (23.7 KB)
Capture

If you’re flattening your list of booleans then your list structure probably doesn’t match anymore. Turn on node previews so we can see what’s going on.

Edit:
Are you closing Dynamo between runs or just hitting Run over and over? That could cause some problems with transactions and not letting Dynamo regen.

Sorry the list structure does match. Elements.IsHiddenInView adds a nested list level. That is why I am flattening the list.

Apologies that my nodes didn’t work for you as you have expected. I made some changes to them below. Please give this a try:

Here’s the Dynamo Definition: 2_HideUnhide.dyn (31.4 KB)

Code for the IsHidden node:

# Copyright(c) 2018, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

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

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

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

def process_parallel_lists(_func, *lists):
    return map(lambda *xs: process_parallel_lists(_func, *xs) if all(type(x) is list for x in xs) else _func(*xs),*lists)

def IsHidden(e, v):
	element = UnwrapElement(e)
	view = UnwrapElement(v)
	return element.IsHidden(view)

try:
    errorReport = None
    output = process_parallel_lists(IsHidden, IN[0], IN[1])

except:
    import traceback
    errorReport = traceback.format_exc()

if None == errorReport:
    OUT = output
else:
    OUT = errorReport

Code for the improved Hide node:

# Copyright(c) 2018, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

import System
from System.Collections.Generic import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

def ProcessListArg(_func, _list, _arg):
    return map( lambda x: ProcessListArg(_func, x, _arg) if type(x)==list else _func(x, _arg), _list )

def HideElements(v, elements):
	view = UnwrapElement(v)
	ids = List[ElementId]()
	for i in elements:
		e = UnwrapElement(i)
		if not e.IsHidden(view) and e.CanBeHidden(view):
			ids.Add(e.Id)
	if ids.Count > 0:
		view.HideElements(ids)
	return None

if isinstance(IN[0], list):
	elements = IN[0]
else:
	elements = [IN[0]]

if isinstance(IN[1], list):
	views = IN[1]
else:
	views = [IN[1]]

if IN[2]:
	try:
		errorReport = None
		TransactionManager.Instance.EnsureInTransaction(doc)
		ProcessListArg(HideElements, views, elements)
		TransactionManager.Instance.TransactionTaskDone()
	except:
		import traceback
		errorReport = traceback.format_exc()
else:
	errorReport = "Set RunIt to True."

if errorReport == None:
	OUT = elements, views
else:
	OUT = errorReport

Code for the improved Unhide node:

# Copyright(c) 2018, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

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

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

import System
from System.Collections.Generic import *

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

def ProcessList(_func, _list):
    return map( lambda x: ProcessList(_func, x) if type(x)==list else _func(x), _list )

def ProcessListArg(_func, _list, _arg):
    return map( lambda x: ProcessListArg(_func, x, _arg) if type(x)==list else _func(x, _arg), _list )

def Unwrap(item):
	return UnwrapElement(item)

def UnhideElements(v, elements):
	view = UnwrapElement(v)
	ids = List[ElementId]()
	for i in elements:
		e = UnwrapElement(i)
		if e.IsHidden(view):
			ids.Add(e.Id)
	if ids.Count > 0:
		view.UnhideElements(ids)
	return None
	
if isinstance(IN[0], list):
	elements = IN[0]
else:
	elements = [IN[0]]

if isinstance(IN[1], list):
	views = IN[1]
else:
	views = [IN[1]]

if IN[2]:
	try:
		errorReport = None
		TransactionManager.Instance.EnsureInTransaction(doc)
		output = ProcessListArg(UnhideElements, views, elements)
		TransactionManager.Instance.TransactionTaskDone()
	except:
		import traceback
		errorReport = traceback.format_exc()
else:
	errorReport = "Set RunIt to True."

if errorReport == None:
	OUT = elements, views
else:
	OUT = errorReport

Good luck!

9 Likes

Thank Konrad,

The updates work perfectly. Don’t be sorry sorry about them not working how I expected. Your package and blog has saved me a lot of time over the last year.

Thank you very much,
Steven

This is excellent!.

Any chance it could run on linked elements too?

2 Likes

Can you post that as a request here: https://github.com/ksobon/archilab/issues

2 Likes