Filter Family and Category at the same time using RevitAPI

Hi,

I am querying all loaded families of a specific category (ex. families of pipe fittings) but I cannot do it in FilteredElementCollector of RevitDB API. I am coding in python.

I tried the code below but I got all the families in Revit with different category:
FilteredElementCollector(doc).OfClass(Family).WhereElementIsNotElementType().ToElements()

I also tried the code below but got all the family types for a specific category.
FilteredElementCollector(doc).OfClass(FamilySymbol).OfCategory(BuiltInCategory.OST_Doors).ToElements()

But when I tried the code below, somehow I get an empty list.
FilteredElementCollector(doc).OfClass(Family).OfCategory(BuiltInCategory.OST_Doors).ToElements()

Is there any way to do querying all the loaded families of a specific category using FilteredElementCollector?

Thank you in advance.
-biboy

Do you mean like that? You don’t need OfClass if you want to look up all elements.

FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Doors).ToElements()

Hi @Deniz_Maral

That will query all the elements of the catevory, not the family.

-biboy

Hi @blsalvio

You could try the other way round first by BuiltInCategory and then ByClass:

1 Like

Hi @Kulkul

Based from the picture you attached, you are querying family types. Is there a way to query the base family? I can query the family of categories now but not of a specific category. I have to add a for loop to query families of a specific category. If possible, I want to use one line only because I am doing 70k pipes and 70k fittings. A lot of for loop may slow my script.

-biboy

Here is the one line code :slight_smile:

import clr

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

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


OUT = [f.Family for f in FilteredElementCollector(doc).OfClass(FamilySymbol).OfCategory(BuiltInCategory.OST_Doors).ToElements()]

2 Likes

Thanks, same with my method, I guess there is no other way. The master has already said the best way :joy:

Another way

import clr
import sys
import System
from System.Collections.Generic import List
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

filter = System.Predicate[System.Object](lambda x : x.FamilyCategory.Id == ElementId(BuiltInCategory.OST_Doors))
fec = List[DB.Element](FilteredElementCollector(doc).OfClass(Family).ToElements()).FindAll(filter)

OUT = fec
5 Likes

This is the solution Ive been looking for. Tested it in c#, it gives me a faster way of querying families.

2 Likes

@blsalvio And here is another method with one line code slightly different than @c.poupin :slight_smile:

6 Likes

hi @Kulkul

Thank you very much. Your code differs with @c.poupin a little bit but when I convert it to c#, it is exactly the same. I am just doing scriptlets in python and if successful, I implement it in c#. Right now, my job is to optimize code and make the existing scripts and nodes faster and efficient to handle hundreds of thousands of elements in one model.

Again, I cannot thank you enough for the suggestions you have given me.

-biboy

3 Likes