添加项目

This commit is contained in:
GG Z
2026-01-01 10:02:59 +08:00
parent 1fd8d2ced7
commit 4df4ce1e6a
105 changed files with 4437 additions and 570 deletions

View File

@@ -4,54 +4,64 @@ using Autodesk.Revit.DB;
namespace ShrlAlgoToolkit.Revit.Assists
{
internal class GeometryAssist
public class GeometryAssist
{
/// <summary>
///获取元素的包围
/// 获取多个元素的合并包围
/// </summary>
/// <param name="elements"></param>
/// <returns></returns>
public static BoundingBoxXYZ GetBoundingBox(List<Element> elements)
/// <param name="elements">元素集合</param>
/// <returns>合并后的 BoundingBoxXYZ如果集合为空或无有效包围盒则返回 null</returns>
public static BoundingBoxXYZ GetCombinedBoundingBox(IEnumerable<Element> elements)
{
if (elements == null || elements.Count == 0)
if (elements == null) return null;
double minX = double.MaxValue;
double minY = double.MaxValue;
double minZ = double.MaxValue;
double maxX = double.MinValue;
double maxY = double.MinValue;
double maxZ = double.MinValue;
bool hasValidBox = false;
foreach (var elem in elements)
{
return new BoundingBoxXYZ();
// 传入 null 表示获取模型坐标系下的包围盒
BoundingBoxXYZ box = elem.get_BoundingBox(null);
// 注意:有些元素(如某些基准面或未放置的族实例)可能返回 null
if (box == null) continue;
// 比较并更新最小值
if (box.Min.X < minX) minX = box.Min.X;
if (box.Min.Y < minY) minY = box.Min.Y;
if (box.Min.Z < minZ) minZ = box.Min.Z;
// 比较并更新最大值
if (box.Max.X > maxX) maxX = box.Max.X;
if (box.Max.Y > maxY) maxY = box.Max.Y;
if (box.Max.Z > maxZ) maxZ = box.Max.Z;
hasValidBox = true;
}
var gmax = XYZ.Zero;
var gmin = XYZ.Zero;
var elementIds = elements.Select(e => e.Id).ToList();
var doc = elements.FirstOrDefault()?.Document;
using (SubTransaction sub = new SubTransaction(doc))
{
sub.Start();
Group g;
if (doc.IsFamilyDocument)
{
g = doc.FamilyCreate.NewGroup(elementIds);
if (!hasValidBox) return null;
}
else
{
g = doc.Create.NewGroup(elementIds);
}
gmax = g.get_BoundingBox(null).Max;
gmin = g.get_BoundingBox(null).Min;
sub.RollBack();
}
return new BoundingBoxXYZ
{
Max = gmax,
Min = gmin
};
// 构造新的包围盒
BoundingBoxXYZ combinedBox = new BoundingBoxXYZ();
combinedBox.Min = new XYZ(minX, minY, minZ);
combinedBox.Max = new XYZ(maxX, maxY, maxZ);
return combinedBox;
}
/// <summary>
/// 对曲线列表进行排序,使其正确排序和定向,形成线串。
/// </summary>
public static void SortCurvesContiguous(IList<Curve> curves, bool debug_output = false)
{
const double _inch = 1.0 / 12.0;
const double _sixteenth = _inch / 16.0;
const double inch = 1.0 / 12.0;
const double sixteenth = inch / 16.0;
int n = curves.Count;
for (int i = 0; i < n; ++i)
{
@@ -71,7 +81,7 @@ namespace ShrlAlgoToolkit.Revit.Assists
// 如果匹配 end->start、
// 这是下一条曲线
if (_sixteenth > p.DistanceTo(endPoint))
if (sixteenth > p.DistanceTo(endPoint))
{
if (debug_output)
{
@@ -93,7 +103,7 @@ namespace ShrlAlgoToolkit.Revit.Assists
// 如果有匹配结果 end->end、
// 反转下一条曲线
if (_sixteenth > p.DistanceTo(endPoint))
if (sixteenth > p.DistanceTo(endPoint))
{
if (i + 1 == j)
{