Files
OpenNest/OpenNest.Desktop/Interaction/TrimExtendCutOffTool.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

119 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using OpenNest.CutOffs;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// The WinForms Trim/Extend Cut-Off action, toolkit-neutral: trims or extends a cut-off against
/// the cut-offs that cross it (see <see cref="CutOffTrim"/>). Trim removes the clicked piece,
/// leaving a gap when it lies between two crossing lines; Extend closes the gap or grows the end
/// nearer the click. Tab (<see cref="ToggleMode"/>) switches mode. Hovering a cut-off previews
/// the edit (<see cref="PreviewPieces"/>), and the status hint says why one is refused; a left
/// click applies it as one "Trim Cut-Off" or "Extend Cut-Off" step. The tool stays active until
/// Escape; nothing is applied while the host's edits are locked.
/// </summary>
public sealed class TrimExtendCutOffTool : PlateTool
{
private CutOff hovered;
private CutOffEdit preview;
private string message;
private Vector? lastPoint;
public TrimExtendCutOffTool(IPlateToolHost host)
: base(host)
{
}
public override string Name => "Trim/Extend Cut-Off";
public override bool IsBusy => false;
/// <summary>Whether clicks extend rather than trim.</summary>
public bool Extending { get; private set; }
/// <summary>The edit a click at the pointer would make, or null off any cut-off.</summary>
public CutOffEdit Preview => preview;
public override string Hint => message ?? (Extending
? "click near the end of a cut-off to extend it or close a gap, Tab switches to Trim, Esc finishes"
: "click the piece of a cut-off to remove, Tab switches to Extend, Esc finishes");
/// <summary>
/// The stretches the hovered edit removes (dashed red) and adds (dashed green); empty when
/// there is none or it is refused.
/// </summary>
public IReadOnlyList<(Line Line, bool Added)> PreviewPieces => CutOffEdits.PreviewPieces(Host.Plate, preview);
public override void Cancel() { }
public override void Deactivate()
{
hovered = null;
preview = null;
lastPoint = null;
Host.Invalidate();
}
private double Tolerance => Host.View.LengthToWorld(SelectTool.CutOffPickPixels);
private CutOffEdit EditAt(CutOff cutOff, Vector point) => Extending
? CutOffTrim.Extend(Host.Plate, cutOff, point)
: CutOffTrim.Trim(Host.Plate, cutOff, point, Host.CutOffSettings);
public override bool PointerMoved(ToolPointer pointer)
{
var plate = Host.Plate;
lastPoint = pointer.World;
hovered = plate == null ? null : Host.HitTester.CutOffAt(plate, pointer.World, Tolerance);
preview = hovered == null ? null : EditAt(hovered, pointer.World);
SetMessage(preview?.Refusal);
Host.Invalidate();
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 = Host.HitTester.CutOffAt(plate, pointer.World, Tolerance);
if (cutOff == null)
return false;
var edit = EditAt(cutOff, pointer.World);
if (CutOffEdits.Apply(plate, edit, Extending ? CutOffEdits.ExtendLabel : CutOffEdits.TrimLabel,
Host.CutOffSettings, label => Host.BeginChange(label)))
{
hovered = null;
preview = null;
SetMessage(edit.Notice);
}
else
{
SetMessage(edit.Refusal);
}
Host.Invalidate();
return true;
}
/// <summary>Tab: switches between Trim and Extend, re-previewing the hovered cut-off.</summary>
public void ToggleMode()
{
Extending = !Extending;
preview = hovered == null || lastPoint is not { } at ? null : EditAt(hovered, at);
message = preview?.Refusal;
RaiseHintChanged();
Host.Invalidate();
}
private void SetMessage(string value)
{
if (message == value)
return;
message = value;
RaiseHintChanged();
}
}