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>
355 lines
11 KiB
C#
355 lines
11 KiB
C#
using System.Globalization;
|
||
using OpenNest.CNC;
|
||
using OpenNest.Desktop.Interaction;
|
||
using OpenNest.Geometry;
|
||
|
||
namespace OpenNest.Desktop.Tests.Interaction;
|
||
|
||
public sealed class CutSimulationPlaybackTests : IDisposable
|
||
{
|
||
private readonly CultureInfo previous = CultureInfo.CurrentCulture;
|
||
|
||
public CutSimulationPlaybackTests() => CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||
|
||
public void Dispose() => CultureInfo.CurrentCulture = previous;
|
||
|
||
private static Plate TwoSquares() => TestNests.TwoSquares().Plates[0];
|
||
|
||
private static CutParameters FullSpeed(double feed = 60) => new()
|
||
{
|
||
Feedrate = feed,
|
||
HoleFeedrate = feed,
|
||
RapidTravelRate = 600,
|
||
PierceTime = TimeSpan.FromSeconds(1),
|
||
Units = Units.Inches,
|
||
};
|
||
|
||
private static CutSimulationPlayback Loaded(Plate plate, CutParameters? parameters = null)
|
||
{
|
||
var playback = new CutSimulationPlayback();
|
||
playback.Load(CutSimulation.Build(plate, parameters ?? FullSpeed()));
|
||
return playback;
|
||
}
|
||
|
||
[Fact]
|
||
public void Time_WithoutATimeline_StaysAtZero()
|
||
{
|
||
var playback = new CutSimulationPlayback();
|
||
var changes = 0;
|
||
playback.Changed += (_, _) => changes++;
|
||
|
||
playback.Time = TimeSpan.FromSeconds(5);
|
||
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
Assert.Equal(0, changes);
|
||
}
|
||
|
||
[Fact]
|
||
public void Time_StaysWithinTheTimeline()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
|
||
playback.Time = TimeSpan.FromHours(1);
|
||
Assert.Equal(playback.Simulation.TotalTime, playback.Time);
|
||
|
||
playback.Time = TimeSpan.FromSeconds(-1);
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void Advance_MovesBySpeedTimesWallTime_AndPausesAtTheEnd()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Speed = 5;
|
||
playback.Play();
|
||
Assert.True(playback.IsPlaying);
|
||
|
||
playback.Advance(TimeSpan.FromSeconds(2));
|
||
Assert.Equal(TimeSpan.FromSeconds(10), playback.Time);
|
||
|
||
playback.Advance(TimeSpan.FromHours(1));
|
||
Assert.Equal(playback.Simulation.TotalTime, playback.Time);
|
||
Assert.False(playback.IsPlaying);
|
||
}
|
||
|
||
[Fact]
|
||
public void Play_AtTheEnd_StartsOver()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.End();
|
||
|
||
playback.Play();
|
||
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
Assert.True(playback.IsPlaying);
|
||
}
|
||
|
||
[Fact]
|
||
public void Play_WithNothingToCut_DoesNotStart()
|
||
{
|
||
var playback = Loaded(new Plate(new Size(48, 96)));
|
||
|
||
playback.Play();
|
||
|
||
Assert.False(playback.IsPlaying);
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_Retimed_KeepsTheTorchWhereItWas()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate, FullSpeed(60));
|
||
playback.Time = TimeSpan.FromSeconds(15);
|
||
var before = playback.Simulation.ToolPositionAt(playback.Time);
|
||
|
||
playback.Load(CutSimulation.Build(plate, FullSpeed(120)));
|
||
|
||
var after = playback.Simulation.ToolPositionAt(playback.Time);
|
||
Assert.Equal(before.X, after.X, 6);
|
||
Assert.Equal(before.Y, after.Y, 6);
|
||
Assert.NotEqual(TimeSpan.FromSeconds(15), playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_DifferentMoves_KeepsThePlayheadTime()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate);
|
||
playback.Time = TimeSpan.FromSeconds(15);
|
||
plate.Parts.RemoveAt(1);
|
||
|
||
playback.Load(CutSimulation.Build(plate, FullSpeed()));
|
||
|
||
Assert.Equal(TimeSpan.FromSeconds(15), playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void Load_Null_Pauses()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Play();
|
||
|
||
playback.Load(null!);
|
||
|
||
Assert.False(playback.IsPlaying);
|
||
Assert.False(playback.IsActive);
|
||
}
|
||
|
||
[Fact]
|
||
public void Parts_JumpForwardAndBack_Pausing()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
var sim = playback.Simulation;
|
||
Assert.Equal(2, sim.PartStarts.Count);
|
||
playback.Play();
|
||
|
||
// The first part starts the timeline, so the next is the second.
|
||
Assert.Equal(TimeSpan.Zero, sim.PartStarts[0]);
|
||
playback.NextPart();
|
||
Assert.False(playback.IsPlaying);
|
||
Assert.Equal(sim.PartStarts[1], playback.Time);
|
||
|
||
playback.NextPart();
|
||
Assert.Equal(sim.TotalTime, playback.Time);
|
||
|
||
playback.PreviousPart();
|
||
Assert.Equal(sim.PartStarts[1], playback.Time);
|
||
|
||
// Already at a part's start: back to the one before.
|
||
playback.PreviousPart();
|
||
Assert.Equal(sim.PartStarts[0], playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void Contours_JumpForwardAndBack()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
var sim = playback.Simulation;
|
||
|
||
playback.NextContour();
|
||
Assert.Equal(sim.ContourStarts[1], playback.Time);
|
||
|
||
playback.Time += TimeSpan.FromSeconds(1);
|
||
playback.PreviousContour();
|
||
Assert.Equal(sim.ContourStarts[1], playback.Time);
|
||
|
||
playback.PreviousContour();
|
||
Assert.Equal(sim.ContourStarts[0], playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void StepMove_GoesOneMoveAtATime()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
var steps = playback.Simulation.Steps;
|
||
|
||
playback.StepMove(1);
|
||
Assert.Equal(steps[1].StartTime, playback.Time);
|
||
playback.StepMove(1);
|
||
Assert.Equal(steps[2].StartTime, playback.Time);
|
||
|
||
// At a move's start, back to the one before; mid-move, back to its start.
|
||
playback.StepMove(-1);
|
||
Assert.Equal(steps[1].StartTime, playback.Time);
|
||
playback.Time = steps[2].StartTime + TimeSpan.FromTicks(steps[2].Duration.Ticks / 2);
|
||
playback.StepMove(-1);
|
||
Assert.Equal(steps[2].StartTime, playback.Time);
|
||
|
||
playback.Time = TimeSpan.Zero;
|
||
playback.StepMove(-1);
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void TogglePlay_RebuildsOnlyAtTheStartOrEnd()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate);
|
||
var rebuilds = 0;
|
||
void Rebuild()
|
||
{
|
||
rebuilds++;
|
||
playback.Load(CutSimulation.Build(plate, FullSpeed()));
|
||
}
|
||
|
||
playback.TogglePlay(Rebuild);
|
||
Assert.True(playback.IsPlaying);
|
||
Assert.Equal(1, rebuilds);
|
||
|
||
playback.TogglePlay(Rebuild);
|
||
Assert.False(playback.IsPlaying);
|
||
|
||
playback.Time = TimeSpan.FromSeconds(10);
|
||
playback.TogglePlay(Rebuild);
|
||
Assert.True(playback.IsPlaying);
|
||
Assert.Equal(1, rebuilds);
|
||
}
|
||
|
||
[Fact]
|
||
public void Restart_KeepsPlaying_EndPauses()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Time = TimeSpan.FromSeconds(10);
|
||
playback.Play();
|
||
|
||
playback.Restart();
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
Assert.True(playback.IsPlaying);
|
||
|
||
playback.End();
|
||
Assert.False(playback.IsPlaying);
|
||
Assert.Equal(playback.Simulation.TotalTime, playback.Time);
|
||
}
|
||
|
||
[Fact]
|
||
public void Seek_AndProgress_AreFractionsOfTheTimeline()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
|
||
playback.Seek(0.5);
|
||
|
||
Assert.Equal(playback.Simulation.TotalTime / 2, playback.Time);
|
||
Assert.Equal(0.5, playback.Progress, 6);
|
||
}
|
||
|
||
[Fact]
|
||
public void IsPartPending_UntilTheTorchReachesThePart()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
var sim = playback.Simulation;
|
||
|
||
playback.Time = TimeSpan.Zero;
|
||
Assert.True(playback.IsPartPending(1));
|
||
|
||
// On the travel to the second part, it has not been touched yet.
|
||
var travel = sim.Steps.First(s => s.PartIndex == 1 && s.Kind == CutSimulationStepKind.Rapid);
|
||
playback.Time = travel.StartTime + TimeSpan.FromTicks(travel.Duration.Ticks / 2);
|
||
Assert.False(playback.IsPartPending(0));
|
||
Assert.True(playback.IsPartPending(1));
|
||
|
||
playback.Time = sim.PartStarts[1] + TimeSpan.FromSeconds(0.5);
|
||
Assert.False(playback.IsPartPending(1));
|
||
|
||
playback.End();
|
||
Assert.False(playback.IsPartPending(1));
|
||
}
|
||
|
||
[Fact]
|
||
public void Clear_DropsTheTimelineAndStops()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Play();
|
||
|
||
playback.Clear();
|
||
|
||
Assert.Null(playback.Simulation);
|
||
Assert.False(playback.IsPlaying);
|
||
Assert.Equal(TimeSpan.Zero, playback.Time);
|
||
Assert.False(playback.IsPartPending(0));
|
||
}
|
||
|
||
[Fact]
|
||
public void Speeds_StartAtFiveTimes()
|
||
{
|
||
Assert.Equal(CutSimulationPlayback.DefaultSpeed, CutSimulationPlayback.Speeds[CutSimulationPlayback.DefaultSpeedIndex]);
|
||
Assert.Equal("5×", CutSimulationStatus.SpeedText(5));
|
||
Assert.Equal("250×", CutSimulationStatus.SpeedText(250));
|
||
}
|
||
|
||
[Fact]
|
||
public void Status_NothingToCut()
|
||
{
|
||
var status = CutSimulationStatus.For(new CutSimulationPlayback(), null!, Units.Inches);
|
||
|
||
Assert.Equal(new CutSimulationStatus("Nothing to cut", "", "", ""), status);
|
||
}
|
||
|
||
[Fact]
|
||
public void Status_Cutting_ShowsTheFeedPerMinute()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate);
|
||
// Mid-way along the first part's cut, past its pierce.
|
||
var cut = playback.Simulation.Steps.First(s => s.Kind == CutSimulationStepKind.Cut);
|
||
playback.Time = cut.StartTime + TimeSpan.FromTicks(cut.Duration.Ticks / 2);
|
||
|
||
var status = CutSimulationStatus.For(playback, plate, Units.Inches);
|
||
|
||
Assert.Equal($"{CutTimeFormat(playback.Time)} / {CutTimeFormat(playback.Simulation.TotalTime)}", status.Time);
|
||
Assert.Equal("1 of 2", status.Part);
|
||
Assert.Equal("square", status.Drawing);
|
||
Assert.Equal("Cutting · 60 in/min", status.Motion);
|
||
}
|
||
|
||
[Fact]
|
||
public void Status_Piercing_HasNoSpeed()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate);
|
||
var pierce = playback.Simulation.Steps.First(s => s.Kind == CutSimulationStepKind.Pierce);
|
||
playback.Time = pierce.StartTime + TimeSpan.FromTicks(pierce.Duration.Ticks / 2);
|
||
|
||
Assert.Equal("Piercing", CutSimulationStatus.For(playback, plate, Units.Inches).Motion);
|
||
}
|
||
|
||
[Fact]
|
||
public void Status_Done()
|
||
{
|
||
var plate = TwoSquares();
|
||
var playback = Loaded(plate);
|
||
playback.End();
|
||
|
||
var status = CutSimulationStatus.For(playback, plate, Units.Inches);
|
||
|
||
Assert.Equal("2 of 2", status.Part);
|
||
Assert.Equal("", status.Drawing);
|
||
Assert.Equal("Done", status.Motion);
|
||
}
|
||
|
||
[Fact]
|
||
public void FeedText_MillimetersPerSecond() =>
|
||
Assert.Equal("25 mm/sec", CutSimulationStatus.FeedText(25, Units.Millimeters));
|
||
|
||
private static string CutTimeFormat(TimeSpan time) => OpenNest.Forms.CutTimeReport.FormatTime(time);
|
||
}
|