From 790d68f8daad75ad9dd207a824e3088b087d9e99 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Tue, 12 Jun 2018 11:11:28 -0700 Subject: [PATCH] Find the filament diameter in our gcode files --- MatterControl.Printing/GCode/GCodeFile.cs | 2 +- .../GCode/GCodeMemoryFile.cs | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/MatterControl.Printing/GCode/GCodeFile.cs b/MatterControl.Printing/GCode/GCodeFile.cs index e707a087b..c109bf957 100644 --- a/MatterControl.Printing/GCode/GCodeFile.cs +++ b/MatterControl.Printing/GCode/GCodeFile.cs @@ -141,7 +141,7 @@ namespace MatterControl.Printing public static bool GetFirstNumberAfter(string stringToCheckAfter, string stringWithNumber, ref double readValue, int startIndex = 0, string stopCheckingString = ";") { - int stringPos = stringWithNumber.IndexOf(stringToCheckAfter, startIndex); + int stringPos = stringWithNumber.IndexOf(stringToCheckAfter, Math.Min(stringWithNumber.Length, startIndex)); int stopPos = stringWithNumber.IndexOf(stopCheckingString); if (stringPos != -1 && (stopPos == -1 || stringPos < stopPos || string.IsNullOrEmpty(stopCheckingString))) diff --git a/MatterControl.Printing/GCode/GCodeMemoryFile.cs b/MatterControl.Printing/GCode/GCodeMemoryFile.cs index d23a9c3ab..91b3f022b 100644 --- a/MatterControl.Printing/GCode/GCodeMemoryFile.cs +++ b/MatterControl.Printing/GCode/GCodeMemoryFile.cs @@ -882,22 +882,24 @@ namespace MatterControl.Printing { if (filamentDiameterCache == 0) { - for (int i = 0; i < Math.Min(20, GCodeCommandQueue.Count); i++) + // check the beginning of the file for the filament diameter + for (int i = 0; i < Math.Min(100, GCodeCommandQueue.Count); i++) { - if (GetFirstNumberAfter("filamentDiameter =", GCodeCommandQueue[i].Line, ref filamentDiameterCache)) + if(FindDiameter(i, ref filamentDiameterCache)) { break; } } + // check the end of the file for the filament diameter if (filamentDiameterCache == 0) { // didn't find it, so look at the end of the file for filament_diameter = - string lookFor = "; filament_diameter =";// 2.85 for (int i = GCodeCommandQueue.Count - 1; i > Math.Max(0, GCodeCommandQueue.Count - 100); i--) { - if (GetFirstNumberAfter(lookFor, GCodeCommandQueue[i].Line, ref filamentDiameterCache)) + if (FindDiameter(i, ref filamentDiameterCache)) { + break; } } } @@ -912,6 +914,21 @@ namespace MatterControl.Printing return filamentDiameterCache; } + private bool FindDiameter(int lineIndex, ref double filamentDiameterCache) + { + if (GetFirstNumberAfter("filamentDiameter = ", GCodeCommandQueue[lineIndex].Line, ref filamentDiameterCache, 0, "")) + { + return true; + } + + if (GetFirstNumberAfter("; filament_diameter = ", GCodeCommandQueue[lineIndex].Line, ref filamentDiameterCache, 0, "")) + { + return true; + } + + return false; + } + public override double GetLayerHeight(int layerIndex) { if (layerHeights.Count > 0)