diff --git a/OpenNest.Core/Labels/PartLabels.cs b/OpenNest.Core/Labels/PartLabels.cs
index d63e5660..0779a2e0 100644
--- a/OpenNest.Core/Labels/PartLabels.cs
+++ b/OpenNest.Core/Labels/PartLabels.cs
@@ -80,18 +80,58 @@ namespace OpenNest.Labels
if (drawing == null || mode == PartLabelMode.Off)
return string.Empty;
- var name = new StringBuilder();
- foreach (var character in drawing.Name ?? string.Empty)
- if (StrokeFont.Has(character) && !char.IsWhiteSpace(character))
- name.Append(char.ToUpperInvariant(character));
-
var groups = mode == PartLabelMode.PartNumberAndBendGroups
? GroupLetters(drawing.Bends, partRotation, grainAngle)
: string.Empty;
- if (groups.Length == 0)
- return name.ToString();
+ return Join(EtchableName(drawing.Name), groups);
+ }
- return name.Length == 0 ? groups : name + "-" + groups;
+ ///
+ /// The label as the CAD Converter previews it before the part is placed: the full
+ /// text for the part unrotated on a sheet
+ /// whose grain runs along X. It is the longest the label gets for that placement, so a box
+ /// that fits it fits the part number alone; other placements may change the letters.
+ ///
+ public static string PreviewText(string name, IEnumerable bends) =>
+ Join(EtchableName(name), GroupLetters(bends, 0, 0));
+
+ // The name in capitals with characters the stroke font lacks, and spaces, dropped.
+ private static string EtchableName(string name)
+ {
+ var etchable = new StringBuilder();
+ foreach (var character in name ?? string.Empty)
+ if (StrokeFont.Has(character) && !char.IsWhiteSpace(character))
+ etchable.Append(char.ToUpperInvariant(character));
+ return etchable.ToString();
+ }
+
+ private static string Join(string name, string groups) =>
+ groups.Length == 0 ? name : name.Length == 0 ? groups : name + "-" + groups;
+
+ ///
+ /// Lays out in , in the box's own frame (the
+ /// drawing's source frame): centered, reading along the box at its height, or shrunk to fit
+ /// its width. is false when the text had to shrink below
+ /// 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.
+ ///
+ public static List Layout(LabelBox box, string text, out bool fits)
+ {
+ fits = false;
+ if (box == null || !box.IsValid || string.IsNullOrEmpty(text))
+ return null;
+
+ var height = box.Height;
+ var width = StrokeFont.Measure(text) * height;
+ if (width > box.Width)
+ {
+ height *= box.Width / width;
+ width = box.Width;
+ }
+
+ fits = height >= box.Height * MinimumHeightShare - Tolerance.Epsilon;
+ return StrokeFont.Strokes(text, height).ConvertAll(stroke =>
+ stroke.Map(point => box.ToWorld(new Vector(point.X - width / 2, point.Y - height / 2))));
}
///
@@ -116,26 +156,17 @@ namespace OpenNest.Labels
if (text.Length == 0)
return null;
- var height = box.Height;
- var width = StrokeFont.Measure(text) * height;
- if (width > box.Width)
- {
- height *= box.Width / width;
- width = box.Width;
- }
-
- if (height < box.Height * MinimumHeightShare - Tolerance.Epsilon)
+ var laidOut = Layout(box, text, out var fits);
+ if (!fits)
return null;
var offset = drawing.Source?.Offset ?? Vector.Zero;
// The source frame loses the program's offset, then turns with the part, as bends do.
- var strokes = StrokeFont.Strokes(text, height).ConvertAll(stroke => stroke.Map(point =>
+ return laidOut.ConvertAll(stroke => stroke.Map(point =>
{
- var world = box.ToWorld(new Vector(point.X - width / 2, point.Y - height / 2)) - offset;
+ var world = point - offset;
return partRotation == 0 ? world : world.Rotate(partRotation);
}));
-
- return strokes;
}
///
diff --git a/OpenNest.Tests/Labels/LabelPreviewTests.cs b/OpenNest.Tests/Labels/LabelPreviewTests.cs
new file mode 100644
index 00000000..d663f38d
--- /dev/null
+++ b/OpenNest.Tests/Labels/LabelPreviewTests.cs
@@ -0,0 +1,63 @@
+using OpenNest.Bending;
+using OpenNest.Geometry;
+using OpenNest.Labels;
+
+namespace OpenNest.Tests.Labels;
+
+public class LabelPreviewTests
+{
+ [Fact]
+ public void PreviewText_IsTheFullLabelForTheUnrotatedPart()
+ {
+ var bends = new List
+ {
+ new() { StartPoint = new Vector(1, 0), EndPoint = new Vector(1, 1) },
+ new() { StartPoint = new Vector(0, 2), EndPoint = new Vector(3, 2) },
+ };
+
+ Assert.Equal("4980A01PT103-AB", PartLabels.PreviewText("4980 A01 pt103", bends));
+ Assert.Equal("1234", PartLabels.PreviewText("1234", null));
+ Assert.Equal("", PartLabels.PreviewText(null, null));
+ }
+
+ [Fact]
+ public void Layout_FitsTextInAWideBox_AndKeepsItInside()
+ {
+ var box = new LabelBox(new Vector(5, 5), 4, 0.5, System.Math.PI / 6);
+
+ var strokes = PartLabels.Layout(box, "1234", out var fits);
+
+ Assert.True(fits);
+ Assert.NotEmpty(strokes!);
+ foreach (var point in strokes!.SelectMany(s => s.Points()))
+ {
+ var local = box.ToLocal(point);
+ Assert.InRange(local.X, -2 - 1e-9, 2 + 1e-9);
+ Assert.InRange(local.Y, -0.25 - 1e-9, 0.25 + 1e-9);
+ }
+ }
+
+ [Fact]
+ public void Layout_InATallSkinnyBox_ShrinksTheTextAndSaysItWillNotEtch()
+ {
+ var box = new LabelBox(new Vector(0, 0), 0.25, 2, 0);
+
+ var strokes = PartLabels.Layout(box, "4980A01PT103", out var fits);
+
+ Assert.False(fits);
+ Assert.NotEmpty(strokes!);
+ var height = strokes!.SelectMany(s => s.Points()).Max(p => p.Y) - strokes.SelectMany(s => s.Points()).Min(p => p.Y);
+ Assert.True(height < 1, $"Text {height} tall was not shrunk.");
+ }
+
+ [Fact]
+ public void Layout_AgreesWithWhatPlanCuttingEtches()
+ {
+ var drawing = new Drawing("12345") { LabelBox = new LabelBox(new Vector(1, 1), 0.3, 0.25, 0) };
+
+ PartLabels.Layout(drawing.LabelBox, PartLabels.Text(drawing, 0, 0), out var fits);
+
+ Assert.False(fits);
+ Assert.Null(PartLabels.Strokes(drawing, 0, 0));
+ }
+}
diff --git a/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs b/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs
index 58ca05d7..8f140143 100644
--- a/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs
+++ b/OpenNest.WinForms.Tests/Forms/CadConverterLabelBoxTests.cs
@@ -193,6 +193,28 @@ public class CadConverterLabelBoxTests
form.Close();
});
+ [Fact]
+ public void TheBoxPreviewsTheLabelText_AndPaintsWhenTooSmallToEtch() => ArchUnitsTests.RunSta(() =>
+ {
+ using var form = new LabelTestForm();
+ Show(form);
+ form.LoadDrawings([Part(10, 6)]);
+ var view = Field(form, "entityView1");
+ var item = Field(form, "fileList").SelectedItem!;
+ Invoke(form, "OnLabelBoxClick", null, EventArgs.Empty);
+
+ Assert.Equal("LABELLED", view.LabelPreviewText!());
+
+ // Tall and skinny: the text would shrink past legibility; painting still works.
+ item.LabelBox = new LabelBox(item.LabelBox!.Center, 0.1, 2, 0);
+ Invoke(form, "RefreshLabelBox", item);
+ Assert.False(PartLabels.Layout(view.LabelBox, view.LabelPreviewText!(), out var fits) == null);
+ Assert.False(fits);
+ using var bitmap = new System.Drawing.Bitmap(view.Width, view.Height);
+ view.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, view.Width, view.Height));
+ form.Close();
+ });
+
private static Drawing Part(double width, double height, List? bends = null)
{
var entities = new List
diff --git a/OpenNest/Controls/EntityView.LabelBox.cs b/OpenNest/Controls/EntityView.LabelBox.cs
index 977633e3..0ec58470 100644
--- a/OpenNest/Controls/EntityView.LabelBox.cs
+++ b/OpenNest/Controls/EntityView.LabelBox.cs
@@ -16,6 +16,7 @@ namespace OpenNest.Controls
private const int RotateHandlePart = 4;
private static readonly double RotateSnap = Angle.ToRadians(5);
private static readonly Color LabelBoxColor = Color.FromArgb(230, 90, 200);
+ private static readonly Color LabelBoxTooSmallColor = Color.FromArgb(230, 60, 40);
// The grabbed corner (0-3, as Labels.LabelBox.Corners orders them), 4 for the rotate
// handle or -1 for the body; null when nothing is being dragged.
@@ -29,6 +30,12 @@ namespace OpenNest.Controls
///
public Labels.LabelBox LabelBox { get; set; }
+ ///
+ /// The text previewed inside , read at each paint so it follows renames
+ /// and bend edits; null or empty previews nothing.
+ ///
+ public Func LabelPreviewText { get; set; }
+
/// The label box was moved, resized or turned; the argument is its new placement.
public event EventHandler LabelBoxChanged;
@@ -114,6 +121,16 @@ namespace OpenNest.Controls
width, height, labelBoxAtPress.Rotation));
}
+ // A short note under the box's lowest corner.
+ private void DrawLabelBoxWarning(Graphics g, PointF[] corners, Color color)
+ {
+ var below = corners.OrderByDescending(p => p.Y).First();
+ var left = corners.Min(p => p.X);
+ using var font = new Font(Font.FontFamily, 8f);
+ using var textBrush = new SolidBrush(color);
+ g.DrawString("Too small to etch", font, textBrush, left, below.Y + LabelBoxHandlePixels + 2);
+ }
+
private Cursor LabelBoxCursor(int part) => part switch
{
< 0 => Cursors.SizeAll,
@@ -126,12 +143,37 @@ namespace OpenNest.Controls
if (LabelBox is not { IsValid: true } box)
return;
+ // The text as Plan Cutting would etch it. A box that would shrink it past legibility
+ // turns red and shows the shrunk text faintly, since nothing would be etched.
+ var text = LabelPreviewText?.Invoke();
+ var strokes = Labels.PartLabels.Layout(box, text, out var fits);
+ var color = strokes == null || fits ? LabelBoxColor : LabelBoxTooSmallColor;
+
var points = box.Corners().Select(PointWorldToGraph).ToArray();
- using var pen = new Pen(LabelBoxColor) { DashStyle = DashStyle.Dash };
- using var brush = new SolidBrush(Color.FromArgb(30, LabelBoxColor));
+ using var pen = new Pen(color) { DashStyle = DashStyle.Dash };
+ using var brush = new SolidBrush(Color.FromArgb(30, color));
g.FillPolygon(brush, points);
g.DrawPolygon(pen, points);
+ if (strokes != null)
+ {
+ using var textPen = new Pen(Color.FromArgb(fits ? 255 : 110, color), 1.5f)
+ {
+ StartCap = LineCap.Round,
+ EndCap = LineCap.Round,
+ LineJoin = LineJoin.Round,
+ };
+ foreach (var stroke in strokes)
+ {
+ var path = stroke.Points().Select(PointWorldToGraph).ToArray();
+ if (path.Length > 1)
+ g.DrawLines(textPen, path);
+ }
+
+ if (!fits)
+ DrawLabelBoxWarning(g, points, color);
+ }
+
using var handleBrush = new SolidBrush(LabelBoxColor);
foreach (var point in points)
{
diff --git a/OpenNest/Forms/CadConverterForm.cs b/OpenNest/Forms/CadConverterForm.cs
index 0e112485..21461598 100644
--- a/OpenNest/Forms/CadConverterForm.cs
+++ b/OpenNest/Forms/CadConverterForm.cs
@@ -16,6 +16,7 @@ using OpenNest.Dimensions;
using OpenNest.Geometry;
using OpenNest.IO;
using OpenNest.IO.Bending;
+using OpenNest.Labels;
using OpenNest.TabbedCutouts;
namespace OpenNest.Forms
@@ -72,6 +73,7 @@ namespace OpenNest.Forms
filterPanel.DimensionRemoved += OnDimensionRemoved;
entityView1.DimensionDragged += OnDimensionDragged;
entityView1.LabelBoxChanged += OnLabelBoxChanged;
+ entityView1.LabelPreviewText = () => CurrentItem is { } item ? PartLabels.PreviewText(item.Name, item.Bends) : null;
entityView1.TextConvertRequested += OnTextConvertRequested;
entityView1.EntityActionRequested += OnEntityActionRequested;
btnSplit.Click += OnSplitClicked;
diff --git a/docs/label-box.md b/docs/label-box.md
index c7655a5a..fa1f1850 100644
--- a/docs/label-box.md
+++ b/docs/label-box.md
@@ -13,6 +13,15 @@ Location** again, **Rotate 90°**, **Make Horizontal** and **Remove**. However i
angle is kept within a quarter turn either way of horizontal, so the text never reads upside
down (a half turn covers the same rectangle).
+The box previews the label inside it as Plan Cutting would etch it, laid out by the same code
+(`PartLabels.Layout`): the drawing name with its bend groups for the part unrotated on a sheet
+whose grain runs along X (`PartLabels.PreviewText`), the longest the label gets there, so a box
+that fits it also fits the part number alone. The text always reads along the box's width and
+never turns by itself: a box made tall and narrow shrinks the text to fit its width, and when that
+leaves it under half the box height, the box turns red, the shrunk text is drawn faintly and
+**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