Application: Revit API
Category: Filtering & Selection
Difficulty: Beginner
Language: C#
You need to find all walls in a project that have a specific parameter value, such as all walls with a fire rating of "2 Hour" or all doors from a particular manufacturer.
Use FilteredElementCollector with a ParameterFilterElement or iterate through elements and check parameter values. For better performance with large models, create a ParameterFilter.
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
public List<Wall> GetWallsByFireRating(Document doc, string fireRating)
{
List<Wall> matchingWalls = new List<Wall>();
// Collect all walls
FilteredElementCollector collector = new FilteredElementCollector(doc)
.OfClass(typeof(Wall))
.WhereElementIsNotElementType();
foreach (Wall wall in collector)
{
// Get fire rating parameter
Parameter fireRatingParam = wall.get_Parameter(BuiltInParameter.DOOR_FIRE_RATING);
if (fireRatingParam != null && fireRatingParam.HasValue)
{
string value = fireRatingParam.AsString();
if (value == fireRating)
{
matchingWalls.Add(wall);
}
}
}
return matchingWalls;
}
// Usage example
public void FindFireRatedWalls(Document doc)
{
List<Wall> twoHourWalls = GetWallsByFireRating(doc, "2 Hour");
TaskDialog.Show("Results", $"Found {twoHourWalls.Count} walls with 2-hour fire rating");
}
public List<Wall> GetWallsByFireRatingFast(Document doc, string fireRating)
{
// Create parameter filter
ParameterValueProvider provider = new ParameterValueProvider(
new ElementId(BuiltInParameter.DOOR_FIRE_RATING));
FilterStringRuleEvaluator evaluator = new FilterStringEquals();
FilterRule rule = new FilterStringRule(provider, evaluator, fireRating, false);
ElementParameterFilter filter = new ElementParameterFilter(rule);
// Apply filter to collector
FilteredElementCollector collector = new FilteredElementCollector(doc)
.OfClass(typeof(Wall))
.WhereElementIsNotElementType()
.WherePasses(filter);
return collector.Cast<Wall>().ToList();
}
HasValue to verify the parameter contains dataLookupParameter("ParameterName") instead of get_Parameter(BuiltInParameter)Mistake: Not checking for null parameters
// WRONG - will crash if parameter doesn't exist
string value = wall.get_Parameter(BuiltInParameter.DOOR_FIRE_RATING).AsString();
// CORRECT
Parameter param = wall.get_Parameter(BuiltInParameter.DOOR_FIRE_RATING);
if (param != null && param.HasValue)
{
string value = param.AsString();
}
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
public List<Wall> GetWallsByFireRating(Document doc, string fireRating)
{
List<Wall> matchingWalls = new List<Wall>();
// Collect all walls
FilteredElementCollector collector = new FilteredElementCollector(doc)
.OfClass(typeof(Wall))
.WhereElementIsNotElementType();
foreach (Wall wall in collector)
{
// Get fire rating parameter
Parameter fireRatingParam = wall.get_Parameter(BuiltInParameter.DOOR_FIRE_RATING);
if (fireRatingParam != null && fireRatingParam.HasValue)
{
string value = fireRatingParam.AsString();
if (value == fireRating)
{
matchingWalls.Add(wall);
}
}
}
return matchingWalls;
}
// Usage example
public void FindFireRatedWalls(Document doc)
{
List<Wall> twoHourWalls = GetWallsByFireRating(doc, "2 Hour");
TaskDialog.Show("Results", $"Found {twoHourWalls.Count} walls with 2-hour fire rating");
}