fix(labels): etch labels in place on imported drawings and keep them out of drawings

Imported drawings are incremental programs, but PartLabels.AddTo appended
absolute points, so each stroke was read as a step from the last and the label
ran far off the sheet. It now writes steps from where the program stops
(ConvertMode.EndPosition).

On load, DrawingMarkRecovery took the etched label from applied part programs
back into the drawing as a lost mark, so it was etched again by every later
plan, labels off included. Drawings with a label box are now skipped.

Also load a labelMode number that names no mode as if it were absent, and show
such a mode as Off in the Cutting Parameters dropdown instead of throwing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-22 18:56:57 -05:00
1 parent fbf155adf3
commit 3108fdb64a
10 files changed
+123 -10

No files matched your search

+6
View File
@@ -62,6 +62,12 @@ namespace OpenNest.Converters
}
}
/// <summary>
/// Where the tool stands after running <paramref name="pgm"/> from its own origin, in
/// its own frame, whatever its mode; sub-program calls included.
/// </summary>
public static Vector EndPosition(Program pgm) => ComputeEndPosition(pgm, new Vector(0, 0));
/// <summary>
/// Computes the tool position after executing <paramref name="pgm"/>,
/// given that the program's frame origin is at <paramref name="startPos"/>
@@ -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.
/// </summary>
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))
+19 -2
View File
@@ -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 <see cref="Strokes"/> has nothing to etch.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
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;
+5 -2
View File
@@ -46,10 +46,13 @@ public class ThicknessCuttingConfig
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool EtchPartLabels { get; set; }
/// <summary><see cref="LabelMode"/>, or the full label when only the old on/off option is set.</summary>
/// <summary>
/// <see cref="LabelMode"/>, 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.
/// </summary>
[JsonIgnore]
public PartLabelMode ResolvedLabelMode =>
LabelMode != PartLabelMode.Off ? LabelMode
LabelMode != PartLabelMode.Off && System.Enum.IsDefined(LabelMode) ? LabelMode
: EtchPartLabels ? PartLabelMode.PartNumberAndBendGroups
: PartLabelMode.Off;
+5 -2
View File
@@ -49,10 +49,13 @@ public class CuttingParametersDto
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public bool EtchPartLabels { get; set; }
/// <summary><see cref="LabelMode"/>, or the full label when only the old on/off option is set.</summary>
/// <summary>
/// <see cref="LabelMode"/>, 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.
/// </summary>
[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();
@@ -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));
}
/// <summary>
/// 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.
+47 -2
View File
@@ -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<Motion>().Select(m => m.EndPoint).ToList();
var actual = incremental.Codes.Skip(before).OfType<Motion>().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<ThicknessCuttingConfig>("{\"LabelMode\":7}");
Assert.Equal(PartLabelMode.Off, MachineCuttingDefaults.CuttingOf(config).LabelMode);
}
}
@@ -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()
{
+2 -1
View File
@@ -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;
+6
View File
@@ -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).