1

我有一个简单的 Revit 插件,可以按类别计算多个元素,并在任务对话框中显示总数。该代码适用于一个类别。当我添加更多的 1 行以计算多个类别时,在第一行返回 0 之后的任何内容,如下图所示。我可以单独运行以下 3 个类别中的任何一个,并返回正确的结果。任何想法为什么多行不会显示结果?谢谢你的帮助!

在此处输入图像描述

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;
        }
    }
}
4

1 回答 1

1

首先,您的调用string.Format绝对没有效果,因为您正在使用+运算符组装结果字符串。

其次,您组装的字符串绝对显示您获得的正确结果。

tagCount和的值wallCount确实总是为零。

这样做的原因是您多次重复使用相同的过滤元素收集器而没有重新初始化它。

您添加到收集器的每个过滤器都会添加到之前的所有过滤器中。

因此,首先你会得到行数。

其次,所有线元素也是标签元素的计数,即零。

第三,也是标记元素和墙壁的所有线元素的计数,即零。

这是The Building Coder最近对需要重新初始化过滤元素收集器的解释。

于 2019-12-01T08:25:58.853 回答