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

tdy*_*tra 1 revit-api

我正在使用 FilteredElementCollector 来检索家庭实例:

    var collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
    var familyInstances = collector.OfClass(typeof(FamilyInstance));
Run Code Online (Sandbox Code Playgroud)

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

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

ali*_*tal 6

familyInstances 将包含活动视图中所有系列的列表(包括嵌套和非嵌套的)。

您需要做的是遍历每个 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);
                                }
                            }
                        }
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)