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); }
private void LayerFeaturesScrollbar_SecondValueChanged(object sender, EventArgs e) { var renderInfo = sceneContext.RenderInfo; int layerIndex = renderInfo.EndLayerIndex - 1; int featuresOnLayer = sceneContext.GCodeRenderer.GetNumFeatures(layerIndex); int featureIndex = (int)(featuresOnLayer * renderInfo.FeatureToEndOnRatio0To1 + .5); int activeFeatureIndex = Math.Max(0, Math.Min(featureIndex, featuresOnLayer - 1) - 1); if (sceneContext.GCodeRenderer[layerIndex, activeFeatureIndex] is RenderFeatureTravel line) { if (rawLine != null) { rawLine.Text = gCodeMemoryFile.Instruction(line.InstructionIndex).Line; } var start = line.Start; var end = line.End; startPointWidget.Text = $"{start}"; endPointWidget.Text = $"{end}"; var length = new Vector2(start).Distance(new Vector2(end)); lengthWidget.Text = $"{length:0.###}"; // Slope // m = (y_2 - y_1) / (x_2 - x_1) // Y-Intercept // n = -x_1 * (y_2 - y_1) / (x_2 - x_1) + y_1 var slope = (end.Y - start.Y) / (end.X - start.X); if (slopeWidget != null) { slopeWidget.Text = $"{slope:0.###}"; } if (yInterceptWidget != null) { // -x_1 * (y_2 - y_1) / (x_2 - x_1) + y_1 var yIntercept = -start.X * slope + start.Y; yInterceptWidget.Text = $"{yIntercept:0.###}"; } if (xInterceptWidget != null) { // x_1 - y_1*(x_2-x_1)/(y_2-y_1) var xIntercept = start.X - start.Y * (end.X - start.X) / (end.Y - start.Y); xInterceptWidget.Text = $"{xIntercept:0.###}"; } // put in the time until the next tool change if (timeToToolChange != null) { var toolChange = gCodeMemoryFile.NextToolChange(line.InstructionIndex); if (toolChange.time < double.PositiveInfinity) { timeToToolChange.Text = $"T{toolChange.toolIndex} : {toolChange.time:0.00}s"; } else { timeToToolChange.Text = $"No More Changes"; } } } }