Get List of Object Styles from Families in Directory

Wanted to share this graph for getting all of the used Object Line Styles (Subcategories) for all Families in a Directory.

#Sean Page, 2021
import clr

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

clr.AddReference('System')
from System.Collections.Generic import List

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

App = DocumentManager.Instance.CurrentUIApplication.Application
#List of Directory Paths
paths = IN[0]

#Create an empty list for the values
ObjectStyles = []

#Loop each family path 
for path in paths:
	#Open the Family
	famDoc = App.OpenDocumentFile(path)
	#Get all Lines in the Family
	lines = FilteredElementCollector(famDoc).OfCategory(BuiltInCategory.OST_Lines).ToElements()
	subs = []
	#Loop each line to get the Subcategory or Object Style of the Line
	for line in lines:
		sub = line.LookupParameter("Subcategory").AsElementId()
		#Get the Name of the Object Style
		subname = famDoc.GetElement(sub).Name
		#Check to see if the item already exists in the List so we only show Unique Styles
		if not subs.Contains(subname):
			#Append it to the list if it isn't already found
			subs.append(subname)
	#Add the List of Object Syles for that Family to the Overall List
	ObjectStyles.append(subs)
	#Close the Family Document without saving	
	famDoc.Close(False)

#Output the List of Object Styles by Family
OUT = ObjectStyles

List Object Styles.dyn (9.6 KB)

9 Likes

Nice.
Of course it would be good to add the referring rfa name to the list so you can know where to find the rogue family with the non-compliant object style.

1 Like

Absolutely, that would be easy to do, but you also have the list of Families right next too it that you can reference. I often try to make my outputs singular so that they are easier to manipulate downstream if you need to do further operations like a Contains or == if you are searching for a specific thing.

It does bomb out if a file has to be upgraded to a later version.
Looking at it.
(We have stuff from 2018 to 2022. But typically use 2020.)

image

Yes, just about like any other file operation it will only work with files that are the same as the current version or older. I am working in R2022 so I wasn’t concerned about that.

The following code would wrap all of it in a try statement which should prevent the error, but I have not tested.

#Sean Page, 2021
import clr

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

clr.AddReference('System')
from System.Collections.Generic import List

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

App = DocumentManager.Instance.CurrentUIApplication.Application
#List of Directory Paths
paths = IN[0]

#Create an empty list for the values
ObjectStyles = []

#Loop each family path 
for path in paths:
	try:
		#Open the Family
		famDoc = App.OpenDocumentFile(path)
		#Get all Lines in the Family
		lines = FilteredElementCollector(famDoc).OfCategory(BuiltInCategory.OST_Lines).ToElements()
		subs = []
		#Loop each line to get the Subcategory or Object Style of the Line
		for line in lines:
			sub = line.LookupParameter("Subcategory").AsElementId()
			#Get the Name of the Object Style
			subname = famDoc.GetElement(sub).Name
			#Check to see if the item already exists in the List so we only show Unique Styles
			if not subs.Contains(subname):
				#Append it to the list if it isn't already found
				subs.append(subname)
		#Add the List of Object Syles for that Family to the Overall List
		ObjectStyles.append(subs)
		#Close the Family Document without saving	
		famDoc.Close(False)
	except:
		ObjectStyles.append(None)

#Output the List of Object Styles by Family
OUT = ObjectStyles

Yes. Just what I did + added path.
Adding on files in python so it doesn’t need dynamo for pyRevit. Will post later.

Do you have something to delete or change the object style name? :heart_eyes:

I think you should be able to set the name the same as getting it.

2 Likes

Theres no nodes for this is there? I need to open a bunch of families. Change the object style name if its not the searched for term, save and close. Sometimes there will be multiple line types which will have to be deleted to make it only one.

1 Like

I contemplated doing this as a part of the process, but unfortunately I had so many variations I decided it would be much easier to just attack the rogue ones manually and thus stopped here, but I didn’t find nodes specifically for this no.