mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-15 19:15:37 -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>
89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using OpenNest.Engine.Jobs;
|
|
|
|
namespace OpenNest.Benchmark
|
|
{
|
|
/// <summary>
|
|
/// Writes one solve's NestJobProgress as log lines prefixed with "[job/engine]". Every
|
|
/// PlateCommitted is written; EvaluatingCandidate is throttled to one line per interval so a
|
|
/// chatty engine cannot flood the console. One instance per solve; Report is thread-safe.
|
|
/// </summary>
|
|
public sealed class JobProgressLog : IProgress<NestJobProgress>
|
|
{
|
|
public static readonly TimeSpan DefaultInterval = TimeSpan.FromSeconds(2);
|
|
|
|
private readonly TextWriter writer;
|
|
private readonly string label;
|
|
private readonly TimeSpan interval;
|
|
private readonly Func<TimeSpan> clock;
|
|
private readonly object sync = new();
|
|
private TimeSpan? lastEvaluating;
|
|
|
|
public JobProgressLog(
|
|
TextWriter writer,
|
|
string label,
|
|
TimeSpan? interval = null,
|
|
Func<TimeSpan> clock = null
|
|
)
|
|
{
|
|
this.writer = writer ?? throw new ArgumentNullException(nameof(writer));
|
|
this.label = label;
|
|
this.interval = interval ?? DefaultInterval;
|
|
if (clock == null)
|
|
{
|
|
var stopwatch = Stopwatch.StartNew();
|
|
clock = () => stopwatch.Elapsed;
|
|
}
|
|
this.clock = clock;
|
|
}
|
|
|
|
public void Started() => Write("started");
|
|
|
|
public void Finished(NestJobResult result, long elapsedMs) =>
|
|
Write(
|
|
$"finished in {elapsedMs} ms: {result.Status} ({result.StopReason}), "
|
|
+ $"{result.Plates.Count} plate(s)"
|
|
);
|
|
|
|
public void Failed(string error, long elapsedMs) =>
|
|
Write($"failed after {elapsedMs} ms: {error}");
|
|
|
|
public void Report(NestJobProgress value)
|
|
{
|
|
if (value == null || value.Stage == NestJobStage.PlanAlternative)
|
|
return;
|
|
|
|
if (value.Stage == NestJobStage.PlateCommitted)
|
|
{
|
|
Write(
|
|
$"committed plate {value.CommittedPlates} on stock {value.StockId} "
|
|
+ $"({value.CommittedParts} parts placed)"
|
|
);
|
|
return;
|
|
}
|
|
|
|
lock (sync)
|
|
{
|
|
var now = clock();
|
|
if (lastEvaluating.HasValue && now - lastEvaluating.Value < interval)
|
|
return;
|
|
lastEvaluating = now;
|
|
}
|
|
|
|
var plate = value.PlateIndex >= 0 ? value.PlateIndex + 1 : value.CommittedPlates + 1;
|
|
Write(
|
|
$"evaluating plate {plate} on stock {value.StockId} "
|
|
+ $"({value.CommittedPlates} plate(s), {value.CommittedParts} parts committed)"
|
|
);
|
|
}
|
|
|
|
private void Write(string message)
|
|
{
|
|
lock (sync)
|
|
writer.WriteLine($"[{label}] {message}");
|
|
}
|
|
}
|
|
}
|