Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Collect Family By Name and Type to Place

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
rvtquestions
4772 Views, 4 Replies

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

Collect Family By Name and Type to Place

 
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?

 

4 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

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
Tags (2)
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

@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?

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.

 

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

@GonçaloFeio1321

 

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.

Post to forums  

Autodesk Design & Make Report