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

时间:2015-03-28 20:25:29

标签: revit-api

我正在使用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 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

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