Get all family from a specific category

Get all family from a specific category

duongphong29
Enthusiast Enthusiast
6,741 Views
8 Replies
Message 1 of 9

Get all family from a specific category

duongphong29
Enthusiast
Enthusiast

I'm trying to get all family from a specific category then I can use GetFamilySymbolIds() to obtain all Ids of family symbols in that family. I just simply think like this:

 

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralColumns).OfClass(Family).ToElements()

 

In that case, I'm trying to get all family in category Structural Columns. But it gives me an empty list. 

  

Here is my code to get ids and names of family in a category:

 

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralColumns).OfClass(Family).ToElements() #Get all family in current project


for m in collector:
    symbols_ids = list(m.GetFamilySymbolIds())
    for i in symbols_ids:
        famsymbol = doc.GetElement(i)
        symbolName = famsymbol.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
        print (symbolName + str(i))

 

Is there a way for me to do this? 

 

Thank you in advance.

 

P/s: I am using revit 2021

0 Likes
Accepted solutions (4)
6,742 Views
8 Replies
Replies (8)
Message 2 of 9

architect.bim
Collaborator
Collaborator
Accepted solution

Hi! If you need to get all families just get all symbols with WhereElementIsElementType method. And then use set object to get unique family ids. That's all.

family_symbols = FilteredElementCollector(doc) \
    .OfCategory(BuiltInCategory.OST_StructuralColumns) \
    .WhereElementIsElementType()

family_ids = set(symbol.Family.Id for symbol in family_symbols)

P.S. Don't pass Families in set instead of Family Ids. It won't give you unique items.


Maxim Stepannikov | Architect, BIM Manager, Instructor
Message 3 of 9

franciscopossetto
Advocate
Advocate
Accepted solution

Hey,

 

I have selected some families by Id and explored their properties using Revit Lookup. The parameter "Category" is null. The property where the Family stores their Category is called FamilyCategory. Maybe this is the reason why your collector does not return elements.

 

Alternatively, I think you could achieve the same result by doing it this way:

 

1. Get all Family Symbols of some Category:

 

 

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsElementType()

 

 

2. If needed,  Filter the Family Symbols by the FamilyName:

 

 

fam_symbols_by_fam_name= list(filter(lambda x: (x.FamilyName == "Basic Wall"), collector)) 

 

 

I hope it helps.

Kind regards.

 

----------------------------------

 

EDITED:

I hadn't seen the @architect.bim  answer. Sorry for the very similar response.

Github:
https://github.com/franpossetto
Message 4 of 9

duongphong29
Enthusiast
Enthusiast
Accepted solution

Thank you very much

 

Thank to you, I have found a way to reach my purpose.

 

For those who came after, this is the way to get all family type names and ids. Results shown in picture 1

 

family_symbols = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_StructuralColumns).WhereElementIsElementType() 

for symbol in family_symbols:
    family_id = symbol.Id
    family_name = symbol.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
    print(family_name + " " + str(family_id))

 

Thank you very much.

 

Message 5 of 9

jeremy_tammik
Alumni
Alumni
Accepted solution

You have encountered a limitation discussed by The Building Coder twelve years ago:

 

https://thebuildingcoder.typepad.com/blog/2009/01/family-category-and-filtering.html

 

Here are some subsequent further notes on this:

 

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 6 of 9

rhanzlick
Advocate
Advocate

looks like this already has an accepted solution that seems to work for the OP. I was searching for the same thing, but to see if the ElementCategoryFilter would dynamically work (i.e. *.WherePasses(new ElementCategoryFilter(bic)) in lieu of the shortcut *.OfCategory(bic)).

 

I have not rigorously tested benchmarks, but for posterity, my preferred syntax should yield the same results:

 

public List<string> GetFamilySymbolsByCategory (Document doc, BuiltInCategory bic)
{
    return new FilteredElementCollector(doc)
        .OfClass(typeof(Family))
        .Cast<Family>()
        .Where(fam => fam.FamilyCategoryId == new ElementId(bic))
        .SelectMany(f => f.GetFamilySymbolIds())
        .Select(symbolId => doc.GetElement(symbolId).Name)
        .ToList();
}

 

This syntax is more verbose than the accepted answer, but it aligns slightly closer with your original thinking. Ultimately, it should yield the same results as the accepted answer. 👍

Message 7 of 9

jeremy_tammik
Alumni
Alumni

Thank you for the new answer. Using the shortcut should bei slightly more efficient,. or exactly the same.

   

By the way...

  

Once upon a time, in the far distant past, I introduced a personal convention of using the abbreviation `bip` for built-in parameter variables, and `bic` for built-in categories. So, seeing you name your built-in category argument `bip` is a bit weird for me. Not that it matters in any way whatsoever, but still...

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 8 of 9

rhanzlick
Advocate
Advocate

I’m an avid reader of the blog, so I’m sure I’ve seen the convention there. I wasn’t trying to make waves breaking the convention, just typed it quickly in the middle of another project🫣. The intention is just that it’s the abbreviated acronym… fixed it so as not to hurt any sensibilities😅

0 Likes
Message 9 of 9

jeremy_tammik
Alumni
Alumni

🙂

  

As said, it does not matter in any way whatsoever. I like super short local variable names. But only local ones, used only within a handful of lines. Everything beyond that I prefer to give a descriptive name. So, in this case, any short name would do equally well.

  

Thank you for your care.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes