mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
Nest > Edit opens Edit Nest as the WinForms dialog has it: the Info tab (name, dates, thickness with the standard gauges, material and grade from the posts and machines, customer, units, status and author), the Defaults tab (plate size, part and edge spacing, quadrant, and "Use these defaults for new nests") and the Notes tab. Done is enabled while the name, thickness and size read and the edge spacing leaves room; it is one "Nest Info" undo step through the shared NestCommands, a typed author is remembered for new nests, and the name stays the file's. A click on the status bar's material label opens it too (a right-click offers it in a menu), with the WinForms tooltip. Nest > Clear Nest... asks before leaving one empty plate, as one "Clear Nest" step; it is refused while Auto Nest or a save runs or the plate tool is busy. Tools > Nest Defaults... edits the machine, units, plate size, quadrant, spacing and common-line usage new nests start from, saved where they are read (the nest server once it holds them, read-only while it cannot be reached), and Save Current Plate as Defaults saves the shown plate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
7.8 KiB
C#
173 lines
7.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Input;
|
|
using Avalonia.Interactivity;
|
|
using OpenNest.Data;
|
|
using OpenNest.Desktop;
|
|
using OpenNest.Forms;
|
|
|
|
namespace OpenNest.Avalonia
|
|
{
|
|
/// <summary>
|
|
/// Nest > Edit (also a click on the status bar's material label) and Clear Nest, and
|
|
/// Tools > Nest Defaults and Save Current Plate as Defaults, as the WinForms main window
|
|
/// has them. Each nest edit is one undo step through the shared <see cref="NestCommands"/>;
|
|
/// none runs while Auto Nest works on the nest. The nest defaults are saved where they are
|
|
/// read: the nest server once it holds them (Database mode), otherwise this PC's file.
|
|
/// </summary>
|
|
public partial class MainWindow
|
|
{
|
|
private void InitializeNestCommands()
|
|
{
|
|
ToolTip.SetTip(MaterialStatus, MainStatusText.MaterialToolTip);
|
|
// Clear Nest is enabled while there is something to clear, checked as the menu opens
|
|
// since parts can be added without a plate change; Save Current Plate as Defaults is
|
|
// shown while a nest is open.
|
|
if (ClearNestMenuItem.Parent is MenuItem nestMenu)
|
|
nestMenu.SubmenuOpened += (_, e) =>
|
|
{
|
|
if (ReferenceEquals(e.Source, nestMenu))
|
|
ClearNestMenuItem.IsEnabled = ActiveView?.Plates.CanClearAll ?? false;
|
|
};
|
|
if (SaveCurrentPlateAsDefaultsMenuItem.Parent is MenuItem toolsMenu)
|
|
toolsMenu.SubmenuOpened += (_, e) =>
|
|
{
|
|
if (ReferenceEquals(e.Source, toolsMenu))
|
|
SaveCurrentPlateAsDefaultsMenuItem.IsVisible = ActiveView != null;
|
|
};
|
|
}
|
|
|
|
private async void OnEditNest(object sender, RoutedEventArgs e) => await EditNestAsync();
|
|
|
|
/// <summary>
|
|
/// Shows Edit Nest on the active nest and writes Done's values as one "Nest Info" step:
|
|
/// a changed material or thickness takes the machine's spacing, an untouched trailing
|
|
/// sheet follows the new plate defaults and the view's cut-off settings follow the
|
|
/// machine. The name is the file's, so it cannot be changed here.
|
|
/// </summary>
|
|
private async Task EditNestAsync()
|
|
{
|
|
var view = ActiveView;
|
|
if (view == null || IsNesting(view.Tab, "editing its info"))
|
|
return;
|
|
var tab = view.Tab;
|
|
var nest = tab.Document.Nest;
|
|
var machines = DesktopSettings.Machines;
|
|
var shownMadeBy = nest.MadeBy ?? "";
|
|
var result = await EditNestDialog.ShowAsync(this, nest, machines.LoadMaterials());
|
|
if (result == null || !tabs.Tabs.Contains(tab) || IsNesting(tab, "editing its info"))
|
|
return;
|
|
|
|
try
|
|
{
|
|
NestCommands.EditNestInfo(nest, view.Plates, result.Values, machines.FindMachine(nest.Machine),
|
|
label => tab.Undo.BeginChange(label),
|
|
() => machines.ApplyCutOffDefaults(view.PlateView.CutOffSettings, nest));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// The step rolled the nest back to its last recorded state.
|
|
ShowNotice($"Could not change the nest info: {ex.Message}");
|
|
view.RefreshInPlace();
|
|
return;
|
|
}
|
|
// A Made By the user typed is what later new nests start from, as WinForms keeps it.
|
|
if (NestInfoValues.MadeByToRemember(shownMadeBy, result.Values.MadeBy) is { } madeBy)
|
|
UserSettingsFile.Update(UserSettingsFile.DefaultPath,
|
|
settings => settings[NewNestUserSettingsFile.MadeByName] = madeBy);
|
|
if (result.UseForNewNests)
|
|
await SaveNestDefaultsAsync(() =>
|
|
NestCommands.NewNestDefaults(nest, machines.LoadSavedNestDefaults(out _)));
|
|
// The plate defaults, spacing and units may have changed.
|
|
view.RefreshInPlace();
|
|
UpdateStatus();
|
|
}
|
|
|
|
private async void OnClearNest(object sender, RoutedEventArgs e)
|
|
{
|
|
var view = ActiveView;
|
|
if (view == null || !CanWrite(view.Tab, "clearing it"))
|
|
return;
|
|
if (PlateWorkGuard.IsBusy(false, new[] { view.PlateView.Tools.Current }))
|
|
{
|
|
await ConfirmDialog.ShowAsync(this, "Clear Nest", NestCommands.ClearNestBusyMessage);
|
|
return;
|
|
}
|
|
view.PlateView.Tools.Select();
|
|
if (!await ConfirmDialog.AskAsync(this, "Clear Nest", NestCommands.ClearNestQuestion, "Yes"))
|
|
return;
|
|
var tab = view.Tab;
|
|
if (!tabs.Tabs.Contains(tab) || !CanWrite(tab, "clearing it"))
|
|
return;
|
|
NestCommands.ClearNest(view.Plates, label => tab.Undo.BeginChange(label));
|
|
view.RefreshInPlace();
|
|
UpdateStatus();
|
|
}
|
|
|
|
// Tools > Nest Defaults: read where they are saved; the server's copy when it holds one.
|
|
private async void OnNestDefaults(object sender, RoutedEventArgs e)
|
|
{
|
|
var machines = DesktopSettings.Machines;
|
|
var defaults = machines.LoadSavedNestDefaults(out _);
|
|
if (DesktopSettings.Shared.TakeOfflineWarning() is { } offline)
|
|
await ConfirmDialog.ShowAsync(this, "Shared Settings", offline);
|
|
var configs = await DesktopSettings.RefreshMachinesAsync();
|
|
var chosen = await NestDefaultsDialog.ShowAsync(this, defaults, configs, DesktopSettings.Shared.NestDefaultsSource());
|
|
if (chosen != null)
|
|
await SaveNestDefaultsAsync(() => chosen);
|
|
}
|
|
|
|
// As WinForms: the shown plate's size, quadrant and spacing in the nest's units, its
|
|
// machine, and the saved common-line usage (a plate holds none).
|
|
private async void OnSaveCurrentPlateAsDefaults(object sender, RoutedEventArgs e)
|
|
{
|
|
var view = ActiveView;
|
|
if (view?.PlateView.Plate is not { } plate)
|
|
return;
|
|
var nest = view.Tab.Document.Nest;
|
|
var machines = DesktopSettings.Machines;
|
|
await SaveNestDefaultsAsync(() =>
|
|
PlateCommands.NewNestDefaults(nest, plate, machines.LoadSavedNestDefaults(out _)));
|
|
}
|
|
|
|
/// <summary>Saves the nest defaults, telling the user when they could not be saved.</summary>
|
|
private async Task SaveNestDefaultsAsync(Func<NestDefaults> defaults)
|
|
{
|
|
try
|
|
{
|
|
DesktopSettings.Machines.SaveNestDefaults(defaults());
|
|
}
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
await ConfirmDialog.ShowAsync(this, "Nest Defaults",
|
|
"The nest defaults could not be saved.\n\n" + ex.Message);
|
|
}
|
|
}
|
|
|
|
// A click edits the nest info; a right-click offers it in a menu, as WinForms does
|
|
// (its Show in Machine Configuration is not in this app yet).
|
|
private async void OnMaterialStatusReleased(object sender, PointerReleasedEventArgs e)
|
|
{
|
|
if (ActiveView == null)
|
|
return;
|
|
if (e.InitialPressMouseButton == MouseButton.Left)
|
|
{
|
|
e.Handled = true;
|
|
await EditNestAsync();
|
|
return;
|
|
}
|
|
if (e.InitialPressMouseButton != MouseButton.Right)
|
|
return;
|
|
e.Handled = true;
|
|
var flyout = new MenuFlyout { Placement = PlacementMode.TopEdgeAlignedLeft };
|
|
var edit = new MenuItem { Header = "Edit Nest..." };
|
|
edit.Click += OnEditNest;
|
|
flyout.Items.Add(edit);
|
|
flyout.ShowAt(MaterialStatus);
|
|
}
|
|
}
|
|
}
|