From e8c52a354d8f8f7bb1d30c834f93656b084bfd6c Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 2 Nov 2026 09:43:19 -0500 Subject: [PATCH] fix(posts): refuse wrong-process output and keep settings across updates Ahead of the new laser schedule, three ways a post could silently cut with the wrong process, and one way its settings silently reset: - Cincinnati CL wrote no G89 when no cut library matched the nest's material, thickness and gas (or no etch library for etch features), so the controller cut with whatever library it loaded last. It now refuses, unless the new "Require libraries" setting is turned off. - Cincinnati CL declared G20/G21 from its own setting without comparing the nest's units, so a metric nest posted 25.4x too large. It now refuses a mismatch. - CI Fiber posted any material missing from its table as the default code (MSN), so stainless or aluminum ran with mild-steel technology. A named, unmapped material is now refused; the default code covers only nests with no material. - Posts saved their settings beside their DLL, inside the install folder a Velopack update replaces, so every update reset them to the shipped defaults. Saves now go to %APPDATA%\OpenNest\Posts and the shipped JSON only seeds them; constructing a post no longer writes. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/PostSettingsFile.cs | 48 ++++++++++++ .../CincinnatiPostProcessorTests.cs | 24 ++++-- .../CincinnatiPostRegressionTests.cs | 37 +++++++++ OpenNest.Tests/Cincinnati/CincinnatiReplay.cs | 1 + .../Cincinnati/HeadLockedCutOffPostTests.cs | 3 +- .../Cincinnati/HeadRaisePostTests.cs | 2 +- .../Cincinnati/UserVariablePostTests.cs | 11 +-- .../CIFiberPostProcessorTests.cs | 14 +++- .../DisplayPerimeterPostTests.cs | 2 +- .../ConsolePostVerificationTests.cs | 12 ++- .../PostSettings/PostSettingsFileTests.cs | 57 ++++++++++++++ .../CincinnatiPostConfig.cs | 7 ++ .../CincinnatiPostProcessor.cs | 75 ++++++++++++------- .../CIFiberPostConfig.cs | 22 ++++-- .../CIFiberPostProcessor.cs | 32 ++------ .../CIFiberProgramWriter.cs | 23 +++++- .../GravographISPostProcessor.cs | 33 ++------ docs/shared-settings.md | 2 +- 18 files changed, 299 insertions(+), 106 deletions(-) create mode 100644 OpenNest.Core/PostSettingsFile.cs create mode 100644 OpenNest.Tests/PostSettings/PostSettingsFileTests.cs diff --git a/OpenNest.Core/PostSettingsFile.cs b/OpenNest.Core/PostSettingsFile.cs new file mode 100644 index 00000000..79f516e9 --- /dev/null +++ b/OpenNest.Core/PostSettingsFile.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using System.Reflection; + +namespace OpenNest +{ + /// + /// Where a post keeps its own settings: %APPDATA%\OpenNest\Posts\<post assembly>.json. + /// The install folder holds only the shipped defaults, because an update replaces it and + /// would silently reset every edit saved there. The shipped JSON is read until the first save. + /// + public static class PostSettingsFile + { + /// Replaces , so test runs never read or write the user's settings. + public static string DirectoryOverride { get; set; } + + public static string Directory => DirectoryOverride ?? Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "OpenNest", "Posts"); + + public static string PathFor(Assembly post) => + Path.Combine(Directory, Path.GetFileNameWithoutExtension(post.Location) + ".json"); + + /// The defaults deployed beside the post's assembly. + public static string ShippedPathFor(Assembly post) => Path.ChangeExtension(post.Location, ".json"); + + /// The saved settings, else the shipped defaults, else null. + public static string Read(Assembly post) + { + var saved = PathFor(post); + if (File.Exists(saved)) + return File.ReadAllText(saved); + var shipped = ShippedPathFor(post); + return File.Exists(shipped) ? File.ReadAllText(shipped) : null; + } + + /// + /// Saves through a temporary file, so a failed write leaves the previous settings intact. + /// + public static void Write(Assembly post, string json) + { + var path = PathFor(post); + System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path)); + var temp = path + ".tmp"; + File.WriteAllText(temp, json); + File.Move(temp, path, overwrite: true); + } + } +} diff --git a/OpenNest.Tests/Cincinnati/CincinnatiPostProcessorTests.cs b/OpenNest.Tests/Cincinnati/CincinnatiPostProcessorTests.cs index 61909e5a..8d980d68 100644 --- a/OpenNest.Tests/Cincinnati/CincinnatiPostProcessorTests.cs +++ b/OpenNest.Tests/Cincinnati/CincinnatiPostProcessorTests.cs @@ -14,7 +14,7 @@ public class CincinnatiPostProcessorTests public void Post_ProducesOutput_ForSinglePlateNest() { var nest = CreateTestNest(); - var config = new CincinnatiPostConfig { ConfigurationName = "CL940", PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, ConfigurationName = "CL940", PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); using var ms = new MemoryStream(); @@ -43,7 +43,7 @@ public class CincinnatiPostProcessorTests public void Post_EmitsLeadOutVariable() { var nest = CreateTestNest(); - var config = new CincinnatiPostConfig { PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); using var ms = new MemoryStream(); @@ -59,6 +59,7 @@ public class CincinnatiPostProcessorTests var nest = CreateTestNest(); var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, ArcFeedrate = ArcFeedrateMode.Variables, }; @@ -79,6 +80,7 @@ public class CincinnatiPostProcessorTests var nest = CreateTestNest(); var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, ArcFeedrate = ArcFeedrateMode.None, }; @@ -97,7 +99,7 @@ public class CincinnatiPostProcessorTests public void Post_EmitsG90InPreamble() { var nest = CreateTestNest(); - var config = new CincinnatiPostConfig { PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); using var ms = new MemoryStream(); @@ -110,7 +112,7 @@ public class CincinnatiPostProcessorTests [Fact] public void Post_ImplementsIPostProcessor() { - var post = new CincinnatiPostProcessor(new CincinnatiPostConfig()); + var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { RequireLibraries = false }); IPostProcessor pp = post; Assert.Equal("Cincinnati CL-707", pp.Name); @@ -120,7 +122,7 @@ public class CincinnatiPostProcessorTests [Fact] public void Post_ImplementsIConfigurablePostProcessor() { - var post = new CincinnatiPostProcessor(new CincinnatiPostConfig()); + var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { RequireLibraries = false }); var configurable = post as IConfigurablePostProcessor; Assert.NotNull(configurable); @@ -137,7 +139,7 @@ public class CincinnatiPostProcessorTests plate2.Parts.Add(new Part(new Drawing("Part1", CreateSquareProgram()))); nest.Plates.Add(plate2); - var config = new CincinnatiPostConfig { PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); using var ms = new MemoryStream(); @@ -154,7 +156,7 @@ public class CincinnatiPostProcessorTests public void Post_ToFile_CreatesFile() { var nest = CreateTestNest(); - var config = new CincinnatiPostConfig { PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); var tempFile = Path.GetTempFileName() + ".CNC"; @@ -177,6 +179,7 @@ public class CincinnatiPostProcessorTests { var config = new CincinnatiPostConfig { + RequireLibraries = false, ConfigurationName = "CL940_CORONA", DefaultAssistGas = "N2", DefaultEtchGas = "N2", @@ -239,6 +242,7 @@ public class CincinnatiPostProcessorTests var nest = CreateTestNest(); var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, UsePartSubprograms = true, PartSubprogramStart = 200, @@ -279,6 +283,7 @@ public class CincinnatiPostProcessorTests var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, UsePartSubprograms = true, PartSubprogramStart = 200, @@ -317,6 +322,7 @@ public class CincinnatiPostProcessorTests var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, UsePartSubprograms = true, PartSubprogramStart = 200, @@ -349,6 +355,7 @@ public class CincinnatiPostProcessorTests var config = new CincinnatiPostConfig { + RequireLibraries = false, PostedAccuracy = 4, UsePartSubprograms = true, PartSubprogramStart = 200, @@ -373,6 +380,7 @@ public class CincinnatiPostProcessorTests { var config = new CincinnatiPostConfig { + RequireLibraries = false, UsePartSubprograms = true, PartSubprogramStart = 300, }; @@ -408,7 +416,7 @@ public class CincinnatiPostProcessorTests plate.Parts.Add(new Part(drawing, new Vector(1, 1))); nest.Plates.Add(plate); - var config = new CincinnatiPostConfig { UsePartSubprograms = true, PostedAccuracy = 4 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, UsePartSubprograms = true, PostedAccuracy = 4 }; var post = new CincinnatiPostProcessor(config); using var ms = new MemoryStream(); diff --git a/OpenNest.Tests/Cincinnati/CincinnatiPostRegressionTests.cs b/OpenNest.Tests/Cincinnati/CincinnatiPostRegressionTests.cs index 27402b9b..c4e167ff 100644 --- a/OpenNest.Tests/Cincinnati/CincinnatiPostRegressionTests.cs +++ b/OpenNest.Tests/Cincinnati/CincinnatiPostRegressionTests.cs @@ -188,6 +188,43 @@ public class CincinnatiPostRegressionTests Assert.Empty(libraries); else Assert.Equal(new[] { $"P{expected}.lib" }, libraries); + + // Without a G89 the controller cuts with whatever library it loaded last, so by + // default a nest with no matching library is refused. + config.RequireLibraries = true; + if (expected == "") + Assert.Throws(() => Post(nest, config)); + else + Post(nest, config); + } + + [Fact] + public void EtchFeatures_WithoutAnEtchLibrary_AreRefused() + { + var nest = SquaresNest(("A", new Vector(5, 5))); + var config = Config(partSubprograms: false); + config.RequireLibraries = true; + config.SelectedLibrary = "MS250O2"; + Post(nest, config); + + foreach (var move in nest.Plates[0].Parts[0].Program.Codes.OfType().Take(4)) + move.Layer = LayerType.Scribe; + + Assert.Throws(() => Post(nest, config)); + config.EtchLibraries = new() { new() { Gas = config.DefaultEtchGas, Library = "ETCHN2" } }; + Post(nest, config); + } + + [Fact] + public void NestUnits_OtherThanThePostedUnits_AreRefused() + { + var nest = SquaresNest(("A", new Vector(5, 5))); + nest.Units = Units.Millimeters; + var config = Config(partSubprograms: false); + + Assert.Throws(() => Post(nest, config)); + config.PostedUnits = Units.Millimeters; + Post(nest, config); } [Theory] diff --git a/OpenNest.Tests/Cincinnati/CincinnatiReplay.cs b/OpenNest.Tests/Cincinnati/CincinnatiReplay.cs index 4cb4c062..46c44bcb 100644 --- a/OpenNest.Tests/Cincinnati/CincinnatiReplay.cs +++ b/OpenNest.Tests/Cincinnati/CincinnatiReplay.cs @@ -20,6 +20,7 @@ internal static class CincinnatiReplay UsePartSubprograms = partSubprograms, UseAntiDive = false, PalletExchange = PalletMode.None, + RequireLibraries = false, }; internal static string Post(Nest nest, bool partSubprograms) => Post(nest, Config(partSubprograms)); diff --git a/OpenNest.Tests/Cincinnati/HeadLockedCutOffPostTests.cs b/OpenNest.Tests/Cincinnati/HeadLockedCutOffPostTests.cs index 1e753d03..f2309acd 100644 --- a/OpenNest.Tests/Cincinnati/HeadLockedCutOffPostTests.cs +++ b/OpenNest.Tests/Cincinnati/HeadLockedCutOffPostTests.cs @@ -46,7 +46,7 @@ public class HeadLockedCutOffPostTests public void ClPost_WithoutLockCodes_RefusesBeforeWriting() { using var stream = new MemoryStream(); - var post = new CincinnatiPostProcessor(new CincinnatiPostConfig()); + var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { RequireLibraries = false }); var refusal = Assert.Throws(() => post.Post(Nest(farEdgeFirst: false), stream)); @@ -92,6 +92,7 @@ public class HeadLockedCutOffPostTests { var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { + RequireLibraries = false, HeadHeightLockCode = LockCode, HeadHeightUnlockCode = UnlockCode, }); diff --git a/OpenNest.Tests/Cincinnati/HeadRaisePostTests.cs b/OpenNest.Tests/Cincinnati/HeadRaisePostTests.cs index 95838984..ea8c3149 100644 --- a/OpenNest.Tests/Cincinnati/HeadRaisePostTests.cs +++ b/OpenNest.Tests/Cincinnati/HeadRaisePostTests.cs @@ -97,7 +97,7 @@ public class HeadRaisePostTests private static List ClLines(Nest nest, M47Mode mode) { - var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { ExteriorM47 = mode, InteriorM47 = mode }); + var post = new CincinnatiPostProcessor(new CincinnatiPostConfig { RequireLibraries = false, ExteriorM47 = mode, InteriorM47 = mode }); using var stream = new MemoryStream(); post.Post(nest, stream); return Lines(stream); diff --git a/OpenNest.Tests/Cincinnati/UserVariablePostTests.cs b/OpenNest.Tests/Cincinnati/UserVariablePostTests.cs index a9454b80..3f20dac9 100644 --- a/OpenNest.Tests/Cincinnati/UserVariablePostTests.cs +++ b/OpenNest.Tests/Cincinnati/UserVariablePostTests.cs @@ -62,7 +62,7 @@ public class UserVariablePostTests plate.Parts.Add(new Part(drawing2, new Vector(50, 0))); nest.Plates.Add(plate); - var config = new CincinnatiPostConfig { UserVariableStart = 200 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, UserVariableStart = 200 }; var post = new CincinnatiPostProcessor(config); var output = PostToString(post, nest); @@ -89,7 +89,7 @@ public class UserVariablePostTests plate.Parts.Add(new Part(drawing2, new Vector(50, 0))); nest.Plates.Add(plate); - var config = new CincinnatiPostConfig { UserVariableStart = 200 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, UserVariableStart = 200 }; var post = new CincinnatiPostProcessor(config); var output = PostToString(post, nest); @@ -103,7 +103,7 @@ public class UserVariablePostTests [Fact] public void UserVariables_StartNumberConfigurable() { - var config = new CincinnatiPostConfig { UserVariableStart = 300 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, UserVariableStart = 300 }; var output = PostNestWithVariables("width = 48.0\nG90\nG01X$widthY0", config); Assert.Contains("#300=48", output); @@ -115,6 +115,7 @@ public class UserVariablePostTests // Create a plate with a vertical cutoff var config = new CincinnatiPostConfig { + RequireLibraries = false, SheetWidthVariable = 110, SheetLengthVariable = 111, }; @@ -150,7 +151,7 @@ public class UserVariablePostTests public void CutOff_SegmentedCut_OnlyEdgeUsesVariable() { // Create a plate with a part in the middle and a vertical cutoff - var config = new CincinnatiPostConfig { SheetWidthVariable = 110 }; + var config = new CincinnatiPostConfig { RequireLibraries = false, SheetWidthVariable = 110 }; var nest = new Nest { Name = "Test" }; var plate = new Plate(new Size(48, 96)); @@ -188,7 +189,7 @@ public class UserVariablePostTests plate.Parts.Add(new Part(drawing, new Vector(0, 0))); nest.Plates.Add(plate); - config ??= new CincinnatiPostConfig { UserVariableStart = 200 }; + config ??= new CincinnatiPostConfig { RequireLibraries = false, UserVariableStart = 200 }; var post = new CincinnatiPostProcessor(config); return PostToString(post, nest); } diff --git a/OpenNest.Tests/CincinnatiCIFiber/CIFiberPostProcessorTests.cs b/OpenNest.Tests/CincinnatiCIFiber/CIFiberPostProcessorTests.cs index c12a3216..60d8f01e 100644 --- a/OpenNest.Tests/CincinnatiCIFiber/CIFiberPostProcessorTests.cs +++ b/OpenNest.Tests/CincinnatiCIFiber/CIFiberPostProcessorTests.cs @@ -450,7 +450,19 @@ public class CIFiberPostProcessorTests Assert.Equal("SSN", config.ResolveMaterialCode("stainless")); Assert.Equal("SSN", config.ResolveMaterialCode(" STAINLESS ")); - Assert.Equal("MSN", config.ResolveMaterialCode("Aluminum")); + Assert.Null(config.ResolveMaterialCode("Aluminum")); + Assert.Equal("MSN", config.ResolveMaterialCode("")); + } + + [Fact] + public void UnmappedMaterial_IsRefusedRatherThanPostedAsTheDefault() + { + var nest = MakeMultiSheetNest(); + nest.Material = new Material("Stainless"); + var post = new CIFiberPostProcessor(new CIFiberPostConfig()); + + var ex = Assert.Throws(() => post.Post(nest, new MemoryStream())); + Assert.Contains("Stainless", ex.Message); } [Fact] diff --git a/OpenNest.Tests/CuttingPlanning/DisplayPerimeterPostTests.cs b/OpenNest.Tests/CuttingPlanning/DisplayPerimeterPostTests.cs index 6bb61674..c2792e83 100644 --- a/OpenNest.Tests/CuttingPlanning/DisplayPerimeterPostTests.cs +++ b/OpenNest.Tests/CuttingPlanning/DisplayPerimeterPostTests.cs @@ -17,7 +17,7 @@ public class DisplayPerimeterPostTests private static IPostProcessor Post(string name) => name switch { - "cincinnati" => new CincinnatiPostProcessor(new CincinnatiPostConfig { ConfigurationName = "CL940", PostedAccuracy = 4 }), + "cincinnati" => new CincinnatiPostProcessor(new CincinnatiPostConfig { RequireLibraries = false, ConfigurationName = "CL940", PostedAccuracy = 4 }), "cifiber" => new CIFiberPostProcessor(new CIFiberPostConfig { ConfigurationName = "CI FIBER 8K" }), _ => new GravographISPostProcessor(), }; diff --git a/OpenNest.Tests/Diagnostics/ConsolePostVerificationTests.cs b/OpenNest.Tests/Diagnostics/ConsolePostVerificationTests.cs index 43bc4c25..ed0b7f38 100644 --- a/OpenNest.Tests/Diagnostics/ConsolePostVerificationTests.cs +++ b/OpenNest.Tests/Diagnostics/ConsolePostVerificationTests.cs @@ -5,6 +5,7 @@ using OpenNest.IO; namespace OpenNest.Tests.Diagnostics; +[Collection(OpenNest.Tests.PostSettings.PostSettingsFileTests.Collection)] public class ConsolePostVerificationTests { [Fact] @@ -12,11 +13,19 @@ public class ConsolePostVerificationTests { var directory = Path.Combine(Path.GetTempPath(), "opennest-post-verification-" + Guid.NewGuid()); Directory.CreateDirectory(directory); + // Post with the shipped settings, never this PC's saved ones. + PostSettingsFile.DirectoryOverride = Path.Combine(directory, "post-settings"); try { var input = Path.Combine(directory, "job.nest"); var output = Path.Combine(directory, "job.cnc"); - var nest = new Nest("VerificationFixture"); + // A stock the shipped post has a cut library for, so only verification can refuse it. + var nest = new Nest("VerificationFixture") + { + Material = new Material("Carbon Steel"), + Thickness = 0.188, + AssistGas = "O2", + }; var program = new OpenNest.CNC.Program(); program.Codes.Add(new RapidMove(0, 0)); program.Codes.Add(new LinearMove(2, 0)); @@ -56,6 +65,7 @@ public class ConsolePostVerificationTests } finally { + PostSettingsFile.DirectoryOverride = null; Directory.Delete(directory, recursive: true); } } diff --git a/OpenNest.Tests/PostSettings/PostSettingsFileTests.cs b/OpenNest.Tests/PostSettings/PostSettingsFileTests.cs new file mode 100644 index 00000000..7a05fb88 --- /dev/null +++ b/OpenNest.Tests/PostSettings/PostSettingsFileTests.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using OpenNest.Posts.Cincinnati; +using OpenNest.Posts.CincinnatiCIFiber; +using Xunit; + +namespace OpenNest.Tests.PostSettings; + +/// +/// An update replaces the install folder, so a post's edited settings must live outside it: +/// saves go to the user's settings directory and the shipped JSON only seeds them. +/// +[Collection(Collection)] +public class PostSettingsFileTests : IDisposable +{ + /// Tests that set share it, so they never run together. + public const string Collection = "PostSettingsFile"; + + private readonly string directory = + Path.Combine(Path.GetTempPath(), "opennest-post-settings-" + Guid.NewGuid()); + + public PostSettingsFileTests() => PostSettingsFile.DirectoryOverride = directory; + + public void Dispose() + { + PostSettingsFile.DirectoryOverride = null; + if (Directory.Exists(directory)) + Directory.Delete(directory, recursive: true); + } + + [Fact] + public void Save_WritesTheUserCopy_AndLeavesTheShippedDefaultsUntouched() + { + var assembly = typeof(CincinnatiPostProcessor).Assembly; + var shipped = PostSettingsFile.ShippedPathFor(assembly); + var shippedBefore = File.ReadAllText(shipped); + + var post = new CincinnatiPostProcessor(); + Assert.NotEmpty(post.Config.MaterialLibraries); + post.Config.ConfigurationName = "EDITED"; + post.SaveConfig(); + + Assert.Equal(shippedBefore, File.ReadAllText(shipped)); + Assert.StartsWith(directory, PostSettingsFile.PathFor(assembly)); + Assert.Equal("EDITED", new CincinnatiPostProcessor().Config.ConfigurationName); + Assert.False(File.Exists(PostSettingsFile.PathFor(assembly) + ".tmp")); + } + + [Fact] + public void Constructing_WithNoSavedCopy_ReadsTheShippedDefaultsWithoutWriting() + { + var post = new CIFiberPostProcessor(); + + Assert.Contains("Carbon Steel", post.Config.MaterialCodes.Keys); + Assert.False(Directory.Exists(directory)); + } +} diff --git a/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostConfig.cs b/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostConfig.cs index 60f7eacf..2bb15101 100644 --- a/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostConfig.cs +++ b/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostConfig.cs @@ -354,6 +354,13 @@ namespace OpenNest.Posts.Cincinnati [PostSetting(LibrariesSection, 1)] public List MaterialLibraries { get; set; } = new(); + [DisplayName("Require libraries")] + [Description( + "Refuse to post a nest with no matching cut library, or etch features with no etch library. Turn off only if the controller's library is always loaded by hand: without a G89 it cuts with whatever library it loaded last." + )] + [PostSetting(LibrariesSection, 3)] + public bool RequireLibraries { get; set; } = true; + [DisplayName("Etch libraries")] [Description("Etch library (G89 file) for each etch gas.")] [PostSetting(LibrariesSection, 2)] diff --git a/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostProcessor.cs b/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostProcessor.cs index 84d0736f..91d039e2 100644 --- a/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostProcessor.cs +++ b/Posts/OpenNest.Posts.Cincinnati/CincinnatiPostProcessor.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -58,19 +59,13 @@ namespace OpenNest.Posts.Cincinnati Config.SelectedLibrary = entry?.Library ?? ""; } + /// Loads this PC's saved settings, else the shipped defaults (see ). public CincinnatiPostProcessor() { - var configPath = GetConfigPath(); - if (File.Exists(configPath)) - { - var json = File.ReadAllText(configPath); - Config = JsonSerializer.Deserialize(json, JsonOptions); - } - else - { - Config = new CincinnatiPostConfig(); - SaveConfig(); - } + var json = PostSettingsFile.Read(typeof(CincinnatiPostProcessor).Assembly); + Config = json == null + ? new CincinnatiPostConfig() + : JsonSerializer.Deserialize(json, JsonOptions) ?? new CincinnatiPostConfig(); } public CincinnatiPostProcessor(CincinnatiPostConfig config) @@ -78,12 +73,7 @@ namespace OpenNest.Posts.Cincinnati Config = config; } - public void SaveConfig() - { - var configPath = GetConfigPath(); - var json = SerializeConfig(); - File.WriteAllText(configPath, json); - } + public void SaveConfig() => PostSettingsFile.Write(typeof(CincinnatiPostProcessor).Assembly, SerializeConfig()); public string SerializeConfig() => JsonSerializer.Serialize(Config, JsonOptions); @@ -92,20 +82,13 @@ namespace OpenNest.Posts.Cincinnati JsonSerializer.Deserialize(json, JsonOptions) ?? throw new JsonException("The post settings are empty."); - private static string GetConfigPath() - { - var assemblyPath = typeof(CincinnatiPostProcessor).Assembly.Location; - var dir = Path.GetDirectoryName(assemblyPath); - var name = Path.GetFileNameWithoutExtension(assemblyPath); - return Path.Combine(dir, name + ".json"); - } - public void Post(Nest nest, Stream outputStream) { // Refuse before writing anything: a head-locked overcut needs the lock codes. HeadHeightLock.EnsurePostable(nest, Config.HasHeadHeightLockCodes(), Name); // M47 raises the head before a marked rapid; the markers must be well formed. HeadRaise.EnsurePostable(nest, true, Name); + EnsureProcessResolvable(nest); // 1. Create variable manager and register standard variables var vars = CreateVariableManager(); @@ -266,10 +249,52 @@ namespace OpenNest.Posts.Cincinnati DisplayPerimeterTail.EnsurePostable(nest); HeadHeightLock.EnsurePostable(nest, Config.HasHeadHeightLockCodes(), Name); HeadRaise.EnsurePostable(nest, true, Name); + EnsureProcessResolvable(nest); using var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write); Post(nest, fs); } + /// + /// Refuses a nest the controller would cut with the wrong process: coordinates in other + /// units than the G20/G21 the post declares, or features with no G89 library, which + /// leave the controller on whatever library it loaded last. + /// + private void EnsureProcessResolvable(Nest nest) + { + if (nest.Units != Config.PostedUnits) + throw new InvalidOperationException( + $"The nest is in {nest.Units} but the {Name} post writes {Config.PostedUnits}. " + + "Change the post's Posted units, or the nest's units, so they match."); + + var parts = nest.Plates.SelectMany(p => p.Parts).Where(p => p?.Program != null).ToList(); + if (!Config.RequireLibraries || parts.Count == 0) + return; + + var resolver = new MaterialLibraryResolver(Config); + var gas = MaterialLibraryResolver.ResolveGas(nest, Config); + if (parts.Any(p => HasMoves(p.Program, etch: false)) + && string.IsNullOrEmpty(resolver.ResolveCutLibrary(nest.Material?.Name ?? "", nest.Thickness, gas))) + throw new InvalidOperationException( + $"The {Name} post has no cut library for {nest.Material?.Name ?? "(no material)"} " + + $"{nest.Thickness} with {gas} assist gas. Add a row to the post's cut libraries " + + "or select a library before posting."); + + if (parts.Any(p => HasMoves(p.Program, etch: true)) + && string.IsNullOrEmpty(resolver.ResolveEtchLibrary(Config.DefaultEtchGas))) + throw new InvalidOperationException( + $"The nest has etch features but the {Name} post has no etch library for " + + $"{Config.DefaultEtchGas} gas. Add one to the post's etch libraries before posting."); + } + + private static bool HasMoves(Program program, bool etch) => + program.Codes.Any(code => code switch + { + LinearMove line => (line.Layer == LayerType.Scribe) == etch, + ArcMove arc => (arc.Layer == LayerType.Scribe) == etch, + _ => false, + }) + || program.Codes.OfType().Any(call => call.Program != null && HasMoves(call.Program, etch)); + private Dictionary<(int drawingId, string varName), int> RegisterUserVariables( ProgramVariableManager vars, List plates diff --git a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostConfig.cs b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostConfig.cs index 070b864c..03b30172 100644 --- a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostConfig.cs +++ b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostConfig.cs @@ -237,21 +237,27 @@ namespace OpenNest.Posts.CincinnatiCIFiber return copy; } - /// Fallback V.E.MATERIAL code when the material name has no mapping. + /// V.E.MATERIAL code for a nest with no material name. [DisplayName("Default material code")] - [Description("Code used when the nest material has no entry in the table above.")] + [Description( + "Code used when the nest has no material. A named material missing from the table above is refused rather than posted with this code, so stainless or aluminum is never cut as steel." + )] [PostSetting(MaterialSection, 1)] public string DefaultMaterialCode { get; set; } = "MSN"; + /// + /// The V.E.MATERIAL code for , or null when a named + /// material has no mapping (or an unnamed one has no default): the caller must refuse. + /// public string ResolveMaterialCode(string materialName) { - if (!string.IsNullOrWhiteSpace(materialName) - && MaterialCodes != null - && MaterialCodes.TryGetValue(materialName.Trim(), out var code) - && !string.IsNullOrWhiteSpace(code)) - return code; + string code; + if (string.IsNullOrWhiteSpace(materialName)) + code = DefaultMaterialCode; + else if (MaterialCodes == null || !MaterialCodes.TryGetValue(materialName.Trim(), out code)) + code = null; - return DefaultMaterialCode ?? "MSN"; + return string.IsNullOrWhiteSpace(code) ? null : code; } /// diff --git a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostProcessor.cs b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostProcessor.cs index 1e65ab38..d3829463 100644 --- a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostProcessor.cs +++ b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberPostProcessor.cs @@ -37,21 +37,13 @@ namespace OpenNest.Posts.CincinnatiCIFiber object IConfigurablePostProcessor.Config => Config; + /// Loads this PC's saved settings, else the shipped defaults (see ). public CIFiberPostProcessor() { - var configPath = GetConfigPath(); - if (File.Exists(configPath)) - { - var json = File.ReadAllText(configPath); - Config = - JsonSerializer.Deserialize(json, JsonOptions) - ?? new CIFiberPostConfig(); - } - else - { - Config = new CIFiberPostConfig(); - SaveConfig(); - } + var json = PostSettingsFile.Read(typeof(CIFiberPostProcessor).Assembly); + Config = json == null + ? new CIFiberPostConfig() + : JsonSerializer.Deserialize(json, JsonOptions) ?? new CIFiberPostConfig(); } public CIFiberPostProcessor(CIFiberPostConfig config) @@ -59,12 +51,7 @@ namespace OpenNest.Posts.CincinnatiCIFiber Config = config ?? throw new ArgumentNullException(nameof(config)); } - public void SaveConfig() - { - var configPath = GetConfigPath(); - var json = SerializeConfig(); - File.WriteAllText(configPath, json); - } + public void SaveConfig() => PostSettingsFile.Write(typeof(CIFiberPostProcessor).Assembly, SerializeConfig()); public string SerializeConfig() => JsonSerializer.Serialize(Config, JsonOptions); @@ -73,13 +60,6 @@ namespace OpenNest.Posts.CincinnatiCIFiber JsonSerializer.Deserialize(json, JsonOptions) ?? throw new JsonException("The post settings are empty."); - private static string GetConfigPath() - { - var assemblyPath = typeof(CIFiberPostProcessor).Assembly.Location; - var dir = Path.GetDirectoryName(assemblyPath); - var name = Path.GetFileNameWithoutExtension(assemblyPath); - return Path.Combine(dir, name + ".json"); - } /// /// Writes every sheet into one program. A stream holds a single program, diff --git a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberProgramWriter.cs b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberProgramWriter.cs index cf287491..63f3aa50 100644 --- a/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberProgramWriter.cs +++ b/Posts/OpenNest.Posts.CincinnatiCIFiber/CIFiberProgramWriter.cs @@ -75,9 +75,28 @@ namespace OpenNest.Posts.CincinnatiCIFiber HeadHeightLock.EnsurePostable(nest, _config.HasHeadHeightLockCodes(), "Cincinnati CI Fiber"); HeadRaise.EnsurePostable(nest, _config.HasHeadRaiseCode(), "Cincinnati CI Fiber"); + EnsureMaterialMapped(nest); Write(nest, PostedSheets(nest), 1, w); } + /// + /// Refuses a material with no V.E.MATERIAL code: posting it under another material's + /// code would have the machine cut it with that material's technology. + /// + private void EnsureMaterialMapped(Nest nest) + { + var materialName = nest.Material?.Name ?? ""; + if (_config.ResolveMaterialCode(materialName) == null) + throw new InvalidOperationException(MissingMaterialMessage(materialName)); + } + + private static string MissingMaterialMessage(string materialName) => + string.IsNullOrWhiteSpace(materialName) + ? "The nest has no material and the Cincinnati CI Fiber post has no default material code. " + + "Set the nest's material before posting." + : $"The Cincinnati CI Fiber post has no material code for \"{materialName.Trim()}\". " + + "Add it to the post's material codes before posting."; + /// /// Writes one sheet as a complete program. Contour labels restart at 1; /// only labels the sheet comment. @@ -91,6 +110,7 @@ namespace OpenNest.Posts.CincinnatiCIFiber HeadHeightLock.EnsurePostable(nest, _config.HasHeadHeightLockCodes(), "Cincinnati CI Fiber"); HeadRaise.EnsurePostable(nest, _config.HasHeadRaiseCode(), "Cincinnati CI Fiber"); + EnsureMaterialMapped(nest); Write(nest, new[] { sheet }, sheetNumber, w); } @@ -164,7 +184,8 @@ namespace OpenNest.Posts.CincinnatiCIFiber thickness = thickness.Substring(1); // sample posts ".060" CIFiberFormatter.Line(w, $"( Material = {materialName} {thickness} )".TrimEnd()); - var code = _config.ResolveMaterialCode(materialName); + var code = _config.ResolveMaterialCode(materialName) + ?? throw new InvalidOperationException(MissingMaterialMessage(materialName)); CIFiberFormatter.Line(w, $"V.E.MATERIAL = \"{code}\""); CIFiberFormatter.Line(w, $"V.E.THICKNESS = {_fmt.Fixed(nest.Thickness)}"); diff --git a/Posts/OpenNest.Posts.GravographIS/GravographISPostProcessor.cs b/Posts/OpenNest.Posts.GravographIS/GravographISPostProcessor.cs index c2e4e054..7b0fa42a 100644 --- a/Posts/OpenNest.Posts.GravographIS/GravographISPostProcessor.cs +++ b/Posts/OpenNest.Posts.GravographIS/GravographISPostProcessor.cs @@ -42,21 +42,13 @@ namespace OpenNest.Posts.GravographIS object IConfigurablePostProcessor.Config => Config; + /// Loads this PC's saved settings, else the shipped defaults (see ). public GravographISPostProcessor() { - var configPath = GetConfigPath(); - if (File.Exists(configPath)) - { - var json = File.ReadAllText(configPath); - Config = - JsonSerializer.Deserialize(json, JsonOptions) - ?? new GravographISPostConfig(); - } - else - { - Config = new GravographISPostConfig(); - SaveConfig(); - } + var json = PostSettingsFile.Read(typeof(GravographISPostProcessor).Assembly); + Config = json == null + ? new GravographISPostConfig() + : JsonSerializer.Deserialize(json, JsonOptions) ?? new GravographISPostConfig(); } public GravographISPostProcessor(GravographISPostConfig config) @@ -64,12 +56,7 @@ namespace OpenNest.Posts.GravographIS Config = config ?? throw new ArgumentNullException(nameof(config)); } - public void SaveConfig() - { - var configPath = GetConfigPath(); - var json = SerializeConfig(); - File.WriteAllText(configPath, json); - } + public void SaveConfig() => PostSettingsFile.Write(typeof(GravographISPostProcessor).Assembly, SerializeConfig()); public string SerializeConfig() => JsonSerializer.Serialize(Config, JsonOptions); @@ -78,14 +65,6 @@ namespace OpenNest.Posts.GravographIS JsonSerializer.Deserialize(json, JsonOptions) ?? throw new JsonException("The post settings are empty."); - private static string GetConfigPath() - { - var assemblyPath = typeof(GravographISPostProcessor).Assembly.Location; - var dir = Path.GetDirectoryName(assemblyPath); - var name = Path.GetFileNameWithoutExtension(assemblyPath); - return Path.Combine(dir, name + ".json"); - } - public void Post(Nest nest, Stream outputStream) { if (nest == null) diff --git a/docs/shared-settings.md b/docs/shared-settings.md index 3bc5b867..7e870fda 100644 --- a/docs/shared-settings.md +++ b/docs/shared-settings.md @@ -7,7 +7,7 @@ building nests and posts the same way: | Kind | Key | What it holds | This PC's own copy | |---|---|---|---| | `machine` | machine id | A Tools > Machine Configuration entry: materials, grades, thicknesses, leads, kerf, cut-offs, motion limits, plate sizes, support grid, clamps | `%APPDATA%\OpenNest\Machines\.json` | -| `post` | post type name, e.g. `OpenNest.Posts.CincinnatiCIFiber.CIFiberPostProcessor` | A post's settings, as its JSON file holds them | `.json` beside the post DLL in `Posts\` | +| `post` | post type name, e.g. `OpenNest.Posts.CincinnatiCIFiber.CIFiberPostProcessor` | A post's settings, as its JSON file holds them | `%APPDATA%\OpenNest\Posts\.json`, else the shipped `.json` beside the post DLL in `Posts\` until the first save (an update replaces the install folder, so edits are never saved there) | | `clamps` | `default` | Legacy clamp settings, read only to copy them onto a machine once (machines now hold their clamps) | `%APPDATA%\OpenNest\clamps.json` | | `nest-defaults` | `default` | Tools > Nest Defaults: units, sheet size, quadrant, spacing, default machine | `%APPDATA%\OpenNest\defaults.json` |