From 7634c95fc7afcffce5f388d82a016766e035e1f2 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 23 Nov 2026 17:52:48 -0500 Subject: [PATCH] feat(labels): find the label box for half-inch text, stepping down to fit Quarter-inch etching is hard for the shop to read, and most of the crew would rather have half-inch text. Find Best Location now tries half-inch, then three-eighths, then quarter-inch text (12, 9, 6 mm) and keeps the largest that fits, so big parts get readable labels and small parts still get one. Only when even the quarter-inch label fits nowhere is a box placed for hand adjustment, at that smallest size. Boxes already placed keep their size until found again. Co-Authored-By: Claude Opus 5.5 --- OpenNest.Core/Labels/LabelBoxFinder.cs | 19 +++++++++++---- OpenNest.Tests/Labels/LabelBoxFinderTests.cs | 7 ++++++ .../Forms/CadConverterLabelBoxTests.cs | 24 +++++++++++++++++-- OpenNest/Forms/CadConverterForm.LabelBox.cs | 13 ++++++---- docs/label-box.md | 20 +++++++++------- 5 files changed, 65 insertions(+), 18 deletions(-) diff --git a/OpenNest.Core/Labels/LabelBoxFinder.cs b/OpenNest.Core/Labels/LabelBoxFinder.cs index 17bbdd49..35dc0bf2 100644 --- a/OpenNest.Core/Labels/LabelBoxFinder.cs +++ b/OpenNest.Core/Labels/LabelBoxFinder.cs @@ -42,10 +42,21 @@ namespace OpenNest.Labels Clearance = clearance, }; - /// A quarter inch (6 mm) label of characters, an eighth inch (3 mm) from anything else. - public static LabelBoxOptions Default(Units units) => units == Units.Millimeters - ? ForText(DefaultCharacters, 6, 3) - : ForText(DefaultCharacters, 0.25, 0.125); + /// + /// The label sizes to try, largest first: characters of + /// half inch, three eighths and quarter inch text (12, 9 and 6 mm), each an eighth inch + /// (3 mm) from anything else. Big text reads from farther off; smaller parts take the + /// largest that fits. + /// + public static IReadOnlyList Sizes(Units units) => units == Units.Millimeters + ? [ForText(DefaultCharacters, 12, 3), ForText(DefaultCharacters, 9, 3), ForText(DefaultCharacters, 6, 3)] + : [ForText(DefaultCharacters, 0.5, 0.125), ForText(DefaultCharacters, 0.375, 0.125), ForText(DefaultCharacters, 0.25, 0.125)]; + + /// The largest of : a half inch (12 mm) label. + public static LabelBoxOptions Default(Units units) => Sizes(units)[0]; + + /// The smallest of : a quarter inch (6 mm) label. + public static LabelBoxOptions Smallest(Units units) => Sizes(units)[^1]; } /// diff --git a/OpenNest.Tests/Labels/LabelBoxFinderTests.cs b/OpenNest.Tests/Labels/LabelBoxFinderTests.cs index cc2721a6..2e93bf43 100644 --- a/OpenNest.Tests/Labels/LabelBoxFinderTests.cs +++ b/OpenNest.Tests/Labels/LabelBoxFinderTests.cs @@ -229,6 +229,13 @@ public class LabelBoxFinderTests Assert.Equal(0.25, options.Height, 9); Assert.Equal(0.125, options.Clearance, 9); Assert.True(LabelBoxOptions.Default(Units.Millimeters).Height > LabelBoxOptions.Default(Units.Inches).Height); + + var sizes = LabelBoxOptions.Sizes(Units.Inches); + Assert.Equal([0.5, 0.375, 0.25], sizes.Select(size => size.Height)); + Assert.Equal(0.5, LabelBoxOptions.Default(Units.Inches).Height, 9); + Assert.Equal(0.25, LabelBoxOptions.Smallest(Units.Inches).Height, 9); + Assert.All(sizes, size => Assert.Equal(0.125, size.Clearance, 9)); + Assert.Equal([12.0, 9, 6], LabelBoxOptions.Sizes(Units.Millimeters).Select(size => size.Height)); } [Fact] diff --git a/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs b/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs index 8f140143..81eb37b9 100644 --- a/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs +++ b/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs @@ -72,8 +72,28 @@ public class CadConverterLabelBoxTests form.Close(); }); + [Theory] + [InlineData(10, 6, 0.5)] + [InlineData(2.6, 0.7, 0.375)] + [InlineData(2.2, 0.6, 0.25)] + public void Finding_TakesTheLargestTextThatFits(double width, double height, double textHeight) => + ArchUnitsTests.RunSta(() => + { + // Half inch text needs about 3.25 x 0.75 of flat part, three eighths 2.5 x 0.625. + using var form = new LabelTestForm(); + Show(form); + form.LoadDrawings([Part(width, height)]); + var item = Field(form, "fileList").SelectedItem!; + + Invoke(form, "OnLabelBoxClick", null, EventArgs.Empty); + + Assert.Equal(textHeight, item.LabelBox!.Height, 6); + Assert.Empty(form.Messages); + form.Close(); + }); + [Fact] - public void NoRoom_PlacesAFullSizeBoxOnTheRoomiestSpotToAdjustByHand() => ArchUnitsTests.RunSta(() => + public void NoRoom_PlacesASmallestSizeBoxOnTheRoomiestSpotToAdjustByHand() => ArchUnitsTests.RunSta(() => { using var form = new LabelTestForm(); Show(form); @@ -84,7 +104,7 @@ public class CadConverterLabelBoxTests Invoke(form, "OnLabelBoxClick", null, EventArgs.Empty); var box = item.LabelBox!; - var full = LabelBoxOptions.Default(Units.Inches); + var full = LabelBoxOptions.Smallest(Units.Inches); Assert.Equal(full.Width, box.Width, 6); Assert.Equal(full.Height, box.Height, 6); Assert.Equal(0.5, box.Center.X, 3); diff --git a/OpenNest/Forms/CadConverterForm.LabelBox.cs b/OpenNest/Forms/CadConverterForm.LabelBox.cs index b06d25de..3215189c 100644 --- a/OpenNest/Forms/CadConverterForm.LabelBox.cs +++ b/OpenNest/Forms/CadConverterForm.LabelBox.cs @@ -55,8 +55,9 @@ namespace OpenNest.Forms private static readonly double[] FallbackLabelScales = { 0.75, 0.5, 0.25 }; /// - /// Places the label box on the best flat area of the part as it will be cut. When the label - /// does not fit, a box is still placed for the user to adjust by hand: the full-size box + /// Places the label box on the best flat area of the part as it will be cut, at the largest + /// text size that fits (). When even the smallest label + /// does not fit, a box is still placed for the user to adjust by hand: the smallest-size box /// centered on the roomiest flat spot a smaller label fits, else on the middle of the part, /// and the user is told so. /// @@ -70,9 +71,13 @@ namespace OpenNest.Forms .Select(l => (System.Collections.Generic.IEnumerable)l.Scrap) .ToList(); + // The largest label size that fits; only when even the smallest does not is a box + // placed for hand adjustment, at that smallest size. var entities = DimensionedEntities(item); - var options = LabelBoxOptions.Default(DrawingUnits); - var box = LabelBoxFinder.Find(entities, item.Bends, options, scrap); + var sizes = LabelBoxOptions.Sizes(DrawingUnits); + var box = sizes.Select(size => LabelBoxFinder.Find(entities, item.Bends, size, scrap)) + .FirstOrDefault(found => found != null); + var options = sizes[^1]; if (box != null) { item.LabelBox = box; diff --git a/docs/label-box.md b/docs/label-box.md index ad31a1aa..29b46248 100644 --- a/docs/label-box.md +++ b/docs/label-box.md @@ -5,7 +5,10 @@ etched, found on flat material clear of everything else. The CAD Converter store placement; Plan Cutting etches the text when **Etch part label** is not Off (see [Etching the label](#etching-the-label)). -In the CAD Converter, **Label Box...** (bottom bar) finds the best spot for the shown part. +In the CAD Converter, **Label Box...** (bottom bar) finds the best spot for the shown part, for +the largest text that fits: half inch text if the part has room for it, so the shop can read it +from farther off, else three eighths, else a quarter inch (12, 9 or 6 mm). The box height is the +text height, so a box can also be resized by hand to change it. The box is drawn as a dashed rectangle. Drag its body to move it, a corner to resize it (the opposite corner stays put) and the round handle above its top side to turn it about its center, in 5° steps or freely with Shift held. Once a box exists the button offers **Find Best @@ -22,9 +25,9 @@ leaves it under half the box height, the box turns red, the shrunk text is drawn **Too small to etch** shows under it, because Plan Cutting would etch nothing there. To run the text up the part, turn the box first (**Rotate 90°**) and then resize it. -When the label fits nowhere, the button still places a full-size box for hand placement and -says so: centered on the roomiest flat spot a smaller label (three quarters, half, then a -quarter of its size) fits clear of everything, at that spot's angle, or in the middle of the +When even the quarter inch label fits nowhere, the button still places a quarter inch box for +hand placement and says so: centered on the roomiest flat spot a smaller label (three quarters, +half, then a quarter of its size) fits clear of everything, at that spot's angle, or in the middle of the part's bounds when even the smallest does not fit. The box may then overlap edges or bend lines until it is moved or resized. @@ -38,10 +41,11 @@ nesting geometry, cut time or posts, and it does not appear in DXF, DWG or SVG e converter's entities. Null when none is set. - `LabelBoxFinder` (`OpenNest.Core/Labels`) is a pure, deterministic function of the part as it will be cut (visible, roles, breakoffs and tabbed cutouts applied, as the Dimensions - code reads it), its bends and a `LabelBoxOptions`. The converter uses - `LabelBoxOptions.Default(units)`: eight characters (room for a four-digit part number and two - circled bend group letters, `1234 AB`) of a quarter inch (6 mm) stroke font, an eighth inch - (3 mm) clear of anything else. + code reads it), its bends and a `LabelBoxOptions`. The converter tries + `LabelBoxOptions.Sizes(units)` largest first and keeps the first box found: eight characters + (room for a four-digit part number and two circled bend group letters, `1234 AB`) of a half + inch, three eighths and quarter inch (12, 9 and 6 mm) stroke font, each an eighth inch (3 mm) + clear of anything else. Boxes placed before keep their size until found again. - Material is what nesting reads (`NestingMaterial.ForNesting`, marks excluded), so a tabbed cutout's slug counts as part and etch, scribe and break cuts do not. The material is eroded by the clearance with the same Clipper inward offset the pierce search uses, a band