AddFilter Method


Adds a new filter at the end of the list.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 2015.0.0.0 (2015.0.0.0)
Since: 2013

Syntax

C#
public void AddFilter(
	ScheduleFilter filter
)
Visual Basic
Public Sub AddFilter ( _
	filter As ScheduleFilter _
)
Visual C++
public:
void AddFilter(
	ScheduleFilter^ filter
)

Parameters

filter
Type: Autodesk.Revit.DB ScheduleFilter
The filter to add.

Examples

Copy C#
public static void AddFilterToSchedule(ViewSchedule schedule, ElementId levelId)
{
    // Find level field
    ScheduleDefinition definition = schedule.Definition;

    ScheduleField levelField = FindField(schedule, BuiltInParameter.ROOM_LEVEL_ID);

    // Add filter
    using (Transaction t = new Transaction(schedule.Document, "Add filter"))
    {
        t.Start();

        // If field not present, add it
        if (levelField == null)
        {
            levelField = definition.AddField(ScheduleFieldType.Instance, new ElementId(BuiltInParameter.ROOM_LEVEL_ID));
        }

        // Set field to hidden
        levelField.IsHidden = true;
        ScheduleFilter filter = new ScheduleFilter(levelField.FieldId, ScheduleFilterType.Equal, levelId);
        definition.AddFilter(filter);

        t.Commit();
    }
}

/// <summary>
/// Finds an existing ScheduleField matching the given parameter
/// </summary>
/// <param name="schedule"></param>
/// <param name="paramEnum"></param>
/// <returns></returns>
public static ScheduleField FindField(ViewSchedule schedule, BuiltInParameter paramEnum)
{
    ScheduleDefinition definition = schedule.Definition;
    ScheduleField foundField = null;
    ElementId paramId = new ElementId(paramEnum);

    foreach (ScheduleFieldId fieldId in definition.GetFieldOrder())
    {
        foundField = definition.GetField(fieldId);
        if (foundField.ParameterId == paramId)
        {
            return foundField;
        }
    }

    return null;
}
Copy VB.NET
Public Shared Sub AddFilterToSchedule(schedule As ViewSchedule, levelId As ElementId)
    ' Find level field
    Dim definition As ScheduleDefinition = schedule.Definition

    Dim levelField As ScheduleField = FindField(schedule, BuiltInParameter.ROOM_LEVEL_ID)

    ' Add filter
    Using t As New Transaction(schedule.Document, "Add filter")
        t.Start()

        ' If field not present, add it
        If levelField Is Nothing Then
            levelField = definition.AddField(ScheduleFieldType.Instance, New ElementId(BuiltInParameter.ROOM_LEVEL_ID))
        End If

        ' Set field to hidden
        levelField.IsHidden = True
        Dim filter As New ScheduleFilter(levelField.FieldId, ScheduleFilterType.Equal, levelId)
        definition.AddFilter(filter)

        t.Commit()
    End Using
End Sub

' <summary>
' Finds an existing ScheduleField matching the given parameter
' </summary>
' <param name="schedule"></param>
' <param name="paramEnum"></param>
' <returns></returns>
Public Shared Function FindField(schedule As ViewSchedule, paramEnum As BuiltInParameter) As ScheduleField
    Dim definition As ScheduleDefinition = schedule.Definition
    Dim foundField As ScheduleField = Nothing
    Dim paramId As New ElementId(paramEnum)

    For Each fieldId As ScheduleFieldId In definition.GetFieldOrder()
        foundField = definition.GetField(fieldId)
        If foundField.ParameterId = paramId Then
            Return foundField
        End If
    Next

    Return Nothing
End Function

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException The field ID is not the ID of a field in this ScheduleDefinition. -or- The field and filter type cannot be used to filter this ScheduleDefinition. -or- The filter value is not valid for the field and filter type.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was NULL
Autodesk.Revit.Exceptions InvalidOperationException This ScheduleDefinition does not support filters. -or- The resulting filter count would be greater than 8.

See Also