Files
OpenNest/OpenNest/Forms/EditPlateForm.cs
T
ajandClaude Opus 5.5 4e831ab6f8 feat(desktop): share the plate list and plate commands
PlateCommands ports the WinForms nest window's plate commands, each one
undo step under its WinForms label: Remove Plate, Remove Empty Plates,
Duplicate Plate (the copy current, with its own cutting parameters),
Move Plate and its Alt+Up/Alt+Down target, Rotate Plate (90 degrees
either way or 180), Resize Plate to its parts by the auto-size factor,
Set Nest Default Plate, and Edit Plate's commit, where an empty plate's
new settings become the nest's defaults and the nested quantities are
counted again. NewNestDefaults is what Set as Default saves for new
nests: the plate, the saved common-line usage and the nest's machine.

PlateEditValues is what Edit Plate edits (quantity, size, part and edge
spacing, quadrant, outline), when Done applies and how it writes the
plate; SheetOutlineImport reads an outline from a CAD file, and SvgScale
holds the scale prompt for an SVG that does not state its size.
PlateListRow is the Plates list's row text and its drag drop slots;
QuadrantPick the quadrant picker's hit test and cells.

The WinForms nest window, plate list, Edit Plate dialog, quadrant
picker, SVG scale prompt and Save Current as Defaults delegate to them
with unchanged behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:11:38 -05:00

230 lines
7.3 KiB
C#

using System;
using System.IO;
using System.Windows.Forms;
using OpenNest.Desktop;
using OpenNest.IO;
using Timer = System.Windows.Forms.Timer;
namespace OpenNest.Forms
{
public partial class EditPlateForm : Form
{
private readonly Plate plate;
private readonly Timer timer;
private Units units;
private SheetOutline outline;
public EditPlateForm(Plate plate)
{
InitializeComponent();
this.plate = plate;
// A WinForms timer ticks on the UI thread. System.Timers.Timer's SynchronizingObject
// cannot marshal before the dialog has a handle, so a busy UI thread (e.g. an open
// cut simulator) let it run EnableCheck on a pool thread while the handle was created.
timer = new Timer { Interval = SystemInformation.KeyboardDelay + 1 };
timer.Tick += (sender, e) =>
{
timer.Stop();
EnableCheck();
};
Disposed += (sender, e) => timer.Dispose();
Load(plate);
EnableCheck();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SetUnits(units);
}
public Units Units
{
get { return units; }
set
{
units = value;
SetUnits(value);
}
}
private void SetUnits(Units units)
{
string suffix = " " + UnitsHelper.GetShortString(units);
var increment = (decimal)PlateEditValues.SpacingIncrement(units);
var controls = new[]
{
numericUpDownPartSpacing,
numericUpDownEdgeSpacingBottom,
numericUpDownEdgeSpacingLeft,
numericUpDownEdgeSpacingRight,
numericUpDownEdgeSpacingTop,
};
foreach (var control in controls)
{
control.Suffix = suffix;
control.Increment = increment;
control.Invalidate(true);
}
}
public string SizeString
{
get { return textBoxSize.Text; }
set { textBoxSize.Text = value; }
}
public double LeftSpacing
{
get { return (double)numericUpDownEdgeSpacingLeft.Value; }
set { numericUpDownEdgeSpacingLeft.Value = (decimal)value; }
}
public double TopSpacing
{
get { return (double)numericUpDownEdgeSpacingTop.Value; }
set { numericUpDownEdgeSpacingTop.Value = (decimal)value; }
}
public double RightSpacing
{
get { return (double)numericUpDownEdgeSpacingRight.Value; }
set { numericUpDownEdgeSpacingRight.Value = (decimal)value; }
}
public double BottomSpacing
{
get { return (double)numericUpDownEdgeSpacingBottom.Value; }
set { numericUpDownEdgeSpacingBottom.Value = (decimal)value; }
}
public double PartSpacing
{
get { return (double)numericUpDownPartSpacing.Value; }
set { numericUpDownPartSpacing.Value = (decimal)value; }
}
public int Quantity
{
get { return (int)numericUpDownQty.Value; }
set { numericUpDownQty.Value = value; }
}
public int Quadrant
{
get { return quadrantSelect1.Quadrant; }
set { quadrantSelect1.Quadrant = value; }
}
/// <summary>
/// The sheet outline the plate gets when the dialog is accepted: the plate's own until
/// one is imported or cleared. An outlined sheet's size is its outline's bounds, so the
/// size box is read-only while there is one.
/// </summary>
public SheetOutline Outline
{
get { return outline; }
set
{
outline = value;
textBoxSize.ReadOnly = value != null;
buttonClearOutline.Enabled = value != null;
if (value != null)
textBoxSize.Text = value.Size.ToString(4);
labelOutline.Text = PlateEditValues.OutlineText(value);
EnableCheck();
}
}
public void EnableCheck() => applyButton.Enabled = Values.CanApplyWithSize(SizeString, out _);
// The values but the size, which the size box holds as typed.
private PlateEditValues Values => new()
{
LeftSpacing = LeftSpacing,
TopSpacing = TopSpacing,
RightSpacing = RightSpacing,
BottomSpacing = BottomSpacing,
PartSpacing = PartSpacing,
Quantity = Quantity,
Quadrant = Quadrant,
Outline = Outline,
};
private new void Load(Plate plate)
{
var values = PlateEditValues.FromPlate(plate);
textBoxSize.Text = values.Size.ToString();
LeftSpacing = values.LeftSpacing;
TopSpacing = values.TopSpacing;
RightSpacing = values.RightSpacing;
BottomSpacing = values.BottomSpacing;
PartSpacing = values.PartSpacing;
Quantity = values.Quantity;
Quadrant = values.Quadrant;
Outline = values.Outline;
}
/// <summary>Writes the dialog's values to the plate, as Done does.</summary>
internal void Apply() => Save();
// The outline carries its own size; a parsed size would drop it.
private void Save() =>
(Values with { Size = Outline?.Size ?? OpenNest.Geometry.Size.Parse(SizeString) }).ApplyTo(plate);
private void textBox1_TextChanged(object sender, EventArgs e)
{
timer.Stop();
timer.Start();
}
private void button1_Click(object sender, EventArgs e)
{
Save();
}
private void spacing_ValueChanged(object sender, EventArgs e)
{
EnableCheck();
}
/// <summary>
/// Reads a sheet outline from a CAD file: the largest closed contour is the sheet and
/// every other closed contour an obstacle, in the file's units (as drawings import).
/// </summary>
private void buttonImportOutline_Click(object sender, EventArgs e)
{
using var dialog = new OpenFileDialog
{
Title = "Import Sheet Outline",
Filter = CadFormats.OpenFileFilter,
};
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
try
{
var options = new CadImportOptions { DrawingUnits = Units };
if (!SvgScaleForm.TryResolve(this, dialog.FileName, options))
return;
Outline = SheetOutlineImport.Read(dialog.FileName, Units, options.SvgUserUnitsPerInch);
}
catch (Exception exception) when (SheetOutlineImport.IsReadFailure(exception))
{
MessageBox.Show(this, SheetOutlineImport.FailureText(exception),
"Import Sheet Outline", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void buttonClearOutline_Click(object sender, EventArgs e)
{
Outline = null;
}
}
}