public static GCodeFile Load(Stream fileStream, Vector4 maxAccelerationMmPerS2, Vector4 maxVelocityMmPerS, Vector4 velocitySameAsStopMmPerS, Vector4 speedMultiplier, CancellationToken cancellationToken) { if (FileTooBigToLoad(fileStream)) { return(new GCodeFileStreamed(fileStream)); } else { return(GCodeMemoryFile.Load(fileStream, maxAccelerationMmPerS2, maxVelocityMmPerS, velocitySameAsStopMmPerS, speedMultiplier, cancellationToken, null)); } }
public GCodeMemoryFile(string pathAndFileName, Vector4 maxAccelerationMmPerS2, Vector4 maxVelocityMmPerS, Vector4 velocitySameAsStopMmPerS, Vector4 speedMultiplier, CancellationToken cancellationToken, bool gcodeHasExplicitLayerChangeInfo = false) { this.gcodeHasExplicitLayerChangeInfo = gcodeHasExplicitLayerChangeInfo; var loadedFile = GCodeMemoryFile.Load(pathAndFileName, maxAccelerationMmPerS2, maxVelocityMmPerS, velocitySameAsStopMmPerS, speedMultiplier, cancellationToken, null); if (loadedFile != null) { this.IndexOfLayerStart = loadedFile.IndexOfLayerStart; this.parsingLastZ = loadedFile.parsingLastZ; this.GCodeCommandQueue = loadedFile.GCodeCommandQueue; } }
private static GCodeMemoryFile ParseFileContents(string gCodeString, Vector4 maxAccelerationMmPerS2, Vector4 maxVelocityMmPerS, Vector4 velocitySameAsStopMmPerS, Vector4 speedMultiplier, CancellationToken cancellationToken, Action <double, string> progressReporter) { if (gCodeString == null) { return(null); } Stopwatch loadTime = Stopwatch.StartNew(); Stopwatch maxProgressReport = new Stopwatch(); maxProgressReport.Start(); PrinterMachineInstruction machineInstructionForLine = new PrinterMachineInstruction("None"); bool gcodeHasExplicitLayerChangeInfo = false; if (gCodeString.Contains("LAYER:") || gCodeString.Contains("; layer")) { gcodeHasExplicitLayerChangeInfo = true; } PrinterMachineInstruction previousInstruction = null; var speeds = new HashSet <float>(); GCodeMemoryFile loadedGCodeFile = new GCodeMemoryFile(gcodeHasExplicitLayerChangeInfo); // Add the first start index (of 0) loadedGCodeFile.IndexOfLayerStart.Add(0); int crCount = CountNumLines(gCodeString); int lineIndex = 0; foreach (string outputString in CustomSplit(gCodeString, '\n')) { string lineString = outputString.Trim(); machineInstructionForLine = new PrinterMachineInstruction(lineString, machineInstructionForLine, false); if (lineString.Length > 0) { switch (lineString[0]) { case 'G': loadedGCodeFile.ParseGLine(lineString, machineInstructionForLine); break; case 'M': loadedGCodeFile.ParseMLine(lineString, machineInstructionForLine); break; case 'T': double extruderIndex = 0; if (GetFirstNumberAfter("T", lineString, ref extruderIndex)) { machineInstructionForLine.ExtruderIndex = (int)extruderIndex; } break; case ';': if (gcodeHasExplicitLayerChangeInfo && IsLayerChange(lineString)) { // The first "layer" statement in the gcode file is after the start gcode and we ignore // it because we already added a marker for the start of the file (before start gcode) if (!loadedGCodeFile.foundFirstLayerMarker) { loadedGCodeFile.foundFirstLayerMarker = true; } else { loadedGCodeFile.IndexOfLayerStart.Add(loadedGCodeFile.GCodeCommandQueue.Count); } } else if (lineString.StartsWith("; LAYER_HEIGHT:")) { double layerWidth = 0; if (GetFirstNumberAfter("LAYER_HEIGHT:", lineString, ref layerWidth, 0, "")) { loadedGCodeFile.layerHeights.Add(layerWidth); } } break; case '@': break; default: #if DEBUG throw new NotImplementedException(); #else break; #endif } } loadedGCodeFile.GCodeCommandQueue.Add(machineInstructionForLine); // Accumulate speeds for extruded moves if (previousInstruction != null && machineInstructionForLine.EPosition > previousInstruction.EPosition && (machineInstructionForLine.Line.IndexOf('X') != -1 || machineInstructionForLine.Line.IndexOf('Y') != -1)) { speeds.Add((float)machineInstructionForLine.FeedRate); } if (progressReporter != null && maxProgressReport.ElapsedMilliseconds > 200) { progressReporter((double)lineIndex / crCount / 2, ""); if (cancellationToken.IsCancellationRequested) { return(null); } maxProgressReport.Restart(); } previousInstruction = machineInstructionForLine; lineIndex++; } loadedGCodeFile.AnalyzeGCodeLines(cancellationToken, progressReporter, maxAccelerationMmPerS2, maxVelocityMmPerS, velocitySameAsStopMmPerS, speedMultiplier); loadedGCodeFile.Speeds = speeds; loadTime.Stop(); Console.WriteLine("Time To Load Seconds: {0:0.00}".FormatWith(loadTime.Elapsed.TotalSeconds)); return(loadedGCodeFile); }