fix(display): label each cut-off segment at its own cut

Every segment of a cut-off is its own part of the one cut-off drawing, but
its sequence label was placed from the drawing's whole program, so all of a
cut-off's segments stacked their numbers on one point. The view culls parts
outside the visible area, so zooming changed which segment drew last and the
shown number appeared to change. Each segment is now labelled at the middle
of its own cutting moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-10-26 09:46:41 -04:00
1 parent 96d8a6874f
commit 7f1c5a3854
2 files changed
+98 -9

No files matched your search

@@ -0,0 +1,52 @@
using System.Drawing.Drawing2D;
using OpenNest.Controls;
using OpenNest.Geometry;
namespace OpenNest.WinForms.Tests.Controls;
/// <summary>
/// Each segment of a cut-off is its own part of the one cut-off drawing. Its sequence label must
/// sit on its own cut: labelled from the whole drawing, every segment stacked its number on one
/// point, and the view's culling decided which one showed as the user zoomed.
/// </summary>
public class CutOffSegmentLabelTests
{
[Fact]
public void EachSegmentIsLabelledAtTheMiddleOfItsOwnCut() => StaTestThread.Run(() =>
{
using var view = new PlateView(ColorSchemeRegistry.Get("Workshop"));
view.Matrix = new Matrix();
view.Plate.Size = new OpenNest.Geometry.Size(100, 100);
view.Plate.Quadrant = 1;
view.Plate.Parts.Add(new Part(new Drawing("Block", Square(20)), new Vector(40, 40)));
view.Plate.CutOffs.Add(new CutOff(new Vector(50, 0), CutOffAxis.Vertical));
view.Plate.RegenerateCutOffs(new CutOffSettings());
var segments = view.Plate.Parts.Where(p => p.BaseDrawing.IsCutOff).ToList();
Assert.Equal(2, segments.Count);
var labels = new List<float>();
foreach (var segment in segments)
{
var layout = LayoutPart.Create(segment, view);
var extent = CutOffSegments.Extent(segment, vertical: true)!.Value;
Assert.Equal(50, layout.LabelScreenPoint.X, 3);
Assert.Equal((extent.Start + extent.End) / 2, layout.LabelScreenPoint.Y, 3);
labels.Add(layout.LabelScreenPoint.Y);
layout.ReleaseDisplayResources();
}
Assert.NotEqual(labels[0], labels[1]);
}, TimeSpan.FromMinutes(1), "Cut-off segment label test timed out.");
private static OpenNest.CNC.Program Square(double size)
{
var program = new OpenNest.CNC.Program();
program.MoveTo(0, 0);
program.LineTo(size, 0);
program.LineTo(size, size);
program.LineTo(0, size);
program.LineTo(0, 0);
return program;
}
}
+46 -9
View File
@@ -36,6 +36,8 @@ namespace OpenNest
private Vector? _labelPoint;
private PointF _labelScreenPoint;
internal PointF LabelScreenPoint => _labelScreenPoint;
// Paths in the drawing's program frame, before placement. A zoom or a move only maps
// these to the screen again; rebuilding them from the program dominates for parts with
// hundreds of holes. They are rebuilt when the program, its rotation or its lead-ins change.
@@ -360,22 +362,57 @@ namespace OpenNest
// _labelPoint is computed from BaseDrawing.Program's current geometry, which already
// carries BaseDrawing.Program.Rotation (nonzero for canonical-frame drawings, e.g. in
// BestFitViewerForm). BasePart.Rotation is cumulative from that same baseline, so it
// must be re-applied net of the baseline already baked into _labelPoint — otherwise
// must be re-applied net of the baseline already baked into _labelPoint — otherwise
// the baseline rotation is counted twice. Mirrors CanonicalFrame.RebindToOriginal.
_labelPoint ??= ComputeLabelPoint();
var baseRotation = BasePart.BaseDrawing.Program.Rotation;
var rotatedLabel = _labelPoint.Value.Rotate(BasePart.Rotation - baseRotation);
var labelPt = new PointF(
(float)(rotatedLabel.X + BasePart.Location.X),
(float)(rotatedLabel.Y + BasePart.Location.Y)
);
var pts = new[] { labelPt };
var label = CutOffSegmentLabelPoint() ?? PlacedLabelPoint();
var pts = new[] { new PointF((float)label.X, (float)label.Y) };
plateView.Matrix.TransformPoints(pts);
_labelScreenPoint = pts[0];
IsDirty = false;
}
private Vector PlacedLabelPoint()
{
_labelPoint ??= ComputeLabelPoint();
var baseRotation = BasePart.BaseDrawing.Program.Rotation;
return _labelPoint.Value.Rotate(BasePart.Rotation - baseRotation) + BasePart.Location;
}
// Every segment of a cut-off is a part of the one cut-off drawing with its own trimmed
// program. Labelled from the drawing, all of them would stack on one point and the view's
// culling would decide which number shows; label each at the middle of its own cut.
private Vector? CutOffSegmentLabelPoint()
{
var program = BasePart.Program;
if (!BasePart.BaseDrawing.IsCutOff || program?.Mode != CNC.Mode.Absolute)
return null;
double minX = double.MaxValue, minY = double.MaxValue;
double maxX = double.MinValue, maxY = double.MinValue;
Vector? position = null;
foreach (var code in program.Codes)
{
if (code is not CNC.Motion motion)
continue;
if (motion is not CNC.RapidMove && position is { } start)
{
foreach (var point in new[] { start, motion.EndPoint })
{
minX = System.Math.Min(minX, point.X);
minY = System.Math.Min(minY, point.Y);
maxX = System.Math.Max(maxX, point.X);
maxY = System.Math.Max(maxY, point.Y);
}
}
position = motion.EndPoint;
}
if (minX > maxX)
return null;
return new Vector((minX + maxX) / 2, (minY + maxY) / 2) + BasePart.Location;
}
public void UpdateOffset(double spacing, double tolerance, Matrix matrix)
{
if (