フィルタを適用する

フィルタを適用する

ElementFilter を使用して FilteredElementCollector にフィルタを適用できます。ElementFilter は、特定の条件を満たしているかどうかを確認するために要素を調べるクラスです。ElementFilter ベース クラスには、要素フィルタを 3 つのカテゴリに区分する 3 つの派生クラスがあります。

ほとんどのフィルタは、フィルタの反転を指定するブール値引数を持ったオーバーロード コンストラクタを使用して反転させることができます。そうすることで、通常はフィルタが受け入れる要素を拒否し、通常は拒否される要素を受け入れることができます。反転できないフィルタについては、それぞれのセクションに注記があります。

一般的な用途に使用可能な一連の定義済みフィルタがあります。これらの組み込みフィルタの多くは、既に FilteredElementCollector のセクションで述べた、FilteredElementCollector ショートカットメソッドの基盤となります。次の 3 つのセクションでは、組み込みフィルタの詳細を説明します。

フィルタを作成したら、FilteredElementCollector に適用する必要があります。単一の ElementFilter を FilteredElementCollector に適用するには、一般的なメソッドである WherePasses() を使用します。

また、FilteredElementCollector が提供する多数のショートカットメソッドを使用してフィルタを適用することもできます。WhereElementIsCurveDriven()のように追加情報を入力することなく特定のフィルタを適用するものもあれば、BuiltInCategory をパラメータとして使用する OfCategory()メソッドのように簡単な入力とともに特定のフィルタを適用するものもあります。最後に、フィルタ同士を結合する UnionWith() などのメソッドもあります。これらのメソッドはすべて同じコレクタを返すので、フィルタ同士を簡単に連結できます。

クイック フィルタ

クイック フィルタは ElementRecord にのみ作用します。これは要素プロパティを読み込むための限定されたインタフェースを持つ低メモリ クラスです。クイック フィルタによって拒否された要素はメモリ内で展開されません。次の表は組み込みクイック フィルタをまとめたものであり、一部のフィルタにはサンプルも掲載されています。

表 13: 組み込みクイック フィルタ

組み込みフィルタ

フィルタ結果

ショートカットメソッド

BoundingBoxContainsPointFilter

特定の点が含まれる境界ボックスを持つ要素

なし

BoundingBoxIntersectsFilter

特定のアウトラインに交差する境界ボックスを持つ要素

なし

BoundingBoxIsInsideFilter

特定のアウトライン内に境界ボックスを持つ要素

なし

ElementCategoryFilter

入力カテゴリ ID に一致する要素

OfCategoryId()

ElementClassFilter

入力ランタイム クラス(または派生クラス)に一致する要素

OfClass()

ElementDesignOptionFilter

特定のデザイン オプション内の要素

ContainedInDesignOption()

ElementIsCurveDrivenFilter

曲線ドリブンの要素

WhereElementIsCurveDriven()

ElementIsElementTypeFilter

「要素タイプ」の要素

WhereElementIsElementType() WhereElementIsNotElementType()

ElementMulticategoryFilter 特定のカテゴリ セットのいずれかに一致する要素 なし
ElementMulticlassFilter 特定のクラス セット(または派生クラス)に一致する要素 なし

ElementOwnerViewFilter

ビュー固有の要素

OwnedByView() WhereElementIsViewIndependent()

ElementStructuralTypeFilter

特定の構造タイプに一致する要素

なし

ExclusionFilter

フィルタに入力した要素 ID を除くすべての要素

Excluding()

FamilySymbolFilter

特定のファミリの記号

注: FamilySymbolFilter は反転できません。
注: 境界ボックス フィルタは、View から派生したすべてのオブジェクトと、ElementType から派生したオブジェクトを除外します。

次の例では、ドキュメント内でアウトラインを作成し、その後 BoundingBoxIntersectsFilter を使用して、そのアウトラインに交差する境界ボックスを持つドキュメント内の要素を検索します。その後、特定のアウトラインに交差しない境界ボックスを持つすべての壁を検索するための、反転フィルタの使用方法を説明します。OfClass()メソッドを使用すると、ElementClassFilter がコレクションにも適用されることに注意してください。

