代码之家  ›  专栏  ›  技术社区  ›  tdykstra

如何在Revit API中检索嵌套族实例

  •  1
  • tdykstra  · 技术社区  · 9 年前

    我正在使用FilteredElementCollector检索族实例:

        var collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
        var familyInstances = collector.OfClass(typeof(FamilyInstance));
    

    这对于没有嵌套族实例的族很有用。但是,如果我在项目中有族A的实例,而族A本身包含族B的实例,则此代码无法获取族B的示例。如何获取族B实例?

    我是Revit API的新手,似乎必须有一个简单的解决方案,但我在网上找不到。我正在使用Revit 2015,如果这会有所不同的话。

    2 回复  |  直到 9 年前
        1
  •  6
  •   alital AlexPera    9 年前

    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);
                                    }
                                }
                            }
                        }
                    }
                }
    
        2
  •  4
  •   Magson Leone    9 年前

    我经常与Linq一起工作,以留下更简洁的代码。请参见以下示例:

    List<Element> listFamilyInstances = new FilteredElementCollector(doc, doc.ActiveView.Id)
                .OfClass(typeof(FamilyInstance))
                .Cast<FamilyInstance>()
                .Where(a => a.SuperComponent == null)
                .SelectMany(a => a.GetSubComponentIds())
                .Select(a => doc.GetElement(a))
                .ToList();