diff --git a/MatterControl.Printing/GCode/GCodeFile.cs b/MatterControl.Printing/GCode/GCodeFile.cs index 2aebdede0..9b7157b23 100644 --- a/MatterControl.Printing/GCode/GCodeFile.cs +++ b/MatterControl.Printing/GCode/GCodeFile.cs @@ -28,6 +28,8 @@ either expressed or implied, of the FreeBSD Project. */ using System; using System.IO; +using System.Linq; +using System.Text.RegularExpressions; using System.Threading; using MatterHackers.Agg; using MatterHackers.VectorMath; @@ -36,7 +38,7 @@ namespace MatterControl.Printing { public abstract class GCodeFile { - public static string PostProcessedExtension = ".postprocessed.gcode"; + public const string PostProcessedExtension = ".postprocessed.gcode"; #if __ANDROID__ protected const int Max32BitFileSize = 10000000; // 10 megs @@ -104,11 +106,39 @@ namespace MatterControl.Printing return checksum; } - public static bool IsLayerChange(string lineString) + private static Regex firstDigitsAfterToken = new Regex("\\d+", RegexOptions.CultureInvariant | RegexOptions.Compiled); + + private static string[] layerLineStartTokens = new[] { - return lineString.StartsWith("; LAYER:") - || lineString.StartsWith(";LAYER:") - || lineString.StartsWith("; layer "); + "; LAYER:", + ";LAYER:", + "; layer ", + }; + + public static bool IsLayerChange(string line) + { + return layerLineStartTokens.Any(l => line.StartsWith(l)); + } + + public static int GetLayerNumber(string line) + { + var layerToken = layerLineStartTokens.FirstOrDefault(t => line.StartsWith(t)); + + if (layerToken != null) + { + line = line.Substring(layerToken.Length); + + // Find the first digits after the layer start token + var match = firstDigitsAfterToken.Match(line); + + if (match.Success + && int.TryParse(match.Value, out int layerNumber)) + { + return layerNumber; + } + } + + return 0; } public static bool FileTooBigToLoad(Stream fileStream) diff --git a/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs b/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs index d2ebdb7c0..b190c9b31 100644 --- a/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/PauseHandlingStream.cs @@ -228,13 +228,14 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io if (GCodeFile.IsLayerChange(lineToSend)) { - string layerNumber = lineToSend.Split(':')[1]; + int layerNumber = GCodeFile.GetLayerNumber(lineToSend); + if (PauseOnLayer(layerNumber)) { - // make the string 1 based (the internal code is 0 based) - int layerIndex; - int.TryParse(layerNumber, out layerIndex); - DoPause(PauseReason.PauseLayerReached, layerIndex + 1); + this.DoPause( + PauseReason.PauseLayerReached, + // make the layer 1 based (the internal code is 0 based) + layerNumber + 1); } } else if (lineToSend.StartsWith("M226") @@ -318,18 +319,17 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io } } - private bool PauseOnLayer(string layer) + private bool PauseOnLayer(int layerNumber) { - int layerNumber; var printerRecoveryStream = internalStream as PrintRecoveryStream; - if (int.TryParse(layer, out layerNumber) - && printer.Settings.Helpers.LayerToPauseOn().Contains(layerNumber) + if (printer.Settings.Helpers.LayerToPauseOn().Contains(layerNumber) && (printerRecoveryStream == null || printerRecoveryStream.RecoveryState == RecoveryState.PrintingToEnd)) { return true; } + return false; } } diff --git a/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs b/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs index 27666024b..1ae5efbc5 100644 --- a/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs @@ -126,7 +126,7 @@ namespace MatterControl.Tests.MatterControl "G1 Z10 F1800", "M114", "M109 S[temperature]", - null, + null, }; AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); @@ -160,7 +160,7 @@ namespace MatterControl.Tests.MatterControl "M117 Ready ", "M119", "switch filament; WRITE_RAW", - null, + null, }; AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); @@ -192,7 +192,7 @@ namespace MatterControl.Tests.MatterControl { "G1 X0 Y0 Z0 E0 F1000", "G1 X10", - null, + null, }; AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); @@ -234,7 +234,7 @@ namespace MatterControl.Tests.MatterControl "G1 X8 Y0 Z-0.1", "G1 X9 Y0 Z-0.1", "G1 X10 Y0 Z-0.1", - null, + null, }; AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); @@ -320,7 +320,7 @@ namespace MatterControl.Tests.MatterControl "G1 E-1 F301", "G1 E-2", "G90", - null, + null, }; AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); @@ -822,7 +822,7 @@ namespace MatterControl.Tests.MatterControl } string expectedLine = expected[expectedIndex++]; - if(expectedLine != actualLineWithoutChecksum) + if (expectedLine != actualLineWithoutChecksum) { int a = 0; } @@ -841,7 +841,7 @@ namespace MatterControl.Tests.MatterControl { Thread.Sleep(1000); } - + // start a print var inputStream = new MemoryStream(Encoding.ASCII.GetBytes(string.Join("\n", inputGCode))); printer.Connection.CommunicationState = MatterHackers.MatterControl.PrinterCommunication.CommunicationStates.PreparingToPrint; @@ -849,7 +849,7 @@ namespace MatterControl.Tests.MatterControl // wait for the print to finish (or 3 minutes to pass) time = Stopwatch.StartNew(); - while(printer.Connection.Printing + while (printer.Connection.Printing && time.ElapsedMilliseconds < (1000 * 60 * 3)) { Thread.Sleep(1000); @@ -863,6 +863,14 @@ namespace MatterControl.Tests.MatterControl throw new System.NotImplementedException(); } + [Test] + public void KnownLayerLinesTest() + { + Assert.AreEqual(8, GCodeFile.GetLayerNumber("; layer 8, Z = 0.800"), "Simplify3D ~ 2019"); + Assert.AreEqual(1, GCodeFile.GetLayerNumber("; LAYER:1"), "Cura/MatterSlice"); + Assert.AreEqual(7, GCodeFile.GetLayerNumber(";LAYER:7"), "Slic3r Prusa Edition 1.38.7-prusa3d on 2018-04-25"); + } + [Test, Category("GCodeStream")] public void WriteReplaceStreamTests() { @@ -903,7 +911,8 @@ namespace MatterControl.Tests.MatterControl var inputLinesStream = new TestGCodeStream(printer, inputLines); var queueStream = new QueuedCommandsStream(printer, inputLinesStream); - ProcessWriteRegexStream writeStream = new ProcessWriteRegexStream(printer, queueStream, queueStream); + + var writeStream = new ProcessWriteRegexStream(printer, queueStream, queueStream); ValidateStreamResponse(expected, writeStream); }