diff --git a/AGENTS.md b/AGENTS.md index 9842191c..a6ebef1c 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 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. 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 the desktop output's `Posts/` directory. 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.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.WinForms.Tests/Forms/CuttingInspectionPanelTests.cs b/OpenNest.WinForms.Tests/Forms/CuttingInspectionPanelTests.cs index e8a57986..a4538f5d 100644 --- a/OpenNest.WinForms.Tests/Forms/CuttingInspectionPanelTests.cs +++ b/OpenNest.WinForms.Tests/Forms/CuttingInspectionPanelTests.cs @@ -567,6 +567,37 @@ public class CuttingInspectionPanelTests Assert.Equal(0.3, ((LineLeadIn)captured.ExternalLeadIn).Length); }); + [Fact] + public void UserSettingsWinOverThePlatesOwn() => RunSta(() => + { + using var editor = Editor(); + var user = new CuttingParameters { ExternalLeadIn = new LineLeadIn { Length = 0.7 } }; + WithUserSettings(CuttingParametersSerializer.Serialize(user), () => + { + editor.Show(); + editor.PlateManager.LoadAt(0); + editor.OpenCuttingInspection(); + var panel = Assert.IsType(editor.CuttingInspection); + var opened = Field(panel, "parameters"); + Assert.Equal(0.7, ((LineLeadIn)opened.ExternalLeadIn).Length); + panel.RequestClose(); + PumpUntil(() => panel.IsDisposed); + }); + }); + + [Theory] + [InlineData("")] + [InlineData("{not json")] + public void ThePlatesOwnSettingsAreTheFallback(string json) => RunSta(() => + { + using var editor = Editor(); + WithUserSettings(json, () => + { + var plate = editor.Nest.Plates[0]; + Assert.Same(plate.CuttingParameters, MachineSettings.CuttingParametersFor(plate, editor.Nest)); + }); + }); + [Fact] public void EmptySentinelIsNotNumberedAsAnOccupiedPlate() => RunSta(() => { @@ -1021,6 +1052,8 @@ public class CuttingInspectionPanelTests private static EditNestForm Editor() { + // The plate's own settings below apply only while the user has none. + OpenNest.Properties.Settings.Default.CuttingParametersJson = ""; var nest = new Nest(); var plate = nest.CreatePlate(); plate.Size = new OpenNest.Geometry.Size(100, 100); @@ -1072,6 +1105,21 @@ public class CuttingInspectionPanelTests Assert.True(condition(), "Timed out waiting for cutting inspection."); } + private static void WithUserSettings(string json, System.Action action) + { + var settings = OpenNest.Properties.Settings.Default; + var saved = settings.CuttingParametersJson; + settings.CuttingParametersJson = json; + try + { + action(); + } + finally + { + settings.CuttingParametersJson = saved; + } + } + private static void RunSta(System.Action action) => StaTestThread.Run(action, TimeSpan.FromSeconds(45), "STA cutting inspection test timed out."); } diff --git a/OpenNest.WinForms.Tests/IsolatedUserSettings.cs b/OpenNest.WinForms.Tests/IsolatedUserSettings.cs new file mode 100644 index 00000000..8c421137 --- /dev/null +++ b/OpenNest.WinForms.Tests/IsolatedUserSettings.cs @@ -0,0 +1,14 @@ +using System.Runtime.CompilerServices; +using OpenNest.Properties; + +namespace OpenNest.WinForms.Tests; + +internal static class IsolatedUserSettings +{ +#pragma warning disable CA2255 // Must run before anything reads Settings.Default. + [ModuleInitializer] + internal static void Redirect() => + AppDataSettingsProvider.PathOverride = Path.Combine( + Path.GetTempPath(), "OpenNest.WinForms.Tests", Guid.NewGuid().ToString("N"), "user-settings.json"); +#pragma warning restore CA2255 +} diff --git a/OpenNest/Actions/ActionLeadIn.cs b/OpenNest/Actions/ActionLeadIn.cs index 5d0dd549..3a4a792d 100644 --- a/OpenNest/Actions/ActionLeadIn.cs +++ b/OpenNest/Actions/ActionLeadIn.cs @@ -104,9 +104,7 @@ namespace OpenNest.Actions cuttingPanel = new CuttingPanel(); cuttingPanel.ParametersChanged += OnToolParametersChanged; - // Load current parameters or defaults - var plate = plateView.Plate; - cuttingPanel.LoadFromParameters(plate?.CuttingParameters ?? MachineSettings.DefaultCuttingParameters(form.Nest)); + cuttingPanel.LoadFromParameters(MachineSettings.CuttingParametersFor(plateView.Plate, form.Nest)); loadedParameters = CuttingParametersSerializer.Serialize(cuttingPanel.BuildParameters()); form.ShowSidePanel(cuttingPanel, "Place Lead-in"); diff --git a/OpenNest/Forms/EditNestForm.cs b/OpenNest/Forms/EditNestForm.cs index 8607740f..fb08d8e2 100644 --- a/OpenNest/Forms/EditNestForm.cs +++ b/OpenNest/Forms/EditNestForm.cs @@ -341,7 +341,7 @@ namespace OpenNest.Forms if (CuttingInspection != null) return; PlateView.EndAction(); - var panel = new CuttingInspectionPanel(this, PlateView.Plate.CuttingParameters ?? MachineSettings.DefaultCuttingParameters(Nest), allPlates, mode) + var panel = new CuttingInspectionPanel(this, MachineSettings.CuttingParametersFor(PlateView.Plate, Nest), allPlates, mode) { BeforePlan = CuttingBeforePlan }; @@ -1351,8 +1351,8 @@ namespace OpenNest.Forms if (PlateView?.Plate == null) return; - // The tool's panel starts from the plate's parameters or the defaults; the plate - // takes them with its first lead-in, so opening the tool changes nothing. + // The tool's panel starts from the user's parameters; the plate takes them with + // its first lead-in, so opening the tool changes nothing. PlateView.SetAction(typeof(Actions.ActionLeadIn)); } diff --git a/OpenNest/Forms/MachineSettings.cs b/OpenNest/Forms/MachineSettings.cs index 4ec3271e..90f65a23 100644 --- a/OpenNest/Forms/MachineSettings.cs +++ b/OpenNest/Forms/MachineSettings.cs @@ -211,8 +211,21 @@ namespace OpenNest.Forms return parameters; } + /// + /// The cutting parameters to plan or place lead-ins on with: the + /// user's (see ), so an edit reaches nests already + /// planned. The plate's own are used only when no user settings are saved or readable. + /// + public static CuttingParameters CuttingParametersFor(Plate plate, Nest nest) => + plate?.CuttingParameters != null && ReadLastCuttingParameters() == null + ? plate.CuttingParameters + : DefaultCuttingParameters(nest); + /// The cutting parameters last applied, or the built-in defaults when none are saved or readable. - public static CuttingParameters LastCuttingParameters() + public static CuttingParameters LastCuttingParameters() => + ReadLastCuttingParameters() ?? new CuttingParameters(); + + private static CuttingParameters ReadLastCuttingParameters() { var json = Properties.Settings.Default.CuttingParametersJson; if (!string.IsNullOrEmpty(json)) @@ -225,7 +238,7 @@ namespace OpenNest.Forms { /* fall through */ } } - return new CuttingParameters(); + return null; } /// diff --git a/OpenNest/Properties/AppDataSettingsProvider.cs b/OpenNest/Properties/AppDataSettingsProvider.cs index a8cedd90..6026d3f2 100644 --- a/OpenNest/Properties/AppDataSettingsProvider.cs +++ b/OpenNest/Properties/AppDataSettingsProvider.cs @@ -26,7 +26,7 @@ namespace OpenNest.Properties private readonly Func _legacyProvider; public AppDataSettingsProvider() - : this(DefaultPath, () => new LocalFileSettingsProvider()) { } + : this(DefaultPath, PathOverride == null ? () => new LocalFileSettingsProvider() : null) { } /// The settings file. /// Supplies values to carry over when does not exist; null for none. @@ -36,8 +36,11 @@ namespace OpenNest.Properties _legacyProvider = legacyProvider; } + /// Replaces , so test runs never write the user's settings. + internal static string PathOverride { get; set; } + /// %APPDATA%\OpenNest\user-settings.json. - public static string DefaultPath => Path.Combine( + public static string DefaultPath => PathOverride ?? Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "OpenNest", "user-settings.json"); diff --git a/docs/machine-cutting-defaults.md b/docs/machine-cutting-defaults.md index 6183402d..07459b4f 100644 --- a/docs/machine-cutting-defaults.md +++ b/docs/machine-cutting-defaults.md @@ -17,15 +17,16 @@ machine's units to the nest's. ## Lead-ins and lead-outs -A plate with no cutting parameters of its own (Plan Cutting, Place Lead-in) starts from the -last applied parameters, as before, with the machine's leads and kerf laid over them and the -machine, material, grade and thickness recorded on the parameters. When the last parameters +Plan Cutting and Place Lead-in start from the user's last applied parameters, with the machine's +leads and kerf laid over them and the machine, material, grade and thickness recorded on the +parameters. A plate's own parameters are used only when no user parameters are saved or readable, +so an edit reaches nests already planned. When the last parameters already record the same machine, material, grade and thickness, the machine's settings are not laid over them again, so lead and tab edits made for that job stick; the cutting panel keeps the record (and the kerf) through an edit. Plan Cutting saves its settings as the last parameters as -soon as its settings dialog is accepted, not only on Apply. A plate that already has -parameters keeps them: changing the machine, material or thickness does not rewrite leads -the plate was planned with. +soon as its settings dialog is accepted, not only on Apply. Changing the settings, machine, +material or thickness does not rewrite the leads a plate was planned with: its programs and +stored parameters stay until a plan is applied. Each thickness holds a lead-in and lead-out for outside cuts (the Outside Cuts tab, external contours) and, optionally, its own pair for inside cuts (the Inside Cuts tab, internal contours, diff --git a/docs/nest-file-format.md b/docs/nest-file-format.md index 319f5477..26fa13f1 100644 --- a/docs/nest-file-format.md +++ b/docs/nest-file-format.md @@ -105,9 +105,11 @@ older version. ## Plate cutting parameters -Each plate's optional `cuttingParameters` stores the settings used by Assign -Lead-ins and Place Lead-in. Missing settings stay null, preserving the old default -selection behavior. `OpenNest.IO.CuttingParametersSerializer` owns the shared +Each plate's optional `cuttingParameters` stores the settings last applied to it. +The desktop starts Plan Cutting and Place Lead-in from the user's settings and falls +back to these only when it has none (see +[machine cutting defaults](machine-cutting-defaults.md)). Missing settings stay null, +preserving the old default selection behavior. `OpenNest.IO.CuttingParametersSerializer` owns the shared cross-platform DTO mapping; the desktop settings serializer delegates to it so existing saved settings keep their JSON contract. Settings are independent of the saved part programs: restoring settings never regenerates those programs.