Collect Family By Name and Type to Place

Collect Family By Name and Type to Place

rvtquestions
Advocate Advocate
5,581 Views
4 Replies
Message 1 of 5

Collect Family By Name and Type to Place

rvtquestions
Advocate
Advocate
 
FamilySymbol famSym = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).Where(q => q.Name == "X").First() as FamilySymbol;

 What is the best way to add on to or revise this line of code to be more specific about which family to collect. Currently it gets family by its name but I would also like it to retrieve by specific type like Family Name "X" Type "1". Any help?

 

0 Likes
Accepted solutions (1)
5,582 Views
4 Replies
Replies (4)
Message 2 of 5

MarryTookMyCoffe
Collaborator
Collaborator

first this is not good way to find a family type, because you never know from with Family you get type.

There can be many FamilySymbols with that same name. you can try:
A use

FamilySymbol famSym = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).Where(q => q.Family.Name == "X").First() as FamilySymbol;

//where X is Family name, not family type name.
 

and If you what specific type find Family first and then use:
newlist = family.GetFamilySymbolIds().ToList();

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes
Message 3 of 5

rvtquestions
Advocate
Advocate

@MarryTookMyCoffe Thanks for the suggestion. Your last piece of code suggests a list of returned ids. If Family X has multiple types such as 1,2,3,4,5 how would one pick out only a specific type?

0 Likes
Message 4 of 5

GonçaloFeio1321
Enthusiast
Enthusiast
Accepted solution

Without Linq:

 

 

        public FamilySymbol GetSymbol(Document document, string familyName, string symbolName)
        {
            using (var collector = new FilteredElementCollector(document))
            {
                using (var families = collector.OfClass(typeof(Family)))
                {
                    foreach(Family family in families)
                    {
                        if (family.Name == familyName)
                        {
                            foreach (var symbolId in family.GetFamilySymbolIds())
                            {
                                var symbol = document.GetElement(symbolId) as FamilySymbol;
                                if (symbol.Name == symbolName)
                                {
                                    return symbol;
                                }
                            }
                        }
                    }
                }
            }

            return null;
        }

 

And with Linq

 

 

        public FamilySymbol GetSymbol(Document document, string familyName, string symbolName)
        {
            return new FilteredElementCollector(document).OfClass(typeof(Family)).OfType<Family>().FirstOrDefault(f => f.Name.Equals(familyName))?.GetFamilySymbolIds().Select(id => document.GetElement(id)).OfType<FamilySymbol>().FirstOrDefault(symbol => symbol.Name.Equals(symbolName));
        }

You can benchmark those two, but my pick is easy.

 

Message 5 of 5

rvtquestions
Advocate
Advocate

@GonçaloFeio1321

 

Exactly what I was looking for. I had the same thought in my head but was missing a few notations. Thanks again!

0 Likes