Admits elements that satisfy two conditions:
- The element's category must be one of a certain set of allowed categories.
- The element must pass a series of filter rules.
Namespace:
Autodesk.Revit.DB
Assembly:
RevitAPI
(in RevitAPI.dll) Version: 21.0.0.0 (21.1.1.109)
Since:
2011
Syntax
C# |
---|
|
Visual Basic |
---|
|
Visual C++ |
---|
|
Examples
Copy
C#
/// <summary>
/// Create an ElementFilter representing a conjunction ("ANDing together") of FilterRules.
/// </summary>
/// <param name="filterRules">A list of FilterRules</param>
/// <returns>The ElementFilter.</returns>
private static ElementFilter CreateElementFilterFromFilterRules(IList<FilterRule> filterRules)
{
// We use a LogicalAndFilter containing one ElementParameterFilter
// for each FilterRule. We could alternatively create a single
// ElementParameterFilter containing the entire list of FilterRules.
IList<ElementFilter> elemFilters = new List<ElementFilter>();
foreach (FilterRule filterRule in filterRules)
{
ElementParameterFilter elemParamFilter = new ElementParameterFilter(filterRule);
elemFilters.Add(elemParamFilter);
}
LogicalAndFilter elemFilter = new LogicalAndFilter(elemFilters);
return elemFilter;
}
/// <summary>
/// Creates a new view filter matching multiple criteria.
/// </summary>
/// <param name="doc"></param>
/// <param name="view"></param>
public static void CreateViewFilter(Document doc, View view)
{
List<ElementId> categories = new List<ElementId>();
categories.Add(new ElementId(BuiltInCategory.OST_Walls));
List<FilterRule> filterRules = new List<FilterRule>();
using (Transaction t = new Transaction(doc, "Add view filter"))
{
t.Start();
// Create filter element assocated to the input categories
ParameterFilterElement parameterFilterElement = ParameterFilterElement.Create(doc, "Example view filter", categories);
// Criterion 1 - wall type Function is "Exterior"
ElementId exteriorParamId = new ElementId(BuiltInParameter.FUNCTION_PARAM);
filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(exteriorParamId, (int)WallFunction.Exterior));
// Criterion 2 - wall height > some number
ElementId lengthId = new ElementId(BuiltInParameter.CURVE_ELEM_LENGTH);
filterRules.Add(ParameterFilterRuleFactory.CreateGreaterOrEqualRule(lengthId, 28.0, 0.0001));
// Criterion 3 - custom shared parameter value matches string pattern
// Get the id for the shared parameter - the ElementId is not hardcoded, so we need to get an instance of this type to find it
Guid spGuid = new Guid("96b00b61-7f5a-4f36-a828-5cd07890a02a");
FilteredElementCollector collector = new FilteredElementCollector(doc);
collector.OfClass(typeof(Wall));
Wall wall = collector.FirstElement() as Wall;
if (wall != null)
{
Parameter sharedParam = wall.get_Parameter(spGuid);
ElementId sharedParamId = sharedParam.Id;
filterRules.Add(ParameterFilterRuleFactory.CreateBeginsWithRule(sharedParamId, "15.", true));
}
ElementFilter elemFilter = CreateElementFilterFromFilterRules(filterRules);
parameterFilterElement.SetElementFilter(elemFilter);
// Apply filter to view
view.AddFilter(parameterFilterElement.Id);
view.SetFilterVisibility(parameterFilterElement.Id, false);
t.Commit();
}
}
Copy
VB.NET
Public Shared Sub CreateViewFilter(doc As Document, view As View)
Dim categories As New List(Of ElementId)()
categories.Add(New ElementId(BuiltInCategory.OST_Walls))
Dim filterRules As New List(Of FilterRule)()
Using t As New Transaction(doc, "Add view filter")
t.Start()
' Create filter element assocated to the input categories
Dim parameterFilterElement__1 As ParameterFilterElement = ParameterFilterElement.Create(doc, "Example view filter", categories)
' Criterion 1 - wall type Function is "Exterior"
Dim exteriorParamId As New ElementId(BuiltInParameter.FUNCTION_PARAM)
filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(exteriorParamId, CInt(WallFunction.Exterior)))
' Criterion 2 - wall height > some number
Dim lengthId As New ElementId(BuiltInParameter.CURVE_ELEM_LENGTH)
filterRules.Add(ParameterFilterRuleFactory.CreateGreaterOrEqualRule(lengthId, 28.0, 0.0001))
' Criterion 3 - custom shared parameter value matches string pattern
' Get the id for the shared parameter - the ElementId is not hardcoded, so we need to get an instance of this type to find it
Dim spGuid As New Guid("96b00b61-7f5a-4f36-a828-5cd07890a02a")
Dim collector As New FilteredElementCollector(doc)
collector.OfClass(GetType(Wall))
Dim wall As Wall = TryCast(collector.FirstElement(), Wall)
If wall IsNot Nothing Then
Dim sharedParam As Parameter = wall.Parameter(spGuid)
Dim sharedParamId As ElementId = sharedParam.Id
filterRules.Add(ParameterFilterRuleFactory.CreateBeginsWithRule(sharedParamId, "15.", True))
End If
' We use a LogicalAndFilter containing one ElementParameterFilter
' for each FilterRule. We could alternatively create a single
' ElementParameterFilter containing the entire list of FilterRules.
Dim elemFilters As New List(Of ElementFilter)()
For Each filterRule As FilterRule In filterRules
Dim elemParamFilter As New ElementParameterFilter(filterRule)
elemFilters.Add(elemParamFilter)
Next
Dim elemFilter As New LogicalAndFilter(elemFilters)
parameterFilterElement__1.SetElementFilter(elemFilter)
' Apply filter to view
view.AddFilter(parameterFilterElement__1.Id)
view.SetFilterVisibility(parameterFilterElement__1.Id, False)
t.Commit()
End Using
End Sub
Inheritance Hierarchy
System
Object
Autodesk.Revit.DB Element
Autodesk.Revit.DB FilterElement
Autodesk.Revit.DB ParameterFilterElement
Autodesk.Revit.DB Element
Autodesk.Revit.DB FilterElement
Autodesk.Revit.DB ParameterFilterElement