Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add M22 AP242 subset STEP exporter scaffold

Yuechen Li1f88388183f5
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
new file mode 100644
index 00000000..828a391a
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs
@@ -0,0 +1,67 @@
+using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Diagnostics;
+using Aetheris.Kernel.Core.Step242;
+
+namespace Aetheris.Kernel.Core.Tests.Step242;
+
+public sealed class Step242ExporterTests
+{
+ [Fact]
+ public void ExportBody_BoxBody_ReturnsStepTextWithExpectedSections()
+ {
+ var boxResult = BrepPrimitives.CreateBox(4d, 6d, 8d);
+ Assert.True(boxResult.IsSuccess);
+
+ var export = Step242Exporter.ExportBody(boxResult.Value);
+
+ Assert.True(export.IsSuccess);
+ Assert.False(string.IsNullOrWhiteSpace(export.Value));
+
+ Assert.Contains("ISO-10303-21;", export.Value, StringComparison.Ordinal);
+ Assert.Contains("HEADER;", export.Value, StringComparison.Ordinal);
+ Assert.Contains("DATA;", export.Value, StringComparison.Ordinal);
+ Assert.Contains("ENDSEC;", export.Value, StringComparison.Ordinal);
+ Assert.Contains("END-ISO-10303-21;", export.Value, StringComparison.Ordinal);
+ Assert.Contains("MANIFOLD_SOLID_BREP", export.Value, StringComparison.Ordinal);
+ Assert.Contains("ADVANCED_FACE", export.Value, StringComparison.Ordinal);
+ }
+
+ [Fact]
+ public void ExportBody_BoxBody_IsDeterministicForSameInput()
+ {
+ var boxResult = BrepPrimitives.CreateBox(10d, 12d, 14d);
+ Assert.True(boxResult.IsSuccess);
+
+ var first = Step242Exporter.ExportBody(boxResult.Value);
+ var second = Step242Exporter.ExportBody(boxResult.Value);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.Equal(first.Value, second.Value);
+
+ var entityLines = first.Value
+ .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
+ .Where(line => line.StartsWith('#', StringComparison.Ordinal))
+ .Take(5)
+ .ToArray();
+
+ Assert.Equal("#1=CARTESIAN_POINT($,(-5,-6,-7));", entityLines[0]);
+ Assert.Equal("#2=VERTEX_POINT($,#1);", entityLines[1]);
+ }
+
+ [Fact]
+ public void ExportBody_Sphere_ReturnsNotImplementedDiagnostic_InsteadOfThrowing()
+ {
+ var sphereResult = BrepPrimitives.CreateSphere(3d);
+ Assert.True(sphereResult.IsSuccess);
+
+ var export = Step242Exporter.ExportBody(sphereResult.Value);
+
+ Assert.False(export.IsSuccess);
+ var diagnostic = Assert.Single(export.Diagnostics);
+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);
+ Assert.Equal(KernelDiagnosticSeverity.Error, diagnostic.Severity);
+ Assert.Equal("Face:1", diagnostic.Source);
+ Assert.Contains("boundary loops", diagnostic.Message, StringComparison.Ordinal);
+ }
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242ExportOptions.cs b/Aetheris.Kernel.Core/Step242/Step242ExportOptions.cs
new file mode 100644
index 00000000..4ba1997c
--- /dev/null
+++ b/Aetheris.Kernel.Core/Step242/Step242ExportOptions.cs
@@ -0,0 +1,10 @@
+namespace Aetheris.Kernel.Core.Step242;
+
+public sealed class Step242ExportOptions
+{
+ public string ProductName { get; init; } = "AetherisBody";
+
+ public string ApplicationName { get; init; } = "Aetheris";
+
+ public string AuthoringSystem { get; init; } = "Aetheris.Kernel";
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242Exporter.cs b/Aetheris.Kernel.Core/Step242/Step242Exporter.cs
new file mode 100644
index 00000000..ed545ef2
--- /dev/null
+++ b/Aetheris.Kernel.Core/Step242/Step242Exporter.cs
@@ -0,0 +1,230 @@
+using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Diagnostics;
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Core.Geometry.Curves;
+using Aetheris.Kernel.Core.Geometry.Surfaces;
+using Aetheris.Kernel.Core.Math;
+using Aetheris.Kernel.Core.Results;
+using Aetheris.Kernel.Core.Topology;
+
+namespace Aetheris.Kernel.Core.Step242;
+
+public static class Step242Exporter
+{
+ public static KernelResult<string> ExportBody(BrepBody body, Step242ExportOptions? options = null)
+ {
+ options ??= new Step242ExportOptions();
+
+ var model = body.Topology;
+ var bodyNodes = model.Bodies.OrderBy(b => b.Id.Value).ToArray();
+ if (bodyNodes.Length != 1)
+ {
+ return Failure("Only single-body export is supported.", "Topology.Bodies");
+ }
+
+ var shellIds = bodyNodes[0].ShellIds.OrderBy(s => s.Value).ToArray();
+ if (shellIds.Length != 1)
+ {
+ return Failure("Only one shell per body is supported.", "Topology.Shells");
+ }
+
+ var shell = model.GetShell(shellIds[0]);
+ var writer = new Step242TextWriter();
+
+ var vertexPointIds = new Dictionary<VertexId, string>();
+ var cartesianPointIds = new Dictionary<VertexId, string>();
+
+ var edgeCurveIds = new Dictionary<EdgeId, string>();
+ var orientedEdgeIds = new Dictionary<CoedgeId, string>();
+ var lineIds = new Dictionary<EdgeId, string>();
+
+ var faceIds = new List<string>();
+
+ foreach (var face in shell.FaceIds.OrderBy(id => id.Value).Select(model.GetFace))
+ {
+ if (face.LoopIds.Count == 0)
+ {
+ return Failure("Faces without boundary loops are not supported in M22 export subset.", $"Face:{face.Id.Value}");
+ }
+
+ if (!body.Bindings.TryGetFaceBinding(face.Id, out var faceBinding))
+ {
+ return Failure("Face is missing surface binding.", $"Face:{face.Id.Value}");
+ }
+
+ if (!body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface) || surface is null)
+ {
+ return Failure("Face surface geometry was not found.", $"Surface:{faceBinding.SurfaceGeometryId.Value}");
+ }
+
+ if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is null)
+ {
+ return Failure($"Unsupported surface kind '{surface.Kind}'. M22 supports planar faces only.", $"Face:{face.Id.Value}");
+ }
+
+ var loopBoundIds = new List<string>();
+ foreach (var loopId in face.LoopIds.OrderBy(id => id.Value))
+ {
+ var loop = model.GetLoop(loopId);
+ var oriented = new List<string>();
+
+ foreach (var coedgeId in loop.CoedgeIds.OrderBy(id => id.Value))
+ {
+ var coedge = model.GetCoedge(coedgeId);
+
+ if (!edgeCurveIds.TryGetValue(coedge.EdgeId, out var edgeCurveId))
+ {
+ var edgeResult = BuildEdgeCurve(body, model, writer, coedge.EdgeId, cartesianPointIds, vertexPointIds, lineIds);
+ if (!edgeResult.IsSuccess)
+ {
+ return KernelResult<string>.Failure(edgeResult.Diagnostics);
+ }
+
+ edgeCurveId = edgeResult.Value;
+ edgeCurveIds[coedge.EdgeId] = edgeCurveId;
+ }
+
+ var orientedEdgeId = writer.AddEntity(
+ "ORIENTED_EDGE",
+ "$",
+ "$",
+ "$",
+ Step242TextWriter.Ref(edgeCurveId),
+ Step242TextWriter.BooleanLogical(!coedge.IsReversed));
+
+ orientedEdgeIds[coedgeId] = orientedEdgeId;
+ oriented.Add(orientedEdgeId);
+ }
+
+ var edgeLoopId = writer.AddEntity("EDGE_LOOP", "$", Step242TextWriter.List(oriented.ToArray()));
+ var boundEntity = loopBoundIds.Count == 0 ? "FACE_OUTER_BOUND" : "FACE_BOUND";
+ var boundId = writer.AddEntity(boundEntity, "$", Step242TextWriter.Ref(edgeLoopId), Step242TextWriter.BooleanLogical(true));
+ loopBoundIds.Add(boundId);
+ }
+
+ var planeId = BuildPlane(writer, surface.Plane.Value);
+ var advancedFaceId = writer.AddEntity(
+ "ADVANCED_FACE",
+ Step242TextWriter.List(loopBoundIds.ToArray()),
+ Step242TextWriter.Ref(planeId),
+ Step242TextWriter.BooleanLogical(true));
+
+ faceIds.Add(advancedFaceId);
+ }
+
+ var closedShellId = writer.AddEntity("CLOSED_SHELL", "$", Step242TextWriter.List(faceIds.ToArray()));
+ var brepId = writer.AddEntity("MANIFOLD_SOLID_BREP", Step242TextWriter.String(options.ProductName), Step242TextWriter.Ref(closedShellId));
+
+ var appContextId = writer.AddEntity("APPLICATION_CONTEXT", Step242TextWriter.String("mechanical design"));
+ var productContextId = writer.AddEntity("PRODUCT_CONTEXT", Step242TextWriter.String(""), Step242TextWriter.Ref(appContextId), Step242TextWriter.String("mechanical"));
+ var productId = writer.AddEntity("PRODUCT", Step242TextWriter.String("AETHERIS"), Step242TextWriter.String(options.ProductName), Step242TextWriter.String(""), Step242TextWriter.List(productContextId));
+ var formationId = writer.AddEntity("PRODUCT_DEFINITION_FORMATION", Step242TextWriter.String(""), Step242TextWriter.String(""), Step242TextWriter.Ref(productId));
+ var definitionContextId = writer.AddEntity("PRODUCT_DEFINITION_CONTEXT", Step242TextWriter.String("design"), Step242TextWriter.Ref(appContextId), Step242TextWriter.String("design"));
+ var definitionId = writer.AddEntity("PRODUCT_DEFINITION", Step242TextWriter.String(""), Step242TextWriter.String(""), Step242TextWriter.Ref(formationId), Step242TextWriter.Ref(definitionContextId));
+ var shapeId = writer.AddEntity("PRODUCT_DEFINITION_SHAPE", Step242TextWriter.String(""), Step242TextWriter.String(""), Step242TextWriter.Ref(definitionId));
+ var repContextId = writer.AddEntity("GEOMETRIC_REPRESENTATION_CONTEXT", "3");
+ var shapeRepresentationId = writer.AddEntity("SHAPE_REPRESENTATION", Step242TextWriter.String(options.ProductName), Step242TextWriter.List(brepId), Step242TextWriter.Ref(repContextId));
+ writer.AddEntity("SHAPE_DEFINITION_REPRESENTATION", Step242TextWriter.Ref(shapeId), Step242TextWriter.Ref(shapeRepresentationId));
+
+ return KernelResult<string>.Success(writer.Build(options.ApplicationName));
+ }
+
+ private static string BuildPlane(Step242TextWriter writer, PlaneSurface plane)
+ {
+ var originId = writer.AddEntity("CARTESIAN_POINT", "$", Step242TextWriter.List(Step242TextWriter.Number(plane.Origin.X), Step242TextWriter.Number(plane.Origin.Y), Step242TextWriter.Number(plane.Origin.Z)));
+ var normal = plane.Normal.ToVector();
+ var uAxis = plane.UAxis.ToVector();
+ var normalId = writer.AddEntity("DIRECTION", "$", Step242TextWriter.List(Step242TextWriter.Number(normal.X), Step242TextWriter.Number(normal.Y), Step242TextWriter.Number(normal.Z)));
+ var refDirId = writer.AddEntity("DIRECTION", "$", Step242TextWriter.List(Step242TextWriter.Number(uAxis.X), Step242TextWriter.Number(uAxis.Y), Step242TextWriter.Number(uAxis.Z)));
+ var axisPlacementId = writer.AddEntity("AXIS2_PLACEMENT_3D", "$", Step242TextWriter.Ref(originId), Step242TextWriter.Ref(normalId), Step242TextWriter.Ref(refDirId));
+ return writer.AddEntity("PLANE", "$", Step242TextWriter.Ref(axisPlacementId));
+ }
+
+ private static KernelResult<string> BuildEdgeCurve(
+ BrepBody body,
+ TopologyModel model,
+ Step242TextWriter writer,
+ EdgeId edgeId,
+ IDictionary<VertexId, string> cartesianPointIds,
+ IDictionary<VertexId, string> vertexPointIds,
+ IDictionary<EdgeId, string> lineIds)
+ {
+ var edge = model.GetEdge(edgeId);
+
+ if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding))
+ {
+ return Failure("Edge is missing curve binding.", $"Edge:{edgeId.Value}");
+ }
+
+ if (!body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve) || curve is null)
+ {
+ return Failure("Edge curve geometry was not found.", $"Curve:{edgeBinding.CurveGeometryId.Value}");
+ }
+
+ if (curve.Kind != CurveGeometryKind.Line3 || curve.Line3 is null)
+ {
+ return Failure($"Unsupported curve kind '{curve.Kind}'. M22 supports line edges only.", $"Edge:{edgeId.Value}");
+ }
+
+ if (edgeBinding.TrimInterval is null)
+ {
+ return Failure("Line edge must provide trim interval for vertex mapping.", $"Edge:{edgeId.Value}");
+ }
+
+ var line = curve.Line3.Value;
+ var startPoint = line.Evaluate(edgeBinding.TrimInterval.Value.Start);
+ var endPoint = line.Evaluate(edgeBinding.TrimInterval.Value.End);
+
+ var startVertexId = EnsureVertex(writer, edge.StartVertexId, startPoint, cartesianPointIds, vertexPointIds);
+ var endVertexId = EnsureVertex(writer, edge.EndVertexId, endPoint, cartesianPointIds, vertexPointIds);
+
+ if (!lineIds.TryGetValue(edgeId, out var lineId))
+ {
+ var originId = writer.AddEntity("CARTESIAN_POINT", "$", PointList(line.Origin));
+ var direction = line.Direction.ToVector();
+ var directionId = writer.AddEntity("DIRECTION", "$", Step242TextWriter.List(Step242TextWriter.Number(direction.X), Step242TextWriter.Number(direction.Y), Step242TextWriter.Number(direction.Z)));
+ var vectorId = writer.AddEntity("VECTOR", "$", Step242TextWriter.Ref(directionId), Step242TextWriter.Number(1d));
+ lineId = writer.AddEntity("LINE", "$", Step242TextWriter.Ref(originId), Step242TextWriter.Ref(vectorId));
+ lineIds[edgeId] = lineId;
+ }
+
+ var edgeCurveId = writer.AddEntity("EDGE_CURVE", "$", Step242TextWriter.Ref(startVertexId), Step242TextWriter.Ref(endVertexId), Step242TextWriter.Ref(lineId), Step242TextWriter.BooleanLogical(true));
+ return KernelResult<string>.Success(edgeCurveId);
+ }
+
+ private static string EnsureVertex(
+ Step242TextWriter writer,
+ VertexId vertexId,
+ Point3D point,
+ IDictionary<VertexId, string> cartesianPointIds,
+ IDictionary<VertexId, string> vertexPointIds)
+ {
+ if (!cartesianPointIds.TryGetValue(vertexId, out var pointId))
+ {
+ pointId = writer.AddEntity("CARTESIAN_POINT", "$", PointList(point));
+ cartesianPointIds[vertexId] = pointId;
+ }
+
+ if (!vertexPointIds.TryGetValue(vertexId, out var vertexPointId))
+ {
+ vertexPointId = writer.AddEntity("VERTEX_POINT", "$", Step242TextWriter.Ref(pointId));
+ vertexPointIds[vertexId] = vertexPointId;
+ }
+
+ return vertexPointId;
+ }
+
+ private static string PointList(Point3D point) => Step242TextWriter.List(
+ Step242TextWriter.Number(point.X),
+ Step242TextWriter.Number(point.Y),
+ Step242TextWriter.Number(point.Z));
+
+ private static KernelResult<string> Failure(string message, string source) =>
+ KernelResult<string>.Failure([
+ new KernelDiagnostic(
+ KernelDiagnosticCode.NotImplemented,
+ KernelDiagnosticSeverity.Error,
+ message,
+ source)
+ ]);
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242TextWriter.cs b/Aetheris.Kernel.Core/Step242/Step242TextWriter.cs
new file mode 100644
index 00000000..87ef989a
--- /dev/null
+++ b/Aetheris.Kernel.Core/Step242/Step242TextWriter.cs
@@ -0,0 +1,51 @@
+using System.Globalization;
+using System.Text;
+
+namespace Aetheris.Kernel.Core.Step242;
+
+internal sealed class Step242TextWriter
+{
+ private readonly List<string> _entities = [];
+
+ public string AddEntity(string entityName, params string[] arguments)
+ {
+ var id = $"#{_entities.Count + 1}";
+ _entities.Add($"{id}={entityName}({string.Join(",", arguments)});");
+ return id;
+ }
+
+ public string Build(string applicationName)
+ {
+ var sb = new StringBuilder();
+ sb.AppendLine("ISO-10303-21;");
+ sb.AppendLine("HEADER;");
+ sb.AppendLine($"FILE_DESCRIPTION(('Aetheris AP242 subset export'),'2;1');");
+ sb.AppendLine($"FILE_NAME('aetheris_export.step','1970-01-01T00:00:00',('Aetheris'),('{EscapeString(applicationName)}'),'Aetheris.Kernel','Aetheris.Kernel','');");
+ sb.AppendLine("FILE_SCHEMA(('AP242_MANAGED_MODEL_BASED_3D_ENGINEERING_MIM_LF'));");
+ sb.AppendLine("ENDSEC;");
+ sb.AppendLine("DATA;");
+
+ foreach (var entity in _entities)
+ {
+ sb.AppendLine(entity);
+ }
+
+ sb.AppendLine("ENDSEC;");
+ sb.AppendLine("END-ISO-10303-21;");
+ return sb.ToString();
+ }
+
+ public static string Ref(string entityId) => entityId;
+
+ public static string String(string value) => $"'{EscapeString(value)}'";
+
+ public static string Enum(string value) => $".{value}.";
+
+ public static string Number(double value) => value.ToString("0.###############", CultureInfo.InvariantCulture);
+
+ public static string BooleanLogical(bool value) => value ? ".T." : ".F.";
+
+ public static string List(params string[] values) => $"({string.Join(",", values)})";
+
+ private static string EscapeString(string value) => value.Replace("'", "''", StringComparison.Ordinal);
+}
diff --git a/docs/milestones.md b/docs/milestones.md
index 8c8039a6..e13aba46 100644
--- a/docs/milestones.md
+++ b/docs/milestones.md
@@ -198,3 +198,12 @@
- [x] Harden routine failure paths to remain diagnostic-first and no-throw for malformed/unsupported requests.
- [x] Keep scope hardening-only: no new modeling capabilities or UI feature expansion.
- [x] Defer known fragilities (general booleans beyond axis-aligned single-box results, broad trimmed-surface tessellation fidelity, and advanced pick acceleration/performance work).
+
+## M22 — AP242 Mapping Layer Skeleton + Export v1 (Subset Only)
+
+- [x] Add a focused AP242 export mapping layer in the kernel (`Aetheris.Kernel.Core.Step242`) with a deterministic text writer and a single export entry point.
+- [x] Keep AP242 as a mapped serialization layer: the kernel B-rep model remains authoritative and unchanged in-memory.
+- [x] Export a narrow, explicit subset: single-body/single-shell solids with planar faces, line edges, and loop-based topology (box primitive path).
+- [x] Return structured `NotImplemented` diagnostics for unsupported export cases (for example: periodic/loopless sphere face, non-planar surfaces, non-line edge curves, multi-body layouts).
+- [x] Add regression tests for successful box export structure, deterministic output stability, and unsupported-case no-throw diagnostics.
+- [x] Defer AP242 import work to M23 and broader schema/entity coverage to later milestones.