Revit API。如何获得多个元素的边界框?

Revit API. How can I get bounding box for several elements?

我需要为许多元素(>100'000 个项目)找到大纲。目标元素来自 FilteredElementCollector。像往常一样,我正在寻找最快的方法。

现在我尝试遍历所有元素以获取其 BoudingBox.MinBoudingBox.Max 并找出 minXminYminZmaxXmaxYmaxZ。它工作得非常准确,但需要太多时间。


上面描述的问题是一个更大问题的一部分。 我需要从 link 模型中找到管道、管道和其他基于曲线的元素的所有交点,在一般模型中包含墙壁、天花板、柱子等,然后在交点处放置开口。

我尝试结合使用 ElementIntersectElement 过滤器和 IntersectSolidAndCurve 方法在元素内找到一部分曲线。

首先使用 ElementIntersectElement 我试图减少集合以进一步使用 IntersectSolidAndCurveIntersectSolidAndCurve 有两个参数:实体和曲线并且必须在两个嵌套的一个中工作其他循环。因此,在我的案例中需要 54000 面墙(过滤后)和 18000 根管道 972'000'000 次操作。

运算次数为 10 ^ 5 时,算法显示可接受的时间。 我决定通过按级别划分搜索区域来减少元素的数量。这对高层建筑很有效,但对扩展的低结构仍然不利。我决定按长度划分建筑物,但我没有找到为几个元素(整个建筑物)找到边界的方法。

我好像走错了路。有没有正确的方法用 revit api instrument

原则上,您所描述的是正确的方法,也是唯一的方法。但是,可能有很多优化代码的可能性。 Building Coder 提供了各种可能有用的实用功能。例如,到 determine the bounding box of an entire family. Many more in The Building Coder samples Util module. Search there for "bounding box". I am sure they can be further optimised as well for your case. For instance, you may be able to extract all the X coordinates from all the individual elements' bounding box Max values and use a generic Max function to determine their maximum in one single call instead of comparing them one by one. Benchmark your code 来发现优化的可能性并分析它们对性能的影响。请分享您的最终结果,以供其他人学习。谢谢!

要找到边界,我们可以利用二进制搜索的思想。

与经典的二分查找算法不同的是没有数组,我们应该找两个数而不是一个数。

几何元素 space 可以表示为 XYZ 点的 3 维排序数组。

Revit api 提供了出色的 Quick Filter: BoundingBoxIntersectsFilter 它采用 Outline

的实例

那么,让我们定义一个区域,其中包含我们要为其寻找边界的所有元素。对于我的情况,例如 500 米,并为初始轮廓创建 minmax

    double b = 500000 / 304.8;
    XYZ min = new XYZ(-b, -b, -b);
    XYZ max = new XYZ(b, b, b);

下面是一个方向的实现,但是,您可以通过调用上一次迭代的结果并将其提供给输入来轻松地将其用于三个方向

     double precision = 10e-6 / 304.8;
     var bb = new BinaryUpperLowerBoundsSearch(doc, precision);

     XYZ[] rx = bb.GetBoundaries(min, max, elems, BinaryUpperLowerBoundsSearch.Direction.X);
     rx = bb.GetBoundaries(rx[0], rx[1], elems, BinaryUpperLowerBoundsSearch.Direction.Y);
     rx = bb.GetBoundaries(rx[0], rx[1], elems, BinaryUpperLowerBoundsSearch.Direction.Z);

GetBoundaries方法returns两个XYZ点:lower和upper,只在目标方向改变,其他两个维度不变

    public class BinaryUpperLowerBoundsSearch
    {
        private Document doc;

        private double tolerance;
        private XYZ min;
        private XYZ max;
        private XYZ direction;

        public BinaryUpperLowerBoundsSearch(Document document, double precision)
        {
            doc = document;
            this.tolerance = precision;
        }

        public enum Direction
        {
            X,
            Y,
            Z
        }

        /// <summary>
        /// Searches for an area that completely includes all elements within a given precision.
        /// The minimum and maximum points are used for the initial assessment. 
        /// The outline must contain all elements.
        /// </summary>
        /// <param name="minPoint">The minimum point of the BoundBox used for the first approximation.</param>
        /// <param name="maxPoint">The maximum point of the BoundBox used for the first approximation.</param>
        /// <param name="elements">Set of elements</param>
        /// <param name="axe">The direction along which the boundaries will be searched</param>
        /// <returns>Returns two points: first is the lower bound, second is the upper bound</returns>
        public XYZ[] GetBoundaries(XYZ minPoint, XYZ maxPoint, ICollection<ElementId> elements, Direction axe)
        {
            // Since Outline is not derived from an Element class there 
            // is no possibility to apply transformation, so
            // we have use as a possible directions only three vectors of basis 
            switch (axe)
            {
                case Direction.X:
                    direction = XYZ.BasisX;
                    break;
                case Direction.Y:
                    direction = XYZ.BasisY;
                    break;
                case Direction.Z:
                    direction = XYZ.BasisZ;
                    break;
                default:
                    break;
            }

            // Get the lower and upper bounds as a projection on a direction vector
            // Projection is an extention method
            double lowerBound = minPoint.Projection(direction);
            double upperBound = maxPoint.Projection(direction);

            // Set the boundary points in the plane perpendicular to the direction vector. 
            // These points are needed to create BoundingBoxIntersectsFilter when IsContainsElements calls.
            min = minPoint - lowerBound * direction;
            max = maxPoint - upperBound * direction;


            double[] res = UpperLower(lowerBound, upperBound, elements);
            return new XYZ[2]
            {
                res[0] * direction + min,
                res[1] * direction + max,
            };
        }

        /// <summary>
        /// Check if there are any elements contains in the segment [lower, upper]
        /// </summary>
        /// <returns>True if any elements are in the segment</returns>
        private ICollection<ElementId> IsContainsElements(double lower, double upper, ICollection<ElementId> ids)
        {
            var outline = new Outline(min + direction * lower, max + direction * upper);
            return new FilteredElementCollector(doc, ids)
                .WhereElementIsNotElementType()
                .WherePasses(new BoundingBoxIntersectsFilter(outline))
                .ToElementIds();
        }


        private double[] UpperLower(double lower, double upper, ICollection<ElementId> ids)
        {
            // Get the Midpoint for segment mid = lower + 0.5 * (upper - lower)
            var mid = Midpoint(lower, upper);

            // Сheck if the first segment contains elements 
            ICollection<ElementId> idsFirst = IsContainsElements(lower, mid, ids);
            bool first = idsFirst.Any();

            // Сheck if the second segment contains elements 
            ICollection<ElementId> idsSecond = IsContainsElements(mid, upper, ids);
            bool second = idsSecond.Any();

            // If elements are in both segments 
            // then the first segment contains the lower border 
            // and the second contains the upper
            // ---------**|***--------
            if (first && second)
            {
                return new double[2]
                {
                    Lower(lower, mid, idsFirst),
                    Upper(mid, upper, idsSecond),
                };
            }

            // If elements are only in the first segment it contains both borders. 
            // We recursively call the method UpperLower until 
            // the lower border turn out in the first segment and 
            // the upper border is in the second
            // ---*****---|-----------
            else if (first && !second)
                return UpperLower(lower, mid, idsFirst);

            // Do the same with the second segment
            // -----------|---*****---
            else if (!first && second)
                return UpperLower(mid, upper, idsSecond);

            // Elements are out of the segment
            // ** -----------|----------- **
            else
                throw new ArgumentException("Segment is not contains elements. Try to make initial boundaries wider", "lower, upper");
        }

        /// <summary>
        /// Search the lower boundary of a segment containing elements
        /// </summary>
        /// <returns>Lower boundary</returns>
        private double Lower(double lower, double upper, ICollection<ElementId> ids)
        {
            // If the boundaries are within tolerance return lower bound
            if (IsInTolerance(lower, upper))
                return lower;

            // Get the Midpoint for segment mid = lower + 0.5 * (upper - lower)
            var mid = Midpoint(lower, upper);

            // Сheck if the segment contains elements 
            ICollection<ElementId> idsFirst = IsContainsElements(lower, mid, ids);
            bool first = idsFirst.Any();

            // ---*****---|-----------
            if (first)
                return Lower(lower, mid, idsFirst);
            // -----------|-----***---
            else
                return Lower(mid, upper, ids);

        }

        /// <summary>
        /// Search the upper boundary of a segment containing elements
        /// </summary>
        /// <returns>Upper boundary</returns>
        private double Upper(double lower, double upper, ICollection<ElementId> ids)
        {
            // If the boundaries are within tolerance return upper bound
            if (IsInTolerance(lower, upper))
                return upper;

            // Get the Midpoint for segment mid = lower + 0.5 * (upper - lower)
            var mid = Midpoint(lower, upper);

            // Сheck if the segment contains elements 
            ICollection<ElementId> idsSecond = IsContainsElements(mid, upper, ids);
            bool second = idsSecond.Any();

            // -----------|----*****--
            if (second)
                return Upper(mid, upper, idsSecond);
            // ---*****---|-----------
            else
                return Upper(lower, mid, ids);
        }

        private double Midpoint(double lower, double upper) => lower + 0.5 * (upper - lower);
        private bool IsInTolerance(double lower, double upper) => upper - lower <= tolerance;

    }

投影是向量的一种扩展方法,用于确定一个向量对另一个向量的投影长度

    public static class PointExt
    {
        public static double Projection(this XYZ vector, XYZ other) =>
            vector.DotProduct(other) / other.GetLength();
    }