Files
OpenNest/OpenNest.Desktop/Interaction/SheetCutOffTool.cs
T
ajandClaude Opus 5.5 2f375215fc feat(desktop): share the cut-off tools and automatic scrap cutoffs
CutOffEdits ports the WinForms manual cut-off edits, each one undo step
under its WinForms label: Add Cut-Off (a Sheet Cut-Off line, stopped at
the first crossing unless full length), Trim/Extend Cut-Off and Swap
Through-Cut, applied to the definitions in Plate.CutOffs, rebuilt with
RegenerateCutOffs and placed with PlateSequencing.PlaceCutOffs.
CutOffDrag is the Move Cut-Off drag, taking the joined lines along.

SheetCutOffTool (Space turns the axis, Tab toggles full length for the
view) and TrimExtendCutOffTool (Tab switches mode) are plate tools; the
Select tool now drags a pressed cut-off.

AutomaticCutOffSession holds the Automatic Scrap Cutoffs dialog's
logic: unit-aware defaults, parsing, planning and applying through
AutomaticCutOffBatch as one step, and the status, details and per-plate
table texts.

The WinForms actions, cut-off handler, junction menu and dialog now
delegate to it with unchanged behavior, except that releasing a
cut-off without moving it no longer rebuilds the cut-offs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:44:49 -05:00

153 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using OpenNest.CutOffs;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// Choices the cut-off tools keep for a view while its nest stays open, as the WinForms plate
/// view's cut-off handler does.
/// </summary>
public sealed class CutOffToolOptions
{
/// <summary>Whether Sheet Cut-Off draws full-length lines instead of stopping at the first crossing.</summary>
public bool FullLengthNewLines { get; set; }
}
/// <summary>
/// The WinForms Sheet Cut-Off action (Plate &gt; Sheet Cut-Off), toolkit-neutral: as the pointer
/// moves, <see cref="Preview"/> is the line a click adds there; a left click adds it as one
/// "Add Cut-Off" step (<see cref="CutOffEdits.Add"/>). The line is vertical at first; Space
/// (<see cref="ToggleAxis"/>) turns it. Unless the view's <see cref="CutOffToolOptions"/> ask for
/// full-length lines, a new line stops at the first crossing cut-off from its start edge; Tab
/// (<see cref="ToggleFullLength"/>) switches, and the choice outlasts the tool. The tool stays
/// active until Escape; nothing happens while the host's edits are locked.
/// </summary>
public sealed class SheetCutOffTool : PlateTool
{
private readonly CutOffToolOptions options;
private Dictionary<Part, Entity> perimeterCache;
private Vector? lastPoint;
private string notice;
public SheetCutOffTool(IPlateToolHost host, CutOffToolOptions options)
: base(host)
{
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
public override string Name => "Sheet Cut-Off";
public override bool IsBusy => false;
/// <summary>The axis the next line runs along.</summary>
public CutOffAxis Axis { get; private set; } = CutOffAxis.Vertical;
/// <summary>Whether new lines run full length rather than stopping at the first crossing.</summary>
public bool FullLength => options.FullLengthNewLines;
/// <summary>The line a click would add at the pointer, its segments regenerated; null before the pointer moves.</summary>
public CutOff Preview { get; private set; }
/// <summary>
/// The tag shown beside the cursor, since the status bar is easy to miss: how to turn the
/// line and switch its length.
/// </summary>
public string CursorTag =>
$"Space: turn {AxisName(OtherAxis(Axis))}\nTab: {(FullLength ? "stop at first crossing" : "full length")}";
public override string Hint
{
get
{
var hint = FullLength
? $"click to add a full-length {AxisName(Axis)} line, Space turns it {AxisName(OtherAxis(Axis))}, Tab stops it at the first crossing, Esc finishes"
: $"click to add a {AxisName(Axis)} line stopped at the first crossing, Space turns it {AxisName(OtherAxis(Axis))}, Tab makes it full length, Esc finishes";
return notice == null ? hint : $"{notice} {hint}";
}
}
public override void Activate()
{
if (Host.Plate != null)
perimeterCache = Plate.BuildPerimeterCache(Host.Plate);
}
public override void Deactivate()
{
Preview = null;
perimeterCache = null;
notice = null;
lastPoint = null;
Host.Invalidate();
}
public override void Cancel() { }
public override bool PointerMoved(ToolPointer pointer)
{
lastPoint = pointer.World;
UpdatePreview();
return true;
}
public override bool PointerPressed(ToolPointer pointer)
{
var plate = Host.Plate;
if (pointer.Button != ToolButton.Left || plate == null || Host.EditsLocked)
return false;
var cutOff = NewCutOff(pointer.World);
CutOffEdits.Add(plate, cutOff, Host.CutOffSettings, label => Host.BeginChange(label));
Host.Invalidate();
return true;
}
/// <summary>Space: turns the next line to the other axis.</summary>
public void ToggleAxis()
{
Axis = OtherAxis(Axis);
RaiseHintChanged();
if (Preview != null)
UpdatePreview();
}
/// <summary>Tab: switches between full-length lines and lines stopped at the first crossing.</summary>
public void ToggleFullLength()
{
options.FullLengthNewLines = !options.FullLengthNewLines;
RaiseHintChanged();
if (Preview != null)
UpdatePreview();
}
private CutOff NewCutOff(Vector point)
{
var (cutOff, newNotice) = CutOffEdits.NewCutOff(Host.Plate, point, Axis, FullLength, Host.CutOffSettings);
if (notice != newNotice)
{
notice = newNotice;
RaiseHintChanged();
}
return cutOff;
}
private void UpdatePreview()
{
var plate = Host.Plate;
if (plate == null || lastPoint is not { } point)
return;
perimeterCache ??= Plate.BuildPerimeterCache(plate);
var preview = NewCutOff(point);
preview.Regenerate(plate, Host.CutOffSettings, perimeterCache);
Preview = preview;
Host.Invalidate();
}
private static CutOffAxis OtherAxis(CutOffAxis axis) =>
axis == CutOffAxis.Vertical ? CutOffAxis.Horizontal : CutOffAxis.Vertical;
private static string AxisName(CutOffAxis axis) =>
axis == CutOffAxis.Vertical ? "vertical" : "horizontal";
}