gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:51:13 24 4
gpt4 key购买 nike

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

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


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

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

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

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

我好像走错了路。有没有正确的方法用revit api工具制作它

最佳答案

要找到边界,我们可以利用二分查找的思想。

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

几何空间中的元素可以表示为 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方法返回两个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();
}

关于geometry - Revit API。如何获得多个元素的边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63083938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com