Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Revit 2011 filter performance testing
// Compare TypeFilter versus using an
// anonymous method to filter elements.
// By Guy Robinson,
// Copyright (C) 2008 by Jeremy Tammik,
// Autodesk Inc. All rights reserved.
// Updated to the Revit 2011 API and added LINQ filtering.
// Changed the anonymous method to a lambda expression.
// By Piotr Zurek,
#region Imported Namespaces
//.NET common used namespaces
using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
//Revit.NET common used namespaces
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Application = Autodesk.Revit.ApplicationServices.Application;
namespace FilterPerformance
public class Commands : IExternalCommand
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Application app = uiApp.Application;
Document doc = uiDoc.Document;
Stopwatch sw = Stopwatch.StartNew();
// f5 = f1 && f4
// = f1 && (f2 || f3)
// = family instance and (door or window)
#region Filters and collector definitions
ElementClassFilter f1 = new ElementClassFilter(typeof(FamilyInstance));
ElementCategoryFilter f2 = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
ElementCategoryFilter f3 = new ElementCategoryFilter(BuiltInCategory.OST_Windows);
LogicalOrFilter f4 = new LogicalOrFilter(f2, f3);
LogicalAndFilter f5 = new LogicalAndFilter(f1, f4);
FilteredElementCollector collector = new FilteredElementCollector(doc);
//#region Filtering with a class filter
//List<Element> openingInstances = collector.WherePasses(f5).ToElements() as List<Element>;
#region Filtering with a lambda expression
List<Element> openings = collector.WherePasses(f4).ToElements() as List<Element>;
List<Element> openingInstances = openings.FindAll(element => element is FamilyInstance);
//#region Filtering with LINQ
//List<Element> openings = collector.WherePasses(f4).ToElements() as List<Element>;
//List<Element> openingInstances = (from instances in openings
// where instances is FamilyInstance
// select instances).ToList<Element>();
int n = openingInstances.Count;
Debug.WriteLine(string.Format("Time to get {0} elements: {1}ms", n, sw.ElapsedMilliseconds));
return Result.Succeeded;
catch (Exception ex)
message = ex.Message + ex.StackTrace;
return Result.Failed;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.