I'm using a FilteredElementCollector to retrieve family instances:
var collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
var familyInstances = collector.OfClass(typeof(FamilyInstance));
This works fine for families that don't have nested family instances. But if I have in the project instances of family A, and family A itself includes instances of family B, this code doesn't get the instances of family B. How do I get the family B instances?
I'm new to Revit API and it seems like there must be a simple solution but I couldn't find one online. I'm using Revit 2015 if that makes a difference.
the familyInstances will have a list of all the families in the active view (including nested and non-nested ones).
what you need to do is iterate through each FamilyInstance and see if it is already a root family (ie contains nested families) or a nested family or none. something like:
var collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
var familyInstances = collector.OfClass(typeof(FamilyInstance));
foreach (var anElem in familyInstances)
{
if (anElem is FamilyInstance)
{
FamilyInstance aFamilyInst = anElem as FamilyInstance;
// we need to skip nested family instances
// since we already get them as per below
if (aFamilyInst.SuperComponent == null)
{
// this is a family that is a root family
// ie might have nested families
// but is not a nested one
var subElements = aFamilyInst.GetSubComponentIds();
if (subElements.Count == 0)
{
// no nested families
System.Diagnostics.Debug.WriteLine(aFamilyInst.Name + " has no nested families");
}
else
{
// has nested families
foreach (var aSubElemId in subElements)
{
var aSubElem = doc.GetElement(aSubElemId);
if (aSubElem is FamilyInstance)
{
System.Diagnostics.Debug.WriteLine(aSubElem.Name + " is a nested family of " + aFamilyInst.Name);
}
}
}
}
}
}