private static void AnalyzeFile(string fileName) { using (var reader = new StreamReader(fileName)) { decimal z = decimal.MinValue; decimal lastE = decimal.MinValue; while (true) { string line = reader.ReadLine(); if (line == null) { break; } var command = new GCodeCommand(line); if ((command.Command == "G0") || (command.Command == "G1")) { if (command.HasParameter('Z')) { z = command.GetParameter('Z'); } if (command.HasParameter('E')) { var e = command.GetParameter('E'); if (e < lastE) { Console.WriteLine($"=> Retract by {lastE - e} at Z {z}"); } else { lastE = e; } } } } } }
static (Extent X, Extent Y, Extent Z) MeasureGCode(TextReader stream) { // Count only G1 moves in X and Y. // Count G0 and G1 moves in Z, but only for Z values where filament is extruded. Extent x, y, z; x.From = y.From = z.From = decimal.MaxValue; x.To = y.To = z.To = decimal.MinValue; decimal lastE = decimal.MinValue; decimal currentZ = decimal.MinValue; while (true) { string line = stream.ReadLine(); if (line == null) { break; } if (string.IsNullOrWhiteSpace(line)) { continue; } var command = new GCodeCommand(line); if (command.Command == "G1") { if (command.HasParameter('X')) { x.Extend(command.GetParameter('X')); } if (command.HasParameter('Y')) { y.Extend(command.GetParameter('Y')); } } if ((command.Command == "G0") || (command.Command == "G1")) { if (command.HasParameter('Z')) { currentZ = command.GetParameter('Z'); } if (command.HasParameter('E')) { decimal e = command.GetParameter('E'); if ((e > lastE) && (currentZ != decimal.MinValue)) { lastE = e; z.Extend(currentZ); } } } } return(x, y, z); }
static void TranslateGCode(TextReader reader, TextWriter writer, decimal firstTowerZ, decimal deltaX, decimal deltaY, List <CurvePoint> curvePoints) { curvePoints.Sort( (left, right) => left.Z.CompareTo(right.Z)); decimal z = decimal.MinValue; var uniqueZValues = new HashSet <decimal>(); decimal lastE = decimal.MinValue; string lastSerialMessage = ""; var gcodeWriter = new GCodeWriter(writer); int numberOfRetractions = 0; while (true) { string line = reader.ReadLine(); if (line == null) { break; } var command = new GCodeCommand(line); if ((command.Command == "G0") || (command.Command == "G1")) { if (command.HasParameter('X')) { command.SetParameter('X', command.GetParameter('X') + deltaX); } if (command.HasParameter('Y')) { command.SetParameter('Y', command.GetParameter('Y') + deltaY); } if (command.HasParameter('Z')) { z = command.GetParameter('Z'); if (uniqueZValues.Add(z)) { Console.Write('#'); } } if (z >= firstTowerZ) { if (command.HasParameter('E')) { decimal e = command.GetParameter('E'); if (e < lastE) { // Retraction! numberOfRetractions++; decimal retraction = GetRetractionForZ(z, curvePoints); command.SetParameter('E', lastE - retraction); string lcdScreenMessage = $"dE {retraction:0.000} at Z {z:#0.0}"; string serialMessage = $"Retraction {retraction:0.00000} at Z {z:#0.0}"; gcodeWriter.WriteLine("M117 " + lcdScreenMessage); if (serialMessage != lastSerialMessage) { gcodeWriter.WriteLine("M118 " + serialMessage); lastSerialMessage = serialMessage; } } lastE = e; } } } gcodeWriter.WriteLine(command); } gcodeWriter.WriteLine("G0 X0 Y0"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Output:"); Console.WriteLine("- {0} characters", gcodeWriter.NumCharactersWritten); Console.WriteLine("- {0} lines", gcodeWriter.NumLines); Console.WriteLine("- {0} commands", gcodeWriter.NumCommands); Console.WriteLine("- {0} movement commands", gcodeWriter.NumMovementCommands); Console.WriteLine("- {0} unique Z values", uniqueZValues.Count); Console.WriteLine("- {0} retractions", numberOfRetractions); }