From 7894b74cb71e8c9c8be1663b85053ae6635a8be3 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 16 Nov 2026 10:45:26 -0500 Subject: [PATCH] fix(shapes): cut shape library parts in the CAD Converter's directions The CAD Converter winds every drawing's outside clockwise and its cutouts counter-clockwise (ShapeProfile.NormalizeEntities) before building the program, but the Shape Library built its programs straight from the generated entities, which run counter-clockwise. A shape part therefore cut its outside the opposite way from an imported part, and opening it in the converter showed (and on save, wrote) the reverse of what was nested. ShapeDefinition.CreateDrawing now normalizes the entities the same way, so every shape cuts outside clockwise and cutouts counter-clockwise. Tests cover each shape type, including the pipe flange's holes, and that rebuilding a shape drawing through the converter's import path keeps its directions. Drawings already made from shapes keep their stored direction until they are recreated or saved from the converter. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/Shapes/ShapeDefinition.cs | 6 +- .../Shapes/ShapeCutDirectionTests.cs | 103 ++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 OpenNest.Tests/Shapes/ShapeCutDirectionTests.cs diff --git a/OpenNest.Core/Shapes/ShapeDefinition.cs b/OpenNest.Core/Shapes/ShapeDefinition.cs index d8f9841a..afd11c20 100644 --- a/OpenNest.Core/Shapes/ShapeDefinition.cs +++ b/OpenNest.Core/Shapes/ShapeDefinition.cs @@ -51,9 +51,13 @@ namespace OpenNest.Shapes protected static string Dim(double value) => value.ToString("0.###"); + /// + /// Builds the shape's drawing with the CAD converter's cut directions: the outside + /// clockwise and every cutout counter-clockwise (see ). + /// protected Drawing CreateDrawing(List entities) { - var pgm = ConvertGeometry.ToProgram(entities); + var pgm = ConvertGeometry.ToProgram(ShapeProfile.NormalizeEntities(entities)); if (pgm == null) throw new InvalidOperationException( diff --git a/OpenNest.Tests/Shapes/ShapeCutDirectionTests.cs b/OpenNest.Tests/Shapes/ShapeCutDirectionTests.cs new file mode 100644 index 00000000..deac5cf2 --- /dev/null +++ b/OpenNest.Tests/Shapes/ShapeCutDirectionTests.cs @@ -0,0 +1,103 @@ +using OpenNest.Bending; +using OpenNest.CNC; +using OpenNest.CNC.Posting; +using OpenNest.Geometry; +using OpenNest.IO; +using OpenNest.Shapes; + +namespace OpenNest.Tests.Shapes; + +/// +/// Shape library drawings cut in the CAD converter's directions: the outside clockwise and +/// every cutout counter-clockwise. +/// +public class ShapeCutDirectionTests +{ + public static TheoryData Shapes() + { + var shapes = new ShapeDefinition[] + { + new CircleShape(), + new IsoscelesTriangleShape(), + new LShape(), + new NgonShape(), + new PipeFlangeShape(), + new PipeFlangeShape { Blind = true }, + new RectangleShape(), + new RightTriangleShape(), + new RingShape(), + new RoundedRectangleShape(), + new TShape(), + new TrapezoidShape(), + }; + + var data = new TheoryData(); + foreach (var shape in shapes) + { + shape.SetPreviewDefaults(); + data.Add(shape); + } + + return data; + } + + [Theory] + [MemberData(nameof(Shapes))] + public void GetDrawing_CutsOutsideClockwiseAndCutoutsCounterClockwise(ShapeDefinition shape) + { + AssertConverterWinding(shape.GetDrawing().Program, shape.Name); + } + + /// + /// Opening a shape drawing in the CAD Converter and saving it rebuilds it from its program + /// geometry; the cut directions must come back unchanged. + /// + [Theory] + [MemberData(nameof(Shapes))] + public void OpenInConverter_KeepsTheShapesCutDirections(ShapeDefinition shape) + { + var drawing = shape.GetDrawing(); + var editable = CadImporter.EditableEntities(drawing); + + var rebuilt = CadImporter.BuildDrawing( + new CadImportResult { Entities = editable, Name = drawing.Name }, editable, + new List(), 1, null, null); + + Assert.Equal(Directions(drawing.Program), Directions(rebuilt.Program)); + AssertConverterWinding(rebuilt.Program, shape.Name); + } + + private static void AssertConverterWinding(Program program, string name) + { + var contours = PostContours.Extract(program, Vector.Zero); + var perimeter = ContourWinding.PerimeterIndex(contours); + + Assert.True(perimeter >= 0, $"{name}: no perimeter"); + for (var i = 0; i < contours.Count; i++) + { + var area = ContourWinding.SignedArea(contours[i]); + if (i == perimeter) + Assert.True(area < 0, $"{name}: outside is not clockwise"); + else + Assert.True(area > 0, $"{name}: cutout {i} is not counter-clockwise"); + } + } + + /// Each contour's winding sign, in a stable order so reordering does not matter. + private static List Directions(Program program) => + PostContours.Extract(program, Vector.Zero) + .Select(c => System.Math.Sign(ContourWinding.SignedArea(c))) + .OrderBy(s => s) + .ToList(); + + [Fact] + public void PipeFlange_HasCutouts() + { + var shape = new PipeFlangeShape(); + shape.SetPreviewDefaults(); + + var contours = PostContours.Extract(shape.GetDrawing().Program, Vector.Zero); + + Assert.True(contours.Count > 1); + } +}