mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-26 17:17:40 -05:00
feat(avalonia): run Auto Nest from the Nest menu and toolbar
Auto Nest nests the remaining quantities on sheets like the shown plate with the saved engine selection (plug-ins loaded as in the desktop app), solving off the UI thread with shared progress text in the status bar. The button becomes Stop, which discards the whole proposal; Open is refused while it runs. A refused result changes nothing and says why. --auto-nest runs it on the opened nest before --snapshot renders, and the snapshot now writes the status line to <png>.txt for headless checks.
This commit is contained in:
1 parent
cce2585d79
commit
73a02cf309
4 files changed
+128
-5
No files matched your search
@@ -17,7 +17,10 @@ namespace OpenNest.Avalonia
|
||||
var args = desktop.Args ?? System.Array.Empty<string>();
|
||||
if (args.Length > 0)
|
||||
window.OpenNest(args[0]);
|
||||
// --snapshot <png> [plate]: render the window to an image and exit, for checking the drawing.
|
||||
// --auto-nest: run Auto Nest on the opened nest (with --snapshot, before rendering).
|
||||
if (System.Array.IndexOf(args, "--auto-nest") >= 0)
|
||||
window.AutoNestOnOpen();
|
||||
// --snapshot <png> [plate]: render the window to an image (status line in <png>.txt) and exit.
|
||||
var snapshot = System.Array.IndexOf(args, "--snapshot");
|
||||
if (snapshot >= 0 && snapshot + 1 < args.Length)
|
||||
window.SaveSnapshotAndClose(args[snapshot + 1],
|
||||
|
||||
@@ -8,10 +8,13 @@
|
||||
<DockPanel>
|
||||
<Menu DockPanel.Dock="Top">
|
||||
<MenuItem Header="_File">
|
||||
<MenuItem Header="_Open..." InputGesture="Ctrl+O" Click="OnOpen" />
|
||||
<MenuItem x:Name="OpenMenuItem" Header="_Open..." InputGesture="Ctrl+O" Click="OnOpen" />
|
||||
<Separator />
|
||||
<MenuItem Header="E_xit" Click="OnExit" />
|
||||
</MenuItem>
|
||||
<MenuItem Header="_Nest">
|
||||
<MenuItem x:Name="AutoNestMenuItem" Header="_Auto Nest" Click="OnAutoNest" />
|
||||
</MenuItem>
|
||||
<MenuItem Header="_View">
|
||||
<MenuItem Header="Zoom to _Fit" InputGesture="F" Click="OnFit" />
|
||||
<MenuItem Header="_Previous Plate" InputGesture="PageUp" Click="OnPreviousPlate" />
|
||||
@@ -21,10 +24,11 @@
|
||||
|
||||
<Border DockPanel.Dock="Top" Padding="4" BorderThickness="0,0,0,1" BorderBrush="#D0D0D0">
|
||||
<StackPanel Orientation="Horizontal" Spacing="4">
|
||||
<Button Content="Open" Click="OnOpen" />
|
||||
<Button x:Name="OpenButton" Content="Open" Click="OnOpen" />
|
||||
<Button Content="Fit" Click="OnFit" />
|
||||
<Button Content="Previous" Click="OnPreviousPlate" />
|
||||
<Button Content="Next" Click="OnNextPlate" />
|
||||
<Button x:Name="AutoNestButton" Content="Auto Nest" Click="OnAutoNest" />
|
||||
</StackPanel>
|
||||
</Border>
|
||||
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Platform.Storage;
|
||||
using OpenNest.Desktop;
|
||||
using OpenNest.Engine;
|
||||
using OpenNest.Engine.Jobs;
|
||||
using OpenNest.Forms;
|
||||
using OpenNest.IO;
|
||||
|
||||
namespace OpenNest.Avalonia
|
||||
@@ -15,12 +21,20 @@ namespace OpenNest.Avalonia
|
||||
{
|
||||
private Document document;
|
||||
private string fileName;
|
||||
private string notice;
|
||||
private CancellationTokenSource nesting;
|
||||
private Task autoNest = Task.CompletedTask;
|
||||
|
||||
private Nest nest => document?.Nest;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
// As the desktop app: plug-in engines first, then the saved engine selection.
|
||||
NestingEngineRegistry.LoadPlugins(Path.Combine(AppContext.BaseDirectory, "Engines"));
|
||||
var engineWarning = EngineSelection.LoadSavedSelection();
|
||||
if (!string.IsNullOrEmpty(engineWarning))
|
||||
notice = engineWarning;
|
||||
Canvas.ViewChanged += (_, _) => UpdateStatus();
|
||||
KeyBindings.Add(new KeyBinding
|
||||
{
|
||||
@@ -33,6 +47,8 @@ namespace OpenNest.Avalonia
|
||||
|
||||
public void OpenNest(string path)
|
||||
{
|
||||
if (nesting != null)
|
||||
return;
|
||||
try
|
||||
{
|
||||
document = new Document { Nest = new NestReader(path).Read() };
|
||||
@@ -45,30 +61,44 @@ namespace OpenNest.Avalonia
|
||||
|
||||
fileName = Path.GetFileName(path);
|
||||
Title = $"{fileName} - OpenNest (Avalonia prototype)";
|
||||
notice = null;
|
||||
Canvas.ClearCache();
|
||||
RefreshPlateList(0);
|
||||
}
|
||||
|
||||
private void RefreshPlateList(int selectIndex)
|
||||
{
|
||||
PlateList.ItemsSource = nest.Plates
|
||||
.Select((plate, i) => $"Plate {i + 1} {plate.Size.Length:0.###} x {plate.Size.Width:0.###} ({plate.Parts.Count} parts)")
|
||||
.ToList();
|
||||
PlateList.SelectedIndex = nest.Plates.Count > 0 ? 0 : -1;
|
||||
PlateList.SelectedIndex = nest.Plates.Count > 0 ? System.Math.Clamp(selectIndex, 0, nest.Plates.Count - 1) : -1;
|
||||
}
|
||||
|
||||
/// <summary>Runs Auto Nest once the window opens, for --auto-nest.</summary>
|
||||
public void AutoNestOnOpen() => Opened += (_, _) => autoNest = RunAutoNestAsync();
|
||||
|
||||
public void SaveSnapshotAndClose(string path, int plateNumber)
|
||||
{
|
||||
Opened += async (_, _) =>
|
||||
{
|
||||
if (nest?.Plates.Count > 0)
|
||||
PlateList.SelectedIndex = System.Math.Clamp(plateNumber - 1, 0, nest.Plates.Count - 1);
|
||||
await autoNest;
|
||||
await System.Threading.Tasks.Task.Delay(500);
|
||||
var size = new global::Avalonia.PixelSize((int)Bounds.Width, (int)Bounds.Height);
|
||||
using var bitmap = new global::Avalonia.Media.Imaging.RenderTargetBitmap(size);
|
||||
bitmap.Render(this);
|
||||
bitmap.Save(path);
|
||||
// The status line beside the image, so headless checks can read it.
|
||||
File.WriteAllText(path + ".txt", Status.Text);
|
||||
Close();
|
||||
};
|
||||
}
|
||||
|
||||
private async void OnOpen(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (nesting != null)
|
||||
return;
|
||||
var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
||||
{
|
||||
Title = "Open Nest",
|
||||
@@ -82,6 +112,88 @@ namespace OpenNest.Avalonia
|
||||
|
||||
private void OnExit(object sender, RoutedEventArgs e) => Close();
|
||||
|
||||
private void OnAutoNest(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Stop always cancels and discards the whole proposal.
|
||||
if (nesting != null)
|
||||
{
|
||||
nesting.Cancel();
|
||||
return;
|
||||
}
|
||||
autoNest = RunAutoNestAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Nests every drawing's remaining quantity on sheets like the shown plate through the
|
||||
/// shared pipeline, off the UI thread. Open is refused until it finishes, so the nest
|
||||
/// and its drawings stay stable from the request to the commit.
|
||||
/// </summary>
|
||||
private async Task RunAutoNestAsync()
|
||||
{
|
||||
if (nest == null || nesting != null)
|
||||
return;
|
||||
var engineName = EngineSelection.EngineName;
|
||||
var template = Canvas.Plate ?? nest.Plates.FirstOrDefault() ?? nest.PlateDefaults.CreateNew();
|
||||
var request = AutoNestJob.ForRemaining(nest, template, engineName);
|
||||
if (request == null)
|
||||
{
|
||||
ShowNotice("Auto Nest: nothing has a remaining quantity to nest.");
|
||||
return;
|
||||
}
|
||||
|
||||
nesting = new CancellationTokenSource();
|
||||
var token = nesting.Token;
|
||||
SetNesting(true);
|
||||
ShowNotice($"{engineName}: starting...");
|
||||
var progress = new Progress<NestProgress>(p =>
|
||||
{
|
||||
if (!token.IsCancellationRequested)
|
||||
ShowNotice(p.Description ?? $"{engineName}: plate {p.PlateNumber}");
|
||||
});
|
||||
try
|
||||
{
|
||||
var jobProgress = JobEngineNest.CreateProgress(engineName, progress);
|
||||
var result = await Task.Run(() => NestPipeline.Run(request, jobProgress, token));
|
||||
// Engines that ignore cancellation still have their proposal discarded.
|
||||
token.ThrowIfCancellationRequested();
|
||||
var firstNew = nest.Plates.Count(p => p.Parts.Count > 0);
|
||||
var kept = AutoNestJob.Keep(nest, result);
|
||||
if (kept.Nested)
|
||||
RefreshPlateList(firstNew);
|
||||
ShowNotice(kept.Nested ? $"{engineName}: {kept.Note}" : AutoNestJob.RefusalReason(result));
|
||||
}
|
||||
catch (OperationCanceledException) when (token.IsCancellationRequested)
|
||||
{
|
||||
ShowNotice("Auto Nest stopped; nothing was changed.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowNotice($"Auto Nest failed: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
nesting.Dispose();
|
||||
nesting = null;
|
||||
SetNesting(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetNesting(bool running)
|
||||
{
|
||||
AutoNestButton.Content = running ? "Stop" : "Auto Nest";
|
||||
AutoNestMenuItem.Header = running ? "_Stop Auto Nest" : "_Auto Nest";
|
||||
OpenButton.IsEnabled = !running;
|
||||
OpenMenuItem.IsEnabled = !running;
|
||||
}
|
||||
|
||||
private void ShowNotice(string text)
|
||||
{
|
||||
notice = text;
|
||||
UpdateStatus();
|
||||
if (Canvas.Plate == null)
|
||||
Status.Text = text;
|
||||
}
|
||||
|
||||
private void OnFit(object sender, RoutedEventArgs e) => Canvas.ZoomToFit();
|
||||
|
||||
private void OnPreviousPlate(object sender, RoutedEventArgs e) => StepPlate(-1);
|
||||
@@ -122,6 +234,8 @@ namespace OpenNest.Avalonia
|
||||
var part = Canvas.HoveredPart ?? Canvas.SelectedPart;
|
||||
if (part != null)
|
||||
text += $" {(ReferenceEquals(part, Canvas.SelectedPart) ? "Selected" : "Over")}: {part.BaseDrawing.Name}";
|
||||
if (notice != null)
|
||||
text += $" | {notice}";
|
||||
Status.Text = text;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// The WinForms app drives undo recording through ChangeScope's internal root/nested factories
|
||||
// and NestFingerprint, and reports Auto Nest progress through the internal JobEngineNest.
|
||||
// and NestFingerprint; both desktop front ends report Auto Nest progress through the internal
|
||||
// JobEngineNest.
|
||||
[assembly: InternalsVisibleTo("OpenNest")]
|
||||
[assembly: InternalsVisibleTo("OpenNest.Avalonia")]
|
||||
[assembly: InternalsVisibleTo("OpenNest.Desktop.Tests")]
|
||||
Reference in new issue
Block a user