mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
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>
391 lines
16 KiB
C#
391 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Threading.Tasks;
|
|
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Media;
|
|
using OpenNest.Avalonia.Controls;
|
|
using OpenNest.Data;
|
|
using OpenNest.Forms;
|
|
using OpenNest.Undo;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// Plate > Automatic Scrap Cutoffs and Nest > Automatic Scrap Cutoffs (All Plates), as the
|
|
/// WinForms dialog, over the shared <see cref="AutomaticCutOffSession"/>: spacing, minimum
|
|
/// strip (half the spacing until edited) and tail, the head-height lock and its overcut; the
|
|
/// active plate's detached preview on the sheet, its measurements, a per-plate table for All
|
|
/// Plates, the issues and a colored status line. Apply replans every plate and adds the
|
|
/// definitions as one "Automatic Scrap Cutoffs" step, then keeps the head-lock choice for new
|
|
/// cut-offs; a blocked or empty plan adds nothing and keeps the dialog open.
|
|
/// </summary>
|
|
internal sealed class AutomaticCutOffDialog : Window
|
|
{
|
|
private static readonly IBrush ReadyBrush = new SolidColorBrush(Color.FromRgb(0, 102, 0));
|
|
private static readonly IBrush BlockedBrush = new SolidColorBrush(Color.FromRgb(192, 0, 0));
|
|
private static readonly IBrush UnchangedBrush = new SolidColorBrush(Color.FromRgb(109, 109, 109));
|
|
private static readonly IBrush WarningBrush = new SolidColorBrush(Color.FromRgb(120, 70, 0));
|
|
|
|
private readonly AutomaticCutOffSession session;
|
|
private readonly PlateCanvas canvas;
|
|
private readonly Func<string, ChangeScope> beginChange;
|
|
private readonly TextBox spacing = new() { Name = "Spacing", Width = 110 };
|
|
private readonly TextBox minimumStrip = new() { Name = "MinimumStrip", Width = 110 };
|
|
private readonly TextBox minimumTail = new() { Name = "MinimumTail", Width = 110 };
|
|
private readonly CheckBox headLock = new() { Name = "HeadLock" };
|
|
private readonly TextBox headLockOvercut = new() { Name = "HeadLockOvercut", Width = 110 };
|
|
private readonly TextBlock sheet = new() { TextWrapping = TextWrapping.Wrap };
|
|
private readonly TextBlock status = new() { FontWeight = FontWeight.Bold, FontSize = 15, TextWrapping = TextWrapping.Wrap };
|
|
private readonly TextBlock occupied = Value();
|
|
private readonly TextBlock used = Value();
|
|
private readonly TextBlock separator = Value();
|
|
private readonly TextBlock tail = Value();
|
|
private readonly TextBlock stripCuts = new() { Text = AutomaticCutOffSession.Unmeasured };
|
|
private readonly DataGrid plates = new()
|
|
{
|
|
Name = "Plates",
|
|
IsReadOnly = true,
|
|
AutoGenerateColumns = false,
|
|
CanUserSortColumns = false,
|
|
Height = 150,
|
|
HeadersVisibility = DataGridHeadersVisibility.Column,
|
|
};
|
|
private readonly TextBox details = new()
|
|
{
|
|
Name = "Details",
|
|
IsReadOnly = true,
|
|
AcceptsReturn = true,
|
|
TextWrapping = TextWrapping.Wrap,
|
|
Height = 110,
|
|
};
|
|
private readonly Button apply = new() { Name = "Apply", MinWidth = 90 };
|
|
private bool minimumStripEdited;
|
|
private bool settingMinimumStrip;
|
|
// The settings last previewed or reset: a box that raises a change without one (as a
|
|
// text box can when it is first laid out) keeps the preview.
|
|
private string shownSettings;
|
|
|
|
/// <summary>Whether Apply added cut-offs.</summary>
|
|
public bool Applied { get; private set; }
|
|
|
|
private AutomaticCutOffDialog(AutomaticCutOffSession session, PlateCanvas canvas,
|
|
Func<string, ChangeScope> beginChange)
|
|
{
|
|
this.session = session;
|
|
this.canvas = canvas;
|
|
this.beginChange = beginChange;
|
|
Title = session.DialogTitle;
|
|
Width = 680;
|
|
SizeToContent = SizeToContent.Height;
|
|
CanResize = false;
|
|
ShowInTaskbar = false;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
|
spacing.Text = session.DefaultSpacingText;
|
|
minimumStrip.Text = AutomaticCutOffSession.MinimumStripFor(spacing.Text);
|
|
minimumTail.Text = session.DefaultMinimumTailText;
|
|
headLock.Content = session.HeadLockLabel;
|
|
headLock.IsChecked = canvas.CutOffSettings.LockHeadHeight;
|
|
ToolTip.SetTip(headLock, "Stock can arrive wider than nominal. New cut-offs cut past the far sheet edge by "
|
|
+ "this much with the head height locked, and Plan Cutting may start them there. Posting needs the "
|
|
+ "post's head-height lock codes.");
|
|
headLockOvercut.Text = canvas.CutOffSettings.HeadLockOvercut.ToString(CultureInfo.CurrentCulture);
|
|
headLockOvercut.IsEnabled = headLock.IsChecked == true;
|
|
sheet.Text = session.InitialSheetText;
|
|
|
|
spacing.TextChanged += (_, _) => OnSpacingChanged();
|
|
minimumStrip.TextChanged += (_, _) =>
|
|
{
|
|
if (!settingMinimumStrip)
|
|
minimumStripEdited = true;
|
|
OnSettingsChanged();
|
|
};
|
|
minimumTail.TextChanged += (_, _) => OnSettingsChanged();
|
|
headLockOvercut.TextChanged += (_, _) => OnSettingsChanged();
|
|
headLock.IsCheckedChanged += (_, _) =>
|
|
{
|
|
headLockOvercut.IsEnabled = headLock.IsChecked == true;
|
|
OnSettingsChanged();
|
|
};
|
|
|
|
var settings = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("*,Auto"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
RowSpacing = 6,
|
|
};
|
|
AddSetting(settings, 0, new TextBlock { Text = session.SpacingLabel }, spacing);
|
|
AddSetting(settings, 1, new TextBlock { Text = session.MinimumStripLabel }, minimumStrip);
|
|
AddSetting(settings, 2, new TextBlock { Text = session.MinimumTailLabel }, minimumTail);
|
|
AddSetting(settings, 3, headLock, headLockOvercut);
|
|
|
|
var metrics = new Grid
|
|
{
|
|
ColumnDefinitions = new ColumnDefinitions("*,*,*,*"),
|
|
RowDefinitions = new RowDefinitions("Auto,Auto"),
|
|
ColumnSpacing = 8,
|
|
};
|
|
AddMetric(metrics, 0, "Occupied span", occupied,
|
|
"Distance from the sheet origin to the furthest real material.");
|
|
AddMetric(metrics, 1, "Used span", used, "Span through the proposed used area, from the sheet origin.");
|
|
AddMetric(metrics, 2, "Separator X", separator,
|
|
"Signed X of the verified separator cut. A negative value means the separator is on the origin side.");
|
|
AddMetric(metrics, 3, "Retained tail", tail,
|
|
"Length along X of the reusable tail beyond the verified separator, at full sheet width.");
|
|
|
|
var warning = new TextBlock
|
|
{
|
|
Text = "⚠ Nominal spacing is not the cut-line length — verify scrap separation and hopper fit.",
|
|
Foreground = WarningBrush,
|
|
TextWrapping = TextWrapping.Wrap,
|
|
};
|
|
ToolTip.SetTip(warning, "Nominal spacing differs from cut-line length. Clearances and omitted segments can "
|
|
+ "leave bridges. Confirm disconnected scrap and hopper fit before relying on it. Tail dimensions "
|
|
+ "describe a proposed program; verify the physical separation.");
|
|
|
|
foreach (var (header, path) in new[]
|
|
{
|
|
("Plate", nameof(AutomaticCutOffPlateRow.Plate)),
|
|
("Cut-offs", nameof(AutomaticCutOffPlateRow.CutOffs)),
|
|
("Occupied", nameof(AutomaticCutOffPlateRow.Occupied)),
|
|
("Used", nameof(AutomaticCutOffPlateRow.Used)),
|
|
("Tail", nameof(AutomaticCutOffPlateRow.Tail)),
|
|
("Status", nameof(AutomaticCutOffPlateRow.Status)),
|
|
})
|
|
{
|
|
plates.Columns.Add(new DataGridTextColumn
|
|
{
|
|
Header = header,
|
|
Binding = new global::Avalonia.Data.Binding(path),
|
|
Width = new DataGridLength(1, DataGridLengthUnitType.Star),
|
|
});
|
|
}
|
|
plates.LoadingRow += (_, e) =>
|
|
{
|
|
if (e.Row.DataContext is AutomaticCutOffPlateRow row)
|
|
e.Row.Foreground = BrushOf(row.Tone);
|
|
};
|
|
plates.IsVisible = session.AllPlates;
|
|
|
|
var preview = new Button { Name = "Preview", Content = "_Preview", MinWidth = 90, IsDefault = true };
|
|
preview.Click += (_, _) => ShowPreview();
|
|
apply.Content = session.ApplyText.Replace('&', '_');
|
|
apply.Click += (_, _) => OnApply();
|
|
var cancel = new Button { Content = "Cancel", IsCancel = true, MinWidth = 90 };
|
|
cancel.Click += (_, _) => Close();
|
|
|
|
Content = new StackPanel
|
|
{
|
|
Margin = new Thickness(12),
|
|
Spacing = 8,
|
|
Children =
|
|
{
|
|
sheet,
|
|
settings,
|
|
status,
|
|
metrics,
|
|
stripCuts,
|
|
warning,
|
|
plates,
|
|
new TextBlock { Text = session.DetailsLabel },
|
|
details,
|
|
new StackPanel
|
|
{
|
|
Orientation = Orientation.Horizontal,
|
|
HorizontalAlignment = HorizontalAlignment.Right,
|
|
Spacing = 8,
|
|
Children = { preview, apply, cancel },
|
|
},
|
|
},
|
|
};
|
|
SetStatus("Preparing preview…", null);
|
|
Opened += (_, _) => ShowPreview();
|
|
Closed += (_, _) => ClearPreview();
|
|
}
|
|
|
|
/// <summary>Shows the dialog over <paramref name="owner"/>; true when Apply added cut-offs.</summary>
|
|
public static async Task<bool> ShowAsync(Window owner, AutomaticCutOffSession session, PlateCanvas canvas,
|
|
Func<string, ChangeScope> beginChange)
|
|
{
|
|
var dialog = new AutomaticCutOffDialog(session, canvas, beginChange);
|
|
await dialog.ShowDialog(owner);
|
|
return dialog.Applied;
|
|
}
|
|
|
|
private static TextBlock Value() => new()
|
|
{
|
|
Text = AutomaticCutOffSession.Unmeasured,
|
|
FontWeight = FontWeight.Bold,
|
|
};
|
|
|
|
private static void AddSetting(Grid grid, int row, Control label, Control box)
|
|
{
|
|
label.VerticalAlignment = VerticalAlignment.Center;
|
|
Grid.SetRow(label, row);
|
|
grid.Children.Add(label);
|
|
Grid.SetRow(box, row);
|
|
Grid.SetColumn(box, 1);
|
|
grid.Children.Add(box);
|
|
}
|
|
|
|
private static void AddMetric(Grid grid, int column, string caption, TextBlock value, string tip)
|
|
{
|
|
var label = new TextBlock { Text = caption };
|
|
ToolTip.SetTip(label, tip);
|
|
Grid.SetColumn(label, column);
|
|
grid.Children.Add(label);
|
|
Grid.SetRow(value, 1);
|
|
Grid.SetColumn(value, column);
|
|
grid.Children.Add(value);
|
|
}
|
|
|
|
private static IBrush BrushOf(AutomaticCutOffTone tone) => tone switch
|
|
{
|
|
AutomaticCutOffTone.Ready => ReadyBrush,
|
|
AutomaticCutOffTone.Blocked => BlockedBrush,
|
|
AutomaticCutOffTone.Unchanged => UnchangedBrush,
|
|
_ => null,
|
|
};
|
|
|
|
private void SetStatus(string text, IBrush brush)
|
|
{
|
|
status.Text = text;
|
|
if (brush == null)
|
|
status.ClearValue(TextBlock.ForegroundProperty);
|
|
else
|
|
status.Foreground = brush;
|
|
}
|
|
|
|
private void OnSpacingChanged()
|
|
{
|
|
if (!minimumStripEdited && AutomaticCutOffSession.MinimumStripFor(spacing.Text) is { } half)
|
|
{
|
|
settingMinimumStrip = true;
|
|
try
|
|
{
|
|
minimumStrip.Text = half;
|
|
}
|
|
finally
|
|
{
|
|
settingMinimumStrip = false;
|
|
}
|
|
}
|
|
OnSettingsChanged();
|
|
}
|
|
|
|
private string Settings() =>
|
|
string.Join("\n", spacing.Text, minimumStrip.Text, minimumTail.Text, headLock.IsChecked, headLockOvercut.Text);
|
|
|
|
private void OnSettingsChanged()
|
|
{
|
|
var settings = Settings();
|
|
if (settings == shownSettings)
|
|
return;
|
|
shownSettings = settings;
|
|
ClearPreview();
|
|
ResetSummary();
|
|
SetStatus(AutomaticCutOffSession.SettingsChangedStatus, null);
|
|
details.Text = string.Empty;
|
|
apply.IsEnabled = true;
|
|
}
|
|
|
|
private void ClearPreview() => canvas.CutOffPreviewParts = null;
|
|
|
|
private void ResetSummary()
|
|
{
|
|
foreach (var value in new[] { occupied, used, separator, tail, stripCuts })
|
|
value.Text = AutomaticCutOffSession.Unmeasured;
|
|
plates.ItemsSource = null;
|
|
}
|
|
|
|
private void ShowError(Exception error)
|
|
{
|
|
ClearPreview();
|
|
ResetSummary();
|
|
apply.IsEnabled = false;
|
|
SetStatus($"Error — {error.Message}", BlockedBrush);
|
|
details.Text = error.Message;
|
|
}
|
|
|
|
// Busy and plate checks come before the settings are read, so their message wins.
|
|
private (Plate[] Plates, IReadOnlyList<CutOffs.AutomaticCutOffPlan> Plans) Calculate()
|
|
{
|
|
session.EnsureReady();
|
|
return session.Calculate(ParseOptions(), WithHeadLock(), apply: false);
|
|
}
|
|
|
|
private CutOffs.AutomaticCutOffOptions ParseOptions() =>
|
|
session.ParseOptions(spacing.Text, minimumStrip.Text, minimumTail.Text);
|
|
|
|
private CutOffs.CutOffSettings WithHeadLock() =>
|
|
session.WithHeadLock(canvas.CutOffSettings, headLock.IsChecked == true, headLockOvercut.Text);
|
|
|
|
private void Display(Plate[] planned, IReadOnlyList<CutOffs.AutomaticCutOffPlan> plans)
|
|
{
|
|
var summary = session.Summarize(planned, plans);
|
|
occupied.Text = summary.Occupied;
|
|
used.Text = summary.Used;
|
|
separator.Text = summary.Separator;
|
|
tail.Text = summary.Tail;
|
|
stripCuts.Text = summary.StripCuts;
|
|
apply.IsEnabled = summary.CanApply;
|
|
details.Text = summary.Details;
|
|
SetStatus(summary.Status, BrushOf(summary.StatusTone));
|
|
if (!session.AllPlates)
|
|
return;
|
|
sheet.Text = summary.SheetText;
|
|
plates.ItemsSource = summary.Rows;
|
|
}
|
|
|
|
private void ShowPreview()
|
|
{
|
|
shownSettings = Settings();
|
|
ClearPreview();
|
|
try
|
|
{
|
|
var (planned, plans) = Calculate();
|
|
Display(planned, plans);
|
|
canvas.CutOffPreviewParts = session.PreviewParts(planned, plans);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ShowError(ex);
|
|
}
|
|
}
|
|
|
|
private void OnApply()
|
|
{
|
|
ClearPreview();
|
|
try
|
|
{
|
|
// The batch service replans every plate before mutating any; never accept previews.
|
|
session.EnsureReady();
|
|
var chosen = WithHeadLock();
|
|
var (planned, plans) = session.Apply(ParseOptions(), chosen, beginChange);
|
|
Display(planned, plans);
|
|
if (!AutomaticCutOffSession.Applied(plans))
|
|
return;
|
|
|
|
// New cut-offs from here on, the manual tool's included, use the choice.
|
|
var saved = session.KeepHeadLock(canvas.CutOffSettings, chosen);
|
|
UserSettingsFile.Update(UserSettingsFile.DefaultPath, saved.WriteTo);
|
|
Applied = true;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ShowError(ex);
|
|
}
|
|
finally
|
|
{
|
|
ClearPreview();
|
|
canvas.InvalidateVisual();
|
|
}
|
|
}
|
|
}
|
|
}
|