Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Fix STEP line edge-curve direction alignment
Yuechen Lid2830b479703
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.csindex d3b44539..14a3e441 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ExporterTests.cs@@ -1,4 +1,7 @@using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Geometry.Curves;+using Aetheris.Kernel.Core.Math;using Aetheris.Kernel.Core.Step242;using System.Globalization;@@ -499,6 +502,45 @@ END-ISO-10303-21;";}}+ [Fact]+ public void ExportBody_WhenStoredLineDirectionIsAntiparallel_UsesEndpointDirectionForEdgeCurveLine()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.PlanarFaceWithRectangularHole);+ Assert.True(import.IsSuccess);++ var bodyWithReversedLine = CloneWithFirstLineDirectionReversed(import.Value);+ var export = Step242Exporter.ExportBody(bodyWithReversedLine);+ Assert.True(export.IsSuccess);++ var records = ParseStepEntities(export.Value);+ AssertLineEdgeCurveDirectionsMatchVertexOrdering(records);+ }++ [Fact]+ public void ExportBody_OcctSimpleBracket_ExportImportRoundTrip_Succeeds()+ {+ var fixturePath = Path.Combine(+ Step242CorpusManifestRunner.RepoRoot(),+ "testdata",+ "firmasm",+ "examples",+ "occt-l-bracket",+ "_part_003_l_bracket.step");+ var source = File.ReadAllText(fixturePath);++ var import = Step242Importer.ImportBody(source);+ Assert.True(import.IsSuccess);++ var export = Step242Exporter.ExportBody(import.Value);+ Assert.True(export.IsSuccess);++ var roundTrip = Step242Importer.ImportBody(export.Value);+ Assert.True(roundTrip.IsSuccess);+ Assert.DoesNotContain(+ roundTrip.Diagnostics,+ diagnostic => diagnostic.Message.Contains("EDGE_CURVE line parameterization is opposite to vertex ordering", StringComparison.Ordinal));+ }+private static IReadOnlyList<double> ExtractConicalSurfaceSemiAngles(string stepText){@@ -720,6 +762,67 @@ END-ISO-10303-21;";return double.Parse(rhs[start..end], CultureInfo.InvariantCulture);}+ private static void AssertLineEdgeCurveDirectionsMatchVertexOrdering(StepEntityRecords records)+ {+ foreach (var edge in records.EdgeCurves)+ {+ if (!records.Lines.TryGetValue(edge.CurveId, out var line))+ {+ continue;+ }++ var start = ResolveVertexPoint(records, edge.StartVertexId);+ var end = ResolveVertexPoint(records, edge.EndVertexId);+ var edgeDirection = Normalize((end.X - start.X, end.Y - start.Y, end.Z - start.Z));+ var lineDirection = Normalize(ResolveDirection(records, line.VectorId));+ var dot = (edgeDirection.X * lineDirection.X) + (edgeDirection.Y * lineDirection.Y) + (edgeDirection.Z * lineDirection.Z);++ Assert.True(+ dot > 0d,+ $"EDGE_CURVE #{edge.EdgeCurveId} uses LINE #{edge.CurveId} with direction opposite to vertex ordering. dot={dot}.");+ }+ }++ private static BrepBody CloneWithFirstLineDirectionReversed(BrepBody source)+ {+ var firstLineBinding = source.Bindings.EdgeBindings+ .OrderBy(binding => binding.EdgeId.Value)+ .First(binding =>+ {+ return source.Geometry.TryGetCurve(binding.CurveGeometryId, out var curve)+ && curve is not null+ && curve.Kind == CurveGeometryKind.Line3+ && curve.Line3.HasValue;+ });++ var lineCurve = source.Geometry.GetCurve(firstLineBinding.CurveGeometryId).Line3!.Value;+ var reversed = new Line3Curve(+ lineCurve.Origin,+ Direction3D.Create(lineCurve.Direction.ToVector() * -1d));++ var geometry = new BrepGeometryStore();+ foreach (var (curveId, curve) in source.Geometry.Curves.OrderBy(entry => entry.Key.Value))+ {+ geometry.AddCurve(+ curveId,+ curveId == firstLineBinding.CurveGeometryId ? CurveGeometry.FromLine(reversed) : curve);+ }++ foreach (var (surfaceId, surface) in source.Geometry.Surfaces.OrderBy(entry => entry.Key.Value))+ {+ geometry.AddSurface(surfaceId, surface);+ }++ return new BrepBody(source.Topology, geometry, source.Bindings);+ }++ private static (double X, double Y, double Z) Normalize((double X, double Y, double Z) v)+ {+ var length = double.Sqrt((v.X * v.X) + (v.Y * v.Y) + (v.Z * v.Z));+ Assert.True(length > 1e-12d, "Expected non-degenerate vector.");+ return (v.X / length, v.Y / length, v.Z / length);+ }+private static string FormatPoint((double X, double Y, double Z) p) => $"({p.X}, {p.Y}, {p.Z})";private static Aetheris.Kernel.Core.Topology.FaceId? FindPlanarFaceWithCircle(BrepBody body, Aetheris.Kernel.Core.Math.Point3D expectedCenter, double expectedRadius)diff --git a/Aetheris.Kernel.Core/Step242/Step242Exporter.cs b/Aetheris.Kernel.Core/Step242/Step242Exporter.csindex d7d2d539..14ded27c 100644--- a/Aetheris.Kernel.Core/Step242/Step242Exporter.cs+++ b/Aetheris.Kernel.Core/Step242/Step242Exporter.cs@@ -509,7 +509,7 @@ public static class Step242Exportervar curveDirection = line.Direction.ToVector();if (curveDirection.TryNormalize(out var normalizedCurveDirection)){- var alignment = double.Abs(normalizedCurveDirection.Dot(endpointDirection));+ var alignment = normalizedCurveDirection.Dot(endpointDirection);if (alignment >= 0.999999999999d){direction = normalizedCurveDirection;diff --git a/testdata/step242/manifests/nist.v0.report.json b/testdata/step242/manifests/nist.v0.report.jsonindex cb16d447..1557ab16 100644--- a/testdata/step242/manifests/nist.v0.report.json+++ b/testdata/step242/manifests/nist.v0.report.json@@ -177,7 +177,7 @@"edges": 616,"faces": 247},- "canonicalSha256": "bdc8e56bfd10539aa2d73c2bdecbcc94d7ffc3821517ffb77687f316590cd213"+ "canonicalSha256": "95810b79893e1371e88b4b6f1cd9e1fc7170818d4f9ca3c98aaf5d3246c1bc17"},{"fileId": "testdata_step242_nist_FTC_nist_ftc_09_asme1_ap242-e1",@@ -237,7 +237,7 @@"edges": 104,"faces": 42},- "canonicalSha256": "d941e5707a91b7a8be0049455af81c42e2499cb620c71875435175e1cccf8426"+ "canonicalSha256": "2d2abcd1332a77eb34f5bac5ea9ada3e204f785deefea9eceba7712cc9f5763a"},{"fileId": "testdata_step242_nist_STC_nist_stc_06_asme1_ap242-e3",@@ -277,7 +277,7 @@"edges": 871,"faces": 306},- "canonicalSha256": "639f0d562dc98d38b3ea323e3dc81cad82cbe9d13e8ecf2a8d6db6703183a1eb"+ "canonicalSha256": "7b4596ce3ea0b4a875b11a2a43a95b30c52b403a9b23785e8fb0d239135d2928"},{"fileId": "testdata_step242_nist_STC_nist_stc_08_asme1_ap242-e3",@@ -337,6 +337,6 @@"edges": 551,"faces": 256},- "canonicalSha256": "3703906cba81c4fba8e935a298fcdb89bbd74c151552ea0d1324eccf429e98a9"+ "canonicalSha256": "d1027aab50cb0b2446bc7321a4050e02b2fbdc4a12352c63c0378a22715aef4a"}]