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:
@@ -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<NestProgress> progress;
|
||||
private CancellationTokenSource cts;
|
||||
private Action<List<Part>> onFillComplete;
|
||||
|
||||
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)
|
||||
{
|
||||
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<Part>());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var engine = new NestEngine(plateView.Plate);
|
||||
engine.Fill(new NestItem { Drawing = drawing }, SelectedArea);
|
||||
plateView.Invalidate();
|
||||
}
|
||||
|
||||
plateView.Invalidate();
|
||||
Update();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<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)
|
||||
|
||||
Reference in New Issue
Block a user