diff --git a/OpenNest.Core/CutOffTrim.cs b/OpenNest.Core/CutOffTrim.cs index f70bf5a0..05ff8480 100644 --- a/OpenNest.Core/CutOffTrim.cs +++ b/OpenNest.Core/CutOffTrim.cs @@ -113,6 +113,32 @@ namespace OpenNest : CutOffEdit.Refused(AtSheetEdge); } + /// + /// Stops a new at the first crossing cut-off from its start edge, + /// the origin side unless cut toward the origin. It stays full + /// length, with no changes, when nothing crosses it or the line left would be shorter than + /// the minimum segment length. need not be on the plate yet. + /// + public static CutOffEdit StopAtFirstCrossing(Plate plate, CutOff target, CutOffSettings settings) + { + var (start, end) = NominalSpan(plate, target); + var crossings = Crossings(plate, target) + .Where(c => c > start + Tolerance.Epsilon && c < end - Tolerance.Epsilon) + .ToList(); + if (crossings.Count == 0) + return CutOffEdit.Of(System.Array.Empty(), null); + + var towardOrigin = settings?.CutDirection == CutDirection.TowardOrigin; + var startsHigh = towardOrigin == CutOff.FarEdgeIsHigh(target.Axis, plate.Quadrant); + var stop = startsHigh ? crossings.Max() : crossings.Min(); + var remaining = startsHigh ? end - stop : stop - start; + if (remaining < System.Math.Max(settings?.MinSegmentLength ?? 0, Tolerance.Epsilon)) + return CutOffEdit.Of(System.Array.Empty(), null); + return startsHigh + ? Single(plate, target, stop, target.EndLimit) + : Single(plate, target, target.StartLimit, stop); + } + /// /// Swaps which of two meeting cut-offs runs through: the one that stopped at the junction /// extends past it (as does), and the one that ran through is trimmed diff --git a/OpenNest.Tests/CutOffs/CutOffFirstCrossingTests.cs b/OpenNest.Tests/CutOffs/CutOffFirstCrossingTests.cs new file mode 100644 index 00000000..651bb4cb --- /dev/null +++ b/OpenNest.Tests/CutOffs/CutOffFirstCrossingTests.cs @@ -0,0 +1,141 @@ +using OpenNest.CNC; +using OpenNest.Geometry; + +namespace OpenNest.Tests.CutOffs; + +/// +/// A new Sheet Cut-Off line runs from its start edge, set by the cut direction, and stops at the +/// first crossing line it meets, unless that would leave it too short. +/// +public class CutOffFirstCrossingTests +{ + private static Plate PlateWithHorizontals(int quadrant, params double[] ys) + { + var plate = new Plate(48, 96) { Quadrant = quadrant }; + foreach (var y in ys) + plate.CutOffs.Add(new CutOff(new Vector(0, y), CutOffAxis.Horizontal)); + return plate; + } + + private static CutOffEdit Stop(Plate plate, CutOff line, CutDirection direction = CutDirection.AwayFromOrigin) + { + var edit = CutOffTrim.StopAtFirstCrossing(plate, line, new CutOffSettings { CutDirection = direction }); + Assert.True(edit.Succeeded, edit.Refusal); + edit.Apply(); + return edit; + } + + [Fact] + public void AwayFromOrigin_StopsAtTheCrossingNearestTheOrigin() + { + var plate = PlateWithHorizontals(1, 30, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Stop(plate, line); + + Assert.Null(line.StartLimit); + Assert.Equal(10, line.EndLimit); + } + + [Fact] + public void TowardOrigin_StopsAtTheCrossingNearestTheFarEdge() + { + var plate = PlateWithHorizontals(1, 30, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Stop(plate, line, CutDirection.TowardOrigin); + + Assert.Equal(30, line.StartLimit); + Assert.Null(line.EndLimit); + } + + [Fact] + public void AwayFromOrigin_StartsAtTheOriginEdgeInAnyQuadrant() + { + var plate = PlateWithHorizontals(3, -30, -10); + var line = new CutOff(new Vector(-40, -25), CutOffAxis.Vertical); + + Stop(plate, line); + + Assert.Equal(-10, line.StartLimit); + Assert.Null(line.EndLimit); + } + + [Fact] + public void HorizontalLineStopsAtTheFirstVertical() + { + var plate = new Plate(48, 96); + plate.CutOffs.Add(new CutOff(new Vector(60, 0), CutOffAxis.Vertical)); + plate.CutOffs.Add(new CutOff(new Vector(20, 0), CutOffAxis.Vertical)); + var line = new CutOff(new Vector(70, 12), CutOffAxis.Horizontal); + + Stop(plate, line); + + Assert.Null(line.StartLimit); + Assert.Equal(20, line.EndLimit); + } + + [Fact] + public void StaysFullLengthWhenNothingCrossesIt() + { + var plate = new Plate(48, 96); + plate.CutOffs.Add(new CutOff(new Vector(10, 0), CutOffAxis.Vertical)); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Assert.Empty(Stop(plate, line).Changes); + Assert.Null(line.StartLimit); + Assert.Null(line.EndLimit); + } + + [Fact] + public void ALineStoppedShortOfItDoesNotCross() + { + var plate = new Plate(48, 96); + plate.CutOffs.Add(new CutOff(new Vector(0, 10), CutOffAxis.Horizontal) { EndLimit = 30 }); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Assert.Empty(Stop(plate, line).Changes); + } + + [Fact] + public void StaysFullLengthWhenTheLineLeftWouldBeTooShort() + { + var plate = PlateWithHorizontals(1, 0.03, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Assert.Empty(Stop(plate, line).Changes); + Assert.Null(line.EndLimit); + } + + [Fact] + public void StoppingShortOfTheFarEdgeSaysTheOvercutIsOff() + { + var plate = PlateWithHorizontals(1, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical) { HeadLockOvercut = 1.5 }; + + Assert.Equal(CutOffTrim.OvercutOff, Stop(plate, line).Notice); + } + + [Fact] + public void ReachingTheFarEdgeKeepsTheOvercut() + { + var plate = PlateWithHorizontals(1, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical) { HeadLockOvercut = 1.5 }; + + Assert.Null(Stop(plate, line, CutDirection.TowardOrigin).Notice); + } + + [Fact] + public void TheRegeneratedLineEndsOnTheCrossing() + { + var plate = PlateWithHorizontals(1, 10); + var line = new CutOff(new Vector(40, 25), CutOffAxis.Vertical); + + Stop(plate, line); + plate.CutOffs.Add(line); + plate.RegenerateCutOffs(new CutOffSettings()); + + var moves = line.Drawing.Program.Codes.OfType().ToList(); + Assert.Equal(10, moves.Max(move => move.EndPoint.Y), 6); + } +} diff --git a/OpenNest.WinForms.Tests/Actions/ActionCutOffHintTests.cs b/OpenNest.WinForms.Tests/Actions/ActionCutOffHintTests.cs index 7d78942d..c409f47f 100644 --- a/OpenNest.WinForms.Tests/Actions/ActionCutOffHintTests.cs +++ b/OpenNest.WinForms.Tests/Actions/ActionCutOffHintTests.cs @@ -1,3 +1,4 @@ +using System.Drawing; using System.Reflection; using System.Windows.Forms; using OpenNest.Actions; @@ -6,13 +7,63 @@ using OpenNest.Help; namespace OpenNest.WinForms.Tests.Actions; -/// The manual cut-off tool says how to turn its line and where its help is. +/// +/// The manual cut-off tool says how to turn its line and where its help is, stops a new line at +/// the first crossing and draws it full length after Tab. +/// public class ActionCutOffHintTests { private static void PressKey(PlateView view, Keys key) => typeof(Control).GetMethod("OnKeyDown", BindingFlags.Instance | BindingFlags.NonPublic)! .Invoke(view, new object[] { new KeyEventArgs(key) }); + private static void Mouse(PlateView view, string handler, MouseButtons button, Point at) => + typeof(PlateView).GetMethod(handler, BindingFlags.Instance | BindingFlags.NonPublic, + new[] { typeof(MouseEventArgs) })! + .Invoke(view, new object[] { new MouseEventArgs(button, 1, at.X, at.Y, 0) }); + + private static CutOff ClickVerticalLine(PlateView view, double x, double y) + { + var at = view.PointWorldToControl(new OpenNest.Geometry.Vector(x, y)); + var point = new Point((int)at.X, (int)at.Y); + Mouse(view, "OnMouseMove", MouseButtons.None, point); + Mouse(view, "OnMouseDown", MouseButtons.Left, point); + Mouse(view, "OnMouseUp", MouseButtons.Left, point); + return view.Plate.CutOffs[^1]; + } + + private static PlateView ViewWithHorizontalAt(double y) + { + var plate = new Plate(48, 96); + plate.CutOffs.Add(new CutOff(new OpenNest.Geometry.Vector(0, y), CutOffAxis.Horizontal)); + plate.RegenerateCutOffs(new CutOffSettings()); + var view = new PlateView { Plate = plate, Size = new Size(800, 600) }; + view.ZoomToFit(); + return view; + } + + [Fact] + public void ANewLineStopsAtTheFirstCrossingUntilTabMakesItFullLength() => StaTestThread.Run(() => + { + using var view = ViewWithHorizontalAt(20); + view.SetAction(typeof(ActionCutOff)); + + var stopped = ClickVerticalLine(view, 40, 30); + Assert.Null(stopped.StartLimit); + Assert.Equal(20, stopped.EndLimit); + + PressKey(view, Keys.Tab); + Assert.StartsWith("Sheet Cut-Off: click to add a full-length vertical line", view.Status); + var full = ClickVerticalLine(view, 60, 30); + Assert.Null(full.StartLimit); + Assert.Null(full.EndLimit); + + // The choice outlasts the tool. + view.SetAction(typeof(ActionSelect)); + view.SetAction(typeof(ActionCutOff)); + Assert.StartsWith("Sheet Cut-Off: click to add a full-length vertical line", view.Status); + }, TimeSpan.FromMinutes(1), "The STA test did not complete."); + [Fact] public void StatusNamesTheAxisAndSpaceTurnsIt() => StaTestThread.Run(() => { @@ -20,12 +71,12 @@ public class ActionCutOffHintTests view.SetAction(typeof(ActionCutOff)); Assert.Equal( - "Sheet Cut-Off: click to add a vertical line, Space turns it horizontal, Esc finishes, F1 for help", + "Sheet Cut-Off: click to add a vertical line stopped at the first crossing, Space turns it horizontal, Tab makes it full length, Esc finishes, F1 for help", view.Status); Assert.Equal(HelpTopics.SheetCutOff, view.ActionHelpTopic); PressKey(view, Keys.Space); - Assert.StartsWith("Sheet Cut-Off: click to add a horizontal line, Space turns it vertical", view.Status); + Assert.StartsWith("Sheet Cut-Off: click to add a horizontal line stopped at the first crossing, Space turns it vertical", view.Status); view.SetAction(typeof(ActionSelect)); Assert.Null(view.ActionHelpTopic); diff --git a/OpenNest/Actions/ActionCutOff.cs b/OpenNest/Actions/ActionCutOff.cs index 556f6588..f464e894 100644 --- a/OpenNest/Actions/ActionCutOff.cs +++ b/OpenNest/Actions/ActionCutOff.cs @@ -17,6 +17,7 @@ namespace OpenNest.Actions private CutOffSettings settings; private CutOffAxis lockedAxis = CutOffAxis.Vertical; private Dictionary perimeterCache; + private string notice; public ActionCutOff(PlateView plateView) : base(plateView) @@ -44,6 +45,7 @@ namespace OpenNest.Actions previewCutOff = null; perimeterCache = null; + notice = null; plateView.Invalidate(); } @@ -51,32 +53,56 @@ namespace OpenNest.Actions public override bool IsBusy() => false; - public override string Hint => - $"click to add a {AxisName(lockedAxis)} line, Space turns it {AxisName(OtherAxis(lockedAxis))}, Esc finishes, F1 for help"; + public override string Hint + { + get + { + var hint = FullLength + ? $"click to add a full-length {AxisName(lockedAxis)} line, Space turns it {AxisName(OtherAxis(lockedAxis))}, Tab stops it at the first crossing, Esc finishes, F1 for help" + : $"click to add a {AxisName(lockedAxis)} line stopped at the first crossing, Space turns it {AxisName(OtherAxis(lockedAxis))}, Tab makes it full length, Esc finishes, F1 for help"; + return notice == null ? hint : $"{notice} {hint}"; + } + } public override string HelpTopic => HelpTopics.SheetCutOff; + private bool FullLength => plateView.CutOffs.FullLengthNewLines; + private static CutOffAxis OtherAxis(CutOffAxis axis) => axis == CutOffAxis.Vertical ? CutOffAxis.Horizontal : CutOffAxis.Vertical; private static string AxisName(CutOffAxis axis) => axis == CutOffAxis.Vertical ? "vertical" : "horizontal"; - private void OnMouseMove(object sender, MouseEventArgs e) + private CutOff NewCutOff(Vector point) { - var pt = plateView.CurrentPoint; - previewCutOff = new CutOff(pt, lockedAxis) { HeadLockOvercut = settings.NewCutOffHeadLockOvercut }; + var cutOff = new CutOff(point, lockedAxis) { HeadLockOvercut = settings.NewCutOffHeadLockOvercut }; + var edit = FullLength ? null : CutOffTrim.StopAtFirstCrossing(plateView.Plate, cutOff, settings); + edit?.Apply(); + + if (notice != edit?.Notice) + { + notice = edit?.Notice; + plateView.RefreshActionStatus(); + } + return cutOff; + } + + private void UpdatePreview() + { + previewCutOff = NewCutOff(plateView.CurrentPoint); previewCutOff.Regenerate(plateView.Plate, settings, perimeterCache); plateView.Invalidate(); } + private void OnMouseMove(object sender, MouseEventArgs e) => UpdatePreview(); + private void OnMouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; - var pt = plateView.CurrentPoint; - var cutoff = new CutOff(pt, lockedAxis) { HeadLockOvercut = settings.NewCutOffHeadLockOvercut }; + var cutoff = NewCutOff(plateView.CurrentPoint); using var change = plateView.BeginChange("Add Cut-Off"); change.MarkChanged(); @@ -94,14 +120,16 @@ namespace OpenNest.Actions plateView.RefreshActionStatus(); if (previewCutOff != null) - { - previewCutOff = new CutOff(plateView.CurrentPoint, lockedAxis) - { - HeadLockOvercut = settings.NewCutOffHeadLockOvercut, - }; - previewCutOff.Regenerate(plateView.Plate, settings, perimeterCache); - plateView.Invalidate(); - } + UpdatePreview(); + } + else if (e.KeyCode == Keys.Tab) + { + plateView.CutOffs.FullLengthNewLines = !FullLength; + plateView.RefreshActionStatus(); + + if (previewCutOff != null) + UpdatePreview(); + e.Handled = true; } else if (e.KeyCode == Keys.Escape) { @@ -145,7 +173,7 @@ namespace OpenNest.Actions // The status bar is easy to miss, so the cursor says how to turn the line. private void DrawRotateTag(Graphics g) { - var text = $"Space: turn {AxisName(OtherAxis(lockedAxis))}"; + var text = $"Space: turn {AxisName(OtherAxis(lockedAxis))}\nTab: {(FullLength ? "stop at first crossing" : "full length")}"; var cursor = plateView.PointWorldToGraph(plateView.CurrentPoint); var size = g.MeasureString(text, plateView.Font); var rect = new RectangleF(cursor.X + 16f, cursor.Y + 16f, size.Width + 6, size.Height + 4); diff --git a/OpenNest/Controls/CutOffHandler.cs b/OpenNest/Controls/CutOffHandler.cs index a95ad0ae..434747f4 100644 --- a/OpenNest/Controls/CutOffHandler.cs +++ b/OpenNest/Controls/CutOffHandler.cs @@ -22,6 +22,9 @@ namespace OpenNest.Controls public bool IsDragging { get; private set; } + /// Whether Sheet Cut-Off draws full-length lines instead of stopping at the first crossing. + public bool FullLengthNewLines { get; set; } + public CutOff TryStartDrag(Vector point, double tolerance) { var hitCutOff = GetCutOffAtPoint(point, tolerance); diff --git a/OpenNest/Controls/PlateView.cs b/OpenNest/Controls/PlateView.cs index be5d3438..9bf530ff 100644 --- a/OpenNest/Controls/PlateView.cs +++ b/OpenNest/Controls/PlateView.cs @@ -656,9 +656,10 @@ namespace OpenNest.Controls return base.ProcessCmdKey(ref msg, keyData); } - // Trim/Extend switches mode with Tab, which would otherwise move focus off the view. + // The cut-off tools switch mode with Tab, which would otherwise move focus off the view. protected override bool IsInputKey(Keys keyData) => - keyData == Keys.Tab && actionManager.CurrentAction is ActionTrimExtendCutOff || base.IsInputKey(keyData); + keyData == Keys.Tab && actionManager.CurrentAction is ActionTrimExtendCutOff or ActionCutOff + || base.IsInputKey(keyData); protected override void OnKeyDown(KeyEventArgs e) { diff --git a/docs/automatic-scrap-cutoffs.md b/docs/automatic-scrap-cutoffs.md index 2f935f25..084d9458 100644 --- a/docs/automatic-scrap-cutoffs.md +++ b/docs/automatic-scrap-cutoffs.md @@ -103,6 +103,8 @@ Saving `.nest` retains ordinary cutoff positions, axes, limits, head-locked over ## Trim and extend +**Sheet Cut-Off** stops a new line at the first crossing cut-off from its start edge: the origin side, or the far edge when the cut direction is `TowardOrigin`, in any plate quadrant. Clicking past that crossing still draws the line from the start edge, as the preview shows. The line runs full length when nothing crosses it or what remains would be shorter than the minimum segment length. Tab switches to full-length lines and back, and the choice lasts until the nest is closed. Only the new line's limits are set (`CutOffTrim.StopAtFirstCrossing`); existing lines are not changed. A head-locked line stopped short of the far edge loses its overcut, and the status bar says so. + **Plate > Trim/Extend Cut-Off** (and the toolbar button beside **Sheet Cut-Off**) edits a cut-off against the cut-offs that cross it, AutoCAD-style. Tab switches between Trim and Extend; Esc finishes. Both change only the cut-off's start and end limits (`CutOffTrim` in Core), so nothing new is saved and Plan Cutting sees the change like any other limit change. - **Trim**: click the piece to remove. The crossing cut-offs on either side of the click (or the line's end) bound the piece, and the limit on that side moves to the crossing line's centre, where half the crossing cut's kerf still overlaps the trimmed end. A crossing line counts when its own span reaches the target, including a line that stops on it.