Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add Firmament STEP export golden path

Yuechen Lif2238b296792
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs
new file mode 100644
index 00000000..1f082ac1
--- /dev/null
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs
@@ -0,0 +1,178 @@
+using Aetheris.Kernel.Core.Step242;
+
+namespace Aetheris.Kernel.Firmament.Tests;
+
+public sealed class FirmamentStepExporterTests
+{
+ [Fact]
+ public void Export_SingleBoxFixture_Produces_NonEmpty_Deterministic_StepText()
+ {
+ var source = FirmamentCorpusHarness.ReadFixtureText("testdata/firmament/fixtures/m10a-valid-single-box-export.firmament");
+
+ var first = Export(source);
+ var second = Export(source);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal(FirmamentStepExporter.LastExecutedGeometricBodyPolicy, first.Value.ExportBodyPolicy);
+ Assert.Equal("base", first.Value.ExportedFeatureId);
+ Assert.NotEmpty(first.Value.StepText);
+ Assert.Contains("ISO-10303-21", first.Value.StepText, StringComparison.Ordinal);
+ Assert.Contains("MANIFOLD_SOLID_BREP", first.Value.StepText, StringComparison.Ordinal);
+ Assert.Equal(first.Value.StepText, second.Value.StepText);
+ }
+
+ [Fact]
+ public void Export_PlacedPrimitiveFixture_Exports_Successfully()
+ {
+ var result = ExportFixture("testdata/firmament/fixtures/m10a-valid-placed-box-export.firmament");
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("placed_box", result.Value.ExportedFeatureId);
+ Assert.Contains("CARTESIAN_POINT", result.Value.StepText, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void Export_BooleanFixture_Exports_Successfully()
+ {
+ var result = ExportFixture("testdata/firmament/fixtures/m10a-valid-boolean-export.firmament");
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("joined", result.Value.ExportedFeatureId);
+ Assert.Contains("ADVANCED_FACE", result.Value.StepText, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void Export_MultipleExecutedGeometryBodies_Selects_Last_Executed_Geometric_Feature_Body()
+ {
+ var compile = CompileFixture("testdata/firmament/fixtures/m3d-mixed-primitive-boolean-validation.firmament");
+ Assert.True(compile.Compilation.IsSuccess);
+
+ var export = FirmamentStepExporter.Export(compile.Compilation.Value);
+ Assert.True(export.IsSuccess);
+ Assert.Equal("cap", export.Value.ExportedFeatureId);
+ Assert.Equal(2, export.Value.ExportedOpIndex);
+
+ var expected = Step242Exporter.ExportBody(
+ compile.Compilation.Value.PrimitiveExecutionResult!.ExecutedPrimitives.Single(p => p.FeatureId == "cap").Body);
+ Assert.True(expected.IsSuccess);
+ Assert.Equal(expected.Value, export.Value.StepText);
+ }
+
+ [Fact]
+ public void Export_ValidationOps_Do_Not_Become_Export_Bodies()
+ {
+ const string source = """
+firmament:
+ version: 1
+
+model:
+ name: validation_only_after_box
+ units: mm
+
+ops[2]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 2
+ 3
+ 4
+ -
+ op: expect_exists
+ target: base
+""";
+
+ var result = Export(source);
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("base", result.Value.ExportedFeatureId);
+ }
+
+ [Fact]
+ public void Export_NoExecutedGeometricBody_Fails_Deterministically()
+ {
+ var first = ExportFixture("testdata/firmament/fixtures/m10a-no-export-body.firmament");
+ var second = ExportFixture("testdata/firmament/fixtures/m10a-no-export-body.firmament");
+
+ Assert.False(first.IsSuccess);
+ Assert.False(second.IsSuccess);
+ var firstDiagnostic = Assert.Single(first.Diagnostics);
+ var secondDiagnostic = Assert.Single(second.Diagnostics);
+ Assert.Equal(firstDiagnostic, secondDiagnostic);
+ Assert.Contains("requires at least one executed primitive or boolean body", firstDiagnostic.Message, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void Export_ValidationFailure_Prevents_Export()
+ {
+ var result = ExportFixture("testdata/firmament/fixtures/m1a-invalid-sphere-missing-radius.firmament");
+
+ Assert.False(result.IsSuccess);
+ var diagnostic = Assert.Single(result.Diagnostics);
+ Assert.Contains("[FIRM-STRUCT-0009]", diagnostic.Message, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void Export_SchemaPresence_Does_Not_Change_Step_Semantics()
+ {
+ const string baseline = """
+firmament:
+ version: 1
+
+model:
+ name: schema_semantics_baseline
+ units: mm
+
+ops[1]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 5
+ 6
+ 7
+""";
+
+ const string withSchema = """
+firmament:
+ version: 1
+
+model:
+ name: schema_semantics_with_schema
+ units: mm
+
+schema:
+ process: additive
+ printer_resolution: 0.1
+
+ops[1]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 5
+ 6
+ 7
+""";
+
+ var first = Export(baseline);
+ var second = Export(withSchema);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal(first.Value.StepText, second.Value.StepText);
+ }
+
+ private static Aetheris.Kernel.Core.Results.KernelResult<FirmamentStepExportResult> ExportFixture(string fixturePath) =>
+ Export(FirmamentCorpusHarness.ReadFixtureText(fixturePath));
+
+ private static Aetheris.Kernel.Core.Results.KernelResult<FirmamentStepExportResult> Export(string source)
+ {
+ var request = new FirmamentCompileRequest(new FirmamentSourceDocument(source));
+ return FirmamentStepExporter.Export(request);
+ }
+
+ private static FirmamentCompileResult CompileFixture(string fixturePath) =>
+ FirmamentCorpusHarness.Compile(FirmamentCorpusHarness.ReadFixtureText(fixturePath));
+}
diff --git a/Aetheris.Kernel.Firmament/FirmamentStepExportResult.cs b/Aetheris.Kernel.Firmament/FirmamentStepExportResult.cs
new file mode 100644
index 00000000..e716d4f8
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/FirmamentStepExportResult.cs
@@ -0,0 +1,7 @@
+namespace Aetheris.Kernel.Firmament;
+
+public sealed record FirmamentStepExportResult(
+ string StepText,
+ string ExportedFeatureId,
+ int ExportedOpIndex,
+ string ExportBodyPolicy = FirmamentStepExporter.LastExecutedGeometricBodyPolicy);
diff --git a/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs b/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs
new file mode 100644
index 00000000..1a930c22
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs
@@ -0,0 +1,100 @@
+using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Diagnostics;
+using Aetheris.Kernel.Core.Results;
+using Aetheris.Kernel.Core.Step242;
+using Aetheris.Kernel.Firmament.Diagnostics;
+using Aetheris.Kernel.Firmament.Execution;
+
+namespace Aetheris.Kernel.Firmament;
+
+public static class FirmamentStepExporter
+{
+ public const string LastExecutedGeometricBodyPolicy = "last-executed-geometric-body";
+
+ public static KernelResult<FirmamentStepExportResult> Export(FirmamentCompileRequest request)
+ {
+ ArgumentNullException.ThrowIfNull(request);
+
+ var compiler = new FirmamentCompiler();
+ var compileResult = compiler.Compile(request);
+ if (!compileResult.Compilation.IsSuccess)
+ {
+ return KernelResult<FirmamentStepExportResult>.Failure(compileResult.Compilation.Diagnostics);
+ }
+
+ return Export(compileResult.Compilation.Value);
+ }
+
+ public static KernelResult<FirmamentStepExportResult> Export(FirmamentCompilationArtifact artifact)
+ {
+ ArgumentNullException.ThrowIfNull(artifact);
+
+ var selectionResult = SelectExportBody(artifact.PrimitiveExecutionResult);
+ if (!selectionResult.IsSuccess)
+ {
+ return KernelResult<FirmamentStepExportResult>.Failure(selectionResult.Diagnostics);
+ }
+
+ var stepResult = Step242Exporter.ExportBody(selectionResult.Value.Body);
+ if (!stepResult.IsSuccess)
+ {
+ return KernelResult<FirmamentStepExportResult>.Failure(stepResult.Diagnostics);
+ }
+
+ return KernelResult<FirmamentStepExportResult>.Success(
+ new FirmamentStepExportResult(
+ stepResult.Value,
+ selectionResult.Value.FeatureId,
+ selectionResult.Value.OpIndex));
+ }
+
+ private static KernelResult<ExportBodySelection> SelectExportBody(FirmamentPrimitiveExecutionResult? primitiveExecutionResult)
+ {
+ if (primitiveExecutionResult is null)
+ {
+ return Failure("Firmament STEP export requires a completed primitive execution result.");
+ }
+
+ ExportBodySelection? selected = null;
+
+ foreach (var primitive in primitiveExecutionResult.ExecutedPrimitives)
+ {
+ selected = SelectLater(
+ selected,
+ new ExportBodySelection(primitive.OpIndex, primitive.FeatureId, primitive.Body));
+ }
+
+ foreach (var boolean in primitiveExecutionResult.ExecutedBooleans)
+ {
+ selected = SelectLater(
+ selected,
+ new ExportBodySelection(boolean.OpIndex, boolean.FeatureId, boolean.Body));
+ }
+
+ return selected is null
+ ? Failure("Firmament STEP export requires at least one executed primitive or boolean body.")
+ : KernelResult<ExportBodySelection>.Success(selected);
+ }
+
+ private static ExportBodySelection SelectLater(ExportBodySelection? current, ExportBodySelection candidate)
+ {
+ if (current is null)
+ {
+ return candidate;
+ }
+
+ return candidate.OpIndex > current.OpIndex ? candidate : current;
+ }
+
+ private static KernelResult<ExportBodySelection> Failure(string message) =>
+ KernelResult<ExportBodySelection>.Failure(
+ [
+ new KernelDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ KernelDiagnosticSeverity.Error,
+ message,
+ FirmamentDiagnosticConventions.Source)
+ ]);
+
+ private sealed record ExportBodySelection(int OpIndex, string FeatureId, BrepBody Body);
+}
diff --git a/testdata/firmament/fixtures/m10a-no-export-body.firmament b/testdata/firmament/fixtures/m10a-no-export-body.firmament
new file mode 100644
index 00000000..4037090e
--- /dev/null
+++ b/testdata/firmament/fixtures/m10a-no-export-body.firmament
@@ -0,0 +1,8 @@
+firmament:
+ version: 1
+
+model:
+ name: m10a_no_export_body
+ units: mm
+
+ops[0]:
diff --git a/testdata/firmament/fixtures/m10a-valid-boolean-export.firmament b/testdata/firmament/fixtures/m10a-valid-boolean-export.firmament
new file mode 100644
index 00000000..a6f13d18
--- /dev/null
+++ b/testdata/firmament/fixtures/m10a-valid-boolean-export.firmament
@@ -0,0 +1,26 @@
+firmament:
+ version: 1
+
+model:
+ name: m10a_boolean_export
+ units: mm
+
+ops[2]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 4
+ 4
+ 4
+
+ -
+ op: add
+ id: joined
+ to: base
+ with:
+ op: box
+ size[3]:
+ 2
+ 2
+ 2
diff --git a/testdata/firmament/fixtures/m10a-valid-placed-box-export.firmament b/testdata/firmament/fixtures/m10a-valid-placed-box-export.firmament
new file mode 100644
index 00000000..d3fae3bd
--- /dev/null
+++ b/testdata/firmament/fixtures/m10a-valid-placed-box-export.firmament
@@ -0,0 +1,21 @@
+firmament:
+ version: 1
+
+model:
+ name: m10a_placed_box_export
+ units: mm
+
+ops[1]:
+ -
+ op: box
+ id: placed_box
+ size[3]:
+ 8
+ 6
+ 4
+ place:
+ on: origin
+ offset[3]:
+ 2
+ -3
+ 5
diff --git a/testdata/firmament/fixtures/m10a-valid-single-box-export.firmament b/testdata/firmament/fixtures/m10a-valid-single-box-export.firmament
new file mode 100644
index 00000000..741508b9
--- /dev/null
+++ b/testdata/firmament/fixtures/m10a-valid-single-box-export.firmament
@@ -0,0 +1,15 @@
+firmament:
+ version: 1
+
+model:
+ name: m10a_single_box_export
+ units: mm
+
+ops[1]:
+ -
+ op: box
+ id: base
+ size[3]:
+ 10
+ 20
+ 30