diff --git a/MatterControl.Printing/GCode/GCodeMemoryFile.cs b/MatterControl.Printing/GCode/GCodeMemoryFile.cs index 97b63cf62..e3b8d8f36 100644 --- a/MatterControl.Printing/GCode/GCodeMemoryFile.cs +++ b/MatterControl.Printing/GCode/GCodeMemoryFile.cs @@ -392,7 +392,12 @@ namespace MatterControl.Printing public override PrinterMachineInstruction Instruction(int index) { - return gCodeCommandQueue[index]; + if (index < gCodeCommandQueue.Count) + { + return gCodeCommandQueue[index]; + } + + return new PrinterMachineInstruction(""); } public override bool IsExtruding(int instructionIndexToCheck) diff --git a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs index bf202b24a..ec8b8d0c4 100644 --- a/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs +++ b/MatterControlLib/PrinterCommunication/Io/ToolChangeStream.cs @@ -137,7 +137,8 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io { // For smoothie, switch back to the extrude we were using before the temp change (smoothie switches to the specified extruder, marlin repetier do not) queuedCommands.Enqueue($"T{activeTool}"); - return $"{lineToSend.Substring(0, 4)} T{requestedToolForTempChange} S{targetTemps[requestedToolForTempChange]}"; + var temp = GetNextToolTemp(requestedToolForTempChange); + return $"{lineToSend.Substring(0, 4)} T{requestedToolForTempChange} S{temp}"; } // if we are waiting to switch to the next tool @@ -281,29 +282,51 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io } else // there are more tool changes in the future { - // get the next time we will use the current tool - var nextTimeThisTool = NextToolChange(activeTool).time; + var temp = GetNextToolTemp(activeTool); // if we do not use this tool again - if (nextTimeThisTool == double.PositiveInfinity) + if (temp == 0) { // turn off its heat gcode.AppendLine($"M104 T{activeTool} S0"); } - // If there is enough time before we will use this tool again, lower the temp by the inactive_cool_down - else if (nextTimeThisTool > timeToReheat) + // else if we need to cool down + else if (temp != targetTemps[activeTool]) { - var targetTemp = targetTemps[activeTool]; - targetTemp = Math.Max(0, targetTemp - printer.Settings.GetValue(SettingsKey.inactive_cool_down)); - if (targetTemp != printer.Connection.GetTargetHotendTemperature(activeTool)) - { - gcode.AppendLine($"M104 T{activeTool} S{targetTemp} ; INACTIVE_COOL_DOWN"); - } + gcode.AppendLine($"M104 T{activeTool} S{temp} ; INACTIVE_COOL_DOWN"); } } } + private double GetNextToolTemp(int toolIndex) + { + var timeToReheat = printer.Settings.GetValue(SettingsKey.seconds_to_reheat); + + // get the next time we will use the current tool + var nextTimeThisTool = NextToolChange(toolIndex).time; + + // if we do not use this tool again + if (nextTimeThisTool == double.PositiveInfinity) + { + // turn off its heat + return 0; + } + + // If there is enough time before we will use this tool again, lower the temp by the inactive_cool_down + else if (nextTimeThisTool > timeToReheat) + { + var targetTemp = targetTemps[toolIndex]; + targetTemp = Math.Max(0, targetTemp - printer.Settings.GetValue(SettingsKey.inactive_cool_down)); + if (targetTemp != targetTemps[toolIndex]) + { + return targetTemp; + } + } + + return targetTemps[toolIndex]; + } + private void ManageReHeating(string line) { var timeToReheat = printer.Settings.GetValue(SettingsKey.seconds_to_reheat);