From ffb743f92551ee3bbb362a911c8e7fd4a4ea463c Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 8 Sep 2021 16:32:23 -0700 Subject: [PATCH] Making gcode visualizer parse relative extruder correctly issue: MatterHackers/MCCentral#3913 Made prusa layer changes show by default Problems with GCode from Simplify3D and relative extrusion --- MatterControl.Printing/GCode/GCodeFile.cs | 22 ++++++++++--- .../GCode/GCodeMemoryFile.cs | 33 ++++++++++++------- .../GCode/PrinterMachineInstruction.cs | 16 ++++++--- .../MappingClasses/MapLayerChangeGCode.cs | 2 +- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/MatterControl.Printing/GCode/GCodeFile.cs b/MatterControl.Printing/GCode/GCodeFile.cs index e3614a9c0..48357be04 100644 --- a/MatterControl.Printing/GCode/GCodeFile.cs +++ b/MatterControl.Printing/GCode/GCodeFile.cs @@ -114,20 +114,34 @@ namespace MatterControl.Printing "; LAYER:", ";LAYER:", "; layer ", + ";LAYER_CHANGE" // prusa slicer default }; public static bool IsLayerChange(string line) { - return LayerLineStartTokens.Any(l => line.StartsWith(l)); + return LayerChangeString(line) != null; + } + + public static string LayerChangeString(string line) + { + for (int i = 0; i < LayerLineStartTokens.Length; i++) + { + if (line.StartsWith(LayerLineStartTokens[i])) + { + return LayerLineStartTokens[i]; + } + } + + return null; } public static int GetLayerNumber(string line) { - var layerToken = LayerLineStartTokens.FirstOrDefault(t => line.StartsWith(t)); + var layerChangeString = LayerChangeString(line); - if (layerToken != null) + if (layerChangeString != null) { - line = line.Substring(layerToken.Length); + line = line.Substring(layerChangeString.Length); // Find the first digits after the layer start token var match = FirstDigitsAfterToken.Match(line); diff --git a/MatterControl.Printing/GCode/GCodeMemoryFile.cs b/MatterControl.Printing/GCode/GCodeMemoryFile.cs index ae773f480..bb0f6e850 100644 --- a/MatterControl.Printing/GCode/GCodeMemoryFile.cs +++ b/MatterControl.Printing/GCode/GCodeMemoryFile.cs @@ -224,14 +224,15 @@ namespace MatterControl.Printing double ePosition = lastEPosition; if (GetFirstNumberAfter("E", lineToParse, ref ePosition)) { - if (instruction.MovementType == PrinterMachineInstruction.MovementTypes.Absolute) + if (instruction.XyzeMovementType == PrinterMachineInstruction.MovementTypes.Relative + || instruction.ExtuderRelativeOverride) { - double deltaEPosition = ePosition - lastEPosition; - filamentMm += deltaEPosition; + filamentMm += ePosition; } else { - filamentMm += ePosition; + double deltaEPosition = ePosition - lastEPosition; + filamentMm += deltaEPosition; } lastEPosition = ePosition; @@ -650,7 +651,7 @@ namespace MatterControl.Printing break; case 'M': - loadedGCodeFile.ParseMLine(lineString); + loadedGCodeFile.ParseMLine(lineString, machineInstructionForLine); break; case 'T': @@ -878,7 +879,7 @@ namespace MatterControl.Printing { double ePosition = processingMachineState.EPosition; var position = processingMachineState.Position; - if (processingMachineState.MovementType == PrinterMachineInstruction.MovementTypes.Relative) + if (processingMachineState.XyzeMovementType == PrinterMachineInstruction.MovementTypes.Relative) { position = Vector3.Zero; ePosition = 0; @@ -895,16 +896,24 @@ namespace MatterControl.Printing processingMachineState.FeedRate = (float)feedrate; } - if (processingMachineState.MovementType == PrinterMachineInstruction.MovementTypes.Absolute) + if (processingMachineState.XyzeMovementType == PrinterMachineInstruction.MovementTypes.Absolute) { processingMachineState.Position = position; - processingMachineState.EPosition = (float)ePosition; } else { processingMachineState.Position += position; + } + + if (processingMachineState.XyzeMovementType == PrinterMachineInstruction.MovementTypes.Relative + || processingMachineState.ExtuderRelativeOverride) + { processingMachineState.EPosition += (float)ePosition; } + else + { + processingMachineState.EPosition = (float)ePosition; + } } if (!gcodeHasExplicitLayerChangeInfo) @@ -942,11 +951,11 @@ namespace MatterControl.Printing break; case "90": // G90 is Absolute Distance Mode - processingMachineState.MovementType = PrinterMachineInstruction.MovementTypes.Absolute; + processingMachineState.XyzeMovementType = PrinterMachineInstruction.MovementTypes.Absolute; break; case "91": // G91 is Incremental Distance Mode - processingMachineState.MovementType = PrinterMachineInstruction.MovementTypes.Relative; + processingMachineState.XyzeMovementType = PrinterMachineInstruction.MovementTypes.Relative; break; case "92": @@ -997,7 +1006,7 @@ namespace MatterControl.Printing } } - private void ParseMLine(string lineString) + private void ParseMLine(string lineString, PrinterMachineInstruction processingMachineState) { // take off any comments before we check its length int commentIndex = lineString.IndexOf(';'); @@ -1039,10 +1048,12 @@ namespace MatterControl.Printing case "82": // set extruder to absolute mode + processingMachineState.ExtuderRelativeOverride = false; break; case "83": // Set extruder to relative mode + processingMachineState.ExtuderRelativeOverride = true; break; case "84": diff --git a/MatterControl.Printing/GCode/PrinterMachineInstruction.cs b/MatterControl.Printing/GCode/PrinterMachineInstruction.cs index 6544396e3..827128868 100644 --- a/MatterControl.Printing/GCode/PrinterMachineInstruction.cs +++ b/MatterControl.Printing/GCode/PrinterMachineInstruction.cs @@ -48,7 +48,12 @@ namespace MatterControl.Printing public byte[] byteLine; // Absolute is the RepRap default - public MovementTypes MovementType = MovementTypes.Absolute; + public MovementTypes XyzeMovementType { get; internal set; } = MovementTypes.Absolute; + + /// + /// Overrides the + /// + public bool ExtuderRelativeOverride { get; internal set; } = false; public float SecondsThisLine; @@ -69,7 +74,8 @@ namespace MatterControl.Printing xyzPosition = copy.xyzPosition; FeedRate = copy.FeedRate; EPosition = copy.EPosition; - MovementType = copy.MovementType; + XyzeMovementType = copy.XyzeMovementType; + ExtuderRelativeOverride = copy.ExtuderRelativeOverride; SecondsToEndFromHere = copy.SecondsToEndFromHere; ToolIndex = copy.ToolIndex; } @@ -111,7 +117,7 @@ namespace MatterControl.Printing get { return xyzPosition.X; } set { - if (MovementType == MovementTypes.Absolute) + if (XyzeMovementType == MovementTypes.Absolute) { xyzPosition.X = (float)value; } @@ -127,7 +133,7 @@ namespace MatterControl.Printing get { return xyzPosition.Y; } set { - if (MovementType == MovementTypes.Absolute) + if (XyzeMovementType == MovementTypes.Absolute) { xyzPosition.Y = (float)value; } @@ -143,7 +149,7 @@ namespace MatterControl.Printing get { return xyzPosition.Z; } set { - if (MovementType == MovementTypes.Absolute) + if (XyzeMovementType == MovementTypes.Absolute) { xyzPosition.Z = (float)value; } diff --git a/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs b/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs index 0fb5d25ac..4d35d8736 100644 --- a/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs +++ b/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs @@ -33,7 +33,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses { public override string Convert(string value, PrinterSettings settings) { - // Unescape newlines + // Un-escape newlines value = value.Replace("\\n", "\n"); if (!value.Contains("; LAYER:")