コード領域 6-2: BoundingBoxIntersectsFilter の例

// Use BoundingBoxIntersects filter to find elements with a bounding box that intersects the 
// given Outline in the document.

// Create a Outline, uses a minimum and maximum XYZ point to initialize the outline. 
Outline myOutLn = new Outline(new XYZ(0, 0, 0), new XYZ(100, 100, 100));

// Create a BoundingBoxIntersects filter with this Outline
BoundingBoxIntersectsFilter filter = new BoundingBoxIntersectsFilter(myOutLn);

// Apply the filter to the elements in the active document
// This filter excludes all objects derived from View and objects derived from ElementType
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> elements = collector.WherePasses(filter).ToElements();

// Find all walls which don't intersect with BoundingBox: use an inverted filter 
// to match elements
// Use shortcut command OfClass() to find walls only
BoundingBoxIntersectsFilter invertFilter = new BoundingBoxIntersectsFilter(myOutLn, true);
collector = new FilteredElementCollector(document);
IList<Element> notIntersectWalls = collector.OfClass(typeof(Wall)).WherePasses(invertFilter).ToElements();

次の例では、除外フィルタを使用して、ドキュメント内で現在選択されていないすべての壁を検索します。

コード領域 6-3: 除外フィルタを作成

// Find all walls that are not currently selected, 
// Get all element ids which are current selected by users, exclude these ids when filtering
ICollection<ElementId> selectedIds = uiDocument.Selection.GetElementIds();

// Use the selection to instantiate an exclusion filter
ExclusionFilter filter = new ExclusionFilter(selectedIds);
// For the sake of simplicity we do not test here whether the selection is empty or not,
// but in production code a proper validation would have to be done to avoid an argument
// exception from the filter's consructor.

// Apply the filter to the elements in the active document,
// Use shortcut method OfClass() to find Walls only
FilteredElementCollector collector = new FilteredElementCollector(uiDocument.Document);
IList<Element> walls = collector.WherePasses(filter).OfClass(typeof(Wall)).ToElements();
注: ElementClassFilter は、入力クラスに完全に一致するクラスを持つ要素か、入力クラスから派生したクラスを持つ要素に一致します。次の例では、ElementClassFilter を使用して、ドキュメント内のすべての荷重を取得します。

コード領域 6-4: ElementClassFilter を使用して荷重を取得

// Use ElementClassFilter to find all loads in the document
// Using typeof(LoadBase) will yield all AreaLoad, LineLoad and PointLoad
ElementClassFilter filter = new ElementClassFilter(typeof(LoadBase));

// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<Element> allLoads = collector.WherePasses(filter).ToElements();
API には、要素クラス フィルタがサポートしていない Element サブクラスの小さなサブセットがあります。これらのタイプは API に存在しますが、Revit のネイティブのオブジェクト モデルではありません。したがって、このフィルタはこれらのタイプをサポートしません。クラス フィルタを使用してこれらのタイプの要素を検索するには、より高いレベルのクラスを使用し、その後、結果に追加処理を行ってサブタイプに一致する要素のみを検索する必要があります。
注: これらのタイプの一部については専用フィルタがあります。
次のタイプはこの制約による影響を受けます。

タイプ

専用フィルタ

Autodesk.Revit.DB.Material のサブクラス

なし

Autodesk.Revit.DB.CurveElement のサブクラス

CurveElementFilter

Autodesk.Revit.DB.ConnectorElement のサブクラス

なし

Autodesk.Revit.DB.HostedSweep のサブクラス

なし

Autodesk.Revit.DB.Architecture.Room

RoomFilter

Autodesk.Revit.DB.Mechanical.Space

SpaceFilter

Autodesk.Revit.DB.Area

AreaFilter

