Search code examples
c#revit-apitaskdialog

c# Revit API task dialog with multiple lines counting elements


I have a simple Revit plugin to count multiple elements by category and display the totals in a task dialog. The code works fine with one category. When I add more that 1 line to count multiple categories anything after the 1st line returns a result of 0 as in the image below. I can run any of the 3 categories below alone and correct results are returned. Any ideas why multiple lines will not display results? Thanks for any help!

enter image description here

using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace MyRevitCommands
{
    [TransactionAttribute(TransactionMode.ReadOnly)]
    public class SomeData : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //Get UIDocument
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            //Create Filtered Element Collector
            FilteredElementCollector collector = new FilteredElementCollector(doc);

            //Create Filter
            ElementCategoryFilter lineFilter = new ElementCategoryFilter(BuiltInCategory.OST_Lines);
            ElementCategoryFilter tagFilter = new ElementCategoryFilter(BuiltInCategory.OST_Tags);
            ElementCategoryFilter wallFilter = new ElementCategoryFilter(BuiltInCategory.OST_Walls);


            //Apply Filter
            IList<Element> lines = collector.WherePasses(lineFilter).WhereElementIsNotElementType().ToElements();
            int lineCount = lines.Count;
            IList<Element> tags = collector.WherePasses(tagFilter).WhereElementIsNotElementType().ToElements();
            int tagCount = tags.Count;
            IList<Element> walls = collector.WherePasses(wallFilter).WhereElementIsNotElementType().ToElements();
            int wallCount = walls.Count;


            **TaskDialog.Show("Model Data", string.Format(
                "Lines: " + lineCount
                + Environment.NewLine + "Tags: " + tagCount
                + Environment.NewLine + "Walls: " + wallCount
                ));**

            return Result.Succeeded;
        }
    }
}

Solution

  • First of all, your call to string.Format has absolutely no effect, because you are assembling the result string using the + operator instead.

    Secondly, the string you assembly is absolutely displaying the correct result that you obtain.

    The values of tagCount and wallCount are indeed always zero.

    The reason for this is that you are reusing the same filtered element collector multiple times over without reinitialising it.

    Every filter that you add to the collector is added to all the previous filters.

    Hence, first you get a count of lines.

    Second, a count of all line elements that are also tag elements, namely zero.

    Third, a count of all line elements that are also tag elements and also walls, namely zero.

    Here is a recent explanation by The Building Coder on the need to Reinitialise the Filtered Element Collector.