diff --git a/OpenNest.Core/CNC/CuttingPlanning/PlateCuttingState.cs b/OpenNest.Core/CNC/CuttingPlanning/PlateCuttingState.cs index b25c8f6e..ad0313e8 100644 --- a/OpenNest.Core/CNC/CuttingPlanning/PlateCuttingState.cs +++ b/OpenNest.Core/CNC/CuttingPlanning/PlateCuttingState.cs @@ -15,6 +15,8 @@ namespace OpenNest.CNC.CuttingPlanning; /// part list instance and order, plate quantity/size/quadrant/settings, cutoff definitions, and /// each part's complete cutting state with owned copies of its placed and drawing programs, /// drawing cutoff classification, drawing pins and pins-off, and exact settings content. +/// When labels are etched it also records what the label text and placement are made from: +/// each drawing's name, label box, bends and source offset, and the plate's grain. /// A commit compares it with the live plate and refuses when anything differs. /// public sealed class PlateCuttingState @@ -32,10 +34,14 @@ public sealed class PlateCuttingState private readonly ObservableList commonLineList; private readonly CommonLineRecord[] commonLines; private readonly bool cutCommonLinesTwice; + private readonly bool labels; + private readonly double grainAngle; private PlateCuttingState(Plate plate, PartRecord[] parts, CutOffRecord[] cutOffs, - Dictionary drawings, Dictionary settings) + Dictionary drawings, Dictionary settings, bool labels) { + this.labels = labels; + grainAngle = plate.GrainAngle; commonLineList = plate.CommonLines; commonLines = plate.CommonLines.Select(group => new CommonLineRecord(group, group.Kerf, group.Members.ToArray())).ToArray(); @@ -63,9 +69,11 @@ public sealed class PlateCuttingState /// Captures on the caller thread. Unsupported or malformed programs throw /// or ; so do part or /// plate settings that are not exact built-in settings types, whose state cannot be - /// captured exactly. + /// captured exactly. With (the plan etches part labels) a later + /// change to a drawing's name, label box, bends or source offset, or to the plate's grain, + /// also makes the state stale, since the etched text and its placement come from them. /// - public static PlateCuttingState Capture(Plate plate, CancellationToken token = default) + public static PlateCuttingState Capture(Plate plate, CancellationToken token = default, bool labels = false) { ArgumentNullException.ThrowIfNull(plate); if (plate.Parts == null || plate.CutOffs == null) @@ -83,7 +91,8 @@ public sealed class PlateCuttingState if (!drawings.ContainsKey(drawing)) drawings.Add(drawing, new(drawing.Program, drawing.Program == null ? null : OwnedProgramCopy.Copy(drawing.Program, token), drawing.IsCutOff, - drawing.CutPins, StateFingerprint.Of(drawing.CutPins))); + drawing.CutPins, StateFingerprint.Of(drawing.CutPins), + labels ? LabelInputs(drawing) : null)); var state = part.CaptureCuttingState(); Fingerprint(state.CuttingParameters); records.Add(new(part, state, OwnedProgramCopy.Copy(part.Program, token), BoxValues(state.BoundingBox), @@ -93,7 +102,7 @@ public sealed class PlateCuttingState ? throw new ArgumentException("Plate contains a missing cutoff definition.") : new CutOffRecord(c, c.Drawing, c.Axis, c.Position, c.StartLimit, c.EndLimit, c.Gaps, c.HeadLockOvercut, c.FarEdgeFirst)).ToArray(); - return new(plate, records.ToArray(), cutOffs, drawings, settings); + return new(plate, records.ToArray(), cutOffs, drawings, settings, labels); void Fingerprint(CuttingParameters parameters) { @@ -117,6 +126,9 @@ public sealed class PlateCuttingState return "Plate quantity, size or quadrant changed."; if (!ReferenceEquals(plate.CuttingParameters, plateSettings) || !SameSettings(plateSettings)) return "Plate cutting settings changed."; + // The bend group letters of every etched label are read against the grain. + if (labels && !Bits(plate.GrainAngle, grainAngle)) + return "Plate grain changed."; if (plate.Parts.Count != parts.Length) return "Parts were added or removed."; for (var i = 0; i < parts.Length; i++) @@ -155,6 +167,8 @@ public sealed class PlateCuttingState if (!ReferenceEquals(part.BaseDrawing.CutPins, drawing.Pins) || StateFingerprint.Of(drawing.Pins) != drawing.PinsFingerprint) return $"Part {i + 1} drawing pins changed."; + if (labels && LabelInputs(part.BaseDrawing) != drawing.LabelInputs) + return $"Part {i + 1} drawing name, label box or bends changed."; } if (plate.CutOffs.Count != cutOffs.Length) return "Cutoffs were added or removed."; @@ -216,6 +230,39 @@ public sealed class PlateCuttingState [BitConverter.DoubleToInt64Bits(box.X), BitConverter.DoubleToInt64Bits(box.Y), BitConverter.DoubleToInt64Bits(box.Width), BitConverter.DoubleToInt64Bits(box.Length)]; + // Exact text of what a part label is made from (PartLabels.Strokes): the name, the box, the + // bends and the source offset, doubles by bit pattern. + private static string LabelInputs(Drawing drawing) + { + var text = new System.Text.StringBuilder(); + text.Append(drawing.Name?.Length ?? -1).Append(':').Append(drawing.Name).Append(';'); + var box = drawing.LabelBox; + if (box == null) + text.Append("nobox;"); + else + Values(box.Center.X, box.Center.Y, box.Width, box.Height, box.Rotation); + var offset = drawing.Source?.Offset ?? Vector.Zero; + Values(offset.X, offset.Y); + if (drawing.Bends == null) + text.Append("nobends;"); + else + foreach (var bend in drawing.Bends) + { + if (bend == null) + text.Append("null;"); + else + Values(bend.StartPoint.X, bend.StartPoint.Y, bend.EndPoint.X, bend.EndPoint.Y); + } + return text.ToString(); + + void Values(params double[] values) + { + foreach (var value in values) + text.Append(BitConverter.DoubleToInt64Bits(value).ToString(System.Globalization.CultureInfo.InvariantCulture)).Append(','); + text.Append(';'); + } + } + private static bool Bits(double a, double b) => BitConverter.DoubleToInt64Bits(a) == BitConverter.DoubleToInt64Bits(b); @@ -226,7 +273,7 @@ public sealed class PlateCuttingState bool DrawingPinsOff); private sealed record DrawingRecord(Program Program, Program Copy, bool IsCutOff, - List Pins, string PinsFingerprint); + List Pins, string PinsFingerprint, string LabelInputs); private sealed record CommonLineRecord(CommonLineGroup Group, double Kerf, Part[] Members); diff --git a/OpenNest.Engine/CuttingPlanning/CuttingPlanService.cs b/OpenNest.Engine/CuttingPlanning/CuttingPlanService.cs index 19ac4221..ec6d656a 100644 --- a/OpenNest.Engine/CuttingPlanning/CuttingPlanService.cs +++ b/OpenNest.Engine/CuttingPlanning/CuttingPlanService.cs @@ -35,7 +35,8 @@ public static class CuttingPlanService if (request?.Plate != null) { // Exact freshness record first, on the caller thread; the planned list must be it. - plateState = PlateCuttingState.Capture(request.Plate, token); + plateState = PlateCuttingState.Capture(request.Plate, token, + labels: request.ConfirmedParameters?.LabelMode is { } mode && mode != PartLabelMode.Off); if (request.Parts == null || !request.Parts.SequenceEqual(plateState.Order, ReferenceEqualityComparer.Instance)) throw new ArgumentException("The plate's parts changed after the request was created."); if (request.Parts.Count == 0 && request.ExpansionBudget > 0 && request.MaxEntries > 0) diff --git a/OpenNest.Tests/Labels/PartLabelTests.cs b/OpenNest.Tests/Labels/PartLabelTests.cs index 98d1fea4..6c9c3e61 100644 --- a/OpenNest.Tests/Labels/PartLabelTests.cs +++ b/OpenNest.Tests/Labels/PartLabelTests.cs @@ -334,6 +334,42 @@ public class PartLabelTests Assert.NotEqual(ScribeOf(a).Count, ScribeOf(b).Count); } + // The etched text and its place come from the drawing's name, box and bends and the plate's + // grain, so changing any of them after planning makes the plan stale, but only when labels are on. + [Theory] + [InlineData("name")] + [InlineData("box")] + [InlineData("boxInPlace")] + [InlineData("bends")] + [InlineData("bendInPlace")] + [InlineData("offset")] + [InlineData("grain")] + public void Apply_LabelInputChangedAfterPlanning_IsStaleOnlyWhenLabelsAreOn(string change) + { + foreach (var mode in new[] { PartLabelMode.PartNumberAndBendGroups, PartLabelMode.Off }) + { + var drawing = Panel("7", BendAt(Quarter)); + var (plate, _) = PlateOf(drawing, Parameters(mode)); + var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, Vector.Zero, + confirmedParameters: Parameters(mode))); + Assert.Equal(CuttingPlanStatus.Ready, result.Status); + + switch (change) + { + case "name": drawing.Name = "8"; break; + case "box": drawing.LabelBox = new LabelBox(new Vector(6, 5), 3.5, 0.5, 0); break; + case "boxInPlace": drawing.LabelBox.Width = 3; break; + case "bends": drawing.Bends.Add(BendAt(0)); break; + case "bendInPlace": drawing.Bends[0].EndPoint = new Vector(6, 2); break; + case "offset": drawing.Source.Offset = new Vector(0.5, 0); break; + case "grain": plate.GrainAngle = Quarter; break; + } + + Assert.Equal(mode == PartLabelMode.Off ? CuttingCommitStatus.Applied : CuttingCommitStatus.Stale, + CuttingPlanService.Apply([result]).Status); + } + } + // ---- the option ---- [Fact] diff --git a/docs/cutting-planner.md b/docs/cutting-planner.md index 3b5da8c6..3fb6c05a 100644 --- a/docs/cutting-planner.md +++ b/docs/cutting-planner.md @@ -678,7 +678,9 @@ replans. Each plate is compared exactly with the state captured with its request part list instance and order, plate quantity/size/quadrant and settings, cutoff definitions, and for every part its program reference and exact content (an in-place edit counts), drawing program, cutoff classification and cut pins, pose bits, -lead-in/lock/pins-off flags, settings (reference and exact content) and bounds. Any +lead-in/lock/pins-off flags, settings (reference and exact content) and bounds. When the +confirmed settings etch part labels, it also compares each drawing's name, label box, bends +and source offset and the plate's grain, which the etched text is made from. Any difference on any plate returns `Stale` and changes nothing, so a proposal that changes a plate can be applied once; an unchanged (no-op) proposal stays current. A malformed live program is also `Stale`, not an exception. A part repeated on diff --git a/docs/label-box.md b/docs/label-box.md index 2f20d25f..41e407e5 100644 --- a/docs/label-box.md +++ b/docs/label-box.md @@ -63,7 +63,9 @@ plans from, so the label is emitted as scribe (etch) moves like any other mark: counts in cut time, posts through the etch path and never enters nesting or collision geometry (marks are not material). Nothing changes while the option is off, and parts planned before it was turned on keep no label until planned again. The plan fingerprint records the mode, so changing it -makes an earlier plan stale. +makes an earlier plan stale. While labels are on, so does changing a drawing's name, label box, +bends or source offset, or the plate's grain, between planning and Apply. Once applied, the etched +label stays as planned until the plate is planned again. Settings saved before the mode existed held an on/off `etchPartLabels` value (in the nest's cutting parameters, the desktop settings and a machine thickness's cutting settings). It meant the full