【问题标题】:How to retrieve nested family instances in Revit API如何在 Revit API 中检索嵌套族实例
【发布时间】:2015-03-28 20:25:28
【问题描述】:

我正在使用 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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2019-03-29
        • 2019-11-28
        相关资源
        最近更新 更多