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

问题描述 投票:0回答:3

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

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

这对于没有嵌套族实例的族来说效果很好。但是如果我的项目中有A族的实例,并且A族本身包含B族的实例,那么这段代码不会获取B族的实例。我如何获取B族的实例?

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

revit-api
3个回答
7
投票

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);
                                }
                            }
                        }
                    }
                }
            }

4
投票

我通常会大量使用 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();

0
投票

我有一个包含嵌套家庭的家庭实例。但是,当我调用方法 a.GetSubComponentIds() 时,它返回 null。你知道为什么会这样吗?

© www.soinside.com 2019 - 2024. All rights reserved.