mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-26 05:27:39 -05:00
feat(labels): etch each bend group letter inside a circle
A dash and letter after the part number (4980A01PT09-A) reads like a revision, and many part numbers already end that way, so the shop could not tell where the number stops and the bend group starts. Each group letter is now etched at 0.6 of the text height inside its own circle the text height across, clear of the name: 1234 (A)(B). One shape for every group keeps the shape itself from seeming to mean something. The circles take more room than "-AB", so the default label box grows from seven characters to eight to keep a four-digit number and two groups at full height. The gusset finder test is pinned to the seven-character size it was written for. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
93fa24a9d7
commit
75f7444aab
7 files changed
+151
-33
No files matched your search
@@ -9,7 +9,7 @@ namespace OpenNest.CNC.CuttingStrategy
|
||||
/// <summary>The drawing name only, for example <c>1234</c>.</summary>
|
||||
PartNumber,
|
||||
|
||||
/// <summary>The drawing name and the bend groups of its bends, for example <c>1234-AB</c>.</summary>
|
||||
/// <summary>The drawing name and the bend groups of its bends, each letter etched in a circle, for example <c>1234 AB</c>.</summary>
|
||||
PartNumberAndBendGroups,
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,11 @@ namespace OpenNest.Labels
|
||||
/// <summary>What <see cref="LabelBoxFinder"/> must fit, in the drawing's units.</summary>
|
||||
public sealed class LabelBoxOptions
|
||||
{
|
||||
/// <summary>Characters in the longest label: a part number and a few bend group letters ("1234-AB").</summary>
|
||||
public const int DefaultCharacters = 7;
|
||||
/// <summary>
|
||||
/// Characters in the longest label: a four-digit part number and two circled bend group
|
||||
/// letters ("1234 AB"), which take the room of eight.
|
||||
/// </summary>
|
||||
public const int DefaultCharacters = 8;
|
||||
|
||||
/// <summary>Etch stroke advance as a share of the character height.</summary>
|
||||
public const double CharacterAdvance = 0.75;
|
||||
|
||||
@@ -28,6 +28,19 @@ namespace OpenNest.Labels
|
||||
/// <summary>The smallest share of the box height the text may be shrunk to; a longer name is not etched.</summary>
|
||||
public const double MinimumHeightShare = 0.5;
|
||||
|
||||
/// <summary>
|
||||
/// Separates the name from the bend groups in a label's text (<c>1234 AB</c>). Names never
|
||||
/// hold one, so <see cref="Layout"/> circles every letter after it.
|
||||
/// </summary>
|
||||
public const char GroupSeparator = ' ';
|
||||
|
||||
// Shares of the character height: the gap from the name to the first circle, the gap
|
||||
// between circles, and the height of a letter inside its circle (the circle's diameter is
|
||||
// the character height).
|
||||
private const double GroupGap = 0.375;
|
||||
private const double CircleGap = 0.125;
|
||||
private const double CircledLetterShare = 0.6;
|
||||
|
||||
// Letter per bucket, indexed by BendGrain.Bucket: with the grain B, across it A, the rest C on.
|
||||
private static readonly char[] BucketLetters = BuildBucketLetters();
|
||||
|
||||
@@ -68,8 +81,9 @@ namespace OpenNest.Labels
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The label text for a drawing placed at <paramref name="partRotation"/>: its name, then a
|
||||
/// dash and its bend groups when it has bends, for example <c>1234-AB</c>. Characters the
|
||||
/// The label text for a drawing placed at <paramref name="partRotation"/>: its name, then
|
||||
/// <see cref="GroupSeparator"/> and its bend groups when it has bends, for example
|
||||
/// <c>1234 AB</c>; <see cref="Layout"/> etches each group letter in a circle. Characters the
|
||||
/// stroke font lacks are dropped from the name. Empty when nothing is left to etch.
|
||||
/// With <see cref="PartLabelMode.PartNumber"/> it is the name alone, and with
|
||||
/// <see cref="PartLabelMode.Off"/> it is empty.
|
||||
@@ -106,11 +120,61 @@ namespace OpenNest.Labels
|
||||
}
|
||||
|
||||
private static string Join(string name, string groups) =>
|
||||
groups.Length == 0 ? name : name.Length == 0 ? groups : name + "-" + groups;
|
||||
groups.Length == 0 ? name : name + GroupSeparator + groups;
|
||||
|
||||
/// <summary>
|
||||
/// The length of a label's <paramref name="text"/> at a character height of 1: the name,
|
||||
/// then a gap and a circle per bend group, the circles one character height across.
|
||||
/// </summary>
|
||||
public static double Measure(string text)
|
||||
{
|
||||
var (name, groups) = Split(text);
|
||||
if (groups.Length == 0)
|
||||
return StrokeFont.Measure(name);
|
||||
return StrokeFont.Measure(name) + (name.Length == 0 ? 0 : GroupGap)
|
||||
+ groups.Length + (groups.Length - 1) * CircleGap;
|
||||
}
|
||||
|
||||
private static (string Name, string Groups) Split(string text)
|
||||
{
|
||||
text ??= string.Empty;
|
||||
var separator = text.IndexOf(GroupSeparator);
|
||||
return separator < 0 ? (text, string.Empty) : (text[..separator], text[(separator + 1)..]);
|
||||
}
|
||||
|
||||
// The text's strokes at height, lower left at the origin: the name, then each group letter
|
||||
// centered in a circle the height of the text.
|
||||
private static List<Stroke> TextStrokes(string text, double height)
|
||||
{
|
||||
var (name, groups) = Split(text);
|
||||
var strokes = StrokeFont.Strokes(name, height);
|
||||
if (groups.Length == 0)
|
||||
return strokes;
|
||||
|
||||
var radius = height / 2;
|
||||
var letterHeight = height * CircledLetterShare;
|
||||
var left = name.Length == 0 ? 0 : (StrokeFont.Measure(name) + GroupGap) * height;
|
||||
foreach (var letter in groups)
|
||||
{
|
||||
var center = new Vector(left + radius, radius);
|
||||
// A full circle as two half arcs, since a stroke's arc is less than a full turn.
|
||||
var circle = new Stroke(new Vector(center.X + radius, center.Y));
|
||||
circle.Segments.Add(new StrokeSegment(new Vector(center.X - radius, center.Y), true, center, false));
|
||||
circle.Segments.Add(new StrokeSegment(new Vector(center.X + radius, center.Y), true, center, false));
|
||||
strokes.Add(circle);
|
||||
|
||||
var corner = new Vector(center.X - letterHeight * StrokeFont.GlyphWidth / 2, center.Y - letterHeight / 2);
|
||||
foreach (var stroke in StrokeFont.Strokes(letter.ToString(), letterHeight))
|
||||
strokes.Add(stroke.Map(point => point + corner));
|
||||
|
||||
left += height * (1 + CircleGap);
|
||||
}
|
||||
return strokes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lays <paramref name="text"/> out in <paramref name="box"/>, in the box's own frame (the
|
||||
/// drawing's source frame): centered, reading along the box at its height, or shrunk to fit
|
||||
/// drawing's source frame), each bend group letter in a circle: centered, reading along the box at its height, or shrunk to fit
|
||||
/// its width. <paramref name="fits"/> is false when the text had to shrink below
|
||||
/// <see cref="MinimumHeightShare"/> of the box height, so Plan Cutting would not etch it;
|
||||
/// the shrunk strokes are still returned, for previewing. Null for an invalid box or no text.
|
||||
@@ -122,7 +186,7 @@ namespace OpenNest.Labels
|
||||
return null;
|
||||
|
||||
var height = box.Height;
|
||||
var width = StrokeFont.Measure(text) * height;
|
||||
var width = Measure(text) * height;
|
||||
if (width > box.Width)
|
||||
{
|
||||
height *= box.Width / width;
|
||||
@@ -130,7 +194,7 @@ namespace OpenNest.Labels
|
||||
}
|
||||
|
||||
fits = height >= box.Height * MinimumHeightShare - Tolerance.Epsilon;
|
||||
return StrokeFont.Strokes(text, height).ConvertAll(stroke =>
|
||||
return TextStrokes(text, height).ConvertAll(stroke =>
|
||||
stroke.Map(point => box.ToWorld(new Vector(point.X - width / 2, point.Y - height / 2))));
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,8 @@ public class LabelBoxFinderTests
|
||||
new Line(1, 8, 0, 8), new Line(0, 8, 0, 0), new Circle(2.6, 2.6, 1.1),
|
||||
];
|
||||
|
||||
var box = LabelBoxFinder.Find(gusset, [], LabelBoxOptions.Default(Units.Inches));
|
||||
// Seven quarter-inch characters: the pocket holds them level, with room to spare.
|
||||
var box = LabelBoxFinder.Find(gusset, [], LabelBoxOptions.ForText(7, 0.25, 0.125));
|
||||
|
||||
Assert.NotNull(box);
|
||||
Assert.Equal(0, box.Rotation, 9);
|
||||
|
||||
@@ -15,7 +15,7 @@ public class LabelPreviewTests
|
||||
new() { StartPoint = new Vector(0, 2), EndPoint = new Vector(3, 2) },
|
||||
};
|
||||
|
||||
Assert.Equal("4980A01PT103-AB", PartLabels.PreviewText("4980 A01 pt103", bends));
|
||||
Assert.Equal("4980A01PT103 AB", PartLabels.PreviewText("4980 A01 pt103", bends));
|
||||
Assert.Equal("1234", PartLabels.PreviewText("1234", null));
|
||||
Assert.Equal("", PartLabels.PreviewText(null, null));
|
||||
}
|
||||
|
||||
@@ -101,8 +101,8 @@ public class PartLabelTests
|
||||
{
|
||||
Assert.Equal(LabelBoxOptions.CharacterAdvance, StrokeFont.Advance);
|
||||
|
||||
var width = StrokeFont.Measure("1234-AB") * 0.25;
|
||||
var options = LabelBoxOptions.ForText(7, 0.25, 0.125);
|
||||
var width = PartLabels.Measure("1234 AB") * 0.25;
|
||||
var options = LabelBoxOptions.ForText(LabelBoxOptions.DefaultCharacters, 0.25, 0.125);
|
||||
Assert.True(width <= options.Width + 1e-9);
|
||||
|
||||
Assert.Equal(BoundsOf(StrokeFont.Strokes("AB", 1)), BoundsOf(StrokeFont.Strokes("ab", 1)));
|
||||
@@ -164,6 +164,51 @@ public class PartLabelTests
|
||||
Assert.Equal(string.Empty, PartLabels.GroupLetters([], 0, 0));
|
||||
}
|
||||
|
||||
// ---- circled groups ----
|
||||
|
||||
[Fact]
|
||||
public void Layout_CirclesEachGroupLetter_ClearOfTheName()
|
||||
{
|
||||
var box = new LabelBox(new Vector(0, 0), 10, 1, 0);
|
||||
|
||||
var plain = PartLabels.Layout(box, "12", out _)!;
|
||||
var circled = PartLabels.Layout(box, "12 AB", out var fits)!;
|
||||
|
||||
Assert.True(fits);
|
||||
// Each group adds a circle (two half arcs) and its letter's strokes.
|
||||
var letters = StrokeFont.Strokes("A", 1).Count + StrokeFont.Strokes("B", 1).Count;
|
||||
Assert.Equal(plain.Count + 2 + letters, circled.Count);
|
||||
|
||||
var circles = circled.Where(s => s.Segments.Count == 2 && s.Segments.All(g => g.IsArc)).ToList();
|
||||
Assert.Equal(2, circles.Count);
|
||||
var nameRight = circled.Take(StrokeFont.Strokes("12", 1).Count).SelectMany(s => s.Points()).Max(p => p.X);
|
||||
foreach (var circle in circles)
|
||||
{
|
||||
var center = circle.Segments[0].Center;
|
||||
Assert.Equal(0.5, center.DistanceTo(circle.Start), 9);
|
||||
Assert.Equal(0, center.Y, 9);
|
||||
Assert.True(center.X - 0.5 > nameRight, "A circle overlaps the name.");
|
||||
}
|
||||
|
||||
// Every letter sits inside its circle.
|
||||
var first = circles[0].Segments[0].Center;
|
||||
var second = circles[1].Segments[0].Center;
|
||||
foreach (var point in circled.Except(circles).SelectMany(s => s.Points()).Where(p => p.X > nameRight))
|
||||
{
|
||||
var center = System.Math.Abs(point.X - first.X) < System.Math.Abs(point.X - second.X) ? first : second;
|
||||
Assert.True(center.DistanceTo(point) < 0.5, $"{point} is outside its circle.");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Measure_IsTheNameThenAGapAndACirclePerGroup()
|
||||
{
|
||||
Assert.Equal(StrokeFont.Measure("1234"), PartLabels.Measure("1234"), 9);
|
||||
Assert.Equal(StrokeFont.Measure("1234") + 0.375 + 2 + 0.125, PartLabels.Measure("1234 AB"), 9);
|
||||
Assert.Equal(1, PartLabels.Measure(" A"), 9);
|
||||
Assert.True(PartLabels.Measure("1234 AB") <= LabelBoxOptions.DefaultCharacters * LabelBoxOptions.CharacterAdvance + 1e-9);
|
||||
}
|
||||
|
||||
// ---- text ----
|
||||
|
||||
[Fact]
|
||||
@@ -171,9 +216,9 @@ public class PartLabelTests
|
||||
{
|
||||
var drawing = Panel("1234", BendAt(0), BendAt(Quarter));
|
||||
|
||||
Assert.Equal("1234-AB", PartLabels.Text(drawing, 0, 0));
|
||||
Assert.Equal("1234 AB", PartLabels.Text(drawing, 0, 0));
|
||||
// Turned a quarter, the same bends are in the same two groups: A and B swap.
|
||||
Assert.Equal("1234-AB", PartLabels.Text(drawing, Quarter, 0));
|
||||
Assert.Equal("1234 AB", PartLabels.Text(drawing, Quarter, 0));
|
||||
Assert.Equal("1234", PartLabels.Text(Panel("1234"), 0, 0));
|
||||
}
|
||||
|
||||
@@ -183,7 +228,7 @@ public class PartLabelTests
|
||||
Assert.Equal("N26-2GT", PartLabels.Text(Panel("n26-2gt"), 0, 0));
|
||||
Assert.Equal("BRACKETA", PartLabels.Text(Panel("bracket (a)"), 0, 0));
|
||||
Assert.Equal(string.Empty, PartLabels.Text(Panel("()"), 0, 0));
|
||||
Assert.Equal("A", PartLabels.Text(Panel("()", BendAt(Quarter)), 0, 0));
|
||||
Assert.Equal(" A", PartLabels.Text(Panel("()", BendAt(Quarter)), 0, 0));
|
||||
Assert.Equal(string.Empty, PartLabels.Text(null, 0, 0));
|
||||
}
|
||||
|
||||
@@ -192,12 +237,13 @@ public class PartLabelTests
|
||||
[Fact]
|
||||
public void Strokes_AreCenteredInTheBoxAtTheBoxHeight()
|
||||
{
|
||||
// H, A and B fill their whole character cell, so the strokes' bounds are the text's.
|
||||
// H fills its whole character cell and the group circles are the text's height, so the
|
||||
// strokes' bounds are the text's.
|
||||
var drawing = Panel("HH", BendAt(0), BendAt(Quarter));
|
||||
|
||||
var bounds = BoundsOf(PartLabels.Strokes(drawing, 0, 0));
|
||||
|
||||
var width = StrokeFont.Measure("HH-AB") * 0.5;
|
||||
var width = PartLabels.Measure("HH AB") * 0.5;
|
||||
Assert.Equal(0.5, bounds.Height, 9);
|
||||
Assert.Equal(width, bounds.Width, 9);
|
||||
Assert.Equal(6, bounds.Left + bounds.Width / 2, 9);
|
||||
@@ -225,7 +271,7 @@ public class PartLabelTests
|
||||
[Fact]
|
||||
public void Strokes_ShrinkALongNameToFitTheBox_AndSkipOneThatWouldBeTooSmall()
|
||||
{
|
||||
// 3.5 fits "1234-AB" (7 characters) at the full 0.5 height; "123456789-AB" is 11.
|
||||
// 3.5 fits "1234 AB" (7 characters) at the full 0.5 height; "123456789 AB" is 11.
|
||||
var shrunk = BoundsOf(PartLabels.Strokes(Panel("123456789", BendAt(0), BendAt(Quarter)), 0, 0));
|
||||
Assert.True(shrunk.Width <= 3.5 + 1e-9);
|
||||
Assert.InRange(shrunk.Height, 0.25 - 1e-9, 0.5);
|
||||
@@ -383,7 +429,7 @@ public class PartLabelTests
|
||||
|
||||
Assert.Equal(CuttingPlanStatus.Ready, a.Status);
|
||||
Assert.Equal(CuttingPlanStatus.Ready, b.Status);
|
||||
// "7-A" and "7-B" differ in their last glyph, so their strokes differ in count.
|
||||
// Circled A and circled B differ in their letter's glyph, so their strokes differ in count.
|
||||
Assert.NotEqual(ScribeOf(a).Count, ScribeOf(b).Count);
|
||||
}
|
||||
|
||||
@@ -428,7 +474,7 @@ public class PartLabelTests
|
||||
[Fact]
|
||||
public void PlanCutting_PartNumberMode_EtchesTheNumberWithoutTheGroups()
|
||||
{
|
||||
// "7" alone is one glyph; "7-A" adds a dash and a letter, so it has more strokes.
|
||||
// "7" alone is one glyph; "7 A" adds a circle and a letter, so it has more strokes.
|
||||
var drawing = Panel("7", BendAt(Quarter));
|
||||
var strokesOf = (PartLabelMode mode) =>
|
||||
{
|
||||
@@ -455,8 +501,8 @@ public class PartLabelTests
|
||||
var drawing = Panel("1234", BendAt(0), BendAt(Quarter));
|
||||
|
||||
Assert.Equal("1234", PartLabels.Text(drawing, 0, 0, PartLabelMode.PartNumber));
|
||||
Assert.Equal("1234-AB", PartLabels.Text(drawing, 0, 0, PartLabelMode.PartNumberAndBendGroups));
|
||||
Assert.Equal("1234-AB", PartLabels.Text(drawing, 0, 0));
|
||||
Assert.Equal("1234 AB", PartLabels.Text(drawing, 0, 0, PartLabelMode.PartNumberAndBendGroups));
|
||||
Assert.Equal("1234 AB", PartLabels.Text(drawing, 0, 0));
|
||||
Assert.Equal(string.Empty, PartLabels.Text(drawing, 0, 0, PartLabelMode.Off));
|
||||
// A name the font cannot draw is nothing to etch in number mode, even with bends.
|
||||
Assert.Equal(string.Empty, PartLabels.Text(Panel("()", BendAt(Quarter)), 0, 0, PartLabelMode.PartNumber));
|
||||
|
||||
+14
-10
@@ -39,9 +39,9 @@ nesting geometry, cut time or posts, and it does not appear in DXF, DWG or SVG e
|
||||
- `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)`: seven characters (a part number and a few bend group
|
||||
letters such as `1234-AB`) of a quarter inch (6 mm) stroke font, an eighth inch (3 mm)
|
||||
clear of anything else.
|
||||
`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.
|
||||
- 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
|
||||
@@ -78,7 +78,7 @@ thickness settings). It has three choices:
|
||||
|--------|--------|---------|
|
||||
| Off | nothing | |
|
||||
| Part number | the drawing name | `1234` |
|
||||
| Part number and bend groups | the name, then a dash and the bend groups | `1234-AB` |
|
||||
| Part number and bend groups | the name, then each bend group letter in a circle | `1234 ⒶⒷ` |
|
||||
|
||||
When it is not Off, Plan Cutting adds each part's label to the clean program it
|
||||
plans from, so the label is emitted as scribe (etch) moves like any other mark: it shows in the plan,
|
||||
@@ -95,10 +95,14 @@ label, so `true` loads as **Part number and bend groups** and `false` or a missi
|
||||
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
|
||||
has bends: `1234-AB`. **Part number** mode leaves the groups out, so a part with bends still gets
|
||||
`1234`.
|
||||
- **Text.** `PartLabels.Text` is the drawing name in capitals (characters the font lacks, and
|
||||
spaces, are dropped), then, in **Part number and bend groups** mode, a space
|
||||
(`PartLabels.GroupSeparator`) and the part's bend groups when it has bends: `1234 AB`. Layout
|
||||
etches each letter after the space inside its own circle, so a group never reads as part of the
|
||||
part number (a trailing `-A` looks like a revision). The circle is the text height across, the
|
||||
letter inside it 0.6 of that, three eighths of the text height clear of the name and an eighth
|
||||
apart (`PartLabels.Measure`). **Part number** mode leaves the groups out, so a part with bends
|
||||
still gets `1234`.
|
||||
- **Bend groups.** A bend's group is its line's direction against the sheet grain in 15° steps
|
||||
(`BendGrain.Bucket`, bend line plus the part's rotation against `Plate.GrainAngle`), named by a
|
||||
fixed letter so a group means the same thing on every part and sheet: **A** across the grain,
|
||||
@@ -119,8 +123,8 @@ A `labelMode` number that names no choice (the reader accepts numbers) loads as
|
||||
the label needs no font file. Strokes are lines and arcs: the round digits (0, 2, 3, 5, 6, 8, 9)
|
||||
are drawn on arcs and etch as arc moves; the zero keeps its slash so it never reads as the letter
|
||||
O. The CHR reader (`ChrFont`) loads vendor fonts from disk and is not used.
|
||||
- **Fit.** The text is centered in the box and reads along it at the box's height. A name longer
|
||||
than the box is shrunk to fit, to half the box height at the least; a longer one is not etched.
|
||||
- **Fit.** The text is centered in the box and reads along it at the box's height, circles
|
||||
included. A name longer than the box is shrunk to fit, to half the box height at the least; a longer one is not etched.
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user