Files
OpenNest/OpenNest.Desktop/Interaction/MeasureAreaTool.cs
T
ajandClaude Opus 5.5 cf6a7df3be feat(desktop): share zoom window, measure area, hover tip and resize scaling
ZoomWindowTool and MeasureAreaTool port the WinForms Zoom Window and Select
Area actions onto the shared plate tool framework; PartHoverTip is the plate
view's delayed drawing-name tooltip; PlateZoom gives the Zoom To areas.
ViewTransform now scales with the viewport on resize as DrawControl does and
zooms about its center by the menu's 1.5 step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:09:17 -05:00

95 lines
3.2 KiB
C#

using System.Collections.Generic;
using OpenNest.CutOffs;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// The WinForms Select Area action behind Tools &gt; Measure Area, toolkit-neutral: as the pointer
/// moves, <see cref="Area"/> is the largest empty box around it inside the plate's work area,
/// clear of the parts (their bounding boxes grown by the part spacing) and the cut-off lines.
/// The box grows up and down first, then sideways; Space (<see cref="ToggleDirection"/>) grows
/// it sideways first instead. The tool stays active until Escape.
/// </summary>
public sealed class MeasureAreaTool : PlateTool
{
private readonly List<Box> obstacles = new();
private Box bounds;
private Vector? lastPoint;
public MeasureAreaTool(IPlateToolHost host)
: base(host)
{
Collect();
}
public override string Name => "Measure Area";
public override bool IsBusy => false;
/// <summary>Whether the box grows sideways first (Space toggles it).</summary>
public bool Horizontal { get; private set; }
/// <summary>The box measured at the pointer, or null when there is none.</summary>
public Box Area { get; private set; }
/// <summary>The box's size as WinForms labels it (its <see cref="Box.Size"/> to two decimals), or null.</summary>
public string Label => Area?.Size.ToString(2);
public override void Cancel() => Host.Invalidate();
public override bool PointerMoved(ToolPointer pointer)
{
lastPoint = pointer.World;
Update();
return true;
}
/// <summary>Space: switches which way the box grows first, at the pointer's last point.</summary>
public void ToggleDirection()
{
Horizontal = !Horizontal;
Update();
}
private void Update()
{
if (bounds == null || lastPoint is not { } point)
Area = null;
else
{
var area = Horizontal
? SpatialQuery.GetLargestBoxHorizontally(point, bounds, obstacles)
: SpatialQuery.GetLargestBoxVertically(point, bounds, obstacles);
Area = ReferenceEquals(area, Box.Empty) ? null : area;
}
Host.Invalidate();
}
// Read once when the tool starts, as WinForms: the parts and cut-offs the box must clear.
private void Collect()
{
var plate = Host.Plate;
if (plate == null)
return;
foreach (var part in plate.Parts)
{
if (part.BaseDrawing.IsCutOff)
continue;
obstacles.Add(part.BoundingBox.Offset(plate.PartSpacing));
}
// A cut-off part's bounds span from the plate origin (its program is absolute), so each
// cut-off is a thin box along its line, from its definition.
var plateBounds = plate.BoundingBox(includeParts: false);
foreach (var cutOff in plate.CutOffs)
{
var line = cutOff.Axis == CutOffAxis.Vertical
? new Box(cutOff.Position.X, plateBounds.Y, 0, plateBounds.Width)
: new Box(plateBounds.X, cutOff.Position.Y, plateBounds.Length, 0);
obstacles.Add(line.Offset(plate.PartSpacing));
}
bounds = plate.WorkArea();
}
}