Autodesk.Revit.DB.Architecture.RoomTag

RoomTagFilter

Autodesk.Revit.DB.Mechanical.SpaceTag

SpaceTagFilter

Autodesk.Revit.DB.AreaTag

AreaTagFilter

Autodesk.Revit.DB.CombinableElement

なし

Autodesk.Revit.DB.Mullion

なし

Autodesk.Revit.DB.Panel

なし

Autodesk.Revit.DB.AnnotationSymbol

なし

Autodesk.Revit.DB.Structure.AreaReinforcementType

なし

Autodesk.Revit.DB.Structure.PathReinforcementType

なし

Autodesk.Revit.DB.AnnotationSymbolType

なし

Autodesk.Revit.DB.Architecture.RoomTagType

なし

Autodesk.Revit.DB.Mechanical.SpaceTagType

なし

Autodesk.Revit.DB.AreaTagType

なし

Autodesk.Revit.DB.Structure.TrussType

なし

低速フィルタ

低速フィルタでは、はじめに、要素を取得してメモリ内に展開する必要があります。したがって、低速フィルタを少なくとも 1 つの ElementQuickFilter と組み合わせるようにしてください。そうすることで、このフィルタで設定された基準で評価するために展開される要素の数が最小限に抑えられます。次の表は、組み込み低速フィルタをまとめたものです。一部のフィルタについては詳しく説明するためのサンプルが記載されています。

表 14: 組み込み低速フィルタ

組み込みフィルタ

フィルタ結果

ショートカットメソッド

AreaFilter

エリア

なし

AreaTagFilter

エリア タグ

なし

CurveElementFilter

CurveElements

なし

ElementLevelFilter

特定のレベル ID に関連付けられた要素

なし

ElementParameterFilter

1 つまたは複数のパラメータ フィルタの規則を渡す要素

なし

ElementPhaseStatusFilter 特定のフェーズにおいて特定のフェーズ ステータスを持つ要素 なし

FamilyInstanceFilter

特定のファミリ インスタンスのインスタンス

なし

FamilyStructuralMaterialTypeFilter

特定の構造マテリアル タイプのファミリ要素

なし

PrimaryDesignOptionMemberFilter

任意のメイン デザイン オプションによって所有されている要素

なし

RoomFilter

部屋

なし

RoomTagFilter

部屋タグ

なし

SpaceFilter

スペース

なし

SpaceTagFilter

スペース タグ

なし

StructuralInstanceUsageFilter

特定の構造用途の FamilyInstances

なし

StructuralMaterialTypeFilter

特定の構造マテリアル タイプの FamilyInstances

なし

StructuralWallUsageFilter

特定の構造壁用途の壁

なし

ElementIntersectsElementFilter 特定の要素のソリッド ジオメトリと交差する要素 なし
ElementIntersectsSolidFilter 特定のソリッド ジオメトリと交差する要素 なし

次の低速フィルタは反転できません。

  • RoomFilter
  • RoomTagFilter
  • AreaFilter
  • AreaTagFilter
  • SpaceFilter
  • SpaceTagFilter
  • FamilyInstanceFilter

クイック フィルタのセクションで述べたように、一部のクラスは ElementClassFilter で機能しません。これらのクラスの一部(Room や RoomTag など)は専用フィルタを持ちます。

コード領域 6-5: 部屋フィルタを使用

// Use a RoomFilter to find all room elements in the document. It is necessary to use the 
// RoomFilter and not an ElementClassFilter or the shortcut method OfClass() because the Room 
// class is not supported by those methods.
RoomFilter filter = new RoomFilter();

// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> rooms = collector.WherePasses(filter).ToElements();

ElementParameterFilter は、設計可能なパラメータの値に基づいて要素を検索できる強力なフィルタです。パラメータの値が特定の値に一致する要素か、ある値よりも大きい/小さい要素を検索できます。さらに、ElementParameterFilter は特定の共有パラメータをサポートする要素を検索するのにも使用できます。

