mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-16 18:15:34 -05:00
The Auto Nest progress window lists up to three whole-job layouts as cards, the engine's best first and then the best of each other family (sheet count and free-strip direction). Every sheet shows the distance from its parts to the right edge (vertical strips) or the top (horizontal strips), dimensioned on the sheet. Any card can be used: during the search it stops it, and a finished search with more than one layout waits for the pick. The layout used is validated and bound like the engine's own result. Engines may report plans that lost to their best as NestJobStage.PlanAlternative; Irregular, Default and the time-limited solve do. Reporting never changes a layout. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
155 lines
5.2 KiB
C#
155 lines
5.2 KiB
C#
using System.Windows.Forms;
|
|
using OpenNest.CNC;
|
|
using OpenNest.Controls;
|
|
using OpenNest.Engine.Jobs;
|
|
using OpenNest.Forms;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.WinForms.Tests.Forms;
|
|
|
|
// The picks read here have already completed on this STA thread; nothing blocks.
|
|
#pragma warning disable xUnit1031
|
|
|
|
/// <summary>
|
|
/// The Auto Nest progress window's top layouts: one card per layout with its sheets, and the
|
|
/// pick of the layout to use, during the run and once it has finished.
|
|
/// </summary>
|
|
public class NestProgressFormLayoutsTests
|
|
{
|
|
private static readonly Drawing Bracket = new("bracket", Rectangle());
|
|
private static readonly NestPlateStock Stock = new("sheet", new Size(48, 96), quantity: null, partSpacing: 0.25);
|
|
|
|
private static Program Rectangle()
|
|
{
|
|
var program = new Program();
|
|
program.MoveTo(0, 0);
|
|
program.LineTo(10, 0);
|
|
program.LineTo(10, 20);
|
|
program.LineTo(0, 20);
|
|
program.LineTo(0, 0);
|
|
return program;
|
|
}
|
|
|
|
private static NestLayoutChoice Layout(params (double X, double Y)[] at)
|
|
{
|
|
var parts = at.Select(p =>
|
|
{
|
|
var part = new Part(Bracket) { Location = new Vector(p.X, p.Y) };
|
|
part.UpdateBounds();
|
|
return part;
|
|
}).ToList();
|
|
return new NestLayoutChoice(new[] { new ProposedPlate(0, Stock, parts) }, _ => null!);
|
|
}
|
|
|
|
private static NestLayoutChoice Columns() => Layout((0, 0), (0, 28), (10, 0));
|
|
|
|
private static NestLayoutChoice Row() => Layout((0, 0), (40, 0), (86, 0));
|
|
|
|
private static void WithForm(Action<NestProgressForm, CancellationTokenSource, Func<bool>> test) => StaTestThread.Run(() =>
|
|
{
|
|
using var cts = new CancellationTokenSource();
|
|
var stopped = false;
|
|
using var form = new NestProgressForm(cts) { AllowAccept = false, HoldOpenUntilCompleted = true };
|
|
form.ShowPlanStrip(new ColorScheme(), () => stopped = true);
|
|
form.Show();
|
|
try
|
|
{
|
|
test(form, cts, () => stopped);
|
|
}
|
|
finally
|
|
{
|
|
form.ShowCompleted();
|
|
form.Close();
|
|
}
|
|
}, TimeSpan.FromSeconds(30), "The STA test did not complete.");
|
|
|
|
private static Button Button(Form form, string name) =>
|
|
Assert.IsType<Button>(form.Controls.Find(name, true).Single());
|
|
|
|
private static LayoutChoiceList List(Control parent) =>
|
|
Descendants(parent).OfType<LayoutChoiceList>().Single();
|
|
|
|
private static IEnumerable<Control> Descendants(Control parent) =>
|
|
parent.Controls.Cast<Control>().SelectMany(c => Descendants(c).Prepend(c));
|
|
|
|
[Fact]
|
|
public void EachLayoutGetsACardAndTheBestIsSelected() => WithForm((form, _, _) =>
|
|
{
|
|
var use = Button(form, "useLayoutButton");
|
|
Assert.False(use.Enabled);
|
|
|
|
var columns = Columns();
|
|
var row = Row();
|
|
form.AddLayout(columns, best: true);
|
|
form.AddLayout(row, best: false);
|
|
|
|
Assert.Equal(new[] { columns, row }, form.Layouts);
|
|
Assert.Equal(2, Descendants(form).OfType<SheetStripControl>().Count());
|
|
Assert.Same(columns, List(form).Selected);
|
|
Assert.True(use.Enabled);
|
|
Assert.Contains("76 to right edge", columns.Summary);
|
|
Assert.Contains("28 to top", row.Summary);
|
|
});
|
|
|
|
[Fact]
|
|
public void UsingALayoutDuringTheRunStopsTheSearchOnly() => WithForm((form, cts, stopped) =>
|
|
{
|
|
var row = Row();
|
|
form.AddLayout(Columns(), best: true);
|
|
form.AddLayout(row, best: false);
|
|
|
|
List(form).Pick(row);
|
|
Button(form, "useLayoutButton").PerformClick();
|
|
|
|
Assert.True(stopped());
|
|
Assert.False(cts.IsCancellationRequested);
|
|
Assert.Same(row, form.ChosenLayout);
|
|
// Already chosen: the finished search does not ask again.
|
|
Assert.True(form.ChooseLayoutAsync(Columns()).Result);
|
|
Assert.Same(row, form.ChosenLayout);
|
|
});
|
|
|
|
[Fact]
|
|
public void AFinishedSearchWaitsForThePickAmongItsLayouts() => WithForm((form, cts, stopped) =>
|
|
{
|
|
form.AddLayout(Columns(), best: true);
|
|
form.AddLayout(Row(), best: false);
|
|
var final = Columns();
|
|
|
|
var pick = form.ChooseLayoutAsync(final);
|
|
|
|
Assert.False(pick.IsCompleted);
|
|
Assert.Equal("Discard", Button(form, "stopButton").Text);
|
|
Assert.Same(final, List(form).Selected);
|
|
Button(form, "useLayoutButton").PerformClick();
|
|
Assert.True(pick.Result);
|
|
Assert.Same(final, form.ChosenLayout);
|
|
Assert.False(stopped());
|
|
Assert.False(cts.IsCancellationRequested);
|
|
});
|
|
|
|
[Fact]
|
|
public void DiscardAfterTheSearchChoosesNothing() => WithForm((form, cts, _) =>
|
|
{
|
|
form.AddLayout(Columns(), best: true);
|
|
form.AddLayout(Row(), best: false);
|
|
|
|
var pick = form.ChooseLayoutAsync(Columns());
|
|
Button(form, "stopButton").PerformClick();
|
|
|
|
Assert.False(pick.Result);
|
|
Assert.Null(form.ChosenLayout);
|
|
Assert.False(cts.IsCancellationRequested);
|
|
});
|
|
|
|
[Fact]
|
|
public void AFinishedSearchWithOneLayoutDoesNotAsk() => WithForm((form, _, _) =>
|
|
{
|
|
form.AddLayout(Columns(), best: true);
|
|
var final = Columns();
|
|
|
|
Assert.True(form.ChooseLayoutAsync(final).Result);
|
|
Assert.Same(final, form.ChosenLayout);
|
|
});
|
|
}
|