mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-18 10:37:41 -05:00
Port the WinForms Clone Parts action as the toolkit-neutral PlaceTool: copies of the sources follow the pointer by their lower-left corner (a move to the same screen point is ignored, keeping arrow-key nudges), and each left click places another copy as one Add Part/Add Parts step. Shift settles toward the quadrant corner on geometry, Ctrl alone on bounding boxes, reading the placement order at each click. Copied common-line groups are joined again among the copies and cut-offs are rebuilt. Delete removes the sources plus selected plate parts in one Delete step, recorded (and cut-offs rebuilt) only when any were on the plate. Settling runs before the step opens, so a failure there leaves nothing to roll back; the copy does not follow the pointer while edits are locked, as WinForms raises no mouse move then. PlateClipboard.Copy/Cut start the tool with the selected parts; Cut then removes the sources as one Cut step without rebuilding cut-offs, as WinForms does. While placing, PlateEdits nudges, steps, pushes and rotates the previews instead of the selection. Unlike WinForms, which records an empty undo step there, these edits open no undo step, and they never rebuild cut-offs since the previews are not on the plate.
65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Engine.Fill;
|
|
|
|
namespace OpenNest.Desktop.Interaction;
|
|
|
|
/// <summary>
|
|
/// Edit > Cut and Copy, toolkit-neutral: both start a <see cref="PlaceTool"/> with the parts
|
|
/// selected on the plate, in selection order. Neither does anything without a plate or selected
|
|
/// parts, or while the host's edits are locked.
|
|
/// </summary>
|
|
public static class PlateClipboard
|
|
{
|
|
/// <summary>
|
|
/// Starts placing copies of the selected parts; null when nothing was started.
|
|
/// </summary>
|
|
public static PlaceTool Copy(
|
|
IPlateToolHost host, PlateToolManager tools, Func<SettleOrder> order)
|
|
{
|
|
var sources = Sources(host, tools);
|
|
if (sources == null)
|
|
return null;
|
|
var tool = new PlaceTool(host, sources, order);
|
|
tools.SetTool(tool);
|
|
return tool;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts placing copies of the selected parts, then removes them from the plate as one
|
|
/// "Cut" undo step. As in WinForms, the plate's cut-offs are not rebuilt here.
|
|
/// </summary>
|
|
public static PlaceTool Cut(
|
|
IPlateToolHost host, PlateToolManager tools, Func<SettleOrder> order)
|
|
{
|
|
var sources = Sources(host, tools);
|
|
if (sources == null)
|
|
return null;
|
|
// The tool reads the sources' common-line groups, which their removal ends.
|
|
var tool = new PlaceTool(host, sources, order);
|
|
tools.SetTool(tool);
|
|
|
|
var plate = host.Plate;
|
|
using var change = host.BeginChange("Cut");
|
|
change.MarkChanged();
|
|
foreach (var part in sources)
|
|
plate.Parts.Remove(part);
|
|
host.Selection.Clear();
|
|
host.Invalidate();
|
|
change.Complete();
|
|
return tool;
|
|
}
|
|
|
|
private static List<Part> Sources(IPlateToolHost host, PlateToolManager tools)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(host);
|
|
ArgumentNullException.ThrowIfNull(tools);
|
|
if (host.Plate == null || host.EditsLocked)
|
|
return null;
|
|
host.Selection.Prune();
|
|
var sources = host.Selection.Parts.ToList();
|
|
return sources.Count == 0 ? null : sources;
|
|
}
|
|
}
|