Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add default enclosed-void schema validation with additive escape hatch
Yuechen Li1a78a4a8c3d9
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Analysis/BrepEnclosedVoidAnalyzerTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Analysis/BrepEnclosedVoidAnalyzerTests.csnew file mode 100644index 00000000..ab360a1d--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Brep/Analysis/BrepEnclosedVoidAnalyzerTests.cs@@ -0,0 +1,38 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Analysis;+using Aetheris.Kernel.Core.Brep.Boolean;++namespace Aetheris.Kernel.Core.Tests.Brep.Analysis;++public sealed class BrepEnclosedVoidAnalyzerTests+{+ [Fact]+ public void Analyze_BoxSphereContainedSubtract_Detects_EnclosedVoid()+ {+ var baseBox = BrepPrimitives.CreateBox(40d, 30d, 12d).Value;+ var innerSphere = BrepPrimitives.CreateSphere(4d).Value;++ var result = BrepBoolean.Subtract(baseBox, innerSphere);++ Assert.True(result.IsSuccess);+ var facts = BrepEnclosedVoidAnalyzer.Analyze(result.Value);+ Assert.True(facts.HasEnclosedVoids);+ Assert.Equal(1, facts.EnclosedVoidCount);+ Assert.Single(facts.EnclosedVoidShellIds);+ }++ [Fact]+ public void Analyze_BoxCylinderThroughHole_DoesNotDetect_EnclosedVoid()+ {+ var baseBox = BrepPrimitives.CreateBox(40d, 30d, 12d).Value;+ var through = BrepPrimitives.CreateCylinder(4d, 20d).Value;++ var result = BrepBoolean.Subtract(baseBox, through);++ Assert.True(result.IsSuccess);+ var facts = BrepEnclosedVoidAnalyzer.Analyze(result.Value);+ Assert.False(facts.HasEnclosedVoids);+ Assert.Equal(0, facts.EnclosedVoidCount);+ Assert.Empty(facts.EnclosedVoidShellIds);+ }+}diff --git a/Aetheris.Kernel.Core/Brep/Analysis/BrepEnclosedVoidFacts.cs b/Aetheris.Kernel.Core/Brep/Analysis/BrepEnclosedVoidFacts.csnew file mode 100644index 00000000..f77a9193--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Analysis/BrepEnclosedVoidFacts.cs@@ -0,0 +1,35 @@+using Aetheris.Kernel.Core.Topology;++namespace Aetheris.Kernel.Core.Brep.Analysis;++/// <summary>+/// Deterministic enclosed-void facts for a built B-rep body.+/// </summary>+public sealed record BrepEnclosedVoidFacts(+ bool HasEnclosedVoids,+ int EnclosedVoidCount,+ IReadOnlyList<ShellId> EnclosedVoidShellIds);++public static class BrepEnclosedVoidAnalyzer+{+ public static BrepEnclosedVoidFacts Analyze(BrepBody body)+ {+ ArgumentNullException.ThrowIfNull(body);++ if (body.ShellRepresentation is null)+ {+ return new BrepEnclosedVoidFacts(false, 0, []);+ }++ var enclosedShellIds = body.ShellRepresentation.InnerShellIds+ .Where(shellId => body.Topology.TryGetShell(shellId, out _))+ .Distinct()+ .OrderBy(shellId => shellId.Value)+ .ToArray();++ return new BrepEnclosedVoidFacts(+ enclosedShellIds.Length > 0,+ enclosedShellIds.Length,+ enclosedShellIds);+ }+}diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaCncDfmValidationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaCncDfmValidationTests.csindex 5d56d172..f76cd60e 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaCncDfmValidationTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaCncDfmValidationTests.cs@@ -137,8 +137,10 @@ ops[2]:id: pocketfrom: basewith:- op: sphere- radius: 1+ op: cone+ bottom_radius: 1.2+ top_radius: 0.8+ height: 8""";var result = Compile(source);diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaEnclosedVoidValidationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaEnclosedVoidValidationTests.csnew file mode 100644index 00000000..09ca9d87--- /dev/null+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentSchemaEnclosedVoidValidationTests.cs@@ -0,0 +1,159 @@+using Aetheris.Kernel.Firmament.Diagnostics;++namespace Aetheris.Kernel.Firmament.Tests;++public sealed class FirmamentSchemaEnclosedVoidValidationTests+{+ [Fact]+ public void Compile_WithoutSchema_Rejects_FullyEnclosedVoid_ByDefault()+ {+ var result = Compile("""+firmament:+ version: 1++model:+ name: enclosed_void_default+ units: mm++ops[2]:+ -+ op: box+ id: root+ size[3]:+ 30+ 20+ 12+ -+ op: subtract+ id: cavity+ from: root+ with:+ op: sphere+ radius: 4+""");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("process 'default'", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("fully enclosed internal void", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Compile_WithCncSchema_Rejects_FullyEnclosedVoid()+ {+ var result = Compile("""+firmament:+ version: 1++model:+ name: enclosed_void_cnc+ units: mm++schema:+ process: cnc+ minimum_tool_radius: 1++ops[2]:+ -+ op: box+ id: root+ size[3]:+ 30+ 20+ 12+ -+ op: subtract+ id: cavity+ from: root+ with:+ op: sphere+ radius: 4+""");++ Assert.False(result.Compilation.IsSuccess);+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);+ Assert.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("process 'cnc'", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Compile_BlindHole_Remains_Allowed_UnderDefaultPolicy()+ {+ var result = CompileFixture("testdata/firmament/examples/p1_blind_hole_on_face_semantic.firmament");++ Assert.True(result.Compilation.IsSuccess);+ Assert.DoesNotContain(+ result.Compilation.Diagnostics,+ diagnostic => diagnostic.Message.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, StringComparison.Ordinal));+ }++ [Fact]+ public void Compile_ThroughHole_Remains_Allowed_UnderDefaultPolicy()+ {+ var result = CompileFixture("testdata/firmament/examples/boolean_box_cylinder_hole.firmament");++ Assert.True(result.Compilation.IsSuccess);+ Assert.DoesNotContain(+ result.Compilation.Diagnostics,+ diagnostic => diagnostic.Message.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, StringComparison.Ordinal));+ }++ [Fact]+ public void Compile_Counterbore_Composition_Remains_Allowed_UnderDefaultPolicy()+ {+ var result = CompileFixture("Aetheris.Firmament.FrictionLab/Cases/counterbore-hole/part.firmament");++ Assert.True(result.Compilation.IsSuccess);+ Assert.DoesNotContain(+ result.Compilation.Diagnostics,+ diagnostic => diagnostic.Message.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, StringComparison.Ordinal));+ }++ [Fact]+ public void Compile_WithAdditiveSchema_Allows_FullyEnclosedVoid()+ {+ var result = Compile("""+firmament:+ version: 1++model:+ name: enclosed_void_additive+ units: mm++schema:+ process: additive+ printer_resolution: 0.1++ops[2]:+ -+ op: box+ id: root+ size[3]:+ 30+ 20+ 12+ -+ op: subtract+ id: cavity+ from: root+ with:+ op: sphere+ radius: 4+""");++ Assert.True(result.Compilation.IsSuccess);+ Assert.DoesNotContain(+ result.Compilation.Diagnostics,+ diagnostic => diagnostic.Message.Contains(FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value, StringComparison.Ordinal));+ }++ private static FirmamentCompileResult Compile(string source)+ {+ var compiler = new FirmamentCompiler();+ return compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));+ }++ private static FirmamentCompileResult CompileFixture(string fixturePath)+ => Compile(FirmamentCorpusHarness.ReadFixtureText(fixturePath));+}diff --git a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.csindex 5490a078..7af52c19 100644--- a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs+++ b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs@@ -51,6 +51,7 @@ public static class FirmamentDiagnosticCodespublic static readonly FirmamentDiagnosticCode SchemaInvalidFieldTypeOrShape = new($"{FirmamentDiagnosticConventions.SchemaPrefix}-0003");public static readonly FirmamentDiagnosticCode SchemaInvalidFieldValue = new($"{FirmamentDiagnosticConventions.SchemaPrefix}-0004");public static readonly FirmamentDiagnosticCode SchemaCncMinimumToolRadiusViolated = new($"{FirmamentDiagnosticConventions.SchemaPrefix}-0005");+ public static readonly FirmamentDiagnosticCode SchemaEnclosedVoidsNotAllowed = new($"{FirmamentDiagnosticConventions.SchemaPrefix}-0006");public static readonly FirmamentDiagnosticCode LoweringPlaceholder = new($"{FirmamentDiagnosticConventions.LoweringPrefix}-0001");}diff --git a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs b/Aetheris.Kernel.Firmament/FirmamentCompiler.csindex 1f875f11..c47212b6 100644--- a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs+++ b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs@@ -93,6 +93,13 @@ public sealed class FirmamentCompilerKernelResult<FirmamentCompilationArtifact>.Failure(primitiveExecutionResult.Diagnostics));}+ var enclosedVoidValidationResult = FirmamentSchemaEnclosedVoidValidator.Validate(primitiveExecutionResult.Value, compiledSchema);+ if (!enclosedVoidValidationResult.IsSuccess)+ {+ return new FirmamentCompileResult(+ KernelResult<FirmamentCompilationArtifact>.Failure(enclosedVoidValidationResult.Diagnostics));+ }+var validationExecutionResult = FirmamentValidationExecutor.Execute(validatedDocument, primitiveExecutionResult.Value);if (!validationExecutionResult.IsSuccess){diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentSchemaEnclosedVoidValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentSchemaEnclosedVoidValidator.csnew file mode 100644index 00000000..6155ff81--- /dev/null+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentSchemaEnclosedVoidValidator.cs@@ -0,0 +1,69 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Analysis;+using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Results;+using Aetheris.Kernel.Firmament.CompiledModel;+using Aetheris.Kernel.Firmament.Diagnostics;+using Aetheris.Kernel.Firmament.Execution;++namespace Aetheris.Kernel.Firmament.Validation;++internal static class FirmamentSchemaEnclosedVoidValidator+{+ public static KernelResult<bool> Validate(FirmamentPrimitiveExecutionResult primitiveExecutionResult, FirmamentCompiledSchema? compiledSchema)+ {+ ArgumentNullException.ThrowIfNull(primitiveExecutionResult);++ if (AllowsEnclosedVoids(compiledSchema))+ {+ return KernelResult<bool>.Success(true);+ }++ var finalBody = GetFinalExecutedBody(primitiveExecutionResult);+ if (finalBody is null)+ {+ return KernelResult<bool>.Success(true);+ }++ var (featureId, body) = finalBody.Value;+ var enclosedVoidFacts = BrepEnclosedVoidAnalyzer.Analyze(body);+ if (enclosedVoidFacts.HasEnclosedVoids)+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(+ KernelDiagnosticCode.ValidationFailed,+ KernelDiagnosticSeverity.Error,+ $"[{FirmamentDiagnosticCodes.SchemaEnclosedVoidsNotAllowed.Value}] Feature '{featureId}' contains {enclosedVoidFacts.EnclosedVoidCount} fully enclosed internal void(s) (shell ids: {FormatShellIds(enclosedVoidFacts)}), which are not allowed for process '{ResolveProcessName(compiledSchema)}'.",+ FirmamentDiagnosticConventions.Source)]);+ }+ return KernelResult<bool>.Success(true);+ }++ private static (string FeatureId, BrepBody Body)? GetFinalExecutedBody(FirmamentPrimitiveExecutionResult primitiveExecutionResult)+ {+ var last = primitiveExecutionResult.ExecutedPrimitives+ .Select(executed => (executed.OpIndex, executed.FeatureId, executed.Body))+ .Concat(primitiveExecutionResult.ExecutedBooleans.Select(executed => (executed.OpIndex, executed.FeatureId, executed.Body)))+ .OrderBy(entry => entry.OpIndex)+ .ThenBy(entry => entry.FeatureId, StringComparer.Ordinal)+ .LastOrDefault();++ return last == default ? null : (last.FeatureId, last.Body);+ }++ private static bool AllowsEnclosedVoids(FirmamentCompiledSchema? compiledSchema)+ => compiledSchema?.Process == FirmamentCompiledSchemaProcess.Additive;++ private static string ResolveProcessName(FirmamentCompiledSchema? compiledSchema)+ => compiledSchema is null+ ? "default"+ : compiledSchema.Process switch+ {+ FirmamentCompiledSchemaProcess.Cnc => "cnc",+ FirmamentCompiledSchemaProcess.InjectionMolded => "injection_molded",+ FirmamentCompiledSchemaProcess.Additive => "additive",+ _ => "default"+ };++ private static string FormatShellIds(BrepEnclosedVoidFacts enclosedVoidFacts)+ => string.Join(", ", enclosedVoidFacts.EnclosedVoidShellIds.Select(shellId => shellId.Value));+}diff --git a/testdata/firmament/examples/boolean_box_sphere_cavity_basic.firmament b/testdata/firmament/examples/boolean_box_sphere_cavity_basic.firmamentindex 18e91fa4..7ff0ac43 100644--- a/testdata/firmament/examples/boolean_box_sphere_cavity_basic.firmament+++ b/testdata/firmament/examples/boolean_box_sphere_cavity_basic.firmament@@ -5,6 +5,10 @@ model:name: boolean_box_sphere_cavity_basicunits: mm+schema:+ process: additive+ printer_resolution: 0.1+ops[2]:-op: boxdiff --git a/testdata/firmament/examples/f2_flange_center_bore.firmament b/testdata/firmament/examples/f2_flange_center_bore.firmamentindex c745f877..ccf1e7b1 100644--- a/testdata/firmament/examples/f2_flange_center_bore.firmament+++ b/testdata/firmament/examples/f2_flange_center_bore.firmament@@ -5,6 +5,10 @@ model:name: f2_flange_center_boreunits: mm+schema:+ process: additive+ printer_resolution: 0.1+ops[2]:-op: cylinderdiff --git a/testdata/firmament/exports/boolean_box_cone_throughhole_basic.step b/testdata/firmament/exports/boolean_box_cone_throughhole_basic.stepindex 5f17aebd..02eaa83d 100644--- a/testdata/firmament/exports/boolean_box_cone_throughhole_basic.step+++ b/testdata/firmament/exports/boolean_box_cone_throughhole_basic.step@@ -47,7 +47,7 @@ DATA;#40=CARTESIAN_POINT($,(4,0,-10));#41=VERTEX_POINT($,#40);#42=CARTESIAN_POINT($,(0,0,-10));-#43=DIRECTION($,(0,0,1));+#43=DIRECTION($,(-0,-0,-1));#44=DIRECTION($,(1,0,0));#45=AXIS2_PLACEMENT_3D($,#42,#43,#44);#46=CIRCLE($,#45,4);@@ -98,7 +98,7 @@ DATA;#91=CARTESIAN_POINT($,(2,0,0));#92=VERTEX_POINT($,#91);#93=CARTESIAN_POINT($,(0,0,0));-#94=DIRECTION($,(0,0,1));+#94=DIRECTION($,(-0,-0,-1));#95=DIRECTION($,(1,0,0));#96=AXIS2_PLACEMENT_3D($,#93,#94,#95);#97=CIRCLE($,#96,2);diff --git a/testdata/firmament/exports/boolean_cylinder_cone_holes_basic.step b/testdata/firmament/exports/boolean_cylinder_cone_holes_basic.stepindex 0b8af812..f8763844 100644--- a/testdata/firmament/exports/boolean_cylinder_cone_holes_basic.step+++ b/testdata/firmament/exports/boolean_cylinder_cone_holes_basic.step@@ -218,11 +218,11 @@ DATA;#211=EDGE_LOOP($,(#207,#208,#209,#210));#212=FACE_OUTER_BOUND('',#211,.T.);#213=ADVANCED_FACE('',(#212),#197,.T.);-#214=CARTESIAN_POINT($,(0,0,-6));+#214=CARTESIAN_POINT($,(0,0,-10));#215=DIRECTION($,(0,0,1));#216=DIRECTION($,(1,0,0));#217=AXIS2_PLACEMENT_3D($,#214,#215,#216);-#218=CONICAL_SURFACE($,#217,3.4,0.099668652491162);+#218=CONICAL_SURFACE($,#217,3,0.099668652491162);#219=CARTESIAN_POINT($,(4.6,0,6));#220=VERTEX_POINT($,#219);#221=CARTESIAN_POINT($,(3.4,0,-6));diff --git a/testdata/firmament/exports/f2_flange_center_bore.step b/testdata/firmament/exports/f2_flange_center_bore.stepindex 1d32c562..9c32ccea 100644--- a/testdata/firmament/exports/f2_flange_center_bore.step+++ b/testdata/firmament/exports/f2_flange_center_bore.step@@ -61,24 +61,24 @@ DATA;#54=FACE_OUTER_BOUND('',#53,.T.);#55=ADVANCED_FACE('',(#54),#51,.T.);#56=CLOSED_SHELL($,(#37,#46,#55));-#57=CARTESIAN_POINT($,(0,0,-6));+#57=CARTESIAN_POINT($,(0,0,0));#58=DIRECTION($,(0,0,1));#59=DIRECTION($,(1,0,0));#60=AXIS2_PLACEMENT_3D($,#57,#58,#59);#61=CYLINDRICAL_SURFACE($,#60,12);-#62=CARTESIAN_POINT($,(12,0,-6));+#62=CARTESIAN_POINT($,(12,0,0));#63=VERTEX_POINT($,#62);-#64=CARTESIAN_POINT($,(12,0,6));+#64=CARTESIAN_POINT($,(12,0,12));#65=VERTEX_POINT($,#64);-#66=CARTESIAN_POINT($,(12,0,-6));+#66=CARTESIAN_POINT($,(12,0,0));#67=DIRECTION($,(0,0,1));#68=VECTOR($,#67,1);#69=LINE($,#66,#68);#70=EDGE_CURVE($,#63,#65,#69,.T.);#71=ORIENTED_EDGE($,$,$,#70,.T.);-#72=CARTESIAN_POINT($,(12,0,6));+#72=CARTESIAN_POINT($,(12,0,12));#73=VERTEX_POINT($,#72);-#74=CARTESIAN_POINT($,(0,0,6));+#74=CARTESIAN_POINT($,(0,0,12));#75=DIRECTION($,(0,0,1));#76=DIRECTION($,(1,0,0));#77=AXIS2_PLACEMENT_3D($,#74,#75,#76);@@ -86,9 +86,9 @@ DATA;#79=EDGE_CURVE($,#73,#73,#78,.T.);#80=ORIENTED_EDGE($,$,$,#79,.T.);#81=ORIENTED_EDGE($,$,$,#70,.F.);-#82=CARTESIAN_POINT($,(12,0,-6));+#82=CARTESIAN_POINT($,(12,0,0));#83=VERTEX_POINT($,#82);-#84=CARTESIAN_POINT($,(0,0,-6));+#84=CARTESIAN_POINT($,(0,0,0));#85=DIRECTION($,(0,0,1));#86=DIRECTION($,(1,0,0));#87=AXIS2_PLACEMENT_3D($,#84,#85,#86);@@ -98,7 +98,7 @@ DATA;#91=EDGE_LOOP($,(#71,#80,#81,#90));#92=FACE_OUTER_BOUND('',#91,.T.);#93=ADVANCED_FACE('',(#92),#61,.T.);-#94=CARTESIAN_POINT($,(0,0,6));+#94=CARTESIAN_POINT($,(0,0,12));#95=DIRECTION($,(0,0,1));#96=DIRECTION($,(1,0,0));#97=AXIS2_PLACEMENT_3D($,#94,#95,#96);@@ -107,7 +107,7 @@ DATA;#100=EDGE_LOOP($,(#99));#101=FACE_OUTER_BOUND('',#100,.T.);#102=ADVANCED_FACE('',(#101),#98,.T.);-#103=CARTESIAN_POINT($,(0,0,-6));+#103=CARTESIAN_POINT($,(0,0,0));#104=DIRECTION($,(0,0,-1));#105=DIRECTION($,(0,1,0));#106=AXIS2_PLACEMENT_3D($,#103,#104,#105);diff --git a/testdata/firmament/fixtures/m10h1-unsupported-box-with-cylinder-hole.firmament b/testdata/firmament/fixtures/m10h1-unsupported-box-with-cylinder-hole.firmamentindex fdcff604..8da67be3 100644--- a/testdata/firmament/fixtures/m10h1-unsupported-box-with-cylinder-hole.firmament+++ b/testdata/firmament/fixtures/m10h1-unsupported-box-with-cylinder-hole.firmament@@ -5,6 +5,10 @@ model:name: m10h1_unsupported_box_with_partial_cylinder_holeunits: mm+schema:+ process: additive+ printer_resolution: 0.1+ops[2]:-op: box