为什么带有选择过滤器的PickObjects会减慢我的选择-PickObjects(ObjectTYpe,ISelectionFilter,String)

时间:2019-02-07 19:23:09

标签: c# revit-api revit

为什么选择筛选器的PickOjects比IExternalCommand(Add-in)中没有选择筛选器的PickObjects慢。我尝试在Revit Macro中输入代码,但两种方法都没有区别。

我的代码运行正常(请参阅下文)唯一的区别是性能。带有“选择过滤器”的PickObjects感觉较慢且缓慢。

我应该以不同的方式编写代码吗?也许我的IselectionFilter不好写?

任何帮助,评论和建议将不胜感激。

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace ConduitSizeShowInfo
{
    [Transaction(TransactionMode.Manual)]
    public class Command : 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;

            try
            {
                //Filter Selection
                SelectElementsFilter conduitFilter = new SelectElementsFilter("Conduits");

                //Pick Multiple Conduits
                IList<Reference> selectedConduits = uidoc.Selection.PickObjects(ObjectType.Element, conduitFilter, "Please select conduits");

                //Show Conduit Size Information
                string showConduitSize = "";
                foreach (Reference conduit in selectedConduits)
                {
                    Element e = doc.GetElement(conduit);
                    Parameter conduitSize = e.LookupParameter("Size");
                    showConduitSize += e.Name + ": " + conduitSize.AsString() + Environment.NewLine;
                }
                TaskDialog.Show("Conduit Sizes", showConduitSize);
            }
            catch(Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return Result.Cancelled;
            }
            catch(Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }

            return Result.Succeeded;
        }

        public class SelectElementsFilter : ISelectionFilter
        {
            static string CategoryName = "";

            public SelectElementsFilter(string name)
            {
                CategoryName = name;
            }

            public bool AllowElement(Element e)
            {
                if (e.Category.Name == CategoryName)
                    return true;
                return false;
            }

            public bool AllowReference(Reference r, XYZ p)
            {
                return false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通过检查类别元素ID而不是类别名称,可以(至少)加快选择过滤器的速度。比较两个整数比比较两个字符串要快。

public bool Equals( Category x, Category y )
{
  return x.Id.IntegerValue.Equals(
    y.Id.IntegerValue );
}