简体   繁体   English

在Revit API中按族进行过滤

[英]Filtering by a Family in Revit API

For a while now I have been looking for a way to filter elements by their family. 一段时间以来,我一直在寻找一种按其家庭筛选元素的方法。 For example, I want all of the elements that are of family Junction Boxes - Load. 例如,我想要属于“接线盒-负载”系列的所有元素。 I am somewhat new to the Revit API, and I do know about category filters and even a family instance filter. 我对Revit API有点陌生,而且我确实了解类别过滤器,甚至是家庭实例过滤器。 But I do not understand how I get the family symbol for the Junction Boxes - Load family, for example. 但是我不明白如何获得接线盒的系列符号-例如负载系列。 Is there even a good way of doing this? 有没有这样做的好方法? Or am I better off filtering specific Types within the family? 还是我最好过滤掉家庭中的特定类型? If so, how would I go about doing this? 如果是这样,我将如何去做?

Welcome to Revit programming! 欢迎使用Revit编程!

The most effective way to get started learning and programming with the Revit API, including installation and use of the SDK, is to work through the getting started material, especially the step-by-step instructions provided by the DevTV and My First Revit Plugin video tutorials: 开始使用Revit API进行学习和编程(包括安装和使用SDK)的最有效方法是阅读入门材料,尤其是DevTV和My First Revit插件视频提供的分步说明。教程:

http://thebuildingcoder.typepad.com/blog/about-the-author.html#2 http://thebuildingcoder.typepad.com/blog/about-the-author.html#2

To answer your question: you use filtered element collectors to retrieve elements from the Revit database: 要回答您的问题:您可以使用过滤的元素收集器从Revit数据库中检索元素:

http://www.revitapidocs.com/2017/263cf06b-98be-6f91-c4da-fb47d01688f3.htm http://www.revitapidocs.com/2017/263cf06b-98be-6f91-c4da-fb47d01688f3.htm

You can apply many filters to them. 您可以对它们应用许多过滤器。 In your case, you can directly filter for FamilySymbol instances. 对于您的情况,您可以直接过滤FamilySymbol实例。

There are two ways to limit the search further to retrieve only those you are interested in, named "Junction Boxes - Load" : 有两种方法可以进一步限制搜索,以仅检索您感兴趣的那些,即"Junction Boxes - Load"

  • Efficient: use a parameter filter 高效:使用参数过滤器
  • Simple: use .NET post-processing or LINQ 简单:使用.NET后处理或LINQ

More details are provided abundantly by The Building Coder in the topic group on Retrieving Elements Using FilteredElementCollector . 建筑编码人员在有关使用FilteredElementCollector检索元素的主题组中提供了更多详细信息。

Jacob, 雅各布

Jeremy's answer is the right one. 杰里米的答案是正确的。 Here's an example of code that could be used to get all Family Instances of certain Family name: 这是一个代码示例,可用于获取某些姓氏的所有Family Instance:

The efficient way that Jeremy mentioned would be to use the Parameter Filter: Jeremy提到的有效方法是使用参数过滤器:

var famType = new FilteredElementCollector(m_doc)
    .OfClass(typeof(Family)) // This is called a class filter
    .FirstOrDefault(x => x.Name == "YourFamilyName");

if (famType != null)
{
    const BuiltInParameter testParam = BuiltInParameter.ELEM_FAMILY_PARAM;
    var pvp = new ParameterValueProvider(new ElementId((int)testParam));
    var fnrv = new FilterNumericEquals();
    var ruleValId = famType.Id;
    var paramFr = new FilterElementIdRule(pvp, fnrv, ruleValId);
    var epf = new ElementParameterFilter(paramFr);

    var results = new FilteredElementCollector(m_doc)
        .OfClass(typeof(FamilyInstance))
        .WherePasses(epf)
        .ToElements();
}

The less efficient way would be to do this: 效率较低的方法是这样做:

var result = new FilteredElementCollector(m_doc)
    .OfClass(typeof(FamilyInstance))
    .Cast<FamilyInstance>()
    .Where(x => x.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString() == "YourFamilyName");

I know that Jeremy mentioned that the second method would be less efficient, but I am personally not entirely sure. 我知道杰里米提到第二种方法效率不高,但是我个人并不完全确定。 The ElementParameterFilter is a Slow Filter which expands all of the elements in memory anyways. ElementParameterFilter是一个慢速过滤器,无论如何都会扩展内存中的所有元素。 Event though it's coupled with two Quick Filters for Family and FamilyInstance selection, that's still a considerable overhead as opposed to the more direct approach that you can take using LINQ. 尽管事件与用于FamilyFamilyInstance选择的两个快速筛选器结合在一起,但与使用LINQ可以采用的更直接的方法相比,这仍然是相当大的开销。

You also asked about being able to select FamilySymbol 您还询问了如何选择FamilySymbol

var famType = new FilteredElementCollector(m_doc)
        .OfClass(typeof(FamilySymbol))
        .FirstOrDefault(x => x.Name == "YourFamilyTypeName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM