mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
The Plates tab lists each plate as the WinForms list does: its number
("New plate" for the trailing empty one), size, part count, utilization
and, beside it, how many sheets are cut from it. A click shows the
plate, a double-click edits it, and its menu duplicates or deletes it.
Dragging a row moves the plate, Ctrl+drag drops a copy, a line showing
where; Alt+Up and Alt+Down move the current plate. The empty plate at
the end stays last.
The Plate menu gains Edit, Set as Default (the nest's default plate and
the defaults new nests start from), Rotate 90 degrees either way or 180
(fitting the turned plate), and Resize to Fit (Ctrl+P, by the auto-size
factor in Options); the Nest menu gains Remove Empty Plates. Each is
one undo step under its WinForms label, through the shared
PlateCommands, and none runs while Auto Nest works on the nest.
Edit Plate edits the quantity, size, part and edge spacing, the
quadrant with a picker (click or arrow keys) and the sheet: rectangular,
or an outline imported from a DXF, DWG or SVG file (an SVG that does not
state its size asks what its units are first), with Clear to make it a
rectangle again. Done is enabled only when the size parses and the edge
spacing leaves room on the sheet; an empty plate's new settings become
the nest's defaults.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
11 KiB
C#
250 lines
11 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Platform.Storage;
|
|
using OpenNest.Avalonia.Controls;
|
|
using OpenNest.Desktop;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// Plate > Edit, as the WinForms Edit Plate dialog: the plate's size, quantity, part and
|
|
/// edge spacing, quadrant, and a rectangular sheet or an outline imported from a DXF, DWG or
|
|
/// SVG file. While there is an outline the size is its bounds and cannot be typed. Done is
|
|
/// enabled when the size parses and the edge spacing leaves room on the sheet; it returns
|
|
/// the values (the caller writes the plate as one undo step), Cancel or Esc returns null.
|
|
/// </summary>
|
|
internal sealed class EditPlateDialog : Window
|
|
{
|
|
private static readonly FilePickerFileType CadFiles =
|
|
new("CAD Files") { Patterns = new[] { "*.dxf", "*.dwg", "*.svg" } };
|
|
|
|
private readonly Units units;
|
|
private readonly TextBox size = new() { Name = "Size", MinWidth = 160 };
|
|
private readonly NumericUpDown quantity;
|
|
private readonly NumericUpDown partSpacing;
|
|
private readonly NumericUpDown left;
|
|
private readonly NumericUpDown top;
|
|
private readonly NumericUpDown right;
|
|
private readonly NumericUpDown bottom;
|
|
private readonly QuadrantPicker quadrant = new() { Name = "Quadrant", Width = 240, Height = 180 };
|
|
private readonly TextBlock outlineText = new() { Name = "OutlineText", VerticalAlignment = VerticalAlignment.Center };
|
|
private readonly Button clearOutline = new() { Name = "ClearOutline", Content = "Clear" };
|
|
private readonly Button done = new() { Name = "Done", Content = "Done", IsDefault = true, MinWidth = 90 };
|
|
private SheetOutline outline;
|
|
|
|
private EditPlateDialog(Plate plate, Units units)
|
|
{
|
|
this.units = units;
|
|
Title = "Edit Plate";
|
|
SizeToContent = SizeToContent.WidthAndHeight;
|
|
CanResize = false;
|
|
ShowInTaskbar = false;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
|
var values = PlateEditValues.FromPlate(plate);
|
|
quantity = Box("Quantity", values.Quantity, decimals: 0, increment: 1);
|
|
var increment = PlateEditValues.SpacingIncrement(units);
|
|
partSpacing = Box("PartSpacing", values.PartSpacing, PlateEditValues.SpacingDecimals, increment);
|
|
left = Box("Left", values.LeftSpacing, PlateEditValues.SpacingDecimals, increment);
|
|
top = Box("Top", values.TopSpacing, PlateEditValues.SpacingDecimals, increment);
|
|
right = Box("Right", values.RightSpacing, PlateEditValues.SpacingDecimals, increment);
|
|
bottom = Box("Bottom", values.BottomSpacing, PlateEditValues.SpacingDecimals, increment);
|
|
size.Text = values.Size.ToString();
|
|
quadrant.Quadrant = values.Quadrant;
|
|
SetOutline(values.Outline);
|
|
|
|
size.TextChanged += (_, _) => EnableCheck();
|
|
foreach (var box in new[] { quantity, partSpacing, left, top, right, bottom })
|
|
box.ValueChanged += (_, _) => EnableCheck();
|
|
|
|
var importOutline = new Button { Name = "ImportOutline", Content = "Outline..." };
|
|
importOutline.Click += async (_, _) => await ImportOutlineAsync();
|
|
clearOutline.Click += (_, _) => SetOutline(null);
|
|
done.Click += (_, _) => Close(Values());
|
|
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
|
cancel.Click += (_, _) => Close(null);
|
|
|
|
var unit = UnitsHelper.GetShortString(units);
|
|
var sheet = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 8,
|
|
};
|
|
AddRow(sheet, 0, "Size :", size, null);
|
|
AddRow(sheet, 1, "Quantity :", quantity, null);
|
|
var outlineRow = new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
Spacing = 6,
|
|
Children = { outlineText, importOutline, clearOutline },
|
|
};
|
|
Grid.SetRow(outlineRow, 2);
|
|
Grid.SetColumnSpan(outlineRow, 3);
|
|
sheet.Children.Add(outlineRow);
|
|
AddRow(sheet, 3, "Part Spacing :", partSpacing, unit);
|
|
|
|
var edges = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 8,
|
|
};
|
|
AddRow(edges, 0, "Left :", left, unit);
|
|
AddRow(edges, 1, "Top :", top, unit);
|
|
AddRow(edges, 2, "Right :", right, unit);
|
|
AddRow(edges, 3, "Bottom :", bottom, unit);
|
|
|
|
var leftColumn = new StackPanel
|
|
{
|
|
Spacing = 14,
|
|
Width = 300,
|
|
Children = { sheet, Group("Edge Spacing", edges) },
|
|
};
|
|
var columns = new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
Spacing = 18,
|
|
Children = { leftColumn, Group("Quadrant", quadrant) },
|
|
};
|
|
Content = new StackPanel
|
|
{
|
|
Margin = new Thickness(18),
|
|
Spacing = 18,
|
|
Children =
|
|
{
|
|
columns,
|
|
new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
HorizontalAlignment = HorizontalAlignment.Right,
|
|
Spacing = 8,
|
|
Children = { done, cancel },
|
|
},
|
|
},
|
|
};
|
|
|
|
AddHandler(KeyDownEvent, (_, e) =>
|
|
{
|
|
if (e.Key != Key.Escape)
|
|
return;
|
|
e.Handled = true;
|
|
Close(null);
|
|
}, RoutingStrategies.Tunnel);
|
|
Opened += (_, _) => size.Focus();
|
|
EnableCheck();
|
|
}
|
|
|
|
/// <summary>The values to write to the plate, or null when the user cancelled.</summary>
|
|
public static Task<PlateEditValues> ShowAsync(Window owner, Plate plate, Units units) =>
|
|
new EditPlateDialog(plate, units).ShowDialog<PlateEditValues>(owner);
|
|
|
|
// As the WinForms boxes: 0 to 100, widened to hold a plate's larger value.
|
|
private static NumericUpDown Box(string name, double value, int decimals, double increment) => new()
|
|
{
|
|
Name = name,
|
|
Minimum = (decimal)PlateEditValues.BoxMinimum,
|
|
Maximum = (decimal)System.Math.Max(PlateEditValues.BoxMaximum, value),
|
|
Increment = (decimal)increment,
|
|
ClipValueToMinMax = true,
|
|
FormatString = decimals == 0 ? "0" : "0." + new string('0', decimals),
|
|
Value = (decimal)value,
|
|
MinWidth = 160,
|
|
};
|
|
|
|
private static void AddRow(Grid grid, int row, string label, Control field, string suffix)
|
|
{
|
|
var text = new TextBlock { Text = label, VerticalAlignment = VerticalAlignment.Center };
|
|
Grid.SetRow(text, row);
|
|
grid.Children.Add(text);
|
|
Grid.SetRow(field, row);
|
|
Grid.SetColumn(field, 1);
|
|
grid.Children.Add(field);
|
|
if (suffix == null)
|
|
return;
|
|
var unit = new TextBlock { Text = suffix, VerticalAlignment = VerticalAlignment.Center };
|
|
Grid.SetRow(unit, row);
|
|
Grid.SetColumn(unit, 2);
|
|
grid.Children.Add(unit);
|
|
}
|
|
|
|
// A titled box, as the WinForms group boxes.
|
|
private static Control Group(string header, Control content) => new StackPanel
|
|
{
|
|
Spacing = 4,
|
|
Children =
|
|
{
|
|
new TextBlock { Text = header, FontWeight = global::Avalonia.Media.FontWeight.SemiBold },
|
|
new Border
|
|
{
|
|
BorderBrush = global::Avalonia.Media.Brush.Parse("#C8CDD2"),
|
|
BorderThickness = new Thickness(1),
|
|
Padding = new Thickness(10),
|
|
Child = content,
|
|
},
|
|
},
|
|
};
|
|
|
|
private static double Number(NumericUpDown box) => (double)(box.Value ?? 0);
|
|
|
|
// Every value but the size, which is the size box's text or the outline's bounds.
|
|
private PlateEditValues Values() => new()
|
|
{
|
|
Size = outline?.Size ?? (Geometry.Size.TryParse(size.Text, out var typed) ? typed : default),
|
|
Quantity = (int)Number(quantity),
|
|
PartSpacing = Number(partSpacing),
|
|
LeftSpacing = Number(left),
|
|
TopSpacing = Number(top),
|
|
RightSpacing = Number(right),
|
|
BottomSpacing = Number(bottom),
|
|
Quadrant = quadrant.Quadrant,
|
|
Outline = outline,
|
|
};
|
|
|
|
private void EnableCheck() => done.IsEnabled = Values().CanApplyWithSize(size.Text, out _);
|
|
|
|
// An outlined sheet's size is its outline's bounds, so the size box is read-only.
|
|
private void SetOutline(SheetOutline value)
|
|
{
|
|
outline = value;
|
|
size.IsReadOnly = value != null;
|
|
clearOutline.IsEnabled = value != null;
|
|
if (value != null)
|
|
size.Text = value.Size.ToString(4);
|
|
outlineText.Text = PlateEditValues.OutlineText(value);
|
|
EnableCheck();
|
|
}
|
|
|
|
private async Task ImportOutlineAsync()
|
|
{
|
|
var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
|
{
|
|
Title = "Import Sheet Outline",
|
|
AllowMultiple = false,
|
|
FileTypeFilter = new[] { CadFiles },
|
|
});
|
|
var path = files.Count > 0 ? files[0].TryGetLocalPath() : null;
|
|
if (path == null)
|
|
return;
|
|
try
|
|
{
|
|
var (import, perInch) = await SvgScaleDialog.ResolveAsync(this, path, units);
|
|
if (!import)
|
|
return;
|
|
SetOutline(SheetOutlineImport.Read(path, units, perInch));
|
|
}
|
|
catch (Exception exception) when (SheetOutlineImport.IsReadFailure(exception))
|
|
{
|
|
await ConfirmDialog.ShowAsync(this, "Import Sheet Outline", SheetOutlineImport.FailureText(exception));
|
|
}
|
|
}
|
|
}
|
|
}
|