refactor(winforms): share duplicated handler code

The cut-off handler's hit test delegates to the Desktop PlateHitTester's
CutOffAt, the CAD Converter's breakoff and tabbed-cutout tab drags and
junction splits share small helpers, and the program editor's contour
start and end points share one entity-endpoint switch. Behavior is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-28 18:29:34 -05:00
1 parent 20323fda37
commit 8a1d89cea5
3 files changed
+39 -53

No files matched your search

+4 -16
View File
@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenNest.CNC;
using OpenNest.CutOffs;
using OpenNest.Desktop.Interaction;
using OpenNest.Engine.Sequencing;
using OpenNest.Geometry;
using OpenNest.Undo;
@@ -11,6 +12,7 @@ namespace OpenNest.Controls
internal class CutOffHandler
{
private readonly PlateView view;
private readonly PlateHitTester hitTester = new();
private Dictionary<Part, Geometry.Entity> dragPerimeterCache;
// Open from the press on a cut-off to the release: a drag undoes as one move.
@@ -102,21 +104,7 @@ namespace OpenNest.Controls
return true;
}
public CutOff GetCutOffAtPoint(Vector point, double tolerance)
{
if (view.Plate?.CutOffs == null)
return null;
foreach (var cutoff in view.Plate.CutOffs)
{
foreach (var line in CutOff.CutStrokes(cutoff.Drawing?.Program))
{
if (line.ClosestPointTo(point).DistanceTo(point) <= tolerance)
return cutoff;
}
}
return null;
}
public CutOff GetCutOffAtPoint(Vector point, double tolerance) =>
hitTester.CutOffAt(view.Plate, point, tolerance);
}
}
+12 -21
View File
@@ -435,29 +435,20 @@ namespace OpenNest.Controls
RebuildAfterReorder(-1);
}
private static Vector GetContourStartPoint(ContourInfo contour)
{
var entity = contour.Shape.Entities[0];
return entity switch
{
Line line => line.StartPoint,
Arc arc => arc.StartPoint(),
Circle circle => new Vector(circle.Center.X + circle.Radius, circle.Center.Y),
_ => new Vector(),
};
}
private static Vector GetContourStartPoint(ContourInfo contour) =>
EndpointOf(contour.Shape.Entities[0], start: true);
private static Vector GetContourEndPoint(ContourInfo contour)
private static Vector GetContourEndPoint(ContourInfo contour) =>
EndpointOf(contour.Shape.Entities[^1], start: false);
// A circle starts and ends at its rightmost point.
private static Vector EndpointOf(Entity entity, bool start) => entity switch
{
var entity = contour.Shape.Entities[^1];
return entity switch
{
Line line => line.EndPoint,
Arc arc => arc.EndPoint(),
Circle circle => new Vector(circle.Center.X + circle.Radius, circle.Center.Y),
_ => new Vector(),
};
}
Line line => start ? line.StartPoint : line.EndPoint,
Arc arc => start ? arc.StartPoint() : arc.EndPoint(),
Circle circle => new Vector(circle.Center.X + circle.Radius, circle.Center.Y),
_ => new Vector(),
};
private void RebuildAfterReorder(int selectIndex)
{
+23 -16
View File
@@ -733,13 +733,9 @@ namespace OpenNest.Forms
return;
foreach (var breakoff in item.Breakoffs)
foreach (var id in breakoff.EdgeIds.ToList())
if (split.TryGetValue(id, out var pieces))
breakoff.EdgeIds.AddRange(pieces.Where(p => p != id));
AddSplitPieces(breakoff.EdgeIds, split);
foreach (var cutout in item.TabbedCutouts)
foreach (var id in cutout.EdgeIds.ToList())
if (split.TryGetValue(id, out var pieces))
cutout.EdgeIds.AddRange(pieces.Where(p => p != id));
AddSplitPieces(cutout.EdgeIds, split);
foreach (var (id, pieces) in split)
{
foreach (var piece in pieces.Where(p => p != id))
@@ -757,6 +753,14 @@ namespace OpenNest.Forms
lblEntityCount.Text = $"{item.EntityCount} entities";
}
// Adds the new pieces of every split edge, keeping the original ID in place.
private static void AddSplitPieces(List<Guid> edgeIds, Dictionary<Guid, List<Guid>> split)
{
foreach (var id in edgeIds.ToList())
if (split.TryGetValue(id, out var pieces))
edgeIds.AddRange(pieces.Where(p => p != id));
}
// The current item when the index addresses one of its features (breakoffs, tabbed
// cutouts or dimensions); null when there is no item or the index is out of range.
private FileListItem ItemWithFeature(int index, Func<FileListItem, int> count)
@@ -853,16 +857,22 @@ namespace OpenNest.Forms
entityView1.Invalidate();
}
// Moves a dragged tab to its new center; false, changing nothing, when there is no such tab.
private static bool TryMoveTab(List<double> tabCenters, int tab, double center)
{
if (tab >= tabCenters.Count)
return false;
tabCenters[tab] = center;
return true;
}
private void OnTabDragged(object sender, TabDragEventArgs e)
{
var item = CurrentItem;
if (item == null || e.Breakoff >= item.Breakoffs.Count)
if (item == null || e.Breakoff >= item.Breakoffs.Count
|| !TryMoveTab(item.Breakoffs[e.Breakoff].TabCenters, e.Tab, e.Center))
return;
var breakoff = item.Breakoffs[e.Breakoff];
if (e.Tab >= breakoff.TabCenters.Count)
return;
breakoff.TabCenters[e.Tab] = e.Center;
entityView1.SelectedBreakoffIndex = e.Breakoff;
RefreshBreakoffs(item);
staleProgram = true;
@@ -997,13 +1007,10 @@ namespace OpenNest.Forms
private void OnCutoutTabDragged(object sender, CutoutTabDragEventArgs e)
{
var item = CurrentItem;
if (item == null || e.Cutout >= item.TabbedCutouts.Count)
if (item == null || e.Cutout >= item.TabbedCutouts.Count
|| !TryMoveTab(item.TabbedCutouts[e.Cutout].TabCenters, e.Tab, e.Center))
return;
var cutout = item.TabbedCutouts[e.Cutout];
if (e.Tab >= cutout.TabCenters.Count)
return;
cutout.TabCenters[e.Tab] = e.Center;
entityView1.SelectedTabbedCutoutIndex = e.Cutout;
RefreshTabbedCutouts(item);
staleProgram = true;