Files
OpenNest/OpenNest.Avalonia/Controls/PlateCanvas.CutOffs.cs
T
ajandClaude Opus 5.5 66ad3ef587 feat(avalonia): sheet cut-offs, trim/extend, swap and automatic scrap cutoffs
Plate > Sheet Cut-Off and Trim/Extend Cut-Off (also toolbar buttons)
run the shared tools: a dashed preview of the line a click adds and a
cursor tag, Space turns the line, Tab switches full length or Trim and
Extend without moving focus. Select drags a cut-off with the lines that
meet it. A right click on a cut-off junction offers Swap Through-Cut,
taking the part menu's place only there.

Plate > Automatic Scrap Cutoffs and Nest > Automatic Scrap Cutoffs (All
Plates) open the dialog over the shared session: settings, measurements,
per-plate table, issues and a colored status, the active plate's
detached preview drawn as dashed lines, and Apply as one undo step that
keeps the head-lock choice for new cut-offs.

Rebuilding the plate list no longer passes through an empty selection,
so a tool is not ended when an edit changes the plate's part count.

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

181 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Input;
using OpenNest.CutOffs;
using OpenNest.Desktop.Interaction;
using OpenNest.Desktop.Rendering;
using OpenNest.Geometry;
using SkiaSharp;
namespace OpenNest.Avalonia.Controls
{
/// <summary>
/// Plate &gt; Sheet Cut-Off and Trim/Extend Cut-Off, the Swap Through-Cut junction menu and the
/// Automatic Scrap Cutoffs preview, as the WinForms plate view: the tools are the shared
/// <see cref="SheetCutOffTool"/> and <see cref="TrimExtendCutOffTool"/> (Space turns a new line,
/// Tab switches its length or the trim mode); dragging a cut-off is the shared Select tool's.
/// </summary>
public sealed partial class PlateCanvas
{
// Kept for the view while its nest is open, as WinForms keeps it per plate view.
private readonly CutOffToolOptions cutOffToolOptions = new();
private IReadOnlyList<Part> cutOffPreviewParts = Array.Empty<Part>();
/// <summary>
/// A right click released on a cut-off junction in Select, while the plate takes edits:
/// the host shows Swap Through-Cut. It takes the place of the part menu there.
/// </summary>
public event EventHandler<CutOffJunction> CutOffMenuRequested;
/// <summary>Plate &gt; Sheet Cut-Off: starts the tool that adds cut-off lines.</summary>
public void StartSheetCutOff()
{
if (plate != null)
tools.SetTool(new SheetCutOffTool(this, cutOffToolOptions));
}
/// <summary>Plate &gt; Trim/Extend Cut-Off: starts the tool that trims and extends cut-offs.</summary>
public void StartTrimExtendCutOff()
{
if (plate != null)
tools.SetTool(new TrimExtendCutOffTool(this));
}
/// <summary>
/// The Automatic Scrap Cutoffs dialog's detached preview of the shown plate, drawn as
/// preview lines and never added to it; empty for none.
/// </summary>
public IReadOnlyList<Part> CutOffPreviewParts
{
get => cutOffPreviewParts;
set
{
cutOffPreviewParts = value ?? Array.Empty<Part>();
InvalidateVisual();
}
}
private CutOffJunction JunctionMenuAt(Point at)
{
if (plate == null || EditsLocked || tools.Current is not SelectTool)
return null;
var tolerance = view.LengthToWorld(SelectTool.CutOffPickPixels);
return CutOffEdits.JunctionAt(plate, ToWorld(at), tolerance) is var (through, stopper)
? new CutOffJunction(plate, through, stopper)
: null;
}
// Space turns a new line; Tab switches its length, or Trim and Extend. Tab is taken
// before focus navigation sees it, as the WinForms view keeps it while these tools run.
private bool HandleCutOffKey(KeyEventArgs e)
{
if (e.KeyModifiers != KeyModifiers.None)
return false;
switch (tools.Current, e.Key)
{
case (SheetCutOffTool sheet, Key.Space):
sheet.ToggleAxis();
return true;
case (SheetCutOffTool sheet, Key.Tab):
sheet.ToggleFullLength();
return true;
case (TrimExtendCutOffTool trim, Key.Tab):
trim.ToggleMode();
return true;
default:
return false;
}
}
private void BuildCutOffToolOverlays(Scene scene)
{
foreach (var part in cutOffPreviewParts)
scene.CutOffPreviewStrokes.AddRange(CutOff.CutStrokes(part.Program, part.Location));
switch (tools.Current)
{
case SheetCutOffTool { Preview: { } preview } sheet:
if (preview.Drawing?.Program is { Codes.Count: > 0 } program)
scene.CutOffPreviewStrokes.AddRange(CutOff.CutStrokes(program));
if (CursorWorld is { } cursor)
{
scene.CutOffTag = sheet.CursorTag;
scene.CutOffTagAnchor = view.WorldToScreen(cursor.X, cursor.Y);
}
break;
case TrimExtendCutOffTool trim:
scene.TrimPieces = trim.PreviewPieces;
break;
}
}
private sealed partial class Scene
{
public readonly List<Line> CutOffPreviewStrokes = new();
public string CutOffTag;
public ScreenPoint CutOffTagAnchor;
public IReadOnlyList<(Line Line, bool Added)> TrimPieces;
}
private sealed partial class PlatePainter
{
// The applied cut-off line's orange-red, translucent so it reads as a preview.
private static readonly SKColor CutOffPreviewColor = new(255, 69, 0, 128);
private static readonly SKColor TrimRemovedColor = new(220, 20, 60, 200);
private static readonly SKColor TrimAddedColor = new(34, 139, 34, 200);
private static readonly SKColor TagBack = new(255, 255, 255, 230);
private static readonly SKColor TagBorder = new(105, 105, 105); // DimGray
private void DrawCutOffToolOverlays()
{
if (scene.CutOffPreviewStrokes.Count > 0)
{
// GDI+'s dash at 1.5 px: three-pixel-and-a-half dashes, one-and-a-half gaps.
using var pen = Stroke(CutOffPreviewColor, 1.5f, new[] { 4.5f, 1.5f });
foreach (var stroke in scene.CutOffPreviewStrokes)
canvas.DrawLine(Screen(stroke.StartPoint), Screen(stroke.EndPoint), pen);
}
DrawTrimPieces();
DrawCutOffTag();
}
// Dashed red over the piece a trim removes, dashed green over the piece an extend adds.
private void DrawTrimPieces()
{
if (scene.TrimPieces is not { Count: > 0 } pieces)
return;
using var removed = Stroke(TrimRemovedColor, 2, new[] { 6f, 2f });
using var added = Stroke(TrimAddedColor, 2, new[] { 6f, 2f });
foreach (var (line, isAdded) in pieces)
canvas.DrawLine(Screen(line.StartPoint), Screen(line.EndPoint), isAdded ? added : removed);
}
// Beside the cursor, since the status bar is easy to miss: how to turn the line.
private void DrawCutOffTag()
{
if (string.IsNullOrEmpty(scene.CutOffTag))
return;
using var text = new SKPaint { Color = SKColors.Black, IsAntialias = true, TextSize = 12 };
var metrics = text.FontMetrics;
var lineHeight = metrics.Descent - metrics.Ascent;
var lines = scene.CutOffTag.Split('\n');
var width = lines.Max(l => text.MeasureText(l));
var anchor = scene.CutOffTagAnchor;
var rect = SKRect.Create((float)anchor.X + 16, (float)anchor.Y + 16, width + 6,
lineHeight * lines.Length + 4);
using var back = Fill(TagBack);
canvas.DrawRect(rect, back);
using var border = Stroke(TagBorder);
border.IsAntialias = false;
canvas.DrawRect(rect, border);
for (var i = 0; i < lines.Length; i++)
canvas.DrawText(lines[i], rect.Left + 3, rect.Top + 2 - metrics.Ascent + i * lineHeight, text);
}
}
}
/// <summary>Where two cut-offs meet on <paramref name="Plate"/>: one runs through, the other stops at it.</summary>
public sealed record CutOffJunction(Plate Plate, CutOff Through, CutOff Stopper);
}