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?
Solved! Go to Solution.
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?
Solved! Go to Solution.
Solved by GonçaloFeio1321. Go to Solution.
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();
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();
@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?
@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?
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.
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.
Exactly what I was looking for. I had the same thought in my head but was missing a few notations. Thanks again!
Exactly what I was looking for. I had the same thought in my head but was missing a few notations. Thanks again!
Can't find what you're looking for? Ask the community or share your knowledge.