public override string ReadLine() { if (LineIndex < GCodeFile.LineCount) { if (LineIndex > 1 && GCodeFile is GCodeMemoryFile currentMemoryFile && switchToGCode != null) { var prevlayerIndex = currentMemoryFile.GetLayerIndex(LineIndex - 1); var layerIndex = currentMemoryFile.GetLayerIndex(LineIndex); // we only try to switch as we are changing layers if (prevlayerIndex < layerIndex) { var currentBottom = currentMemoryFile.GetLayerBottom(layerIndex); // see if there is a layer height that is compatible in the new gcode for (int i = 0; i < switchToGCode.LayerCount; i++) { // find the first layer in the new code that is greater than or eaqual to our current height var switchBottom = switchToGCode.GetLayerBottom(i); if (switchBottom >= currentBottom) { // is the current gcode the same or bigger than the new gcode if (currentBottom >= switchBottom) { // switch the first time we can GCodeFile = switchToGCode; LineIndex = switchToGCode.GetFirstLayerInstruction(i); var line = $"G92 E{switchToGCode.Instruction(LineIndex).EPosition:0.###}"; switchToGCode = null; return(line); } else // only switch if we are within one layer height of the new gcode { if (currentBottom - switchBottom < switchToGCode.GetLayerHeight(layerIndex)) { GCodeFile = switchToGCode; LineIndex = switchToGCode.GetFirstLayerInstruction(i); switchToGCode = null; var line = $"G92 E{switchToGCode.Instruction(LineIndex).EPosition:0.###}"; switchToGCode = null; return(line); } } // we are done evaluating after the first found layer break; } } } } return(GCodeFile.Instruction(LineIndex++).Line); } return(null); }
public override string ReadLine() { lock (locker) { if (commandQueue.Count > 0) { var lineToSend = commandQueue[0]; commandQueue.RemoveAt(0); return(lineToSend); } } if (LineIndex < GCodeFile.LineCount) { if (LineIndex > 1 && GCodeFile is GCodeMemoryFile currentMemoryFile && switchToGCode != null) { var prevlayerIndex = currentMemoryFile.GetLayerIndex(LineIndex - 1); var layerIndex = currentMemoryFile.GetLayerIndex(LineIndex); // we only try to switch as we are changing layers if (prevlayerIndex < layerIndex) { var currentBottom = currentMemoryFile.GetLayerBottom(layerIndex); // see if there is a layer height that is compatible in the new gcode for (int i = 0; i < switchToGCode.LayerCount; i++) { // find the first layer in the new code that is greater than or equal to our current height var switchBottom = switchToGCode.GetLayerBottom(i); if (switchBottom >= currentBottom) { bool change = false; // is the current gcode the same or bigger than the new gcode if (currentBottom >= switchBottom) { change = true; } else // only switch if we are within one layer height of the new gcode { if (currentBottom - switchBottom < switchToGCode.GetLayerHeight(layerIndex)) { change = true; } } if (change) { GCodeFile = switchToGCode; LineIndex = switchToGCode.GetFirstLayerInstruction(i); var line = $"G92 E{switchToGCode.Instruction(LineIndex).EPosition:0.###}"; lock (locker) { commandQueue.Add(line); } switchToGCode = null; // return a dwell to exhaust the command queue on the firmware return("G4 P1"); } // we are done evaluating after the first found layer break; } } } } return(GCodeFile.Instruction(LineIndex++).Line); } return(null); }