Files
OpenNest/OpenNest.Core/Labels/LabelBox.cs
T
ajandClaude Sonnet 5.5 6b37953be4 feat(cad-converter): find and show a part number label box
Add LabelBoxFinder (Core): erodes the cut material by an etch clearance,
cuts bands out around bend lines and breakoff scrap, then centers a
label-sized box in the roomiest empty rectangle over a few orientations
(axes, bend and edge directions). Placement is stored on
Drawing.LabelBox in the source frame, persisted in the .nest file and
carried through BuildDrawing/ApplyEdit; it never affects the program.

The CAD Converter gets a Label Box... button that finds the best spot,
draws it as a dashed rectangle, lets the user drag and resize it, and
warns when nothing fits. Etching the text is a later step.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 17:30:24 -05:00

83 lines
3.1 KiB
C#

using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Labels
{
/// <summary>
/// Where a part's number label would be etched: a rectangle on the flat face, in the drawing's
/// source frame. Only the placement is stored. It is an annotation, like a dimension: it never
/// affects the program, nesting geometry or posts.
/// </summary>
public sealed class LabelBox
{
public LabelBox()
{
}
public LabelBox(Vector center, double width, double height, double rotation)
{
Center = center;
Width = width;
Height = height;
Rotation = rotation;
}
public Vector Center { get; set; }
/// <summary>The side along the text, before <see cref="Rotation"/>.</summary>
public double Width { get; set; }
/// <summary>The side across the text, before <see cref="Rotation"/>.</summary>
public double Height { get; set; }
/// <summary>Counterclockwise turn about <see cref="Center"/>, in radians.</summary>
public double Rotation { get; set; }
/// <summary>True when the center, both sides and the rotation are finite and both sides are positive.</summary>
public bool IsValid =>
double.IsFinite(Center.X) && double.IsFinite(Center.Y)
&& double.IsFinite(Width) && Width > 0
&& double.IsFinite(Height) && Height > 0
&& double.IsFinite(Rotation);
public LabelBox Clone() => new(Center, Width, Height, Rotation);
/// <summary>The corners counterclockwise from the text's lower left.</summary>
public Vector[] Corners()
{
var cos = PortableMath.Cos(Rotation);
var sin = PortableMath.Sin(Rotation);
var hw = Width / 2;
var hh = Height / 2;
Vector Corner(double x, double y) => new(Center.X + x * cos - y * sin, Center.Y + x * sin + y * cos);
return new[] { Corner(-hw, -hh), Corner(hw, -hh), Corner(hw, hh), Corner(-hw, hh) };
}
/// <summary>A point in the box's own frame (x along the text, y across it), origin at the center.</summary>
public Vector ToLocal(Vector point)
{
var cos = PortableMath.Cos(Rotation);
var sin = PortableMath.Sin(Rotation);
var dx = point.X - Center.X;
var dy = point.Y - Center.Y;
return new Vector(dx * cos + dy * sin, -dx * sin + dy * cos);
}
/// <summary>The point at <paramref name="local"/> in the box's own frame.</summary>
public Vector ToWorld(Vector local)
{
var cos = PortableMath.Cos(Rotation);
var sin = PortableMath.Sin(Rotation);
return new Vector(
Center.X + local.X * cos - local.Y * sin,
Center.Y + local.X * sin + local.Y * cos);
}
public bool Contains(Vector point)
{
var local = ToLocal(point);
return System.Math.Abs(local.X) <= Width / 2 && System.Math.Abs(local.Y) <= Height / 2;
}
}
}