Family Element Visibility Settings Python

#1

Can someone help me use this the Revit API to toggle Family Element Visibility settings.
Specifically I would like to be able to have access to the "Visibility/Graphics Override detail levels.

Family%20Element%20Visibility

In the API docs I think the class to use is called FamilyElementVisibility Class that you can see here http://www.revitapidocs.com/2020/fae58e2d-817c-77f6-1747-58b0a4e01c7a.htm

I have written this python code using the class but I think I’m using it incorrectly…

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

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

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

clr.AddReference('DSCoreNodes')
import DSCore
from DSCore.List import *

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

# Import List ( ICollection(ElementId) = List[ElementId]() )
clr.AddReference("System")
from System.Collections.Generic import List 

def tolist(obj):
	if isinstance(obj, list):
		return UnwrapElement(obj)
	else:
		return [UnwrapElement(obj)]

#The inputs to this node will be stored as a list in the IN variables.
input = tolist(IN[0])
bool = tolist(IN[1])


doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

TransactionManager.Instance.EnsureInTransaction(doc)



a = FamilyElementVisibility.IsShownInFine(input)


OUT = a


TransactionManager.Instance.TransactionTaskDone()

I’ve attached a version 2.1.0 dyanmo file and a sample family Revit file to use. Below is also an example of my graph.

rac_basic_sample_family.rfa (296 KB)

Family Visibilty.dyn (7.4 KB)

Can anyone help me use this class correctly or have any advice on how I can change these family element settings using dynamo?

Thanks!

#2

I don’t have Revit 2019 or Dynamo 2.1 so I made my own in 2018 with Dynamo 2.0.2.

So this is the very basic (slightly janky) version that will work.

import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import * 
doc = DocumentManager.Instance.CurrentDBDocument

def tolist(obj):
	if isinstance(obj, list):
		return obj
	else:
		return [obj]


elems = tolist(UnwrapElement(IN[0]))
bools = tolist(IN[1])

out = []

for elem, b in zip(elems, bools):
	TransactionManager.Instance.EnsureInTransaction(doc)
	try:
		fev = FamilyElementVisibility(FamilyElementVisibilityType.Model)
		fev.IsShownInFine = b
		elem.SetVisibility(fev)
	except:
		fev = FamilyElementVisibility(FamilyElementVisibilityType.ViewSpecific)
		fev.IsShownInFine = b
		elem.SetVisibility(fev)
	TransactionManager.Instance.TransactionTaskDone()
	out.append(True)

OUT = out

There are two types of FamilyElementVisibility classes, one for 3d/multiple view and one for view specific elements but I couldn’t use the property .ViewSpecific to figure out which it should be, it would always just return false even when it is supposed to be true. So I just made it into a try/except statement.

I used your code as a base so there are other limitations on it from that. For example, it is assuming you are entering two equal sized inputs of elements and booleans. If you want to do multiple elements with only a single false boolean, then the script has to be changed.

familyelementvisibility

2 Likes
#3

Can I ask what in particular was limiting from my code? I’m not in anyway attached to the way I wrote it.

I have a decent grasp of python but when using the Revit API docs I still do not understand how it works… How to find the function you want to use and then correctly implement it in Python. After weeks of trail and error I don’t feel like I am getting a better grasp using the API.

How did you know to use elem.SetVisibility(fev)?
I don’t see “SetVisibility” in the Revit API Doc FamilyElementVisibility Class.

#4

Sorry, was gone for long weekend. The only part limiting your code was mostly because I was too lazy to do things like checking for equal length lists or using a single bool to work for the entire input of elements and also things like error checking.

The try/except statement is also partly lazy coding on my half because I couldn’t find a better solution.

As for learning API, honestly I just learned by writing random scripts to try out different things. Any time I got an error, I would google/search the forum or look for similar things done in nodes that have their code released. Took me almost half a year to feel even a little confident that I could write to API, but a lot of that time was just reading through the API docs. Treat it like a wikipedia, where you click on a random class, read through its methods/properties, click on the output of one of their functions, and do it over and over again until you get bored.

As for finding elem.SetVisibility, a couple things. One is that the command in Revit is a per element property, so it isn’t a document-wide function but on the element level. To be honest, since it is only available in the family docs, I searched the family doc related classes first to see if it was attached to something like FamilyManager.

Anyways, the class FamilyElementVisibility says it “provides access to … of family elements”. That is also a hint that it is probably used in conjunction with an element related method. It isn’t family instance related because we are inside the family. So I went through the element class and looked for visibility-related methods and I found it.

But for full disclosure, after I found it, I realized I spent way too much effort. A lot of classes on the API docs have sample code written on how to use that class. On the FamilyElementVisibility class the sample code shows the .SetVisibility function in use.

1 Like