Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Sheet metal: bind formed faces by geometry
Claudec474f6a9736e
diff --git a/Aetheris.SheetMetal.Tests/SheetMetalFaceOrientationTests.cs b/Aetheris.SheetMetal.Tests/SheetMetalFaceOrientationTests.csnew file mode 100644index 00000000..0727ab4f--- /dev/null+++ b/Aetheris.SheetMetal.Tests/SheetMetalFaceOrientationTests.cs@@ -0,0 +1,42 @@+using System.Text.RegularExpressions;+using Aetheris.Kernel.Core.Step242;+using Xunit;++namespace Aetheris.SheetMetal.Tests;++/// <summary>+/// Every face of a formed sheet faces out of the material: skins away from the sheet, walls away from the region their+/// contour encloses (into a cut), whatever way round the authored contour runs. The importer derives orientation from+/// geometry alone, so its answer must equal the same_sense the lowering wrote.+/// </summary>+public sealed class SheetMetalFaceOrientationTests+{+ [Theory]+ [InlineData("l-bracket")]+ [InlineData("l-bracket-with-hole")]+ [InlineData("u-channel")]+ [InlineData("four-wall-tray-multiple-openings")]+ [InlineData("network-appliance-enclosure")]+ public void FormedFacesBindTheirOutwardNormal(string fixture)+ {+ var compiled = SheetMetalFirmament.CompileFile(Path.Combine(RepoRoot(), $"fixtures/Canonical/SheetMetal/{fixture}.firmament"));+ Assert.True(compiled.IsSuccess, string.Join("\n", compiled.Diagnostics.Select(d => d.Message)));+ var step = Step242Exporter.ExportBody(compiled.Part!.FormedBody!);+ Assert.True(step.IsSuccess);+ Assert.DoesNotContain(step.Diagnostics, d => d.Message.StartsWith("step-export-face-normal-inverted", StringComparison.Ordinal));+ var imported = Step242Importer.ImportBody(step.Value);+ Assert.True(imported.IsSuccess);+ var written = Regex.Matches(step.Value, @"ADVANCED_FACE\([^;]*,\.(T|F)\.\);").Select(m => m.Groups[1].Value == "T").ToArray();+ var faces = imported.Value.Topology.Faces.OrderBy(f => f.Id.Value).ToArray();+ Assert.Equal(written.Length, faces.Length);+ for (var i = 0; i < faces.Length; i++)+ Assert.True(written[i] == imported.Value.Bindings.GetFaceBinding(faces[i].Id).Orientation.IsAlignedWithSurface, $"face {i} was written facing into the material");+ }++ private static string RepoRoot()+ {+ var directory = new DirectoryInfo(AppContext.BaseDirectory);+ while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "Aetheris.slnx"))) directory = directory.Parent;+ return directory!.FullName;+ }+}diff --git a/Aetheris.SheetMetal/SheetMetalAuthoredLowering.cs b/Aetheris.SheetMetal/SheetMetalAuthoredLowering.csindex 3382b84f..b655a3f0 100644--- a/Aetheris.SheetMetal/SheetMetalAuthoredLowering.cs+++ b/Aetheris.SheetMetal/SheetMetalAuthoredLowering.cs@@ -785,7 +785,7 @@ internal static class AuthoredSheetMetalLowering{var b=new TopologyBuilder();var g=new BrepGeometryStore();var bindings=new BrepBindingModel();var points=new Dictionary<VertexId,Point3D>();var vertices=new Dictionary<Key,VertexId>();var edges=new Dictionary<EdgeKey,EdgeId>();var edgeGeometry=new Dictionary<EdgeId,EdgeGeometry>();var useCount=new Dictionary<EdgeId,int>();var faces=new List<FaceId>();- var boundaryCandidates=new List<(EdgeId Positive,EdgeId Negative,Point3D PA,Point3D PB,Point3D NA,Point3D NB,bool ProfileArc)>();+ var boundaryCandidates=new List<(EdgeId Positive,EdgeId Negative,Point3D PA,Point3D PB,Point3D NA,Point3D NB,bool ProfileArc,Vector3D Outward)>();var cutsByRegion=new Dictionary<string,List<CutBoundary>>(StringComparer.Ordinal);VertexId Vertex(Point3D p){var key=Key.Of(p);if(vertices.TryGetValue(key,out var id))return id;id=b.AddVertex();vertices[key]=id;points[id]=p;return id;}EdgeId Edge(Point3D a,Point3D q,EdgeGeometry? geometry=null){var key=EdgeKey.Of(a,q);if(edges.TryGetValue(key,out var id))return id;id=b.AddEdge(Vertex(a),Vertex(q));edges[key]=id;edgeGeometry[id]=geometry??Line(a,q);return id;}@@ -801,7 +801,7 @@ internal static class AuthoredSheetMetalLowering{var j=(i+1)%count;var pg=CurveFor(patch,true,i,patch.Positive[i],patch.Positive[j]);var ng=CurveFor(patch,false,i,patch.Negative[i],patch.Negative[j]);var pe=Edge(patch.Positive[i],patch.Positive[j],pg);var ne=Edge(patch.Negative[i],patch.Negative[j],ng);- boundaryCandidates.Add((pe,ne,patch.Positive[i],patch.Positive[j],patch.Negative[i],patch.Negative[j],patch.Kind==SheetRegionKind.Planar&&pg.Curve.Kind==CurveGeometryKind.Circle3));+ boundaryCandidates.Add((pe,ne,patch.Positive[i],patch.Positive[j],patch.Negative[i],patch.Negative[j],patch.Kind==SheetRegionKind.Planar&&pg.Curve.Kind==CurveGeometryKind.Circle3,WallOutward(patch.Positive,patch.Negative,i)));}}foreach(var candidate in boundaryCandidates)@@ -810,9 +810,9 @@ internal static class AuthoredSheetMetalLoweringvar loop=new[]{DirectedEdge(candidate.PA,candidate.PB),DirectedEdge(candidate.PB,candidate.NB),DirectedEdge(candidate.NB,candidate.NA),DirectedEdge(candidate.NA,candidate.PA)};if(candidate.ProfileArc&&edgeGeometry[candidate.Positive].Curve is { Kind:CurveGeometryKind.Circle3,Circle3:{ } arcCircle }){- var axis=Unit(candidate.PA-candidate.NA);Face([loop],SurfaceGeometry.FromCylinder(new CylinderSurface(arcCircle.Center,Direction3D.Create(axis),arcCircle.Radius,Direction3D.Create(candidate.PA-arcCircle.Center))));+ var axis=Unit(candidate.PA-candidate.NA);var wall=SurfaceGeometry.FromCylinder(new CylinderSurface(arcCircle.Center,Direction3D.Create(axis),arcCircle.Radius,Direction3D.Create(candidate.PA-arcCircle.Center)));Face([loop],wall,Aligned(wall,Mid(candidate.PA,candidate.PB),candidate.Outward));}- else{var u=candidate.PB-candidate.PA;var v=candidate.NA-candidate.PA;var normal=Unit(u.Cross(v));Face([loop],SurfaceGeometry.FromPlane(new PlaneSurface(candidate.PA,Direction3D.Create(normal),Direction3D.Create(Unit(u)))));}+ else{var u=candidate.PB-candidate.PA;var v=candidate.NA-candidate.PA;var normal=Unit(u.Cross(v));var wall=SurfaceGeometry.FromPlane(new PlaneSurface(candidate.PA,Direction3D.Create(normal),Direction3D.Create(Unit(u))));Face([loop],wall,Aligned(wall,Mid(candidate.PA,candidate.PB),candidate.Outward));}}AddCutWalls();foreach(var (edge,geometry) in edgeGeometry.OrderBy(x=>x.Key.Value)){var cid=new CurveGeometryId(g.Curves.Count()+1);g.AddCurve(cid,geometry.Curve);bindings.AddEdgeBinding(new(edge,cid,geometry.Interval));}@@ -830,7 +830,9 @@ internal static class AuthoredSheetMetalLoweringif(cut.Circle)loops.Add([ExistingEdge(cutEdges[0],points[0])]);else{var cutUses=new List<Use>();for(var i=0;i<points.Count;i++){var original=positive?(points.Count-2-i+points.Count)%points.Count:i;cutUses.Add(ExistingEdge(cutEdges[original],points[i]));}loops.Add(cutUses);}}- Face(loops,surface,positive);+ // A skin faces away from the sheet: the positive skin along the thickness direction, the negative against it.+ var across=patch.Positive[0]-patch.Negative[0];+ Face(loops,surface,positive?Aligned(surface,patch.Positive[0],across):Aligned(surface,patch.Negative[0],-across));}EdgeGeometry CurveFor(Patch patch,bool positive,int index,Point3D a,Point3D q){@@ -874,15 +876,15 @@ internal static class AuthoredSheetMetalLowering}else for(var i=0;i<cut.Positive.Count;i++){- var j=(i+1)%cut.Positive.Count;var loop=new[]{ExistingEdge(cut.PositiveEdges[i],cut.Positive[i]),DirectedEdge(cut.Positive[j],cut.Negative[j]),ExistingEdge(cut.NegativeEdges[i],cut.Negative[j]),DirectedEdge(cut.Negative[i],cut.Positive[i])};+ var j=(i+1)%cut.Positive.Count;var outward=-WallOutward(cut.Positive,cut.Negative,i);var loop=new[]{ExistingEdge(cut.PositiveEdges[i],cut.Positive[i]),DirectedEdge(cut.Positive[j],cut.Negative[j]),ExistingEdge(cut.NegativeEdges[i],cut.Negative[j]),DirectedEdge(cut.Negative[i],cut.Positive[i])};if(edgeGeometry[cut.PositiveEdges[i]].Curve is { Kind:CurveGeometryKind.Circle3,Circle3:{ } circle }){var axis=Unit(cut.Positive[i]-cut.Negative[i]);var center=circle.Center-axis*(spec.Thickness/2);- Face([loop],SurfaceGeometry.FromCylinder(new CylinderSurface(center,Direction3D.Create(axis),circle.Radius,Direction3D.Create(cut.Positive[i]-circle.Center))));+ var wall=SurfaceGeometry.FromCylinder(new CylinderSurface(center,Direction3D.Create(axis),circle.Radius,Direction3D.Create(cut.Positive[i]-circle.Center)));Face([loop],wall,Aligned(wall,Mid(cut.Positive[i],cut.Positive[j]),outward));}else{- var u=cut.Positive[j]-cut.Positive[i];var v=cut.Negative[i]-cut.Positive[i];var normal=Unit(u.Cross(v));Face([loop],SurfaceGeometry.FromPlane(new PlaneSurface(cut.Positive[i],Direction3D.Create(normal),Direction3D.Create(Unit(u)))));+ var u=cut.Positive[j]-cut.Positive[i];var v=cut.Negative[i]-cut.Positive[i];var normal=Unit(u.Cross(v));var wall=SurfaceGeometry.FromPlane(new PlaneSurface(cut.Positive[i],Direction3D.Create(normal),Direction3D.Create(Unit(u))));Face([loop],wall,Aligned(wall,Mid(cut.Positive[i],cut.Positive[j]),outward));}}}@@ -897,6 +899,29 @@ internal static class AuthoredSheetMetalLowering}return Line(a,q);}+ // Faces are bound by geometry: a surface is aligned when its normal points out of the material there.+ static bool Aligned(SurfaceGeometry surface,Point3D at,Vector3D outward)+ {+ Vector3D normal=surface.Kind switch+ {+ SurfaceGeometryKind.Plane=>surface.Plane!.Value.Normal.ToVector(),+ SurfaceGeometryKind.Cylinder=>Radial(at,surface.Cylinder!.Value.Origin,surface.Cylinder!.Value.Axis.ToVector()),+ _=>throw new InvalidOperationException($"Sheet faces are planes and cylinders, not {surface.Kind}.")+ };+ return normal.Dot(outward)>0d;+ static Vector3D Radial(Point3D p,Point3D origin,Vector3D axis){var d=p-origin;return d-axis*d.Dot(axis);}+ }+ static Point3D Mid(Point3D a,Point3D q)=>new((a.X+q.X)/2,(a.Y+q.Y)/2,(a.Z+q.Z)/2);+ // The wall on boundary span i of a contour (positive and negative skins as parallel point loops) faces away+ // from the region the contour encloses: right of travel when the contour runs counter-clockwise about the+ // thickness direction.+ static Vector3D WallOutward(IReadOnlyList<Point3D> positive,IReadOnlyList<Point3D> negative,int i)+ {+ var across=Vector3D.Zero;var area=Vector3D.Zero;+ for(var k=0;k<positive.Count;k++){across+=positive[k]-negative[k];var a=positive[k];var q=positive[(k+1)%positive.Count];area+=new Vector3D(a.Y*q.Z-a.Z*q.Y,a.Z*q.X-a.X*q.Z,a.X*q.Y-a.Y*q.X);}+ var n=Unit(positive[i]-negative[i]);var u=positive[(i+1)%positive.Count]-positive[i];var right=u.Cross(n);+ return area.Dot(across)>0d?right:-right;+ }EdgeGeometry Line(Point3D a,Point3D q){var d=q-a;return new(CurveGeometry.FromLine(new Line3Curve(a,Direction3D.Create(d))),new(0,d.Length));}Point3D ProjectToAxis(Point3D p,Point3D origin,Vector3D axis)=>origin+axis*((p-origin).Dot(axis));}diff --git a/docs/development/reports/2026-11/HOLLOW-GENERALIZATION.md b/docs/development/reports/2026-11/HOLLOW-GENERALIZATION.mdindex 733c9511..749a79c5 100644--- a/docs/development/reports/2026-11/HOLLOW-GENERALIZATION.md+++ b/docs/development/reports/2026-11/HOLLOW-GENERALIZATION.md@@ -227,12 +227,20 @@ That pass exposed builders whose faces declare the wrong outward normal, which t- `WireCoils`: the tube's B-spline nets have normals into the wire but were bound aligned. Fixed (bound opposed, loopsreversed). The STEP importer's B-spline alignment fallback, a polygon-normal test that is meaningless on a faceturning through more than a right angle, now reads same_sense instead.-- Still open, now reported by the writer as `step-export-face-normal-inverted` (a warning; the CLI does not print- export warnings): 42 fixtures, by planar face count: sheet-metal lowering (most of the 7–20 counts), structural- members and joints, PlasticShell rib/standoff faces, `EdgeFinish` end faces of a bounded fillet, the counterbore- shoulder, `EquationCurves` arch caps, `Revolve/arbitrary-axis`, one section-chain duct. OCCT reorients these on read- and our importer re-solves them, so volumes are right; a receiving system that trusts same_sense sees faces turned- inward.+- Sheet-metal lowering (`SheetMetalAuthoredLowering.Emit`): fixed. It bound the negative skin opposed to a surface+ whose normal already pointed against the thickness direction, and gave every side and cut wall the normal+ `edge × thickness`, which faces outward only when the authored contour runs clockwise. Faces are now bound by+ geometry: a skin faces away from the sheet, a wall away from the region its contour encloses (into a cut), whichever+ way the contour runs. All 19 sheet-metal fixtures keep their OCCT volumes, and the importer, which derives+ orientation from geometry alone, now agrees with every same_sense written (it disagreed on 5 of 14 faces of the+ L-bracket).+- Still open. The writer reports flat faces as `step-export-face-normal-inverted` (a warning the CLI does not print).+ Comparing the importer's geometric orientation with the written same_sense over the whole sweep finds disagreements+ in: structural members and joints, PlasticShell rib/standoff faces, `EdgeFinish` end faces of a bounded fillet, the+ counterbore shoulder, `EquationCurves` arch caps, `Revolve/arbitrary-axis`, the section-chain and sculpt bodies+ (cylinders), the freeform `surf-x1`/`surf-x2` housings, `Hollow/bracket-tray` (one cylinder) and the internal spur+ gear (its linear-extrusion flanks). Some of these may be importer misreadings rather than builder errors; each needs+ a look. OCCT reorients on read and volumes are right; a receiver that trusts same_sense sees inward faces.`BrepRevolvedShell` now binds an annulus's larger circle as its outer boundary (the writer had labelled whichevercircle came first).@@ -246,5 +254,5 @@ moves to 188525.9 (OCCT 188312.8) with the B-spline fallback.- Closed vessels: the cavity is a second shell; the section stack and the revolved shell both need void shells.- Apexes and poles in `BrepRevolvedShell` (pointed cones, domed bottoms).- Perforation onto the general hollow body instead of its own cylinder builder.-- Outward normals in the builders listed above.+- Outward normals in the builders still listed above.- Phases 4 and 5 as planned.