revit二次开发之 过滤器一

        Revit通过过滤器来区分不同的元素,这些过滤器为应用程序获取不同的元素提供了方便灵活的接口。在使用过滤器的过程中,会应用到一个常用元素FilteredElementCollector类,我们称之为“收集器”,指定对应的收集器以后,再传入对用的过滤器,对需要的元素进行过滤。

1、收集器

常用的收集器,有以下几个:

1.1 元素收集器,FilteredElementCollector

   通过传入文档、视图和元素集合,实现对元素进行收集。

FilteredElementCollector collector = new FilteredElementCollector(doc);
 collector.WherePasses(new ElementClassFilter(typeof(FamilyInstance)));
var sphereElements = from element in collector where element.Name == "sphere" select element;
if (sphereElements.Count() == 0)
{
            TaskDialog.Show("Error", "Sphere family must be loaded");
            return;
 }

1.2 工作集元素收集器,FilteredWorksetCollector

   用于在工作集中,文档中过滤出指定工作集的元素,其代码如下:

FilteredWorksetCollector col = new FilteredWorksetCollector(doc);
            _AllWorksetList = col.Where(p => p.Kind == WorksetKind.UserWorkset).OrderBy(p => p.Name).ToList();
            foreach (Workset ws in _AllWorksetList)
            {
                checkedListBox1.Items.Add(ws.Name);
                _SumCount++;
 }

收集器通过方法WherePasses实现对元素的过滤。

2、过滤器

revit的过滤器无非三种类别:

Autodesk.Revit.DB..::..ElementFilter
    Autodesk.Revit.DB..::..ElementLogicalFilter
    Autodesk.Revit.DB..::..ElementQuickFilter
    Autodesk.Revit.DB..::..ElementSlowFilter

2.1 ElementQuickFilter元素快速过滤器

 

名称    

类型

作用

备注

BoundingBoxContainsPointFilter

快速过滤器

其边界盒包含了给定点的元素

无

BoundingBoxIntersectsFilter

快速过滤器

与指定边界盒相交的元素

无

BoundingBoxIsInsideFilter

快速过滤器

在指定边界盒之内的元素

无

ElementCategoryFilter

快速过滤器

通过指定的类别过滤

无

ElementClassFilter

快速过滤器 

指定元素类进行过滤

无

ElementDesignOptionFilter 

快速过滤器

元素的详细程度过滤

无

ElementIsCurveDrivenFilter

快速过滤器

元素是线驱动过滤器

无

ElementIsElementTypeFilter

快速过滤器

指定元素类型进行驱动 

无

ElementMulticategoryFilter 

快速过滤器

元素的多类别过滤 

无

ElementMulticlassFilter

快速过滤器

元素的多类过滤 

无

ElementOwnerViewFilter

快速过滤器

视图检索

无

ElementStructuralTypeFilter 

快速过滤器

元素的结构类型 

无

ElementWorksetFilter 

快速过滤器

指定工作集的元素过滤

无

ExclusionFilter

快速过滤器

排除过滤器

无

ExtensibleStorageFilter

快速过滤器

Schema类型的排除计算器

无

FamilySymbolFilter

快速过滤器

族过滤器

无

  案例:

案例1:查找包含指定点的墙体元素BoundingBoxContainsPointFilter

BoundingBoxContainsPointFilter notContainFilter =
    new BoundingBoxContainsPointFilter(basePnt, true); 
collector = new FilteredElementCollector(document);
IList<Element> notContainFounds =
    collector.OfClass(typeof(Wall)).WherePasses(notContainFilter).ToElements();

案例2:查找指定区域相交的元素对象BoundingBoxIntersectsFilter

Outline myOutLn = new Outline(new XYZ(0, 0, 0), new XYZ(100, 100, 100));

//创建轮廓
BoundingBoxIntersectsFilter filter = new BoundingBoxIntersectsFilter(myOutLn);
derived from ElementType
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> elements = collector.WherePasses(filter).ToElements();

案例3:过滤查找指定区域内的元素

Outline myOutLn = new Outline(new XYZ(0, 0, 0), new XYZ(100, 100, 100));

//创建边界
BoundingBoxIsInsideFilter filter = new BoundingBoxIsInsideFilter(myOutLn);

derived from ElementType
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> elements = collector.WherePasses(filter).ToElements();

案例4:通过元素的类别进行过滤指定元素

ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);

FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> walls = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
String prompt = "The walls in the current document are:\n";
foreach (Element e in walls)
{
    prompt += e.Name + "\n";
}
TaskDialog.Show("Revit", prompt);

案例5:通过元素的类型进行过滤ElementClassFilter

ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance));


FilteredElementCollector collector = new FilteredElementCollector(document);
collector.WherePasses(filter);


var query = from element in collector
            where element.Name == "60\" x 30\" Student"
            select element;
List<FamilyInstance> familyInstances = query.Cast<FamilyInstance>().ToList<FamilyInstance>();

案例6:

2.2 ElementSlowFilter 慢过滤器常用在一个图形比较方面

慢过滤器要求首先获得元素并在内存中展开。因此,最好将慢过滤器与至少一个ElementQuickFilter耦合,这应使扩展的元素数量最小化,以便根据该滤波器设置的标准进行评估。

名称    

类型

作用

备注

RoomFilter

慢过滤器

查找房间

无

RoomTagFilter

慢过滤器

查找房间标记

无

AreaFilter

慢过滤器

查找区域

无

AreaTagFilter

慢过滤器

查找区域标记

无

CurveElementFilter

慢过滤器

指定元素类进行过滤

无

ElementIntersectsFilter

慢过滤器

查找相关信息

无

ElementLevelFilter

慢过滤器

查找标高信息

无

ElementParameterFilter

慢过滤器

通过元素的参数

无

ElementPhaseStatusFilter

慢过滤器

和指定阶段状态联系的元素

无

FamilyInstanceFilter

慢过滤器

族实例查找

无

SpaceFilter

慢过滤器

空间查找

无

SpaceTagFilter

慢过滤器

空间标记查找

无

PrimaryDesignOptionMemberFilter

慢过滤器

主选项所拥有的元素

无

FamilyStructuralMaterialTypeFilter

慢过滤器

组的结构性材质过滤器

无

StructuralInstanceUsageFilter

慢过滤器

结构实例

无

StructuralMaterialTypeFilter

慢过滤器

结构性材质

无

2.3 ElementLogicalFilter 逻辑过滤器,是实现过滤器的组合和复杂应用

逻辑过滤器是实现组合和复杂过滤的过滤器。

名称    

类型

作用

备注

LogicalAndFilter

逻辑过滤器

逻辑与

 

LogicalOrFilter

逻辑过滤器

逻辑或

 

主要解决过滤中的“或”与“与”的问题,and就是所有满足其中一个条件即可,OR就是要满足所有的条件。

 

posted @ 2021-01-09 18:14  Albert_MIN  阅读(1953)  评论(0)    收藏  举报