FirstElementId Method


Returns the id of the first element to pass the filter(s).

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

Syntax

C#
public ElementId FirstElementId()
Visual Basic
Public Function FirstElementId As ElementId
Visual C++
public:
ElementId^ FirstElementId()

Return Value

The first element id.

Remarks

This will reset the collector to the beginning and find the first element that passes the applied filter(s). If you have an active iterator to this same collector it will be stopped by this call.

Examples

Copy C#
FilteredElementCollector collector = new FilteredElementCollector(document);

// Use shortcut OfClass to get View elements
collector.OfClass(typeof(View3D));

// Get the Id of the first view
ElementId viewId = collector.FirstElementId();

// Test if the view is valid for element filtering
if (FilteredElementCollector.IsViewValidForElementIteration(document, viewId))
{
    FilteredElementCollector viewCollector = new FilteredElementCollector(document, viewId);

    // Get all FamilyInstance items in the view and delete them
    viewCollector.OfClass(typeof(FamilyInstance));
    ICollection<ElementId> familyInstanceIds = viewCollector.ToElementIds();

    document.Delete(familyInstanceIds);
}
Copy VB.NET
Dim collector As New FilteredElementCollector(document)

' Use shortcut OfClass to get View elements
collector.OfClass(GetType(View3D))

' Get the Id of the first view
Dim viewId As ElementId = collector.FirstElementId()

' Test if the view is valid for element filtering
If FilteredElementCollector.IsViewValidForElementIteration(document, viewId) Then
   Dim viewCollector As New FilteredElementCollector(document, viewId)

   ' Get all FamilyInstance items in the view and delete them
   viewCollector.OfClass(GetType(FamilyInstance))
   Dim familyInstanceIds As ICollection(Of ElementId) = viewCollector.ToElementIds()

   document.Delete(familyInstanceIds)
End If

Exceptions

Exception Condition
Autodesk.Revit.Exceptions InvalidOperationException The collector does not have a filter applied. Extraction or iteration of elements is not permitted without a filter.

See Also