mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 16:29:24 -05:00
While the cut simulator is open, a left click on the plate (Select tool) pauses at the time the tool passes nearest the click instead of selecting parts. Cuts, scribes and pierces win over rapids; overlapping passes resolve to the one nearest the current time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
382 lines
11 KiB
C#
382 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);
|
||
|
||
[Fact]
|
||
public void SeekTo_PausesWhereTheToolPassesTheClick()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Play();
|
||
|
||
Assert.True(playback.SeekTo(new Vector(25, 10.1), 0.5));
|
||
|
||
Assert.False(playback.IsPlaying);
|
||
var position = playback.Simulation.ToolPositionAt(playback.Time);
|
||
Assert.Equal(25, position.X, 4);
|
||
Assert.Equal(10, position.Y, 4);
|
||
}
|
||
|
||
[Fact]
|
||
public void SeekTo_OffTheToolpath_ChangesNothing()
|
||
{
|
||
var playback = Loaded(TwoSquares());
|
||
playback.Time = TimeSpan.FromSeconds(3);
|
||
playback.Play();
|
||
|
||
Assert.False(playback.SeekTo(new Vector(15, 50), 0.5));
|
||
|
||
Assert.True(playback.IsPlaying);
|
||
Assert.Equal(TimeSpan.FromSeconds(3), playback.Time);
|
||
}
|
||
}
|