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); + } +}