fix(view): flag bends that run with the grain, not across it

Bend lines should run perpendicular to the sheet's grain to avoid
cracking on the outside of the bend. The grain warning had it backwards:
it outlined parts whose bends were not parallel to the grain, so on a
60x120 sheet (grain side to side along X) it flagged the correctly
oriented parts and passed the ones bent with the grain.

It now outlines a part when any of its bends is more than 5 degrees off
perpendicular to the plate's grain, and the check is a tested helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-18 10:25:18 -05:00
1 parent 250b52321e
commit bf412707f8
3 files changed
+83 -34

No files matched your search

@@ -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));
}
}
+45 -33
View File
@@ -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)
);
}
/// <summary>
/// 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.
/// </summary>
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;
}
/// <summary>The angle between two undirected lines, in [0, π/2].</summary>
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)
+1 -1
View File
@@ -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