diff --git a/MatterControl.Printing/GCode/GCodeFileStreamed.cs b/MatterControl.Printing/GCode/GCodeFileStreamed.cs index bbf676802..7e5fbd2dc 100644 --- a/MatterControl.Printing/GCode/GCodeFileStreamed.cs +++ b/MatterControl.Printing/GCode/GCodeFileStreamed.cs @@ -231,9 +231,6 @@ namespace MatterControl.Printing int ringBufferIndex = readLineCount % MaxLinesToBuffer; readLinesRingBuffer[ringBufferIndex] = new PrinterMachineInstruction(line); - PrinterMachineInstruction instruction = readLinesRingBuffer[ringBufferIndex]; - Vector3 deltaPositionThisLine = new Vector3(); - double deltaEPositionThisLine = 0; string lineToParse = line.ToUpper().Trim(); if (lineToParse.StartsWith("G0") || lineToParse.StartsWith("G1")) { @@ -251,9 +248,6 @@ namespace MatterControl.Printing double ePosition = lastEPosition; GetFirstNumberAfter("E", lineToParse, ref ePosition); - deltaPositionThisLine = attemptedDestination - lastPrinterPosition; - deltaEPositionThisLine = Math.Abs(ePosition - lastEPosition); - lastPrinterPosition = attemptedDestination; lastEPosition = ePosition; } diff --git a/MatterControlLib/Library/Export/GCodeExport.cs b/MatterControlLib/Library/Export/GCodeExport.cs index 3898f09d9..de3992374 100644 --- a/MatterControlLib/Library/Export/GCodeExport.cs +++ b/MatterControlLib/Library/Export/GCodeExport.cs @@ -41,6 +41,7 @@ using MatterHackers.Agg.UI; using MatterHackers.DataConverters3D; using MatterHackers.Localizations; using MatterHackers.MatterControl.ConfigurationPage.PrintLeveling; +using MatterHackers.MatterControl.PrinterCommunication; using MatterHackers.MatterControl.PrinterCommunication.Io; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.VectorMath; @@ -348,6 +349,7 @@ namespace MatterHackers.MatterControl.Library.Export // Run each line from the source gcode through the loaded pipeline and dump to the output location using (var file = new StreamWriter(outputPath)) { + PrinterMove currentDestination = PrinterMove.Unknown; string nextLine = finalStream.ReadLine(); while (nextLine != null) { @@ -357,6 +359,23 @@ namespace MatterHackers.MatterControl.Library.Export } nextLine = finalStream.ReadLine(); + + if (nextLine != null) + { + if (nextLine.StartsWith("G92")) + { + // read out the position and store right now + GCodeFile.GetFirstNumberAfter("X", nextLine, ref currentDestination.position.X); + GCodeFile.GetFirstNumberAfter("Y", nextLine, ref currentDestination.position.Y); + GCodeFile.GetFirstNumberAfter("Z", nextLine, ref currentDestination.position.X); + GCodeFile.GetFirstNumberAfter("E", nextLine, ref currentDestination.extrusion); + + // The printer position has changed, make sure all the streams know + finalStream.SetPrinterPosition(currentDestination); + } + + PrinterConnection.KeepTrackOfAbsolutePositionAndDestination(nextLine, ref currentDestination); + } } } } diff --git a/MatterControlLib/PrinterCommunication/PrinterConnection.cs b/MatterControlLib/PrinterCommunication/PrinterConnection.cs index 96fa2c4d0..928832f4d 100644 --- a/MatterControlLib/PrinterCommunication/PrinterConnection.cs +++ b/MatterControlLib/PrinterCommunication/PrinterConnection.cs @@ -2324,7 +2324,7 @@ Make sure that your printer is turned on. Some printers will appear to be connec PrintingCanContinue(line); } - private void KeepTrackOfAbsolutePositionAndDestination(string lineBeingSent) + public static bool KeepTrackOfAbsolutePositionAndDestination(string lineBeingSent, ref PrinterMove currentDestination) { if (lineBeingSent.StartsWith("G0 ") || lineBeingSent.StartsWith("G1 ") @@ -2344,9 +2344,11 @@ Make sure that your printer is turned on. Some printers will appear to be connec || currentDestination.extrusion != newDestination.extrusion) { currentDestination = newDestination; - DestinationChanged?.Invoke(this, null); + return true; } } + + return false; } private void CreateStreamProcessors(Stream gcodeStream = null) @@ -2891,7 +2893,10 @@ Make sure that your printer is turned on. Some printers will appear to be connec // remove the comment if any lineToWrite = RemoveCommentIfAny(lineToWrite); - KeepTrackOfAbsolutePositionAndDestination(lineToWrite); + if (KeepTrackOfAbsolutePositionAndDestination(lineToWrite, ref currentDestination)) + { + DestinationChanged?.Invoke(this, null); + } if (this.SendWithChecksum && sendLineWithChecksum) { diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 78eb57f49..930312b7c 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 78eb57f49593f4dec1c8df517ab94bcba54c8cd2 +Subproject commit 930312b7cd5c835511297c374064f7b6f83e0a83 diff --git a/Tests/MatterControl.AutomationTests/ExportItemWindowTests.cs b/Tests/MatterControl.AutomationTests/ExportItemWindowTests.cs index 67b2a34f4..5d19fd5bc 100644 --- a/Tests/MatterControl.AutomationTests/ExportItemWindowTests.cs +++ b/Tests/MatterControl.AutomationTests/ExportItemWindowTests.cs @@ -8,6 +8,8 @@ using NUnit.Framework; using MatterHackers.MatterControl.PrinterCommunication.Io; using MatterHackers.VectorMath; using System.Collections.Generic; +using System.Diagnostics; +using System; namespace MatterHackers.MatterControl.Tests.Automation { @@ -63,8 +65,6 @@ namespace MatterHackers.MatterControl.Tests.Automation [Test] public async Task ExportStreamG92HandlingTest() { - var startGCode = "G28\\nM109 S[Temperature]\\nG1 Y5 X5 Z0.8 F1800\\nG92 E0\\nG1 X100 Z0.3 E25 F900\\nG92 E0\\nG1 E-2 F2400\\nG92 E0\\nG1 E1 F900"; - await MatterControlUtilities.RunTest(testRunner => { testRunner.WaitForFirstDraw(); @@ -72,7 +72,6 @@ namespace MatterHackers.MatterControl.Tests.Automation testRunner.CloneAndSelectPrinter("No Retraction after Purge"); var printer = testRunner.FirstPrinter(); - printer.Settings.SetValue(SettingsKey.start_gcode, startGCode); //Navigate to Downloads Library Provider testRunner.NavigateToFolder("Print Queue Row Item Collection"); @@ -104,21 +103,10 @@ namespace MatterHackers.MatterControl.Tests.Automation var filename = fullPathToGcodeFile + ".gcode"; testRunner.WaitFor(() => File.Exists(filename), 10) + .WaitFor(() => !IsFileLocked(filename), 1000) .Delay(2); - var inputLines = new string[] - { - "G28 ; home all axes", - "M109 S[temperature]", - "", - "G1 Y5 X5 Z0.8 F1800; Purge line", - "G92 E0; Purge line", - "G1 X100 Z0.3 E25 F900; Purge line", - "G92 E0; Purge line", - "G1 E-2 F2400; Purge line", - "M75; start print timer" - }; - + // validate that the actual printer output has the right lines var expectedLines = new string[] { "G28 ; home all axes", @@ -127,18 +115,18 @@ namespace MatterHackers.MatterControl.Tests.Automation "M280 P0 S90", "M109 S205", "G1 X5 Y5 Z3.13 F1800", - "G92 E0; Purge line", - "G1 X100 Y5 Z2.28 E25 F900", - "G92 E0; Purge line", - "G1 E-2", - "M75 ; start print timer" + "G92 E0 ; Purge line", + "G1 X5.83 Y5 Z3.04 E0.833 F900", + "G1 X6.67 Y5 Z2.96 E1.667", + "G1 X7.5 Y5 Z2.87 E2.5", + "G1 X8.33 Y5 Z2.79 E3.333", + "G1 X9.17 Y5 Z2.7 E4.167", + "G1 X10 Y5 Z2.62 E5", + "G92 E0 ; Purge line", + "G1 X10 Y5 Z2.62 E-2 F2400", + "M75 ; start print timer", }; - // validate that the gcode export stack has the right output - var testStream = GCodeExport.GetExportStream(printer, new TestGCodeStream(printer, inputLines), true); - ValidateStreamResponse(expectedLines, testStream); - - // validate that the actual printer output has the right lines var actualLines = File.ReadAllLines(filename); ValidateLinesStartingWithFirstExpected(expectedLines, actualLines); @@ -148,89 +136,46 @@ namespace MatterHackers.MatterControl.Tests.Automation }, maxTimeToRun: 200); } - private void ValidateLinesStartingWithFirstExpected(string[] expectedLines, string[] actualLines) + private bool IsFileLocked(string file) { - throw new System.NotImplementedException(); - } - - public static void ValidateStreamResponse(string[] expected, GCodeStream testStream, List streamList = null) - { - int lineIndex = 0; - - // Advance - string actualLine = testStream.ReadLine(); - string expectedLine = expected[lineIndex++]; - - while (actualLine != null) + try { - if (actualLine.StartsWith("G92 E0")) + using (FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None)) { - testStream.SetPrinterPosition(new PrinterMove(default(Vector3), 0, 300)); - } - - if (actualLine.StartsWith("G92 Z0")) - { - testStream.SetPrinterPosition(new PrinterMove(new Vector3(), 0, 0)); - } - - if (actualLine == "; do_resume") - { - PauseHandlingStream pauseStream = null; - foreach (var stream in streamList) - { - if (stream as PauseHandlingStream != null) - { - pauseStream = (PauseHandlingStream)stream; - pauseStream.Resume(); - } - } - } - - if (expectedLine != actualLine) - { - int a = 0; - } - - Debug.WriteLine(actualLine); - Assert.AreEqual(expectedLine, actualLine, "Unexpected response from testStream"); - - // Advance - actualLine = testStream.ReadLine(); - if (lineIndex < expected.Length) - { - expectedLine = expected[lineIndex++]; + stream.Close(); } } + catch (IOException) + { + //the file is unavailable because it is: + //still being written to + //or being processed by another thread + //or does not exist (has already been processed) + return true; + } + + //file is not locked + return false; + } + + private void ValidateLinesStartingWithFirstExpected(string[] expectedLines, string[] actualLines) + { + // search actual lines until we find the first expectedLine + for (int i = 0; i < actualLines.Length; i++) + { + if (actualLines[i] == expectedLines[0]) + { + for (int j = 0; j < expectedLines.Length; j++) + { + Assert.AreEqual(actualLines[i + j], expectedLines[j], "All lines should match"); + // Debug.WriteLine("\"" + actualLines[i + j] + "\","); + } + + return; + } + } + + throw new Exception("Did not find the first expected line"); } } - - public class TestGCodeStream : GCodeStream - { - private int index = 0; - private string[] lines; - - public TestGCodeStream(PrinterConfig printer, string[] lines) - : base(printer) - { - this.lines = lines; - } - - public override void Dispose() - { - } - - public override string ReadLine() - { - return index < lines.Length ? lines[index++] : null; - } - - public override void SetPrinterPosition(PrinterMove position) - { - } - - public override GCodeStream InternalStream => null; - - public override string DebugInfo => ""; - } - } diff --git a/Tests/TestData/TestProfiles/No Retraction after Purge.printer b/Tests/TestData/TestProfiles/No Retraction after Purge.printer index c74cebba1..9d9ba809f 100644 --- a/Tests/TestData/TestProfiles/No Retraction after Purge.printer +++ b/Tests/TestData/TestProfiles/No Retraction after Purge.printer @@ -363,7 +363,7 @@ "solid_infill_speed": "60", "spiral_vase": "0", "standby_temperature_delta": "-5", - "start_gcode": "G28 ; home all axes\\nM109 S[temperature]\\n\\nG1 Y5 X5 Z0.8 F1800 ; Purge line\\nG92 E0 ; Purge line\\nG1 X100 Z0.3 E25 F900 ; Purge line\\nG92 E0 ; Purge line\\nG1 E-2 F2400 ; Purge line\\nM75 ; start print timer ", + "start_gcode": "G28 ; home all axes\\nM109 S[temperature]\\n\\nG1 Y5 X5 Z0.8 F1800 ; Purge line\\nG92 E0 ; Purge line\\nG1 X10 Z0.3 E5 F900 ; Purge line\\nG92 E0 ; Purge line\\nG1 E-2 F2400 ; Purge line\\nM75 ; start print timer ", "start_perimeters_at_concave_points": "0", "start_perimeters_at_non_overhang": "0", "support_material": "0",