feat(release): pick a top layout while Make Nests nests

Each released nest's progress window offers the top-layout cards and
Use This Layout, as Auto Nest does: using one stops that nest's search
and goes on with it. A search that finishes with several layouts counts
down 5 seconds before going on with the engine's result; picking a card
stops the countdown and waits for Use This Layout or Discard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-19 19:31:07 -05:00
1 parent c9230af39e
commit 4be141b038
6 files changed
+151 -10

No files matched your search

@@ -142,6 +142,71 @@ public class NestProgressFormLayoutsTests
Assert.False(cts.IsCancellationRequested);
});
// Runs the form's timers until the task completes or the wait gives up.
private static void PumpUntil(Task task, TimeSpan max)
{
var waited = System.Diagnostics.Stopwatch.StartNew();
while (!task.IsCompleted && waited.Elapsed < max)
{
Application.DoEvents();
Thread.Sleep(5);
}
}
private static Label Status(Form form) =>
Assert.IsType<Label>(form.Controls.Find("descriptionValue", true).Single());
[Fact]
public void ATimedPickUsesTheSearchResultWhenTheTimeIsUp() => WithForm((form, cts, stopped) =>
{
form.AddLayout(Columns(), best: true);
form.AddLayout(Row(), best: false);
var final = Columns();
var pick = form.ChooseLayoutAsync(final, TimeSpan.FromMilliseconds(300));
Assert.False(pick.IsCompleted);
Assert.Contains("Using the best layout in 1 s", Status(form).Text);
PumpUntil(pick, TimeSpan.FromSeconds(10));
Assert.True(pick.IsCompleted);
Assert.True(pick.Result);
Assert.Same(final, form.ChosenLayout);
Assert.False(stopped());
Assert.False(cts.IsCancellationRequested);
});
[Fact]
public void PickingACardStopsTheCountdown() => WithForm((form, _, _) =>
{
form.AddLayout(Columns(), best: true);
var row = Row();
form.AddLayout(row, best: false);
var pick = form.ChooseLayoutAsync(Columns(), TimeSpan.FromMilliseconds(100));
List(form).Pick(row);
PumpUntil(pick, TimeSpan.FromMilliseconds(600));
Assert.False(pick.IsCompleted);
Assert.Equal("Search finished. Pick the layout to use.", Status(form).Text);
Button(form, "useLayoutButton").PerformClick();
Assert.True(pick.Result);
Assert.Same(row, form.ChosenLayout);
});
[Fact]
public void DiscardDuringTheCountdownChoosesNothing() => WithForm((form, _, _) =>
{
form.AddLayout(Columns(), best: true);
form.AddLayout(Row(), best: false);
var pick = form.ChooseLayoutAsync(Columns(), TimeSpan.FromMilliseconds(200));
Button(form, "stopButton").PerformClick();
PumpUntil(Task.Delay(400), TimeSpan.FromSeconds(2));
Assert.False(pick.Result);
Assert.Null(form.ChosenLayout);
});
[Fact]
public void AFinishedSearchWithOneLayoutDoesNotAsk() => WithForm((form, _, _) =>
{
+8 -2
View File
@@ -230,8 +230,13 @@ namespace OpenNest.Forms.Commands
/// during the run stops the search, and a finished run with more than one waits for the
/// pick. The layout used is validated and bound like the engine's own result. Off, the
/// engine's result is returned as soon as it is known.</param>
/// <param name="choiceTimeout">With <paramref name="chooseLayout"/>, how long a finished
/// run with more than one layout waits for the pick before using its own result (Make
/// Nests, which goes on to the next nest); picking a card stops the countdown. Null waits
/// until the user picks or discards.</param>
internal static async Task<NestPipelineResult> SolveAsync(IWin32Window owner, EditNestForm target,
NestPipelineRequest request, CancellationTokenSource cts, bool chooseLayout = false)
NestPipelineRequest request, CancellationTokenSource cts, bool chooseLayout = false,
TimeSpan? choiceTimeout = null)
{
using var progressForm = new NestProgressForm(cts, showPlateRow: true)
{
@@ -271,7 +276,8 @@ namespace OpenNest.Forms.Commands
}
receivingProgress = false;
if (result != null
&& !await progressForm.ChooseLayoutAsync(new NestLayoutChoice(result.Plates, _ => result)))
&& !await progressForm.ChooseLayoutAsync(new NestLayoutChoice(result.Plates, _ => result),
choiceTimeout))
{
cts.Cancel();
throw new OperationCanceledException(cts.Token);
+10 -4
View File
@@ -16,14 +16,19 @@ namespace OpenNest.Forms
/// <summary>
/// Make Nests with Nest after release ticked nests here, where the operator can watch: each
/// built nest opens in its own tab before it is saved and Auto Nest runs on it with the
/// usual progress window and plan strip, one nest at a time in plan order. The release
/// keeps only a valid layout, as headless (<see cref="NestRenest"/>), without the Keep or
/// validation review. Stop in the progress window, or switching tabs, stops the release.
/// usual progress window and plan strip, one nest at a time in plan order. Use This Layout
/// stops a search on the card picked; a search that finishes with several layouts waits
/// <see cref="ReleaseChoiceTimeout"/> for a pick (a pick stops the countdown), then goes on
/// with the engine's result. The release keeps only a valid layout, as headless
/// (<see cref="NestRenest"/>), without the Keep or validation review. Stop in the progress window, or switching tabs, stops the release.
/// Once saved, a tab is bound to its server record; a tab whose nest was not saved
/// (refused, failed or stopped before upload) is closed.
/// </summary>
public partial class MainForm : IReleaseNestingHost
{
/// <summary>How long a released nest's finished search waits for a layout pick.</summary>
internal static readonly TimeSpan ReleaseChoiceTimeout = TimeSpan.FromSeconds(5);
// Tabs Make Nests opened for nests it has not saved yet.
private readonly List<EditNestForm> releaseTabs = new();
@@ -65,7 +70,8 @@ namespace OpenNest.Forms
// Ledger stock first, then the plate defaults: each stage is its own Auto Nest
// run and undo step, with its ledger codes recorded in that step.
var solve = SolveReleasedNest
?? ((t, r, c) => AutoNestCommand.SolveAsync(this, t, r, c));
?? ((t, r, c) => AutoNestCommand.SolveAsync(this, t, r, c,
chooseLayout: true, choiceTimeout: ReleaseChoiceTimeout));
renested = await ReleaseRenest.RunAsync(nest, ledger, EngineSelection.EngineName,
AutoNestForm.DefaultMinRemnantSize,
request => solve(tab, request, cts),
+62 -2
View File
@@ -228,9 +228,11 @@ namespace OpenNest.Forms
/// The search is over and <paramref name="final"/> is its result: with other layouts to
/// pick from, waits until the user uses one (true, in <see cref="ChosenLayout"/>) or
/// discards them all (false). With nothing to pick from, or a layout already chosen
/// during the run, returns at once.
/// during the run, returns at once. With <paramref name="timeout"/>, <paramref name="final"/>
/// is used once it passes, counting down in the status line, unless the user picks a card
/// first, which stops the countdown.
/// </summary>
internal Task<bool> ChooseLayoutAsync(NestLayoutChoice final)
internal Task<bool> ChooseLayoutAsync(NestLayoutChoice final, TimeSpan? timeout = null)
{
if (ChosenLayout != null)
return Task.FromResult(true);
@@ -256,9 +258,63 @@ namespace OpenNest.Forms
stopButton.Text = "Discard";
stopButton.Enabled = true;
UpdateUseLayout();
if (timeout is { } limit)
StartChoiceCountdown(final, limit);
return choice.Task;
}
private System.Windows.Forms.Timer choiceTimer;
// Uses the search's result when the time is up, unless a card is picked first.
private void StartChoiceCountdown(NestLayoutChoice final, TimeSpan limit)
{
var waited = Stopwatch.StartNew();
void ShowLeft(TimeSpan left) => descriptionValue.Text =
$"Search finished. Using the best layout in {System.Math.Max(1, (int)System.Math.Ceiling(left.TotalSeconds))} s; pick a layout to choose.";
ShowLeft(limit);
choiceTimer = new System.Windows.Forms.Timer
{
Interval = (int)System.Math.Clamp(limit.TotalMilliseconds, 1, 250),
};
choiceTimer.Tick += (s, e) =>
{
var left = limit - waited.Elapsed;
if (left > TimeSpan.Zero)
{
ShowLeft(left);
return;
}
StopChoiceCountdown();
if (ChosenLayout != null || choice == null || choice.Task.IsCompleted)
return;
ChosenLayout = final;
layoutList.AllowSelection = false;
useLayoutButton.Enabled = false;
stopButton.Enabled = false;
choice.TrySetResult(true);
};
layoutList.SelectionChanged += PickStopsCountdown;
choiceTimer.Start();
}
private void PickStopsCountdown(object sender, EventArgs e)
{
if (choiceTimer == null)
return;
StopChoiceCountdown();
descriptionValue.Text = "Search finished. Pick the layout to use.";
}
private void StopChoiceCountdown()
{
if (choiceTimer == null)
return;
layoutList.SelectionChanged -= PickStopsCountdown;
choiceTimer.Stop();
choiceTimer.Dispose();
choiceTimer = null;
}
private void UpdateUseLayout()
{
if (useLayoutButton != null && ChosenLayout == null)
@@ -269,6 +325,7 @@ namespace OpenNest.Forms
{
if (useLayoutButton == null || ChosenLayout != null || layoutList.Selected?.Accept == null)
return;
StopChoiceCountdown();
ChosenLayout = layoutList.Selected;
layoutList.AllowSelection = false;
useLayoutButton.Enabled = false;
@@ -330,6 +387,7 @@ namespace OpenNest.Forms
if (choice != null)
{
// The search is over: this is Discard.
StopChoiceCountdown();
stopButton.Enabled = false;
choice.TrySetResult(false);
return;
@@ -347,6 +405,7 @@ namespace OpenNest.Forms
if (HoldOpenUntilCompleted && !completed)
{
// Closing while picking a layout discards them all, as Discard does.
StopChoiceCountdown();
if (choice != null)
choice.TrySetResult(false);
else
@@ -354,6 +413,7 @@ namespace OpenNest.Forms
e.Cancel = true;
return;
}
StopChoiceCountdown();
fadeTimer.Stop();
fadeTimer.Dispose();
elapsedTimer.Stop();
+1 -1
View File
@@ -30,7 +30,7 @@ Progress is owned and modal so the input drawings cannot be edited while a worke
Auto Nest never draws its parts on the plate view before they are kept: its result is a set of sheets, possibly of several sizes, not parts on the current plate. Ghost (preview) parts on the plate view mean only "what this plate gets if you accept now", and stay with interactive Fill and the remnant tools. Instead the progress window shows **Top layouts**: up to three whole-job layouts as cards, the engine's best so far first. A card draws every sheet of its layout side by side at one scale, identical sheets once with their count, each captioned with size, part count, utilization and its free strip, which is also dimensioned on the sheet: the distance from the parts to the right edge when they were packed in vertical strips, or to the top for horizontal strips (the left edge or the bottom in the quadrants whose origin is on the other side; whichever strip has more area is the one shown, and an outlined sheet shows none). The card's title line totals the layout and gives the last sheet's free strip, the usable drop. An engine reports a plan with `NestJobStage.PlanImproved` and `NestJobProgress.Plan`; `NestPipeline` adds `PlanSheets`, parts on detached drawings built from the job geometry, so nothing touches the caller's drawings or quantities. The plan is unvalidated and may be replaced. An engine may also report plans that lost to its best (`NestJobStage.PlanAlternative`); the window keeps the best layout of each family (sheet count, sheet sizes and free-strip direction) other than the best's, so the cards are real alternatives, such as the same job on one 60 x 120 sheet instead of one 48 x 144, or packed toward the left edge and toward the bottom, not near copies. The alternatives are ordered by parts placed, then fewer sheets, less sheet area and the larger free strip. Default, Irregular, Rectangles and Circles report plans (see [nesting engines](nesting-engines.md#best-plan-so-far)); FlatBar, the fixed-strategy engines and plug-ins that report none show the strip empty until the review.
Any card can be used. Click a card to select it (the best stays selected until you do) and choose **Use This Layout**, or double-click the card: during the search this stops it; when the search finishes with more than one layout the window stays open, titled **Nesting Finished**, until a layout is used or **Discard** drops them all. A search that ends with a single layout goes straight to the review, as do Make Nests runs, which always take the engine's result. The layout used goes through the same validation and binding as the engine's result (`NestJobProgress.AcceptPlan`), so a layout that breaks a rule shows the validation report below; last-resort clamp removal applies only to the engine's own result.
Any card can be used. Click a card to select it (the best stays selected until you do) and choose **Use This Layout**, or double-click the card: during the search this stops it; when the search finishes with more than one layout the window stays open, titled **Nesting Finished**, until a layout is used or **Discard** drops them all. A search that ends with a single layout goes straight to the review. Make Nests runs offer the same cards: using one during the search stops it and goes on with that layout, so a good enough layout need not wait for the search to finish. A Make Nests search that finishes with more than one layout counts down 5 seconds in the status line, then uses the engine's result and goes on to the next nest; picking a card stops the countdown, so the window waits for **Use This Layout** or **Discard** (which, like Stop, stops the release). The layout used goes through the same validation and binding as the engine's result (`NestJobProgress.AcceptPlan`), so a layout that breaks a rule shows the validation report below; last-resort clamp removal applies only to the engine's own result.
After a valid result, or an invalid one kept in the validation report, a review window shows the final sheets in the same strip with totals, the supplied-cost summary and every unplaced drawing, count and reason. **Keep** (Enter) commits the whole proposal; **Discard** (Escape) applies nothing.
+5 -1
View File
@@ -840,7 +840,11 @@ Each group's nest opens in its own main-window tab as soon as it is built, befor
it is saved, and Auto Nest runs on it there with the usual progress window and
live plan strip (`AutoNestCommand.SolveAsync`), one nest at a time in plan order,
switching to each tab in turn. A nest nested on ledger stock and then the plate
defaults shows two Auto Nest runs, one per step. The release keeps the layout as headless
defaults shows two Auto Nest runs, one per step. As in Auto Nest, a top-layout card can be
used during the search (**Use This Layout**, or double-click it), which stops the search and
goes on with that layout. A search that finishes with more than one layout counts down 5
seconds, then goes on with the engine's result; picking a card stops the countdown and the
window waits for Use This Layout or Discard, which stops the release as Stop does. The release keeps the layout as headless
`NestRenest` would (only a valid one, with no Keep or validation review). Stop in
the progress window (or switching tabs, as for Auto Nest) stops the whole release,
as the panel's Stop does: nothing is saved. Until Make Nests finishes the main