次の例では、ElementParameterFilter を使用して、100 平方フィートを超えるサイズの部屋と 100 平方フィート未満のサイズの部屋を検索します。

コード領域 6-6: パラメータ フィルタを使用

// Creates an ElementParameter filter to find rooms whose area is 
// greater than specified value
// Create filter by provider and evaluator 
BuiltInParameter areaParam = BuiltInParameter.ROOM_AREA;
// provider
ParameterValueProvider pvp = new ParameterValueProvider(new ElementId((int)areaParam));
// evaluator
FilterNumericRuleEvaluator fnrv = new FilterNumericGreater();
// rule value 
double ruleValue = 100.0f;      // filter room whose area is greater than 100 SF
// rule
FilterRule fRule = new FilterDoubleRule(pvp, fnrv, ruleValue, 1E-6);

// Create an ElementParameter filter
ElementParameterFilter filter = new ElementParameterFilter(fRule);

// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> rooms = collector.WherePasses(filter).ToElements();

// Find rooms whose area is less than or equal to 100: 
// Use inverted filter to match elements
ElementParameterFilter lessOrEqualFilter = new ElementParameterFilter(fRule, true); 
collector = new FilteredElementCollector(document);
IList<Element> lessOrEqualFounds = collector.WherePasses(lessOrEqualFilter).ToElements();

次の例は、マテリアル タイプが木材となっているすべてのファミリを検索するための FamilyStructuralMaterialTypeFilter の使用方法を説明しています。さらに、反転フィルタを使用した、マテリアル タイプが木材以外のすべてのファミリの検索方法も説明します。

コード領域 6-7: 木材マテリアルのファミリをすべて検索

// Use FamilyStructuralMaterialType filter to find families whose material type is Wood
FamilyStructuralMaterialTypeFilter filter = new FamilyStructuralMaterialTypeFilter(StructuralMaterialType.Wood);

// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<Element> woodFamiles = collector.WherePasses(filter).ToElements();

// Find families are not Wood: Use inverted filter to match families
FamilyStructuralMaterialTypeFilter notWoodFilter = 
        new FamilyStructuralMaterialTypeFilter(StructuralMaterialType.Wood, true);
collector = new FilteredElementCollector(document);
ICollection<Element> notWoodFamilies = collector.WherePasses(notWoodFilter).ToElements();
最後の 2 つの低速フィルタは、ジオメトリと交差する要素に一致させるために使用するフィルタのベース クラスである ElementIntersectsFilter から派生したものです。このタイプのフィルタの使用例については、「ジオメトリ ユーティリティ クラス」セクションの「コード領域: 近くの壁を検索」を参照してください。

論理フィルタ

論理フィルタは複数のフィルタを論理的に結合します。次の表は、組み込み論理フィルタをまとめたものです。

表 15: 組み込み論理フィルタ

組み込みフィルタ

フィルタ結果

ショートカットメソッド

LogicalAndFilter

複数のフィルタを通過する要素

WherePasses() - 追加フィルタを 1 つ追加

IntersectWith() - 独立したフィルタの 2 つのセットを結合

LogicalOrFilter

複数のフィルタのうち少なくとも 1 つを通過する要素

UnionWith() - 独立したフィルタの 2 つのセットを結合

次の例では、論理フィルタを使用して 2 つのクイック フィルタを組み合わせ、ドキュメント内のすべてのドア FamilyInstance 要素を取得します。

コード領域 6-8: LogicalAndFilter を使用してすべてのドア インスタンスを検索

// Find all door instances in the project by finding all elements that both belong to the
// door category and are family instances.
ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));

// Create a category filter for Doors
ElementCategoryFilter doorsCategoryfilter = 
        new ElementCategoryFilter(BuiltInCategory.OST_Doors);

// Create a logic And filter for all Door FamilyInstances
LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, 
        doorsCategoryfilter);

// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
IList<Element> doors = collector.WherePasses(doorInstancesFilter).ToElements();