familyInstance将具有活动视图中所有族的列表(包括嵌套族和非嵌套族)。
您需要做的是遍历每个FamilyInstance,看看它是否已经是根族(即包含嵌套族)或嵌套族或没有。类似于:
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);
}
}
}
}
}
}