using System.Drawing; using OpenNest.CNC; using OpenNest.CNC.Thermal; using OpenNest.Controls; using OpenNest.Geometry; using static OpenNest.WinForms.Tests.FormTesting; namespace OpenNest.WinForms.Tests.Controls; public class CutSimulationOverlayTests { private static readonly CutParameters Parameters = new() { Feedrate = 100, RapidTravelRate = 600, PierceTime = TimeSpan.FromSeconds(1), Units = Units.Inches, }; private static Plate TwoSquares() { var plate = new Plate(48, 96); plate.Parts.Add(new Part(new Drawing("a", TestPrograms.Square(10)), new Vector(5, 5))); plate.Parts.Add(new Part(new Drawing("b", TestPrograms.Square(10)), new Vector(25, 5))); return plate; } [Fact] public void PartsAheadOfTheTorchArePending() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares() }; var overlay = view.CutSimulation; var sim = CutSimulation.Build(view.Plate, Parameters); overlay.Load(sim); Assert.True(overlay.IsPartPending(0)); // still traveling to the first part var firstCut = sim.Steps.First(s => s.Kind == CutSimulationStepKind.Cut); overlay.Time = firstCut.StartTime + firstCut.Duration / 2; Assert.False(overlay.IsPartPending(0)); Assert.True(overlay.IsPartPending(1)); overlay.Time = sim.TotalTime; Assert.False(overlay.IsPartPending(1)); }); [Fact] public void FinishedCutsAreDrawnOnlyBehindThePlayhead() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares() }; var sim = CutSimulation.Build(view.Plate, Parameters); view.CutSimulation.Load(sim); var secondPartCut = sim.Steps.First(s => s.PartIndex == 1 && s.Kind == CutSimulationStepKind.Cut); view.CutSimulation.Time = secondPartCut.StartTime; using var bitmap = new Bitmap(400, 400); using var graphics = Graphics.FromImage(bitmap); graphics.TranslateTransform(20, 380); graphics.Clear(Color.Black); view.CutSimulation.Draw(graphics); // Bottom edge midpoints of each square, in graph coordinates. var cut = Pixel(view, bitmap, new Vector(10, 5)); var uncut = Pixel(view, bitmap, new Vector(35, 15)); Assert.True(cut.R > 200 && cut.G < 160, $"first part trail missing: {cut}"); Assert.Equal(Color.Black.ToArgb(), uncut.ToArgb()); }); [Fact] public void ZoomedInTrailIsDrawnWherePannedTo() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares() }; var sim = CutSimulation.Build(view.Plate, Parameters); view.CutSimulation.Load(sim); view.CutSimulation.Time = sim.TotalTime; view.ZoomToPoint(new Vector(10, 5), 100 / view.ViewScale, false); // trail far larger than the screen using var bitmap = new Bitmap(400, 400); using var graphics = Graphics.FromImage(bitmap); foreach (var target in new[] { new Vector(10, 5), new Vector(35, 15) }) { var point = view.PointWorldToGraph(target); graphics.ResetTransform(); graphics.Clear(Color.Black); graphics.TranslateTransform(200 - point.X, 200 - point.Y); view.CutSimulation.Draw(graphics); var pixel = bitmap.GetPixel(200, 200); Assert.True(pixel.R > 200 && pixel.G < 160, $"trail missing at {target}: {pixel}"); } }); // Small etched text zoomed out: each arc is a few hundredths of a pixel long, and playback // adds them to the trail one at a time. GDI+ throws "Out of memory" stroking such an arc // on its own. [Theory] [InlineData(0.3f)] [InlineData(1f)] [InlineData(3f)] public void SubPixelArcsDrawnOneAtATimeDoNotFailThePaint(float scale) => RunSta(() => { var plate = new Plate(48, 96); plate.Parts.Add(new Part(new Drawing("dot", TinyArcs(0.03)), new Vector(5, 5))); plate.Parts.Add(new Part(new Drawing("wide", TinyArcs(1)), new Vector(8, 5))); using var view = new PlateView { Plate = plate }; view.ZoomToPoint(new Vector(0, 0), scale / view.ViewScale, false); var sim = CutSimulation.Build(plate, Parameters); view.CutSimulation.Load(sim); Assert.Contains(sim.Steps, s => s.IsArc); using var bitmap = new Bitmap(400, 400); using var graphics = Graphics.FromImage(bitmap); graphics.TranslateTransform(20.4f, 380.2f); foreach (var step in sim.Steps) { view.CutSimulation.Time = step.StartTime + step.Duration; view.CutSimulation.Draw(graphics); } }); // A closed loop of arcs of mixed sweeps, 1 to 20 degrees, on one circle. private static Program TinyArcs(double radius) { var program = new Program(); program.MoveTo(radius, 0); var angle = 0.0; var sweeps = new[] { 1.0, 3.0, 20.0 }; for (var i = 0; angle < 360; i++) { angle = System.Math.Min(360, angle + sweeps[i % sweeps.Length]); var radians = angle * System.Math.PI / 180; program.ArcTo(radius * System.Math.Cos(radians), radius * System.Math.Sin(radians), 0, 0, RotationType.CCW); } return program; } [Fact] public void RetimingKeepsTheTorchWhereItWas() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares() }; var overlay = view.CutSimulation; var slow = CutSimulation.Build(view.Plate, Parameters); overlay.Load(slow); var cut = slow.Steps.Last(s => s.Kind == CutSimulationStepKind.Cut); overlay.Time = cut.StartTime + cut.Duration * 0.4; var before = slow.ToolPositionAt(overlay.Time); var fast = CutSimulation.Build(view.Plate, new CutParameters { Feedrate = 100, RapidTravelRate = 6000, PierceTime = TimeSpan.FromSeconds(1), Units = Units.Inches, }); overlay.Load(fast); Assert.True(overlay.Time < cut.StartTime); // faster rapids moved it earlier var after = fast.ToolPositionAt(overlay.Time); Assert.Equal(before.X, after.X, 6); Assert.Equal(before.Y, after.Y, 6); }); [Fact] public void PlateSwitchClearsTheSimulation() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares() }; view.CutSimulation.Load(CutSimulation.Build(view.Plate, Parameters)); view.CutSimulation.Play(); Assert.True(view.CutSimulation.IsPlaying); view.Plate = new Plate(); Assert.False(view.CutSimulation.IsActive); Assert.False(view.CutSimulation.IsPlaying); }); [Fact] public void ClickingThePlate_SeeksThereInsteadOfSelecting() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares(), Size = new System.Drawing.Size(800, 600) }; view.ZoomToFit(); var sim = CutSimulation.Build(view.Plate, Parameters); view.CutSimulation.Load(sim); // The second square's bottom edge, partway along. var at = view.PointWorldToControl(new Vector(30, 5)); Mouse(view, "OnMouseDown", at); Mouse(view, "OnMouseUp", at); Assert.Empty(view.SelectedParts); Assert.Equal(1, sim.Steps[sim.IndexAt(view.CutSimulation.Time)].PartIndex); Assert.True(sim.ToolPositionAt(view.CutSimulation.Time).DistanceTo(new Vector(30, 5)) < 6 / view.ViewScale); }); [Fact] public void ClickingThePlate_SelectsOnceTheSimulatorCloses() => RunSta(() => { using var view = new PlateView { Plate = TwoSquares(), Size = new System.Drawing.Size(800, 600) }; view.ZoomToFit(); view.CutSimulation.Load(CutSimulation.Build(view.Plate, Parameters)); view.CutSimulation.Clear(); var at = view.PointWorldToControl(new Vector(30, 10)); Mouse(view, "OnMouseDown", at); Mouse(view, "OnMouseUp", at); Assert.Single(view.SelectedParts); }); [Fact] public void PartsShareOneGrayUnderTheHeatLayer() => RunSta(() => { var plate = TwoSquares(); plate.Parts[0].BaseDrawing.Color = Color.FromArgb(60, 200, 60); using var view = new PlateView { Plate = plate, Size = new System.Drawing.Size(400, 400) }; view.ZoomToFit(); var sim = CutSimulation.Build(plate, Parameters); view.CutSimulation.Load(sim); view.CutSimulation.Time = sim.TotalTime; // both parts cut, so neither is muted var heat = new PlateHeatSimulation(plate, sim, new ThermalParameters { Thickness = 0.25, Units = Units.Inches, }); // A scale no cell comes near keeps the layer clear, leaving the part fill to read. view.CutSimulation.LoadHeat(heat, 1e9f); var center = view.PointWorldToControl(new Vector(7, 7)); // clear of the number label view.CutSimulation.HeatMode = HeatView.Now; var gray = CutSimulationOverlay.HeatPartFill(view.ColorScheme.LayoutFillColor); Assert.Equal(gray.ToArgb(), PaintedPixel(view, center).ToArgb()); view.CutSimulation.HeatMode = HeatView.Off; Assert.Equal(Color.FromArgb(60, 200, 60).ToArgb(), PaintedPixel(view, center).ToArgb()); }); [Theory] [InlineData(242, 242, 242)] [InlineData(52, 48, 58)] public void HeatPartFillIsANeutralGrayApartFromTheSheet(int r, int g, int b) { var sheet = Color.FromArgb(r, g, b); var fill = CutSimulationOverlay.HeatPartFill(sheet); Assert.True(fill.R == fill.G && fill.G == fill.B); Assert.True(System.Math.Abs(fill.R - sheet.G) >= 50); } private static Color PaintedPixel(PlateView view, System.Drawing.Point at) { using var bitmap = new Bitmap(view.Width, view.Height); using (var graphics = Graphics.FromImage(bitmap)) Raise(view, "OnPaint", new System.Windows.Forms.PaintEventArgs(graphics, view.ClientRectangle)); return bitmap.GetPixel(at.X, at.Y); } private static Color Pixel(PlateView view, Bitmap bitmap, Vector world) { var point = view.PointWorldToGraph(world); return bitmap.GetPixel((int)System.Math.Round(point.X + 20), (int)System.Math.Round(point.Y + 380)); } private static void RunSta(System.Action action) => StaTestThread.Run(action, TimeSpan.FromSeconds(30), "Cut simulation overlay test timed out."); }