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

时间:2020-07-25 03:14:24

标签: geometry revit-api revit

我需要找到许多元素的轮廓(> 100'000个项目)。目标元素来自FilteredElementCollector。和往常一样,我正在寻找最快的方式。

现在,我尝试遍历所有元素以获取其BoudingBox.MinBoudingBox.Max并找出minXminYminZ,{{1 }},maxXmaxY。它的工作原理非常准确,但是需要太多时间。


上面描述的问题是一个更大的问题的一部分。 我需要从通用模型中具有墙壁,天花板,圆柱等的链接模型中找到风管,管道和其他基于曲线的元素的所有交点,然后在交点中放置开口。

我尝试结合使用maxZ过滤器和ElementIntersectElement方法来查找元素内部曲线的一部分。

首先使用一个 IntersectSolidAndCurve,我尝试减少一个集合,以进一步使用ElementIntersectElementIntersectSolidAndCurve接受两个参数:实体和曲线,并且必须在两个嵌套的另一个中工作循环。因此,在我的案例972'000'000操作中,需要54000壁(过滤后)和18000管道。

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

我似乎走错了路。是否有正确的方法使用Revit API工具

2 个答案:

答案 0 :(得分:1)

原则上,您所描述的是正确的方法和唯一的方法。但是,可能存在许多优化代码的可能性。建筑编码器提供了可能有用的各种实用程序功能。例如,到determine the bounding box of an entire familyThe Building Coder samples Util module中还有更多内容。在此处搜索“边界框”。我相信它们也可以针对您的情况进行进一步优化。例如,您也许可以从所有单个元素的边界框X值中提取所有Max坐标,并使用通用的Max函数来在一次调用中确定其最大值一一比较它们。 Benchmark your code来发现优化可能性并分析其对性能的影响。请分享您的最终结果,供其他人学习。谢谢!

答案 1 :(得分:1)

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

与经典二进制搜索算法的区别在于没有数组,我们应该找到两个数字而不是一个。

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

Revit api提供了出色的Quick FilterBoundingBoxIntersectsFilter,它使用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方法返回两个XYZ点:较低和较高的点,它们仅在目标方向上改变,其他两个维度保持不变

    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();
    }