Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Fix additive-root union/subtract frame fidelity regressions

Yuechen Li2fdc1c067144
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs
index 8699e80e..9056b239 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs
@@ -149,6 +149,21 @@ public sealed class BrepBooleanTests
var second = BrepBoolean.Union(first.Value, flange);
Assert.True(second.IsSuccess);
Assert.True(BrepBindingValidator.Validate(second.Value, requireAllEdgeAndFaceBindings: true).IsSuccess);
+ Assert.False(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(second.Value, ToleranceContext.Default, out _, out _));
+ Assert.NotNull(second.Value.SafeBooleanComposition);
+ Assert.True(second.Value.SafeBooleanComposition!.OccupiedCells is { Count: > 2 });
+
+ var planarFaces = second.Value.Topology.Faces
+ .Select(face =>
+ {
+ second.Value.TryGetFaceSurfaceGeometry(face.Id, out var surface);
+ return surface;
+ })
+ .Where(surface => surface?.Kind == SurfaceGeometryKind.Plane)
+ .Select(surface => surface!.Plane!.Value)
+ .ToArray();
+ Assert.Contains(planarFaces, plane => plane.Normal.ToVector().X > 0.9d && System.Math.Abs(plane.Origin.X + 12d) < 1e-6d);
+ Assert.Contains(planarFaces, plane => plane.Normal.ToVector().X < -0.9d && System.Math.Abs(plane.Origin.X - 12d) < 1e-6d);
}
[Fact]
diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs
index eedcba2e..70b57d6f 100644
--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs
+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs
@@ -326,6 +326,32 @@ public static class BrepBoolean
if (operation == BooleanOperation.Union)
{
+ string? additiveUnsupportedReason = null;
+ if (intersections.Analysis.LeftSafeComposition is { OccupiedCells.Count: > 0 } leftComposition
+ && TryClassifyBoundedOrthogonalUnionWithExistingRoot(leftComposition, right, tolerance, out var additiveComposition, out additiveUnsupportedReason))
+ {
+ return new BooleanClassificationData(
+ intersections,
+ IsComputed: true,
+ FragmentCount: 1,
+ SingleBoxResult: null,
+ SafeCompositionResult: additiveComposition,
+ UnsupportedReason: null,
+ OrthogonalUnionCells: additiveComposition.OccupiedCells);
+ }
+
+ if (intersections.Analysis.LeftSafeComposition is { OccupiedCells.Count: > 0 }
+ && additiveUnsupportedReason is not null)
+ {
+ return new BooleanClassificationData(
+ intersections,
+ IsComputed: true,
+ FragmentCount: 0,
+ SingleBoxResult: null,
+ SafeCompositionResult: null,
+ UnsupportedReason: additiveUnsupportedReason);
+ }
+
if (left.ApproximatelyEquals(right, tolerance))
{
return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: left, SafeCompositionResult: null, UnsupportedReason: null);
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
index e0e2636d..b2b1c487 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs
@@ -391,6 +391,82 @@ public sealed class FirmamentBuildAndExportTests
Assert.Contains("MANIFOLD_SOLID_BREP", result.Value.Export.StepText, StringComparison.Ordinal);
}
+ [Fact]
+ public void Run_FrictionLabSimpleBracket_ExportedStep_Remains_NonBox_LShape()
+ {
+ var sourceText = File.ReadAllText(
+ Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/simple-bracket/part.firmament"),
+ Encoding.UTF8);
+ var compiler = new FirmamentCompiler();
+ var compilation = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(sourceText))).Compilation;
+
+ Assert.True(compilation.IsSuccess, string.Join(Environment.NewLine, compilation.Diagnostics.Select(d => d.Message)));
+ var finalBody = Assert.Single(compilation.Value.PrimitiveExecutionResult!.ExecutedBooleans, b => b.FeatureId == "bracket").Body;
+ Assert.False(Aetheris.Kernel.Core.Brep.Boolean.BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(finalBody, Aetheris.Kernel.Core.Numerics.ToleranceContext.Default, out _, out _));
+
+ var planarFaces = finalBody.Topology.Faces
+ .Select(face =>
+ {
+ finalBody.TryGetFaceSurfaceGeometry(face.Id, out var surface);
+ return surface;
+ })
+ .Where(surface => surface?.Kind == Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Plane)
+ .Select(surface => surface!.Plane!.Value)
+ .ToArray();
+
+ Assert.Contains(planarFaces, plane => Math.Abs(plane.Normal.ToVector().X) > 0.9d && Math.Abs(plane.Origin.X + 12d) < 1e-6d);
+ Assert.True(planarFaces.Length > 6, "Simple bracket must not collapse to a six-face bounding box.");
+
+ var export = FirmamentStepExporter.Export(compilation.Value);
+ Assert.True(export.IsSuccess, string.Join(Environment.NewLine, export.Diagnostics.Select(d => d.Message)));
+ Assert.True(CountOccurrences(export.Value.StepText, "ADVANCED_FACE") > 6);
+ }
+
+ [Fact]
+ public void Run_FrictionLabMountingBracketBasic_ContinuedRoot_SubtractPlacement_Uses_World_Frame()
+ {
+ var sourceText = File.ReadAllText(
+ Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/mounting-bracket-basic/part.firmament"),
+ Encoding.UTF8);
+ var compiler = new FirmamentCompiler();
+ var compilation = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(sourceText))).Compilation;
+
+ Assert.True(compilation.IsSuccess, string.Join(Environment.NewLine, compilation.Diagnostics.Select(d => d.Message)));
+ var finalBody = Assert.Single(compilation.Value.PrimitiveExecutionResult!.ExecutedBooleans, b => b.FeatureId == "upright_hole").Body;
+ Assert.NotNull(finalBody.SafeBooleanComposition);
+ var composition = finalBody.SafeBooleanComposition!;
+ Assert.Equal(2, composition.Holes.Count);
+
+ Assert.Contains(composition.Holes, hole => Math.Abs(hole.CenterX + 15d) < 1e-6d && Math.Abs(hole.CenterY) < 1e-6d);
+ Assert.Contains(composition.Holes, hole => Math.Abs(hole.CenterX - 26d) < 1e-6d && Math.Abs(hole.CenterY) < 1e-6d);
+ }
+
+ [Fact]
+ public void Run_FrictionLabMountingBracketBasic_ExportedStep_Holes_Are_At_Intended_X_Positions()
+ {
+ var fullSourcePath = Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/mounting-bracket-basic/part.firmament");
+ var result = FirmamentBuildAndExport.Run(fullSourcePath);
+
+ Assert.True(result.IsSuccess, string.Join(Environment.NewLine, result.Diagnostics.Select(d => d.Message)));
+ var import = Aetheris.Kernel.Core.Step242.Step242Importer.ImportBody(result.Value.Export.StepText);
+ Assert.True(import.IsSuccess, string.Join(Environment.NewLine, import.Diagnostics.Select(d => d.Message)));
+
+ var cylinderOriginsX = import.Value.Topology.Faces
+ .Select(face =>
+ {
+ import.Value.TryGetFaceSurfaceGeometry(face.Id, out var surface);
+ return surface;
+ })
+ .Where(surface => surface?.Kind == Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Cylinder)
+ .Select(surface => surface!.Cylinder!.Value.Origin.X)
+ .Distinct()
+ .OrderBy(x => x)
+ .ToArray();
+
+ Assert.Contains(cylinderOriginsX, x => Math.Abs(x + 15d) < 1e-6d);
+ Assert.Contains(cylinderOriginsX, x => Math.Abs(x - 26d) < 1e-6d);
+ }
+
[Theory]
[InlineData("testdata/firmament/fixtures/m10j-unsupported-box-add-cylinder.firmament", "m10j-unsupported-box-add-cylinder.step", "joined", "add")]
[InlineData("testdata/firmament/fixtures/m10j-unsupported-box-intersect-cylinder.firmament", "m10j-unsupported-box-intersect-cylinder.step", "overlap", "intersect")]
@@ -475,4 +551,17 @@ public sealed class FirmamentBuildAndExportTests
|| message.Contains("Boolean feature", StringComparison.Ordinal)
|| message.Contains("analytic hole surface kind", StringComparison.Ordinal)
|| message.Contains("fully enclosed spherical cavity", StringComparison.Ordinal);
+
+ private static int CountOccurrences(string value, string token)
+ {
+ var count = 0;
+ var index = 0;
+ while ((index = value.IndexOf(token, index, StringComparison.Ordinal)) >= 0)
+ {
+ count++;
+ index += token.Length;
+ }
+
+ return count;
+ }
}
diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
index 0989b103..932d90f3 100644
--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs
@@ -332,13 +332,22 @@ internal static class FirmamentPrimitiveExecutor
private static bool ShouldUseSemanticToolPlacement(FirmamentLoweredBoolean boolean, BrepBody baseBody, BrepBody toolBody)
{
if (boolean.Placement is null
- || string.IsNullOrWhiteSpace(boolean.Placement.OnFace)
|| boolean.Kind != FirmamentLoweredBooleanKind.Subtract
|| !string.Equals(boolean.Tool.OpName, "cylinder", StringComparison.Ordinal))
{
return false;
}
+ if (baseBody.SafeBooleanComposition is { Holes.Count: > 0 })
+ {
+ return true;
+ }
+
+ if (string.IsNullOrWhiteSpace(boolean.Placement.OnFace))
+ {
+ return false;
+ }
+
if (!BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(baseBody, ToleranceContext.Default, out var rootBox, out _)
|| !BrepBooleanCylinderRecognition.TryRecognizeCylinder(toolBody, ToleranceContext.Default, out var cylinder, out _))
{