diff --git a/OpenNest.WinForms.Tests/Controls/CutOffSegmentLabelTests.cs b/OpenNest.WinForms.Tests/Controls/CutOffSegmentLabelTests.cs new file mode 100644 index 00000000..bc0e4cc4 --- /dev/null +++ b/OpenNest.WinForms.Tests/Controls/CutOffSegmentLabelTests.cs @@ -0,0 +1,52 @@ +using System.Drawing.Drawing2D; +using OpenNest.Controls; +using OpenNest.Geometry; + +namespace OpenNest.WinForms.Tests.Controls; + +/// +/// 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. +/// +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(); + 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; + } +} diff --git a/OpenNest/LayoutPart.cs b/OpenNest/LayoutPart.cs index 887deeb2..56470e46 100644 --- a/OpenNest/LayoutPart.cs +++ b/OpenNest/LayoutPart.cs @@ -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 (