Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Extract shared STEP import infrastructure helpers

Yuechen Li4432d6975597
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ImportSharedUtilitiesTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ImportSharedUtilitiesTests.cs
new file mode 100644
index 00000000..c8536955
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ImportSharedUtilitiesTests.cs
@@ -0,0 +1,51 @@
+using Aetheris.Kernel.Core.Step242;
+
+namespace Aetheris.Kernel.Core.Tests.Step242;
+
+public sealed class Step242ImportSharedUtilitiesTests
+{
+ [Fact]
+ public void RequireSingleEntityByName_FindsSingleRoot()
+ {
+ var parse = Step242SubsetParser.Parse(Step242FixtureCorpus.CanonicalBoxGolden);
+ Assert.True(parse.IsSuccess);
+
+ var result = Step242ImportSharedUtilities.RequireSingleEntityByName(
+ parse.Value,
+ "MANIFOLD_SOLID_BREP",
+ "missing",
+ "Importer.Missing",
+ "multiple",
+ "Importer.Multiple");
+
+ Assert.True(result.IsSuccess);
+ Assert.Equal("MANIFOLD_SOLID_BREP", result.Value.Name);
+ }
+
+ [Fact]
+ public void RequireSingleEntityByName_ReturnsMissingDiagnosticWhenAbsent()
+ {
+ var parse = Step242SubsetParser.Parse("ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\nENDSEC;\nEND-ISO-10303-21;");
+ Assert.True(parse.IsSuccess);
+
+ var result = Step242ImportSharedUtilities.RequireSingleEntityByName(
+ parse.Value,
+ "MANIFOLD_SOLID_BREP",
+ "missing",
+ "Importer.Missing",
+ "multiple",
+ "Importer.Multiple");
+
+ Assert.False(result.IsSuccess);
+ Assert.Equal("Importer.Missing", Assert.Single(result.Diagnostics).Source);
+ }
+
+ [Fact]
+ public void ExecuteWithGuardrail_ConvertsArgumentExceptionToDiagnosticFailure()
+ {
+ var result = Step242ImportSharedUtilities.ExecuteWithGuardrail(() => throw new ArgumentException("boom"));
+
+ Assert.False(result.IsSuccess);
+ Assert.Equal("Importer.Guardrail", Assert.Single(result.Diagnostics).Source);
+ }
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242ImportSharedUtilities.cs b/Aetheris.Kernel.Core/Step242/Step242ImportSharedUtilities.cs
new file mode 100644
index 00000000..8dda2471
--- /dev/null
+++ b/Aetheris.Kernel.Core/Step242/Step242ImportSharedUtilities.cs
@@ -0,0 +1,57 @@
+using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Diagnostics;
+using Aetheris.Kernel.Core.Results;
+
+namespace Aetheris.Kernel.Core.Step242;
+
+internal static class Step242ImportSharedUtilities
+{
+ internal static KernelResult<BrepBody> ExecuteWithGuardrail(Func<KernelResult<BrepBody>> import)
+ {
+ try
+ {
+ return import();
+ }
+ catch (Exception ex) when (ex is ArgumentException or InvalidOperationException)
+ {
+ return KernelResult<BrepBody>.Failure([
+ new KernelDiagnostic(
+ KernelDiagnosticCode.InvalidArgument,
+ KernelDiagnosticSeverity.Error,
+ $"Importer rejected parseable STEP input: {ex.Message}",
+ "Importer.Guardrail")
+ ]);
+ }
+ }
+
+ internal static KernelResult<Step242ParsedEntity> RequireSingleEntityByName(
+ Step242ParsedDocument document,
+ string entityName,
+ string missingMessage,
+ string missingSource,
+ string multipleMessage,
+ string multipleSource)
+ {
+ var candidates = document.Entities
+ .Where(e => string.Equals(e.Name, entityName, StringComparison.OrdinalIgnoreCase))
+ .ToList();
+
+ if (candidates.Count == 0)
+ {
+ return NotImplementedFailure<Step242ParsedEntity>(missingMessage, missingSource);
+ }
+
+ if (candidates.Count > 1)
+ {
+ return NotImplementedFailure<Step242ParsedEntity>(multipleMessage, multipleSource);
+ }
+
+ return KernelResult<Step242ParsedEntity>.Success(candidates[0]);
+ }
+
+ internal static KernelResult<T> NotImplementedFailure<T>(string message, string source)
+ => KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+
+ internal static KernelResult<T> ValidationFailure<T>(string message, string source)
+ => KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
index 9fae71f1..76526b25 100644
--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs
+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
@@ -70,21 +70,19 @@ public static class Step242Importer
internal static KernelResult<BrepBody> ImportExactBrepCore(Step242ParsedDocument document)
{
- var manifoldSolidBreps = document.Entities
- .Where(e => string.Equals(e.Name, "MANIFOLD_SOLID_BREP", StringComparison.OrdinalIgnoreCase))
- .ToList();
-
- if (manifoldSolidBreps.Count == 0)
- {
- return Failure("Missing MANIFOLD_SOLID_BREP root entity.", "Importer.TopologyRoot");
- }
-
- if (manifoldSolidBreps.Count > 1)
+ var brepEntityResult = Step242ImportSharedUtilities.RequireSingleEntityByName(
+ document,
+ entityName: "MANIFOLD_SOLID_BREP",
+ missingMessage: "Missing MANIFOLD_SOLID_BREP root entity.",
+ missingSource: "Importer.TopologyRoot",
+ multipleMessage: "Multiple MANIFOLD_SOLID_BREP roots are unsupported in M23 import subset.",
+ multipleSource: "Importer.SingleSolid");
+ if (!brepEntityResult.IsSuccess)
{
- return Failure("Multiple MANIFOLD_SOLID_BREP roots are unsupported in M23 import subset.", "Importer.SingleSolid");
+ return KernelResult<BrepBody>.Failure(brepEntityResult.Diagnostics);
}
- var brepEntity = manifoldSolidBreps[0];
+ var brepEntity = brepEntityResult.Value;
var shellRefResult = Step242SubsetDecoder.ReadReference(brepEntity, 1, "MANIFOLD_SOLID_BREP shell");
if (!shellRefResult.IsSuccess)
@@ -2875,28 +2873,28 @@ public static class Step242Importer
KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
private static KernelResult<BrepBody> Failure(string message, string source) =>
- KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<BrepBody>(message, source);
private static KernelResult<EdgeId> FailureEdge(string message, string source) =>
- KernelResult<EdgeId>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<EdgeId>(message, source);
private static KernelResult<(CurveGeometry CurveGeometry, ParameterInterval TrimInterval)> FailureCurveBinding(string message, string source) =>
- KernelResult<(CurveGeometry CurveGeometry, ParameterInterval TrimInterval)>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<(CurveGeometry CurveGeometry, ParameterInterval TrimInterval)>(message, source);
private static KernelResult<(SurfaceGeometryId SurfaceGeometryId, SurfaceGeometry SurfaceGeometry)> FailureSurfaceBinding(string message, string source) =>
- KernelResult<(SurfaceGeometryId SurfaceGeometryId, SurfaceGeometry SurfaceGeometry)>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<(SurfaceGeometryId SurfaceGeometryId, SurfaceGeometry SurfaceGeometry)>(message, source);
private static KernelResult<ParameterInterval> FailureCircleTrim(string message, string source) =>
- KernelResult<ParameterInterval>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.ValidationFailure<ParameterInterval>(message, source);
private static KernelResult<double> FailureCircleTrimAngle(string message, string source) =>
- KernelResult<double>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.ValidationFailure<double>(message, source);
private static KernelResult<ParameterInterval> FailureEllipseTrim(string message, string source) =>
- KernelResult<ParameterInterval>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.ValidationFailure<ParameterInterval>(message, source);
private static KernelResult<double> FailureEllipseTrimAngle(string message, string source) =>
- KernelResult<double>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.ValidationFailure<double>(message, source);
private static KernelResult<T> OrientationFailure<T>(string message, string source) =>
KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);
diff --git a/Aetheris.Kernel.Core/Step242/StepImportConnectorAndLanes.cs b/Aetheris.Kernel.Core/Step242/StepImportConnectorAndLanes.cs
index b0d3bb72..e4258bd5 100644
--- a/Aetheris.Kernel.Core/Step242/StepImportConnectorAndLanes.cs
+++ b/Aetheris.Kernel.Core/Step242/StepImportConnectorAndLanes.cs
@@ -63,20 +63,7 @@ internal sealed class Step242ExactBRepImportLane : IImportLane
internal static KernelResult<BrepBody> ImportExactBrep(Step242ParsedDocument document)
{
- try
- {
- return Step242Importer.ImportExactBrepCore(document);
- }
- catch (Exception ex) when (ex is ArgumentException or InvalidOperationException)
- {
- return KernelResult<BrepBody>.Failure([
- new KernelDiagnostic(
- KernelDiagnosticCode.InvalidArgument,
- KernelDiagnosticSeverity.Error,
- $"Importer rejected parseable STEP input: {ex.Message}",
- "Importer.Guardrail")
- ]);
- }
+ return Step242ImportSharedUtilities.ExecuteWithGuardrail(() => Step242Importer.ImportExactBrepCore(document));
}
}
@@ -105,20 +92,7 @@ internal sealed class Step242TessellatedImportLane : IImportLane
internal static KernelResult<BrepBody> ImportFromParsedDocument(Step242ParsedDocument document)
{
- try
- {
- return ImportTessellated(document);
- }
- catch (Exception ex) when (ex is ArgumentException or InvalidOperationException)
- {
- return KernelResult<BrepBody>.Failure([
- new KernelDiagnostic(
- KernelDiagnosticCode.InvalidArgument,
- KernelDiagnosticSeverity.Error,
- $"Importer rejected parseable STEP input: {ex.Message}",
- "Importer.Guardrail")
- ]);
- }
+ return Step242ImportSharedUtilities.ExecuteWithGuardrail(() => ImportTessellated(document));
}
internal static bool HasTessellatedRoot(Step242ParsedDocument document)
@@ -128,31 +102,24 @@ internal sealed class Step242TessellatedImportLane : IImportLane
private static KernelResult<BrepBody> ImportTessellated(Step242ParsedDocument document)
{
- var tessellatedSolidResult = TryImportTessellatedSolid(document);
- if (tessellatedSolidResult is not null)
- {
- return tessellatedSolidResult;
- }
-
- return Failure("Requested tessellated import lane, but no TESSELLATED_SOLID root was found.", "Importer.TessellatedSolid.MissingRoot");
+ return TryImportTessellatedSolid(document);
}
- private static KernelResult<BrepBody>? TryImportTessellatedSolid(Step242ParsedDocument document)
+ private static KernelResult<BrepBody> TryImportTessellatedSolid(Step242ParsedDocument document)
{
- var tessellatedSolids = document.Entities
- .Where(e => string.Equals(e.Name, "TESSELLATED_SOLID", StringComparison.OrdinalIgnoreCase))
- .ToList();
- if (tessellatedSolids.Count == 0)
- {
- return null;
- }
-
- if (tessellatedSolids.Count > 1)
- {
- return Failure("Multiple TESSELLATED_SOLID roots are unsupported in M118 tessellated import subset.", "Importer.TessellatedSolid.SingleRoot");
+ var tessellatedSolidResult = Step242ImportSharedUtilities.RequireSingleEntityByName(
+ document,
+ entityName: "TESSELLATED_SOLID",
+ missingMessage: "Requested tessellated import lane, but no TESSELLATED_SOLID root was found.",
+ missingSource: "Importer.TessellatedSolid.MissingRoot",
+ multipleMessage: "Multiple TESSELLATED_SOLID roots are unsupported in M118 tessellated import subset.",
+ multipleSource: "Importer.TessellatedSolid.SingleRoot");
+ if (!tessellatedSolidResult.IsSuccess)
+ {
+ return KernelResult<BrepBody>.Failure(tessellatedSolidResult.Diagnostics);
}
- var shellRefsResult = Step242SubsetDecoder.ReadReferenceList(tessellatedSolids[0], 1, "TESSELLATED_SOLID shells");
+ var shellRefsResult = Step242SubsetDecoder.ReadReferenceList(tessellatedSolidResult.Value, 1, "TESSELLATED_SOLID shells");
if (!shellRefsResult.IsSuccess)
{
return KernelResult<BrepBody>.Failure(shellRefsResult.Diagnostics);
@@ -474,17 +441,17 @@ internal sealed class Step242TessellatedImportLane : IImportLane
}
private static KernelResult<BrepBody> Failure(string message, string source) =>
- KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<BrepBody>(message, source);
private static KernelResult<IReadOnlyList<(Point3D A, Point3D B, Point3D C)>> FailureTessellatedFaceTriangles(string message, string source) =>
- KernelResult<IReadOnlyList<(Point3D A, Point3D B, Point3D C)>>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<IReadOnlyList<(Point3D A, Point3D B, Point3D C)>>(message, source);
private static KernelResult<IReadOnlyList<Point3D>> FailureTessellatedFacePoints(string message, string source) =>
- KernelResult<IReadOnlyList<Point3D>>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<IReadOnlyList<Point3D>>(message, source);
private static KernelResult<IReadOnlyList<int>> FailureIntegerList(string message, string source) =>
- KernelResult<IReadOnlyList<int>>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<IReadOnlyList<int>>(message, source);
private static KernelResult<IReadOnlyList<IReadOnlyList<int>>> FailureIntegerListList(string message, string source) =>
- KernelResult<IReadOnlyList<IReadOnlyList<int>>>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);
+ Step242ImportSharedUtilities.NotImplementedFailure<IReadOnlyList<IReadOnlyList<int>>>(message, source);
}
diff --git a/docs/step242/mx119d-shared-helper-classification.md b/docs/step242/mx119d-shared-helper-classification.md
new file mode 100644
index 00000000..4fad1ce8
--- /dev/null
+++ b/docs/step242/mx119d-shared-helper-classification.md
@@ -0,0 +1,21 @@
+# Mx119d helper classification (diagnostics-first)
+
+## 1) Shared neutral infrastructure (extracted)
+- Guardrail wrapper that converts `ArgumentException` / `InvalidOperationException` into deterministic import diagnostics (`Importer.Guardrail`).
+- Generic single-root STEP entity lookup helper for deterministic root selection error handling.
+- Generic diagnostic constructors for `NotImplemented` and `ValidationFailed` result types.
+
+## 2) Exact-lane-owned policy/helpers (intentionally kept lane-owned)
+- Exact AP242 root/entity policy (`MANIFOLD_SOLID_BREP` traversal semantics and all exact topology/surface decode behavior).
+- Loop-role normalization, containment, spherical/conical special handling, and orientation/topology policy.
+
+## 3) Tessellated-lane-owned policy/helpers (intentionally kept lane-owned)
+- `TESSELLATED_SOLID` shell/face family policy.
+- `COMPLEX_TRIANGULATED_FACE` triangulation and triangle-to-brep topology mapping.
+- Tessellated subset constraints and diagnostics specific to tg path behavior.
+
+## 4) Not-worth-extracting residue (for now)
+- Narrow helpers used only once in a lane and encoding nearby policy/context.
+- Data-shape readers that look similar but carry lane-specific failure context strings and constraints.
+
+This milestone intentionally extracts only neutral shared infrastructure and leaves lane policy where it belongs.