diff --git a/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs b/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs index a1f088cc..66ef4197 100644 --- a/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs +++ b/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs @@ -37,4 +37,41 @@ public class BendLinePlacementTests Assert.Equal(10, world.X, 9); Assert.Equal(25, world.Y, 9); } + + // Grain angle 0 runs along X, the long side-to-side direction of a 60x120 sheet. + // Bends should run across it (perpendicular) to avoid cracking. + [Theory] + [InlineData(0, 0, true)] // bend along X, with the grain + [InlineData(0, 90, false)] // bend along Y, across the grain + [InlineData(90, 90, true)] // rotated 90°, now with the grain + [InlineData(0, 45, true)] // diagonal + [InlineData(0, 87, false)] // inside the 5° tolerance + [InlineData(0, 270, false)] // direction of the bend line does not matter + public void HasBendNotAcrossGrain_FlagsBendsThatAreNotPerpendicularToTheGrain( + double rotationDegrees, double bendDegrees, bool flagged) + { + var angle = Angle.ToRadians(bendDegrees); + var drawing = new Drawing("bent", new CNC.Program()); + drawing.Bends.Add(new Bend + { + StartPoint = new Vector(0, 0), + EndPoint = new Vector(10 * System.Math.Cos(angle), 10 * System.Math.Sin(angle)), + }); + var part = new Part(drawing, new Vector(0, 0)); + if (rotationDegrees != 0) + part.Rotate(Angle.ToRadians(rotationDegrees), part.Location); + + Assert.Equal(flagged, PlateRenderer.HasBendNotAcrossGrain(part, grainAngle: 0)); + } + + [Fact] + public void HasBendNotAcrossGrain_FlagsAPartWhenAnyBendRunsWithTheGrain() + { + var drawing = new Drawing("bracket", new CNC.Program()); + drawing.Bends.Add(new Bend { StartPoint = new Vector(5, 0), EndPoint = new Vector(5, 10) }); + drawing.Bends.Add(new Bend { StartPoint = new Vector(0, 5), EndPoint = new Vector(10, 5) }); + var part = new Part(drawing, new Vector(0, 0)); + + Assert.True(PlateRenderer.HasBendNotAcrossGrain(part, grainAngle: 0)); + } } diff --git a/OpenNest/Controls/PlateRenderer.cs b/OpenNest/Controls/PlateRenderer.cs index 579b71f5..9e049d04 100644 --- a/OpenNest/Controls/PlateRenderer.cs +++ b/OpenNest/Controls/PlateRenderer.cs @@ -627,42 +627,54 @@ namespace OpenNest.Controls ) return; - var grainAngle = plate.GrainAngle; + if (!HasBendNotAcrossGrain(part, plate.GrainAngle)) + return; + + // The placed part's world bounds, so rotated parts are outlined where they sit. + var box = part.BoundingBox; + var pt1 = view.PointWorldToGraph(box.Location); + var pt2 = view.PointWorldToGraph( + new Vector(box.X + box.Length, box.Y + box.Width) + ); + using var warnPen = new Pen(muted ? Color.Gray : Color.FromArgb(180, 255, 140, 0), 2f); + g.DrawRectangle( + warnPen, + pt1.X, + pt2.Y, + System.Math.Abs(pt2.X - pt1.X), + System.Math.Abs(pt2.Y - pt1.Y) + ); + } + + /// + /// Whether any of the placed part's bends is not across the grain. Bend lines should run + /// perpendicular to the sheet's grain to avoid cracking on the outside of the bend, so a + /// bend more than 5° off perpendicular is flagged. + /// + internal static bool HasBendNotAcrossGrain(Part part, double grainAngle) + { + var bends = part.BaseDrawing.Bends; + if (bends == null) + return false; + var tolerance = Angle.ToRadians(5); - - foreach (var bend in part.BaseDrawing.Bends) + foreach (var bend in bends) { - var bendAngle = bend.LineAngle + part.Rotation; - bendAngle = bendAngle % System.Math.PI; - if (bendAngle < 0) - bendAngle += System.Math.PI; - - var grainNormalized = grainAngle % System.Math.PI; - if (grainNormalized < 0) - grainNormalized += System.Math.PI; - - var diff = System.Math.Abs(bendAngle - grainNormalized); - diff = System.Math.Min(diff, System.Math.PI - diff); - - if (diff > tolerance) - { - // The placed part's world bounds, so rotated parts are outlined where they sit. - var box = part.BoundingBox; - var pt1 = view.PointWorldToGraph(box.Location); - var pt2 = view.PointWorldToGraph( - new Vector(box.X + box.Length, box.Y + box.Width) - ); - using var warnPen = new Pen(muted ? Color.Gray : Color.FromArgb(180, 255, 140, 0), 2f); - g.DrawRectangle( - warnPen, - pt1.X, - pt2.Y, - System.Math.Abs(pt2.X - pt1.X), - System.Math.Abs(pt2.Y - pt1.Y) - ); - return; - } + var diff = LineAngleBetween(bend.LineAngle + part.Rotation, grainAngle); + if (System.Math.Abs(diff - Angle.HalfPI) > tolerance) + return true; } + + return false; + } + + /// The angle between two undirected lines, in [0, π/2]. + private static double LineAngleBetween(double a, double b) + { + var diff = (a - b) % System.Math.PI; + if (diff < 0) + diff += System.Math.PI; + return System.Math.Min(diff, System.Math.PI - diff); } private void DrawOffsetGeometry(Graphics g) diff --git a/OpenNest/Forms/MainForm.Designer.cs b/OpenNest/Forms/MainForm.Designer.cs index 1c0522e9..b96ac4a5 100644 --- a/OpenNest/Forms/MainForm.Designer.cs +++ b/OpenNest/Forms/MainForm.Designer.cs @@ -510,7 +510,7 @@ namespace OpenNest.Forms mnuViewShowBendLines.Name = "mnuViewShowBendLines"; mnuViewShowBendLines.Size = new System.Drawing.Size(222, 22); mnuViewShowBendLines.Text = "Show Bend Lines"; - mnuViewShowBendLines.ToolTipText = "Show bend lines on bent parts and outline parts whose bends are not parallel to the plate's grain"; + mnuViewShowBendLines.ToolTipText = "Show bend lines on bent parts and outline parts with a bend that does not run across the plate's grain"; mnuViewShowBendLines.Click += ToggleShowBendLines_Click; // // toolStripMenuItem5