From 98c4c88fc3cebd588e232835caf0ca4cc9ed79ee Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 13 Mar 2026 08:33:09 -0400 Subject: [PATCH] feat(ui): add progress support to ActionFillArea and FillArea_Click Co-Authored-By: Claude Opus 4.6 --- OpenNest/Actions/ActionFillArea.cs | 50 ++++++++++++++++++++++++++---- OpenNest/Forms/MainForm.cs | 29 ++++++++++++++++- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/OpenNest/Actions/ActionFillArea.cs b/OpenNest/Actions/ActionFillArea.cs index aec5099..00d2da3 100644 --- a/OpenNest/Actions/ActionFillArea.cs +++ b/OpenNest/Actions/ActionFillArea.cs @@ -1,4 +1,8 @@ -using System.ComponentModel; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using OpenNest.Controls; @@ -8,26 +12,60 @@ namespace OpenNest.Actions public class ActionFillArea : ActionSelectArea { private Drawing drawing; + private IProgress progress; + private CancellationTokenSource cts; + private Action> onFillComplete; public ActionFillArea(PlateView plateView, Drawing drawing) + : this(plateView, drawing, null, null, null) + { + } + + public ActionFillArea(PlateView plateView, Drawing drawing, + IProgress progress, CancellationTokenSource cts, + Action> onFillComplete) : base(plateView) { plateView.PreviewKeyDown += plateView_PreviewKeyDown; this.drawing = drawing; + this.progress = progress; + this.cts = cts; + this.onFillComplete = onFillComplete; } - private void plateView_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e) + private void plateView_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { if (e.KeyCode == Keys.Enter) FillArea(); + else if (e.KeyCode == Keys.Escape && cts != null) + cts.Cancel(); } - private void FillArea() + private async void FillArea() { - var engine = new NestEngine(plateView.Plate); - engine.Fill(new NestItem { Drawing = drawing }, SelectedArea); + if (progress != null && cts != null) + { + try + { + var engine = new NestEngine(plateView.Plate); + var parts = await Task.Run(() => + engine.Fill(new NestItem { Drawing = drawing }, + SelectedArea, progress, cts.Token)); + + onFillComplete?.Invoke(parts); + } + catch (Exception) + { + onFillComplete?.Invoke(new List()); + } + } + else + { + var engine = new NestEngine(plateView.Plate); + engine.Fill(new NestItem { Drawing = drawing }, SelectedArea); + plateView.Invalidate(); + } - plateView.Invalidate(); Update(); } diff --git a/OpenNest/Forms/MainForm.cs b/OpenNest/Forms/MainForm.cs index 09576c8..7c83f3d 100644 --- a/OpenNest/Forms/MainForm.cs +++ b/OpenNest/Forms/MainForm.cs @@ -938,7 +938,34 @@ namespace OpenNest.Forms if (drawing == null) return; - activeForm.PlateView.SetAction(typeof(ActionFillArea), drawing); + nestingCts = new CancellationTokenSource(); + + var progressForm = new NestProgressForm(nestingCts, showPlateRow: false); + + var progress = new Progress(p => + { + progressForm.UpdateProgress(p); + activeForm.PlateView.SetTemporaryParts(p.BestParts); + }); + + Action> onComplete = parts => + { + if (parts != null && parts.Count > 0) + activeForm.PlateView.AcceptTemporaryParts(); + else + activeForm.PlateView.ClearTemporaryParts(); + + progressForm.Close(); + SetNestingLockout(false); + nestingCts.Dispose(); + nestingCts = null; + }; + + progressForm.Show(this); + SetNestingLockout(true); + + activeForm.PlateView.SetAction(typeof(ActionFillArea), + drawing, progress, nestingCts, onComplete); } private void AddPlate_Click(object sender, EventArgs e)