FilteredElementCollector Constructor (Document, ElementId)


Constructs a new FilteredElementCollector that will search and filter the visible elements in a view.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 17.0.0.0 (17.0.484.0)
Since: 2011

Syntax

C#
public FilteredElementCollector(
	Document document,
	ElementId viewId
)
Visual Basic
Public Sub New ( _
	document As Document, _
	viewId As ElementId _
)
Visual C++
public:
FilteredElementCollector(
	Document^ document, 
	ElementId^ viewId
)

Parameters

document
Type: Autodesk.Revit.DB Document
The document that owns the view.
viewId
Type: Autodesk.Revit.DB ElementId
The view id.

Remarks

Elements that will be passed by the collector have graphics that may be visible in the input view. Some elements may still be hidden because they are obscured by other elements. Accessing these visible elements may require Revit to rebuild the geometry of the view. The first time your code constructs a collector for a given view, or the first time your code constructs a collector for a view whose display settings have just been changed, you may experience a significant performance degradation.

Examples

Copy C#
private void CountElements(UIDocument uiDoc)
{
    StringBuilder message = new StringBuilder();
    FilteredElementCollector viewCollector = 
        new FilteredElementCollector(uiDoc.Document, uiDoc.ActiveView.Id);
    viewCollector.OfCategory(BuiltInCategory.OST_Walls);
    message.AppendLine("Wall category elements within active View: "
       + viewCollector.ToElementIds().Count);

    FilteredElementCollector docCollector = new FilteredElementCollector(uiDoc.Document);
    docCollector.OfCategory(BuiltInCategory.OST_Walls);
    message.AppendLine("Wall category elements within document: "
       + docCollector.ToElementIds().Count);

    TaskDialog.Show("Revit", message.ToString());
}
Copy C#
public static void GetScheduleContents(ViewSchedule viewSchedule)
{
    // Collect types displayed in the schedule
    FilteredElementCollector typeCollector = new FilteredElementCollector(viewSchedule.Document, viewSchedule.Id);
    typeCollector.WhereElementIsElementType();

    int numberOfTypes = typeCollector.Count();

    // Collect instances displayed in the schedule
    FilteredElementCollector instCollector = new FilteredElementCollector(viewSchedule.Document, viewSchedule.Id);
    instCollector.WhereElementIsNotElementType();

    int numberOfInstances = instCollector.Count();

    TaskDialog.Show("Elements in schedule", String.Format("Types {0} instances {1}", numberOfTypes, numberOfInstances));
}
Copy VB.NET
Private Sub CountElements(uiDoc As UIDocument)
   Dim message As New StringBuilder()
   Dim viewCollector As New FilteredElementCollector(uiDoc.Document, uiDoc.ActiveView.Id)
   viewCollector.OfCategory(BuiltInCategory.OST_Walls)
   message.AppendLine("Wall category elements within active View: " + viewCollector.ToElementIds().Count)

   Dim docCollector As New FilteredElementCollector(uiDoc.Document)
   docCollector.OfCategory(BuiltInCategory.OST_Walls)
   message.AppendLine("Wall category elements within document: " + docCollector.ToElementIds().Count)

   TaskDialog.Show("Revit", message.ToString())
End Sub
Copy VB.NET
Public Shared Sub GetScheduleContents(viewSchedule As ViewSchedule)
    ' Collect types displayed in the schedule
    Dim typeCollector As New FilteredElementCollector(viewSchedule.Document, viewSchedule.Id)
    typeCollector.WhereElementIsElementType()

    Dim numberOfTypes As Integer = typeCollector.Count()

    ' Collect instances displayed in the schedule
    Dim instCollector As New FilteredElementCollector(viewSchedule.Document, viewSchedule.Id)
    instCollector.WhereElementIsNotElementType()

    Dim numberOfInstances As Integer = instCollector.Count()

    TaskDialog.Show("Elements in schedule", [String].Format("Types {0} instances {1}", numberOfTypes, numberOfInstances))
End Sub

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException viewId is not a view. -or- viewId is not valid for element iteration, because it has no way of representing drawn elements. Many view templates will fail this check.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was NULL

See Also