feat(ui): add progress support to ActionFillArea and FillArea_Click

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 08:33:09 -04:00
parent 09fef203df
commit 98c4c88fc3
2 changed files with 72 additions and 7 deletions
+42 -4
View File
@@ -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 System.Windows.Forms;
using OpenNest.Controls; using OpenNest.Controls;
@@ -8,26 +12,60 @@ namespace OpenNest.Actions
public class ActionFillArea : ActionSelectArea public class ActionFillArea : ActionSelectArea
{ {
private Drawing drawing; private Drawing drawing;
private IProgress<NestProgress> progress;
private CancellationTokenSource cts;
private Action<List<Part>> onFillComplete;
public ActionFillArea(PlateView plateView, Drawing drawing) public ActionFillArea(PlateView plateView, Drawing drawing)
: this(plateView, drawing, null, null, null)
{
}
public ActionFillArea(PlateView plateView, Drawing drawing,
IProgress<NestProgress> progress, CancellationTokenSource cts,
Action<List<Part>> onFillComplete)
: base(plateView) : base(plateView)
{ {
plateView.PreviewKeyDown += plateView_PreviewKeyDown; plateView.PreviewKeyDown += plateView_PreviewKeyDown;
this.drawing = drawing; 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) if (e.KeyCode == Keys.Enter)
FillArea(); FillArea();
else if (e.KeyCode == Keys.Escape && cts != null)
cts.Cancel();
} }
private void FillArea() private async void FillArea()
{
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<Part>());
}
}
else
{ {
var engine = new NestEngine(plateView.Plate); var engine = new NestEngine(plateView.Plate);
engine.Fill(new NestItem { Drawing = drawing }, SelectedArea); engine.Fill(new NestItem { Drawing = drawing }, SelectedArea);
plateView.Invalidate(); plateView.Invalidate();
}
Update(); Update();
} }
+28 -1
View File
@@ -938,7 +938,34 @@ namespace OpenNest.Forms
if (drawing == null) if (drawing == null)
return; return;
activeForm.PlateView.SetAction(typeof(ActionFillArea), drawing); nestingCts = new CancellationTokenSource();
var progressForm = new NestProgressForm(nestingCts, showPlateRow: false);
var progress = new Progress<NestProgress>(p =>
{
progressForm.UpdateProgress(p);
activeForm.PlateView.SetTemporaryParts(p.BestParts);
});
Action<List<Part>> 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) private void AddPlate_Click(object sender, EventArgs e)