ここからさらに条件を設定して、その条件と一致するElementのみ取得する、なんてことをしたいです!
今回は指定したクラスのElementのみを取得できるOfClassメソッドを紹介します。
なお、FilteredElementCollectorクラスの基本的な使い方については下記の記事を読んでいただければ幸いです!

もくじ
OfClassメソッドについて
OfClassはFilteredElementCollectorクラスのメソッドです。
下記のコードのように、FilteredElementCollectorクラスで生成したオブジェクトfilteredElementCollectorに対して
filteredElementCollector.OfClass(typeof(取得したいクラス))
というように.OfClass(typeof( ))のカッコ内 に取得したいクラス名を直接記入する形で使用します。
Ofclassの使い方
List<Element> elementList = new List<Element>();
elementList = filteredElementCollector.OfClass(typeof(取得したいクラス)).ToList();
OfClassメソッド:システムファミリ
OfClassメソッドを使ったサンプルをいくつか紹介します。
各サンプルで、取得したElementのクラス名とタイプ名を表示するようにしています。
element.GetType().Nameでクラス名を取得できます。
まずはシステムファミリから。
床(Floorクラス)取得のサンプル
Sample1
{
UIApplication uiapp = this.ActiveUIDocument.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app =
uiapp.Application as Autodesk.Revit.ApplicationServices.Application;
Autodesk.Revit.DB.Document doc = uidoc.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
List<Element> elementList = new List<Element>();
elementList = filteredElementCollector.OfClass(typeof(Floor)).ToList();
string result = “”;
foreach(Element element in elementList)
{
result += element.GetType().Name + “_” + element.Name + “\n”;
}
TaskDialog.Show(“Message”, result);
}
天井(Ceilingクラス)取得のサンプル
Sample2
{
UIApplication uiapp = this.ActiveUIDocument.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app =
uiapp.Application as Autodesk.Revit.ApplicationServices.Application;
Autodesk.Revit.DB.Document doc = uidoc.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
List<Element> elementList = new List<Element>();
elementList = filteredElementCollector.OfClass(typeof(Ceiling)).ToList();
string result = “”;
foreach(Element element in elementList)
{
result += element.GetType().Name + “_” + element.Name + “\n”;
}
TaskDialog.Show(“Message”, result);
}
ソースコードの解説
システムファミリは床はFloorクラス、天井はCeilingクラスのようにファミリごとにクラスがあります。
なのでSample1とSample2のように
OfClass(typeof(Floor))やOfClass(typeof(Ceiling))と書くことで特定ファミリが取得できます。
OfClassメソッド:コンポーネントファミリ
コンポーネントファミリ(FamilyInstanceクラス)取得のサンプル
Sample3
{
UIApplication uiapp = this.ActiveUIDocument.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app =
uiapp.Application as Autodesk.Revit.ApplicationServices.Application;
Autodesk.Revit.DB.Document doc = uidoc.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
List<Element> elementList = new List<Element>();
elementList = filteredElementCollector.OfClass(typeof(FamilyInstance)).ToList();
string result = “”;
foreach(Element element in elementList)
{
result += element.GetType().Name + “_” + element.Name + “\n”;
}
TaskDialog.Show(“Message”, result);
}
ソースコードの解説
コンポーネントファミリはFamilyInstanceクラスなので
Sample3のようにOfClass(typeof(FamilyInstance))と書きます。
これを実行したら、ドアやら窓やらすべてのコンポーネントファミリが取得されてしまいます。
なのでドアだけを取得したければ、ここからさらにカテゴリ等で
取得したElementを絞っていくこととなります。
特定カテゴリでFilteredElementCollectorクラスのオブジェクトをフィルタリングするにはOfCategoryメソッドを使います。
OfClassメソッド:ファミリタイプ
壁タイプ(WallType)取得のサンプル
Sample4
{
UIApplication uiapp = this.ActiveUIDocument.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app =
uiapp.Application as Autodesk.Revit.ApplicationServices.Application;
Autodesk.Revit.DB.Document doc = uidoc.Document;
FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
List<Element> elementList = new List<Element>();
elementList = filteredElementCollector.OfClass(typeof(WallType)).ToList();
string result = “”;
foreach(Element element in elementList)
{
result += element.GetType().Name + “_” + element.Name + “\n”;
}
TaskDialog.Show(“Message”, result);
}
解説
FilteredElementCollectorはElementを収集するクラスです。
なので取得したい要素がElementクラスであれば、
インスタンスとしてビュー上の存在していないものでも取得できます。
というわけで、壁タイプを取得したのがSample4です。
プロジェクトブラウザから壁タイプをガバッと取得してるイメージです。
OfClassメソッドはOfClass(typeof(WallType))と書きます。
プロジェクトブラウザの壁タイプ
コンポーネントファミリの場合は先ほどの解説と同様、OfCategoryメソッドとの併用が必要です。
OfClassメソッドまとめ
以上でOfClassメソッドの基本的な使い方解説を終わります。
最後まで読んでいただき、ありがとうございました!
C#初心者の僕が独学でお世話になった本
プログラミング初心者の僕がコーディング学習でお世話になった本