Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add semantic PMI core model and legacy hole demo migration seam
Yuechen Liec7f98ab9a84
diff --git a/Aetheris.Kernel.Core.Tests/Pmi/PmiModelTests.cs b/Aetheris.Kernel.Core.Tests/Pmi/PmiModelTests.csnew file mode 100644index 00000000..f83fc8ae--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Pmi/PmiModelTests.cs@@ -0,0 +1,76 @@+using Aetheris.Kernel.Core.Pmi;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Tests.Pmi;++public sealed class PmiModelTests+{+ [Fact]+ public void PmiModel_CanDeclarePlanarDatumFeature()+ {+ var model = PmiModel.Empty("part_main")+ .AddDatum(new PmiDatumFeature(+ "datum:A",+ "A",+ PmiDatumFeatureKind.Planar,+ new PmiPlanarFaceReference("base", "base.top")));++ var datum = Assert.Single(model.DatumFeatures);+ Assert.Equal("A", datum.Label);+ Assert.Equal(PmiDatumFeatureKind.Planar, datum.Kind);+ var planar = Assert.IsType<PmiPlanarFaceReference>(datum.Target);+ Assert.Equal("base.top", planar.Selector);+ }++ [Fact]+ public void PmiModel_CanRepresentLinearDistanceToDatumPlane()+ {+ var model = PmiModel.Empty("part_main")+ .AddDatum(new PmiDatumFeature(+ "datum:A",+ "A",+ PmiDatumFeatureKind.Planar,+ new PmiPlanarFaceReference("base", "base.top")))+ .AddDimension(new PmiDimension(+ "distance:hole_to_A",+ PmiDimensionKind.LinearDistanceToDatum,+ new PmiCylindricalFeatureReference("main_hole", "through_or_blind_cylindrical"),+ new PmiDatumReference("datum:A"),+ 12.5d,+ SourceTag: "distance-to-datum"));++ var dimension = Assert.Single(model.Dimensions);+ Assert.Equal(PmiDimensionKind.LinearDistanceToDatum, dimension.Kind);+ Assert.IsType<PmiCylindricalFeatureReference>(dimension.PrimaryReference);+ var datumRef = Assert.IsType<PmiDatumReference>(dimension.SecondaryReference);+ Assert.Equal("datum:A", datumRef.DatumId);+ }++ [Fact]+ public void PmiStep242Adapter_ConvertsPlanarDatumAndDiameterDimension_ToLegacyStepPayload()+ {+ var model = PmiModel.Empty("part_main")+ .AddDatum(new PmiDatumFeature(+ "datum:A",+ "A",+ PmiDatumFeatureKind.Planar,+ new PmiPlanarFaceReference("base", "base.top")))+ .AddDimension(new PmiDimension(+ "diameter:hole_1",+ PmiDimensionKind.Diameter,+ new PmiCylindricalFeatureReference("hole_1", "through_or_blind_cylindrical"),+ null,+ 10d,+ SourceTag: "legacy-auto-hole-demo"));++ var converted = PmiStep242Adapter.ToStep242SemanticPmi(model);++ var datum = Assert.IsType<Step242SemanticPmiDatum>(Assert.Single(converted.OfType<Step242SemanticPmiDatum>()));+ Assert.Equal("A", datum.Label);+ Assert.Equal("plane", datum.DatumKind);++ var hole = Assert.IsType<Step242SemanticPmiHole>(Assert.Single(converted.OfType<Step242SemanticPmiHole>()));+ Assert.Equal("hole_1", hole.FeatureId);+ Assert.Equal(10d, hole.Diameter);+ }+}diff --git a/Aetheris.Kernel.Core/Pmi/PmiModel.cs b/Aetheris.Kernel.Core/Pmi/PmiModel.csnew file mode 100644index 00000000..ecd0295c--- /dev/null+++ b/Aetheris.Kernel.Core/Pmi/PmiModel.cs@@ -0,0 +1,61 @@+namespace Aetheris.Kernel.Core.Pmi;++/// <summary>+/// Kernel-owned semantic PMI container scoped to one logical body/model export context.+/// </summary>+public sealed record PmiModel(+ string BodyFeatureId,+ IReadOnlyList<PmiDatumFeature> DatumFeatures,+ IReadOnlyList<PmiDimension> Dimensions)+{+ public static PmiModel Empty(string bodyFeatureId)+ => new(bodyFeatureId, [], []);++ public PmiModel AddDatum(PmiDatumFeature datum)+ => this with { DatumFeatures = DatumFeatures.Concat([datum]).ToArray() };++ public PmiModel AddDimension(PmiDimension dimension)+ => this with { Dimensions = Dimensions.Concat([dimension]).ToArray() };+}++public enum PmiDatumFeatureKind+{+ Planar+}++public sealed record PmiDatumFeature(+ string DatumId,+ string Label,+ PmiDatumFeatureKind Kind,+ PmiSemanticReference Target);++public enum PmiDimensionKind+{+ Diameter,+ LinearDistanceToDatum+}++public sealed record PmiDimension(+ string DimensionId,+ PmiDimensionKind Kind,+ PmiSemanticReference PrimaryReference,+ PmiSemanticReference? SecondaryReference,+ double NominalValue,+ string? SourceTag = null);++public abstract record PmiSemanticReference;++/// <summary>+/// Semantic reference to a planar target selected from a feature context.+/// </summary>+public sealed record PmiPlanarFaceReference(string FeatureId, string Selector) : PmiSemanticReference;++/// <summary>+/// Semantic reference to a cylindrical/hole-like feature.+/// </summary>+public sealed record PmiCylindricalFeatureReference(string FeatureId, string? Family = null) : PmiSemanticReference;++/// <summary>+/// Semantic reference to an already declared datum feature.+/// </summary>+public sealed record PmiDatumReference(string DatumId) : PmiSemanticReference;diff --git a/Aetheris.Kernel.Core/Pmi/PmiStep242Adapter.cs b/Aetheris.Kernel.Core/Pmi/PmiStep242Adapter.csnew file mode 100644index 00000000..9521abcb--- /dev/null+++ b/Aetheris.Kernel.Core/Pmi/PmiStep242Adapter.cs@@ -0,0 +1,48 @@+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Pmi;++/// <summary>+/// Bounded adapter from kernel semantic PMI entities into current STEP242 semantic PMI payload records.+/// This is the migration seam while STEP export still consumes legacy Step242SemanticPmi records.+/// </summary>+public static class PmiStep242Adapter+{+ public static IReadOnlyList<Step242SemanticPmi> ToStep242SemanticPmi(PmiModel model, IReadOnlyList<Step242SemanticPmiNote>? passthroughNotes = null)+ {+ ArgumentNullException.ThrowIfNull(model);++ var result = new List<Step242SemanticPmi>(model.DatumFeatures.Count + model.Dimensions.Count + (passthroughNotes?.Count ?? 0));++ foreach (var datum in model.DatumFeatures)+ {+ if (datum.Kind == PmiDatumFeatureKind.Planar+ && datum.Target is PmiPlanarFaceReference planar)+ {+ result.Add(new Step242SemanticPmiDatum(datum.DatumId, "plane", datum.Label, planar.Selector));+ }+ }++ foreach (var dimension in model.Dimensions)+ {+ if (dimension.Kind == PmiDimensionKind.Diameter+ && dimension.PrimaryReference is PmiCylindricalFeatureReference cylinder)+ {+ result.Add(new Step242SemanticPmiHole(+ cylinder.FeatureId,+ dimension.NominalValue,+ null,+ cylinder.Family,+ null,+ null));+ }+ }++ if (passthroughNotes is not null)+ {+ result.AddRange(passthroughNotes);+ }++ return result;+ }+}diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPmiTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPmiTests.csindex a4683641..6200444b 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPmiTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPmiTests.cs@@ -1,3 +1,4 @@+using Aetheris.Kernel.Core.Pmi;using Aetheris.Kernel.Core.Diagnostics;using Aetheris.Kernel.Core.Results;@@ -41,6 +42,27 @@ public sealed class FirmamentPmiTestsAssert.Contains("unknown selector root feature 'missing'", diagnostic.Message, StringComparison.Ordinal);}++ [Fact]+ public void LegacyAutoHoleDemo_BuildsSemanticDiameterDimensions_InKernelPmiModel()+ {+ var compile = CompileFixture("testdata/firmament/examples/boolean_two_cylinder_holes_basic.firmament");+ Assert.True(compile.Compilation.IsSuccess);++ var loweringPlan = Assert.IsType<FirmamentCompilationArtifact>(compile.Compilation.Value).PrimitiveLoweringPlan;+ Assert.NotNull(loweringPlan);++ var model = FirmamentStepExporter.BuildLegacyAutoHolePmiModel(loweringPlan!, selectionFeatureId: "hole_b");++ Assert.Equal(2, model.Dimensions.Count);+ Assert.All(model.Dimensions, d => Assert.Equal(PmiDimensionKind.Diameter, d.Kind));+ Assert.All(model.Dimensions, d => Assert.Equal("legacy-auto-hole-demo", d.SourceTag));+ Assert.Collection(+ model.Dimensions,+ first => Assert.Equal("diameter:hole_a", first.DimensionId),+ second => Assert.Equal("diameter:hole_b", second.DimensionId));+ }+private static FirmamentCompileResult CompileFixture(string fixturePath){var sourceText = FirmamentCorpusHarness.ReadFixtureText(fixturePath);diff --git a/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs b/Aetheris.Kernel.Firmament/FirmamentStepExporter.csindex f76261b9..9c90d799 100644--- a/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs+++ b/Aetheris.Kernel.Firmament/FirmamentStepExporter.cs@@ -1,5 +1,6 @@using Aetheris.Kernel.Core.Brep;using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Pmi;using Aetheris.Kernel.Core.Results;using Aetheris.Kernel.Core.Step242;using Aetheris.Kernel.Firmament.Diagnostics;@@ -39,7 +40,8 @@ public static class FirmamentStepExporterreturn KernelResult<FirmamentStepExportResult>.Failure(selectionResult.Diagnostics);}- var semanticPmi = DeriveSemanticPmi(artifact.ParsedDocument, artifact.PrimitiveLoweringPlan, artifact.PrimitiveExecutionResult, selectionResult.Value);+ var pmiModel = DerivePmiModel(artifact.ParsedDocument, artifact.PrimitiveLoweringPlan, artifact.PrimitiveExecutionResult, selectionResult.Value);+ var semanticPmi = PmiStep242Adapter.ToStep242SemanticPmi(pmiModel.Model, pmiModel.PassthroughNotes);var stepResult = Step242Exporter.ExportBody(selectionResult.Value.Body, semanticPmi);if (!stepResult.IsSuccess){@@ -116,14 +118,16 @@ public static class FirmamentStepExporterprivate const string ExportBodyCategoryPrimitive = "primitive";private const string ExportBodyCategoryBoolean = "boolean";- private static IReadOnlyList<Step242SemanticPmi> DeriveSemanticPmi(+ private static DerivedPmiPayload DerivePmiModel(FirmamentParsedDocument? parsedDocument,FirmamentPrimitiveLoweringPlan? loweringPlan,FirmamentPrimitiveExecutionResult? executionResult,ExportBodySelection selection){var explicitPmi = DeriveExplicitPmi(parsedDocument, executionResult);- if (explicitPmi.Count > 0)+ if (explicitPmi.Model.DatumFeatures.Count > 0+ || explicitPmi.Model.Dimensions.Count > 0+ || explicitPmi.PassthroughNotes.Count > 0){return explicitPmi;}@@ -132,12 +136,22 @@ public static class FirmamentStepExporter|| !string.Equals(selection.BodyCategory, ExportBodyCategoryBoolean, StringComparison.Ordinal)|| !string.Equals(selection.FeatureKind, "subtract", StringComparison.Ordinal)){- return [];+ return new DerivedPmiPayload(PmiModel.Empty(selection.FeatureId), []);}+ var model = BuildLegacyAutoHolePmiModel(loweringPlan, selection.FeatureId);++ return new DerivedPmiPayload(model, []);+ }+++ internal static PmiModel BuildLegacyAutoHolePmiModel(FirmamentPrimitiveLoweringPlan loweringPlan, string selectionFeatureId)+ {+ ArgumentNullException.ThrowIfNull(loweringPlan);+var booleansByFeatureId = loweringPlan.Booleans.ToDictionary(boolean => boolean.FeatureId, StringComparer.Ordinal);- var currentFeatureId = selection.FeatureId;- var derived = new List<(int OpIndex, Step242SemanticPmiHole Item)>();+ var currentFeatureId = selectionFeatureId;+ var derived = new List<(int OpIndex, PmiDimension Item)>();while (booleansByFeatureId.TryGetValue(currentFeatureId, out var boolean)&& boolean.Kind == FirmamentLoweredBooleanKind.Subtract)@@ -147,29 +161,44 @@ public static class FirmamentStepExporter&& !string.IsNullOrWhiteSpace(radiusRaw)){var radius = FirmamentPrimitiveToolParsing.ParseScalar(radiusRaw);- derived.Add((boolean.OpIndex, new Step242SemanticPmiHole(boolean.FeatureId, radius * 2d, null, "through_or_blind_cylindrical", null, null)));+ derived.Add((+ boolean.OpIndex,+ new PmiDimension(+ $"diameter:{boolean.FeatureId}",+ PmiDimensionKind.Diameter,+ new PmiCylindricalFeatureReference(boolean.FeatureId, "through_or_blind_cylindrical"),+ null,+ radius * 2d,+ SourceTag: "legacy-auto-hole-demo")));}currentFeatureId = boolean.PrimaryReferenceFeatureId;}- return derived- .OrderBy(item => item.OpIndex)- .Select(item => item.Item)- .ToArray();+ var model = PmiModel.Empty(selectionFeatureId);+ foreach (var dimension in derived.OrderBy(item => item.OpIndex).Select(item => item.Item))+ {+ model = model.AddDimension(dimension);+ }++ return model;}- private static IReadOnlyList<Step242SemanticPmi> DeriveExplicitPmi(+ private static DerivedPmiPayload DeriveExplicitPmi(FirmamentParsedDocument? parsedDocument,FirmamentPrimitiveExecutionResult? executionResult){if (parsedDocument?.Pmi is null || parsedDocument.Pmi.Entries.Count == 0 || executionResult is null){- return [];+ return new DerivedPmiPayload(PmiModel.Empty("unspecified"), []);}var featureBodies = BuildFeatureBodyMap(executionResult);- var resolved = new List<Step242SemanticPmi>();+ var bodyFeatureId = executionResult.ExecutedBooleans.OrderByDescending(b => b.OpIndex).Select(b => b.FeatureId).FirstOrDefault()+ ?? executionResult.ExecutedPrimitives.OrderByDescending(p => p.OpIndex).Select(p => p.FeatureId).FirstOrDefault()+ ?? "unspecified";+ var model = PmiModel.Empty(bodyFeatureId);+ var passthroughNotes = new List<Step242SemanticPmiNote>();foreach (var entry in parsedDocument.Pmi.Entries){@@ -195,13 +224,13 @@ public static class FirmamentStepExporterdepth = parsedDepth;}- resolved.Add(new Step242SemanticPmiHole(- featureId,+ model = model.AddDimension(new PmiDimension(+ $"diameter:{featureId}",+ PmiDimensionKind.Diameter,+ new PmiCylindricalFeatureReference(featureId, entry.RawFields.TryGetValue("hole_family", out var familyRaw) ? familyRaw : null),+ null,diameter,- depth,- entry.RawFields.TryGetValue("hole_family", out var familyRaw) ? familyRaw : null,- entry.RawFields.TryGetValue("tol_plus", out var tolPlusRaw) && double.TryParse(tolPlusRaw, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var tolPlus) ? tolPlus : null,- entry.RawFields.TryGetValue("tol_minus", out var tolMinusRaw) && double.TryParse(tolMinusRaw, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var tolMinus) ? tolMinus : null));+ SourceTag: depth.HasValue ? "explicit-hole-depth-provided" : "explicit-hole"));}break;@@ -209,7 +238,15 @@ public static class FirmamentStepExporterif (entry.RawFields.TryGetValue("datum_kind", out var datumKind)&& entry.RawFields.TryGetValue("label", out var label)){- resolved.Add(new Step242SemanticPmiDatum(featureId, datumKind, label, targetRaw));+ if (string.Equals(datumKind, "plane", StringComparison.Ordinal)+ && targetRaw.Contains(".", StringComparison.Ordinal))+ {+ model = model.AddDatum(new PmiDatumFeature(+ $"datum:{label}",+ label,+ PmiDatumFeatureKind.Planar,+ new PmiPlanarFaceReference(featureId, targetRaw)));+ }}break;@@ -226,14 +263,14 @@ public static class FirmamentStepExporter}}- resolved.Add(new Step242SemanticPmiNote(featureId, targetRaw, text));+ passthroughNotes.Add(new Step242SemanticPmiNote(featureId, targetRaw, text));}break;}}- return resolved;+ return new DerivedPmiPayload(model, passthroughNotes);}private static IReadOnlyDictionary<string, BrepBody> BuildFeatureBodyMap(FirmamentPrimitiveExecutionResult executionResult)@@ -252,6 +289,8 @@ public static class FirmamentStepExporterreturn map;}+ private sealed record DerivedPmiPayload(PmiModel Model, IReadOnlyList<Step242SemanticPmiNote> PassthroughNotes);+private sealed record ExportBodySelection(int OpIndex,string FeatureId,