From 8fe38398e9275c0eb1134b83ba5099eeed7249f9 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 11 Nov 2026 21:57:39 -0500 Subject: [PATCH] feat(avalonia): load posts and machines at startup The main window loads the post processors in Posts/ beside the app (their materials registered) as it loads engine plug-ins, and reports any that fail in the status line. It then reads the machines off the UI thread through the shared MachineSettingsSource, following storage.json's mode (the nest server's copy in Database mode), copies legacy clamps onto the defaults' or only machine as WinForms does, and shows the offline-copy or clamp warning in the status line. The machines and posts are kept in DesktopSettings for later screens; nothing uses them yet. The two posts the WinForms app ships now build with the Avalonia app and copy to its Posts/ directory, with their settings JSON unless an edited copy is already deployed. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- OpenNest.Avalonia/DesktopSettings.cs | 66 ++++++++++++++++++++++ OpenNest.Avalonia/MainWindow.axaml.cs | 23 ++++++++ OpenNest.Avalonia/OpenNest.Avalonia.csproj | 19 +++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 OpenNest.Avalonia/DesktopSettings.cs diff --git a/AGENTS.md b/AGENTS.md index 9cb3f09b..3982975e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,7 +42,7 @@ Releases: follow [the release procedure](docs/releasing.md) and `scripts/Publish - `OpenNest.Engine`: whole-job API in `Jobs/`, interactive proposals via `PlateFillService`, fill strategies, best-fit pairs, packing, sequencing and rapid planning. `INestingEngine.Solve(NestJob)` returns stock IDs/poses; boundary adapters map drawings and materialize results. `NestJobRunner` validates its candidates before committing demand/stock accounting. Do not assume arbitrary plug-in output or interactive paths received that validation. Job identity is reference-based, not drawing-name-based. - Built-in whole-job engines live in `OpenNest.Engine/NestingEngines//`, named for the jobs they suit; see [nesting engines](docs/nesting-engines.md). A change must beat that engine's current benchmark result with every layout valid. External plug-ins implement `INestingEngine` with a public parameterless constructor and load from `Engines/` beside the host; keep their projects out of this solution. - `OpenNest.IO`: ACadSharp and SVG import/export and ZIP-based `.nest` persistence; depends on Core only (an IO.Tests guard enforces it). Best fits persist only when callers pass `BestFitCacheStore.Shared` as `NestReader`/`NestWriter.BestFitStore`. All DXF-to-Drawing conversion goes through `CadImporter`: `Import` + `BuildDrawing` for editable/reporting flows, `ImportDrawing` for headless callers. Preserve source offsets, entity IDs, suppressed entities and bends. Bend repair is opt-in, requires explicit source units and may not alter cut geometry or unrelated marks. -- `OpenNest`: WinForms UI (`Forms/`, `Controls/PlateView`, `Actions/`). `OpenNest.Data` holds cross-platform persistence; new-nest defaults live in `%APPDATA%\OpenNest\defaults.json` (in Database mode, the server's shared copy once it holds one). WinForms `Properties.Settings` persist to `%APPDATA%\OpenNest\user-settings.json` through `AppDataSettingsProvider`, never inside the Velopack install folder; `OpenNest.WinForms.Tests` redirects them to a temp file, so a test run never writes the user's settings. Posts live in `Posts/OpenNest.Posts./` and deploy to the desktop output's `Posts/` directory. Desktop nest edits go through `BeginChange`/`RecordChange`; see [undo and redo](docs/undo-redo.md). +- `OpenNest`: WinForms UI (`Forms/`, `Controls/PlateView`, `Actions/`). `OpenNest.Data` holds cross-platform persistence; new-nest defaults live in `%APPDATA%\OpenNest\defaults.json` (in Database mode, the server's shared copy once it holds one). WinForms `Properties.Settings` persist to `%APPDATA%\OpenNest\user-settings.json` through `AppDataSettingsProvider`, never inside the Velopack install folder; `OpenNest.WinForms.Tests` redirects them to a temp file, so a test run never writes the user's settings. Posts live in `Posts/OpenNest.Posts./` and deploy to each desktop app's output `Posts/` directory (WinForms and Avalonia). Desktop nest edits go through `BeginChange`/`RecordChange`; see [undo and redo](docs/undo-redo.md). - `OpenNest.Desktop` (net8.0): toolkit-neutral desktop logic shared by the WinForms app and `OpenNest.Avalonia`, the cross-platform desktop in progress. It must not reference WinForms/WindowsDesktop, `System.Drawing.Common` or Avalonia (`OpenNest.Desktop.Tests` guards this, including transitive packages); put toolkit-specific rendering, dialogs and settings behind adapters in the front ends. - `OpenNest.Server`: nest storage HTTP service, kept off Engine and IO. `OpenNest.Server.Worker`: the server nesting worker, shipped in the same image. - `OpenNest.Console`, `OpenNest.Mcp`, `OpenNest.Api`: front ends; `OpenNest.Benchmark`: whole-job engine comparisons; `OpenNest.Gpu`: GPU evaluators; `OpenNest.Training`: ML data collection and the Python sketch-search prototype (`sketch-search/README.md`). Benchmark timing comparisons require `--parallel 1`; validate layouts and fulfillment, not just elapsed time. diff --git a/OpenNest.Avalonia/DesktopSettings.cs b/OpenNest.Avalonia/DesktopSettings.cs new file mode 100644 index 00000000..15ab5861 --- /dev/null +++ b/OpenNest.Avalonia/DesktopSettings.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using OpenNest.Data; +using OpenNest.Desktop; + +namespace OpenNest.Avalonia +{ + /// + /// The machines, nest defaults and post processors this app works with, from the shared + /// desktop library: in Database mode (storage.json, shared with the WinForms app) the nest + /// server's copy once it holds one, otherwise this PC's files, with the user settings both + /// apps keep in user-settings.json. + /// + internal static class DesktopSettings + { + public static SharedSettingsSource Shared { get; } = SharedSettingsSource.FromStorageFile(); + + public static MachineSettingsSource Machines { get; } = + new(Shared, new MachineUserSettingsFile(UserSettingsFile.DefaultPath)); + + /// The post processors loaded from Posts/ beside the app, by load order. + public static IReadOnlyList PostProcessors { get; private set; } = []; + + /// The readable machine configurations, once has run. + public static IReadOnlyList MachineConfigs { get; private set; } = []; + + /// + /// Loads the post processors in (each DLL on its own, so one + /// that fails does not stop the others) and registers their materials. Returns the + /// failures to show, or null when every post loaded. + /// + public static string LoadPosts(string directory) + { + var result = PostProcessorLoader.Load(directory); + PostProcessors = result.PostProcessors; + return result.Failures.Count == 0 + ? null + : "These post processors could not be loaded: " + string.Join("; ", result.Failures); + } + + /// + /// Reads the machines off the UI thread (the nest server can take seconds to answer), then + /// copies the legacy clamps onto the default or only machine, as the WinForms app does at + /// startup (with several machines and no default it waits for the WinForms app to ask). + /// Returns what to tell the user: the machines' source when the server could not be + /// reached, or why the clamps were not copied; null when there is nothing to say. + /// + public static Task LoadMachinesAsync() => Task.Run(() => + { + MachineConfigs = Machines.LoadMachines(); + var clamps = Machines.MigrateLegacyClamps(askMachine: null); + var offline = Shared.TakeOfflineWarning(); + var notices = new[] { offline, clamps } + .Where(text => !string.IsNullOrEmpty(text)) + .Select(OneLine) + .ToList(); + return notices.Count == 0 ? null : string.Join(" ", notices); + }); + + private static string OneLine(string text) => + string.Join(" ", text.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); + } +} diff --git a/OpenNest.Avalonia/MainWindow.axaml.cs b/OpenNest.Avalonia/MainWindow.axaml.cs index fe89c532..b2ff72a1 100644 --- a/OpenNest.Avalonia/MainWindow.axaml.cs +++ b/OpenNest.Avalonia/MainWindow.axaml.cs @@ -50,6 +50,11 @@ namespace OpenNest.Avalonia var engineWarning = EngineSelection.LoadSavedSelection(); if (!string.IsNullOrEmpty(engineWarning)) notice = engineWarning; + // Post processors from Posts/ (their materials registered), then the machines, + // reported in the status line as the WinForms app reports them in dialogs. + if (DesktopSettings.LoadPosts(Path.Combine(AppContext.BaseDirectory, "Posts")) is { } postWarning) + notice = notice == null ? postWarning : $"{notice} {postWarning}"; + MachinesLoaded = LoadMachinesAsync(); // The colour scheme chosen in the WinForms app's options, from the shared user settings. ColorSchemeRegistry.ApplySaved(Data.UserSettingsFile.Read(Data.UserSettingsFile.DefaultPath)); tabs.Changed += (_, _) => ShowActiveTab(); @@ -649,6 +654,24 @@ namespace OpenNest.Avalonia AutoNestMenuItem.Header = running ? "_Stop Auto Nest" : "_Auto Nest"; } + /// Completes once the machines are read (see ). + internal Task MachinesLoaded { get; } + + private async Task LoadMachinesAsync() + { + string warning; + try + { + warning = await DesktopSettings.LoadMachinesAsync(); + } + catch (Exception ex) + { + warning = $"The machines could not be read: {ex.Message}"; + } + if (warning != null) + ShowNotice(warning); + } + private void ShowNotice(string text) { notice = text; diff --git a/OpenNest.Avalonia/OpenNest.Avalonia.csproj b/OpenNest.Avalonia/OpenNest.Avalonia.csproj index 2a5a8f46..214f7160 100644 --- a/OpenNest.Avalonia/OpenNest.Avalonia.csproj +++ b/OpenNest.Avalonia/OpenNest.Avalonia.csproj @@ -18,4 +18,23 @@ + + + + + + + + + + + + + + + + + +