diff --git a/OpenNest.Core/Converters/ConvertMode.cs b/OpenNest.Core/Converters/ConvertMode.cs index bd420dd8..8e8b9937 100644 --- a/OpenNest.Core/Converters/ConvertMode.cs +++ b/OpenNest.Core/Converters/ConvertMode.cs @@ -62,6 +62,12 @@ namespace OpenNest.Converters } } + /// + /// Where the tool stands after running from its own origin, in + /// its own frame, whatever its mode; sub-program calls included. + /// + public static Vector EndPosition(Program pgm) => ComputeEndPosition(pgm, new Vector(0, 0)); + /// /// Computes the tool position after executing , /// given that the program's frame origin is at diff --git a/OpenNest.Core/Converters/DrawingMarkRecovery.cs b/OpenNest.Core/Converters/DrawingMarkRecovery.cs index bcb4c417..9b14e2e3 100644 --- a/OpenNest.Core/Converters/DrawingMarkRecovery.cs +++ b/OpenNest.Core/Converters/DrawingMarkRecovery.cs @@ -19,7 +19,9 @@ public static class DrawingMarkRecovery /// For each drawing without etch marks whose saved placed programs all carry the same marks, /// adds those marks to the drawing in its own frame and refreshes the drawing's clean placed /// parts. Saved programs are never changed. Drawings whose saved programs disagree, or that - /// already have marks, are left alone. Returns the number of drawings that were given marks. + /// already have marks, are left alone. So are drawings with a label box: their placed programs + /// may carry the label Plan Cutting etches, which is made for each plan, not a mark the drawing + /// lost (docs/label-box.md). Returns the number of drawings that were given marks. /// public static int Apply(Nest nest) { @@ -42,6 +44,9 @@ public static class DrawingMarkRecovery { if (drawing?.Program == null || drawing.IsCutOff || placedParts == null) return false; + // The planner's etched label is not a lost mark: recovering it would etch it twice. + if (drawing.LabelBox != null) + return false; // An incremental call's end position is not tracked by Program.EndPoint, so only // flat drawings (which is what conversion produces) can be extended safely. if (HasMarks(drawing.Program) || drawing.Program.Codes.Any(code => code is SubProgramCall)) diff --git a/OpenNest.Core/Labels/PartLabels.cs b/OpenNest.Core/Labels/PartLabels.cs index eb781f04..c660690c 100644 --- a/OpenNest.Core/Labels/PartLabels.cs +++ b/OpenNest.Core/Labels/PartLabels.cs @@ -4,6 +4,7 @@ using System.Text; using OpenNest.Bending; using OpenNest.CNC; using OpenNest.CNC.CuttingStrategy; +using OpenNest.Converters; using OpenNest.Geometry; using OpenNest.Math; @@ -144,6 +145,11 @@ namespace OpenNest.Labels /// (etch) moves, each stroke started by a rapid. Returns false and leaves the program /// unchanged when has nothing to etch. /// + /// + /// The strokes are points in the program's frame. An incremental program (every imported + /// drawing's) takes each move as a step from the last, so the moves are written as steps + /// from where the program stops. + /// public static bool AddTo(Program program, Drawing drawing, double partRotation, double grainAngle, PartLabelMode mode = PartLabelMode.PartNumberAndBendGroups) { @@ -151,14 +157,25 @@ namespace OpenNest.Labels if (program == null || strokes == null || strokes.Count == 0) return false; + var incremental = program.Mode == Mode.Incremental; + var position = incremental ? ConvertMode.EndPosition(program) : Vector.Zero; + Vector To(Vector point) + { + if (!incremental) + return point; + var step = point - position; + position = point; + return step; + } + foreach (var stroke in strokes) { if (stroke.Count < 2) continue; - program.Codes.Add(new RapidMove(stroke[0])); + program.Codes.Add(new RapidMove(To(stroke[0]))); for (var i = 1; i < stroke.Count; i++) - program.Codes.Add(new LinearMove(stroke[i]) { Layer = LayerType.Scribe }); + program.Codes.Add(new LinearMove(To(stroke[i])) { Layer = LayerType.Scribe }); } return true; diff --git a/OpenNest.Data/ThicknessCuttingConfig.cs b/OpenNest.Data/ThicknessCuttingConfig.cs index 80b05f33..624cbb04 100644 --- a/OpenNest.Data/ThicknessCuttingConfig.cs +++ b/OpenNest.Data/ThicknessCuttingConfig.cs @@ -46,10 +46,13 @@ public class ThicknessCuttingConfig [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool EtchPartLabels { get; set; } - /// , or the full label when only the old on/off option is set. + /// + /// , or the full label when only the old on/off option is set. A number + /// that names no mode (the reader accepts numbers) loads as the old option, so as Off without it. + /// [JsonIgnore] public PartLabelMode ResolvedLabelMode => - LabelMode != PartLabelMode.Off ? LabelMode + LabelMode != PartLabelMode.Off && System.Enum.IsDefined(LabelMode) ? LabelMode : EtchPartLabels ? PartLabelMode.PartNumberAndBendGroups : PartLabelMode.Off; diff --git a/OpenNest.IO/CuttingParametersDto.cs b/OpenNest.IO/CuttingParametersDto.cs index 85cfd02f..c74b0aac 100644 --- a/OpenNest.IO/CuttingParametersDto.cs +++ b/OpenNest.IO/CuttingParametersDto.cs @@ -49,10 +49,13 @@ public class CuttingParametersDto [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] public bool EtchPartLabels { get; set; } - /// , or the full label when only the old on/off option is set. + /// + /// , or the full label when only the old on/off option is set. A number + /// that names no mode (the reader accepts numbers) loads as the old option, so as Off without it. + /// [System.Text.Json.Serialization.JsonIgnore] public PartLabelMode ResolvedLabelMode => - LabelMode != PartLabelMode.Off ? LabelMode + LabelMode != PartLabelMode.Off && System.Enum.IsDefined(LabelMode) ? LabelMode : EtchPartLabels ? PartLabelMode.PartNumberAndBendGroups : PartLabelMode.Off; public SequenceDto Sequencing { get; set; } = new(); diff --git a/OpenNest.Tests/IO/ConvertedEtchMarkTests.cs b/OpenNest.Tests/IO/ConvertedEtchMarkTests.cs index ffd8c20d..640aff36 100644 --- a/OpenNest.Tests/IO/ConvertedEtchMarkTests.cs +++ b/OpenNest.Tests/IO/ConvertedEtchMarkTests.cs @@ -126,6 +126,22 @@ public class ConvertedEtchMarkTests Assert.True(loaded.Plates[0].Parts.All(p => p.HasManualLeadIns)); } + // A drawing with a label box gets its etched label from Plan Cutting at each plan; the label + // in its applied programs must not be taken back into the drawing as a mark. + [Fact] + public void DrawingWithALabelBoxIsLeftWithoutMarks() + { + var nest = ConvertedNest(Mode.Incremental); + nest.Drawings.Single().LabelBox = new OpenNest.Labels.LabelBox(new Vector(5, 5), 2, 0.5, 0); + var saved = nest.Plates[0].Parts.Take(2).Select(p => NestWriter.GetProgramText(p.Program)).ToArray(); + + var loaded = Read(Save(nest)); + + Assert.Equal(0, MarkMoves(loaded.Drawings.Single().Program)); + Assert.Equal(saved[0], NestWriter.GetProgramText(loaded.Plates[0].Parts[0].Program)); + Assert.Equal(saved[1], NestWriter.GetProgramText(loaded.Plates[0].Parts[1].Program)); + } + /// /// A converted nest: the drawing is material only, two placements keep the full PEP cutting /// program (etch line and etch circle) at different turns, and one placement is clean. diff --git a/OpenNest.Tests/Labels/PartLabelTests.cs b/OpenNest.Tests/Labels/PartLabelTests.cs index 6c9c3e61..4cc6b1de 100644 --- a/OpenNest.Tests/Labels/PartLabelTests.cs +++ b/OpenNest.Tests/Labels/PartLabelTests.cs @@ -249,6 +249,31 @@ public class PartLabelTests Assert.Equal(before, untouched.Codes.Count); } + // Imported drawings are incremental: each move is a step from the last, so the label's + // points must be written as steps from where the program stops, not as absolute points. + [Fact] + public void AddTo_InAnIncrementalProgram_EtchesTheSamePointsAsInAnAbsoluteOne() + { + var drawing = Panel("1234", BendAt(Quarter)); + var absolute = (Program)drawing.Program.Clone(); + var incremental = (Program)drawing.Program.Clone(); + incremental.Mode = Mode.Incremental; + var before = absolute.Codes.Count; + + Assert.True(PartLabels.AddTo(absolute, drawing, Quarter, 0)); + Assert.True(PartLabels.AddTo(incremental, drawing, Quarter, 0)); + incremental.Mode = Mode.Absolute; + + var expected = absolute.Codes.Skip(before).OfType().Select(m => m.EndPoint).ToList(); + var actual = incremental.Codes.Skip(before).OfType().Select(m => m.EndPoint).ToList(); + Assert.Equal(expected.Count, actual.Count); + for (var i = 0; i < expected.Count; i++) + { + Assert.Equal(expected[i].X, actual[i].X, 9); + Assert.Equal(expected[i].Y, actual[i].Y, 9); + } + } + // ---- Plan Cutting ---- private static CuttingParameters Parameters(bool labels) => @@ -279,10 +304,14 @@ public class PartLabelTests .Where(m => !m.Rapid && m.Layer == LayerType.Scribe).ToList(); } - [Fact] - public void PlanCutting_EtchesTheLabelInsideTheBoxOnTheSheet_WhenOn() + [Theory] + [InlineData(false)] + [InlineData(true)] + public void PlanCutting_EtchesTheLabelInsideTheBoxOnTheSheet_WhenOn(bool incremental) { var drawing = Panel("1234", BendAt(0), BendAt(Quarter)); + if (incremental) + drawing.Program.Mode = Mode.Incremental; var (plate, _) = PlateOf(drawing, Parameters(true)); var result = CuttingPlanService.Plan(CuttingPlanRequest.ForPlate(plate, Vector.Zero, @@ -495,4 +524,20 @@ public class PartLabelTests Assert.Contains("\"LabelMode\":\"PartNumberAndBendGroups\"", saved); Assert.DoesNotContain("EtchPartLabels", saved); } + + // The reader accepts numbers for the mode, so a hand-edited file can name one that does not exist. + [Fact] + public void ANumberNamingNoMode_LoadsAsOff_OrAsTheOldOptionWhenItIsSet() + { + var current = CuttingParametersSerializer.Serialize(Parameters(PartLabelMode.PartNumber)); + var unknown = current.Replace("\"labelMode\":\"PartNumber\"", "\"labelMode\":7"); + Assert.Equal(PartLabelMode.Off, CuttingParametersSerializer.Deserialize(unknown).LabelMode); + var numbered = current.Replace("\"labelMode\":\"PartNumber\"", "\"labelMode\":1"); + Assert.Equal(PartLabelMode.PartNumber, CuttingParametersSerializer.Deserialize(numbered).LabelMode); + var withOld = current.Replace("\"labelMode\":\"PartNumber\"", "\"labelMode\":-1,\"etchPartLabels\":true"); + Assert.Equal(PartLabelMode.PartNumberAndBendGroups, CuttingParametersSerializer.Deserialize(withOld).LabelMode); + + var config = System.Text.Json.JsonSerializer.Deserialize("{\"LabelMode\":7}"); + Assert.Equal(PartLabelMode.Off, MachineCuttingDefaults.CuttingOf(config).LabelMode); + } } diff --git a/OpenNest.WinForms.Tests/Controls/CuttingPanelTests.cs b/OpenNest.WinForms.Tests/Controls/CuttingPanelTests.cs index 04bb4756..b75e517a 100644 --- a/OpenNest.WinForms.Tests/Controls/CuttingPanelTests.cs +++ b/OpenNest.WinForms.Tests/Controls/CuttingPanelTests.cs @@ -79,6 +79,17 @@ public class CuttingPanelTests }, TimeSpan.FromSeconds(30), "The cutting panel did not round-trip the part label option."); } + [Fact] + public void LabelMode_ThatNamesNoChoice_ShowsAsOff() + { + StaTestThread.Run(() => + { + using var panel = new CuttingPanel(); + panel.LoadFromParameters(new CuttingParameters { LabelMode = (PartLabelMode)7 }); + Assert.Equal(PartLabelMode.Off, panel.BuildParameters().LabelMode); + }, TimeSpan.FromSeconds(30), "The cutting panel did not load an unknown part label mode as Off."); + } + [Fact] public void TabEveryPart_LoadsAsTabsBySizeOverEveryWidth() { diff --git a/OpenNest/Controls/CuttingPanel.cs b/OpenNest/Controls/CuttingPanel.cs index 4372e424..74d0f9d1 100644 --- a/OpenNest/Controls/CuttingPanel.cs +++ b/OpenNest/Controls/CuttingPanel.cs @@ -422,7 +422,8 @@ namespace OpenNest.Controls tabOptions.Enabled = chkTabsEnabled.Checked; UpdateSupportTabOptions(); chkSliceSlugs.Checked = p.SliceTippingSlugs; - cboLabelMode.SelectedIndex = (int)p.LabelMode; + // A mode the list does not offer (set in code; files load it as Off) shows as Off. + cboLabelMode.SelectedIndex = System.Enum.IsDefined(p.LabelMode) ? (int)p.LabelMode : (int)PartLabelMode.Off; nudPierceClearance.Value = Clamp(nudPierceClearance, p.PierceClearance); nudPierceDiameter.Value = Clamp(nudPierceDiameter, p.PierceDiameter); chkRoundLeadInAngles.Checked = p.RoundLeadInAngles; diff --git a/docs/label-box.md b/docs/label-box.md index 41e407e5..6e4fef3d 100644 --- a/docs/label-box.md +++ b/docs/label-box.md @@ -71,6 +71,7 @@ Settings saved before the mode existed held an on/off `etchPartLabels` value (in parameters, the desktop settings and a machine thickness's cutting settings). It meant the full label, so `true` loads as **Part number and bend groups** and `false` or a missing value as **Off**; the old key is read but never written again. When both keys are present, `labelMode` wins. +A `labelMode` number that names no choice (the reader accepts numbers) loads as if it were absent. - **Text.** `PartLabels.Text` is the drawing name in capitals (characters the font lacks are dropped), then, in **Part number and bend groups** mode, a dash and the part's bend groups when it @@ -91,6 +92,11 @@ the old key is read but never written again. When both keys are present, `labelM The text turns with the part, so it can read upside down or sideways on the sheet. - **Frames.** The box and the bends are in the source frame: the program's offset (`Drawing.Source.Offset`) comes off, then the part's rotation applies, as `BendToWorld` does. + Imported drawings are incremental programs, so the strokes are written as steps from where the + program stops, not as absolute points. +- **Reopening.** The label lives only in the applied part programs. Loading a nest never takes it + back into the drawing as a lost etch mark (`DrawingMarkRecovery` skips drawings with a label box), + so it is not etched twice and turning the option off removes it at the next plan. - **What gets none.** A drawing with no box, parts whose cutting is locked or left out of the plan (the label is added only where Plan Cutting regenerates the program), and common-line members (their programs are rebuilt from their shared spans).