mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 06:41:42 -05:00
CutTimeReport computes and words what Calculate Cut Time shows for a plate or nest (time from the simulation with acceleration plus clamp removal, distances and counts from the timing info) as the WinForms timing window. CutTimeEntry and CutTimeMachineChoice hold the dialog's box ranges, the entered values' cut parameters and the Acceleration list's entries. CutSimulationPlayback is the simulator's playhead without drawing: play, pause and speed, scrubbing, part, cutout and move stepping, retiming that keeps the torch in place and which parts it has not reached; CutSimulationStatus words its status rows. The WinForms timing window, cut parameters dialog, machine box, simulator and overlay now call them; their behavior is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
7.9 KiB
C#
234 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using OpenNest.CNC;
|
|
|
|
namespace OpenNest.Desktop.Interaction;
|
|
|
|
/// <summary>
|
|
/// The Cut Simulator's playhead over a <see cref="CutSimulation"/>, without drawing: the time,
|
|
/// play and pause at a speed, the jumps between parts, cutouts and moves, and which parts the
|
|
/// torch has not reached. The host runs a timer that calls <see cref="Advance()"/> while
|
|
/// <see cref="IsPlaying"/> and redraws after each change.
|
|
/// </summary>
|
|
public sealed class CutSimulationPlayback
|
|
{
|
|
/// <summary>The simulator's speed choices, as multiples of real time.</summary>
|
|
public static IReadOnlyList<double> Speeds { get; } = new double[] { 1, 2, 5, 10, 25, 50, 100, 250 };
|
|
|
|
/// <summary>The speed the simulator starts at.</summary>
|
|
public const double DefaultSpeed = 5;
|
|
|
|
/// <summary>The index of <see cref="DefaultSpeed"/> in <see cref="Speeds"/>.</summary>
|
|
public static int DefaultSpeedIndex => 2;
|
|
|
|
private readonly Stopwatch clock = new();
|
|
private CutSimulation simulation;
|
|
private TimeSpan time;
|
|
private bool playing;
|
|
|
|
/// <summary>Raised when the playhead, play state or timeline changes.</summary>
|
|
public event EventHandler Changed;
|
|
|
|
public CutSimulation Simulation => simulation;
|
|
|
|
public bool IsActive => simulation != null;
|
|
|
|
public bool IsPlaying => playing;
|
|
|
|
/// <summary>Simulated time per wall-clock time.</summary>
|
|
public double Speed { get; set; } = 10;
|
|
|
|
/// <summary>The playhead, kept within the timeline; nothing changes without one.</summary>
|
|
public TimeSpan Time
|
|
{
|
|
get => time;
|
|
set
|
|
{
|
|
if (simulation == null)
|
|
return;
|
|
time = value < TimeSpan.Zero ? TimeSpan.Zero : value > simulation.TotalTime ? simulation.TotalTime : value;
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows <paramref name="value"/>, keeping the torch where it was when the moves are the same
|
|
/// (new rates or acceleration only retime them), else keeping the playhead's time.
|
|
/// </summary>
|
|
public void Load(CutSimulation value)
|
|
{
|
|
var anchored = Anchor(simulation, value, time);
|
|
simulation = value;
|
|
Time = anchored;
|
|
if (simulation == null)
|
|
Pause();
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
/// <summary>Where <paramref name="time"/> on <paramref name="old"/> falls on <paramref name="next"/>.</summary>
|
|
public static TimeSpan Anchor(CutSimulation old, CutSimulation next, TimeSpan time)
|
|
{
|
|
if (old == null || next == null || old.Steps.Count == 0 || old.Steps.Count != next.Steps.Count)
|
|
return time;
|
|
var index = old.IndexAt(time);
|
|
if (index >= old.Steps.Count)
|
|
return next.TotalTime;
|
|
return next.TimeAt(index, old.FractionAt(index, time));
|
|
}
|
|
|
|
/// <summary>Plays from the playhead, or from the start once it is at the end.</summary>
|
|
public void Play()
|
|
{
|
|
if (simulation == null || simulation.Steps.Count == 0)
|
|
return;
|
|
if (time >= simulation.TotalTime)
|
|
Time = TimeSpan.Zero;
|
|
clock.Restart();
|
|
playing = true;
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
if (!playing)
|
|
return;
|
|
playing = false;
|
|
clock.Reset();
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
/// <summary>Drops the timeline and stops.</summary>
|
|
public void Clear()
|
|
{
|
|
playing = false;
|
|
clock.Reset();
|
|
simulation = null;
|
|
time = TimeSpan.Zero;
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
/// <summary>Moves the playhead by the wall time since the last advance (or since Play).</summary>
|
|
public void Advance()
|
|
{
|
|
var elapsed = clock.Elapsed;
|
|
clock.Restart();
|
|
Advance(elapsed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves the playhead by <paramref name="wallElapsed"/> times <see cref="Speed"/>, pausing at
|
|
/// the end.
|
|
/// </summary>
|
|
public void Advance(TimeSpan wallElapsed)
|
|
{
|
|
if (simulation == null)
|
|
return;
|
|
var next = time + TimeSpan.FromTicks((long)(wallElapsed.Ticks * Speed));
|
|
time = next > simulation.TotalTime ? simulation.TotalTime : next;
|
|
Changed?.Invoke(this, EventArgs.Empty);
|
|
if (time >= simulation.TotalTime)
|
|
Pause();
|
|
}
|
|
|
|
/// <summary>
|
|
/// The play button: pauses while playing; otherwise plays, first calling
|
|
/// <paramref name="rebuild"/> (parts may have moved, which raises no plate event) when there
|
|
/// is no timeline or the playhead is at its start or end.
|
|
/// </summary>
|
|
public void TogglePlay(Action rebuild)
|
|
{
|
|
if (playing)
|
|
{
|
|
Pause();
|
|
return;
|
|
}
|
|
if (simulation == null || time <= TimeSpan.Zero || time >= simulation.TotalTime)
|
|
rebuild?.Invoke();
|
|
Play();
|
|
}
|
|
|
|
/// <summary>Back to the start (Home), still playing if it was.</summary>
|
|
public void Restart() => Time = TimeSpan.Zero;
|
|
|
|
/// <summary>Pauses at the end.</summary>
|
|
public void End()
|
|
{
|
|
Pause();
|
|
Time = simulation?.TotalTime ?? TimeSpan.Zero;
|
|
}
|
|
|
|
public void NextContour() => Jump(sim => sim.NextContour(time));
|
|
|
|
public void PreviousContour() => Jump(sim => sim.PreviousContour(time));
|
|
|
|
public void NextPart() => Jump(sim => sim.NextPart(time));
|
|
|
|
public void PreviousPart() => Jump(sim => sim.PreviousPart(time));
|
|
|
|
// Jumps pause so the jumped-to contour stays on screen.
|
|
private void Jump(Func<CutSimulation, TimeSpan> target)
|
|
{
|
|
if (simulation is not { } sim || sim.Steps.Count == 0)
|
|
return;
|
|
Pause();
|
|
Time = target(sim);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pauses at the start of the next move (<paramref name="direction"/> > 0), or back at the
|
|
/// start of the current move, or of the one before when already there.
|
|
/// </summary>
|
|
public void StepMove(int direction)
|
|
{
|
|
var sim = simulation;
|
|
if (sim == null || sim.Steps.Count == 0)
|
|
return;
|
|
Pause();
|
|
var index = System.Math.Clamp(sim.IndexAt(time), 0, sim.Steps.Count);
|
|
var atStart = index < sim.Steps.Count && time == sim.Steps[index].StartTime;
|
|
var target = direction > 0 ? index + 1 : atStart ? index - 1 : index;
|
|
Time = target <= 0 ? TimeSpan.Zero
|
|
: target >= sim.Steps.Count ? sim.TotalTime
|
|
: sim.Steps[target].StartTime;
|
|
}
|
|
|
|
/// <summary>The playhead as a fraction of the timeline (0 without one).</summary>
|
|
public double Progress =>
|
|
simulation is { } sim && sim.TotalTime > TimeSpan.Zero ? time / sim.TotalTime : 0;
|
|
|
|
/// <summary>Moves the playhead to <paramref name="fraction"/> of the timeline (a scrub).</summary>
|
|
public void Seek(double fraction)
|
|
{
|
|
if (simulation != null)
|
|
Time = simulation.TotalTime * fraction;
|
|
}
|
|
|
|
/// <summary>True for a part the torch has not started yet.</summary>
|
|
public bool IsPartPending(int partIndex)
|
|
{
|
|
if (simulation == null)
|
|
return false;
|
|
var index = simulation.IndexAt(time);
|
|
if (index >= simulation.Steps.Count)
|
|
return false;
|
|
if (index < 0)
|
|
return true;
|
|
var current = simulation.Steps[index];
|
|
// Travel toward a part has not touched it yet.
|
|
return partIndex > current.PartIndex
|
|
|| partIndex == current.PartIndex && current.Kind == CutSimulationStepKind.Rapid && !PartStarted(index);
|
|
}
|
|
|
|
private bool PartStarted(int index)
|
|
{
|
|
var part = simulation.Steps[index].PartIndex;
|
|
for (var i = index - 1; i >= 0 && simulation.Steps[i].PartIndex == part; i--)
|
|
{
|
|
if (simulation.Steps[i].Kind != CutSimulationStepKind.Rapid)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|