Example #1
0
        public static void ParseFileContents(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string gCodeString = (string)doWorkEventArgs.Argument;
            if (gCodeString == null)
            {
                return;
            }

            BackgroundWorker backgroundWorker = sender as BackgroundWorker;

            Stopwatch maxProgressReport = new Stopwatch();
            maxProgressReport.Start();
            PrinterMachineInstruction machineInstructionForLine = new PrinterMachineInstruction("None");

            bool gcodeHasExplicitLayerChangeInfo = false;
            if (gCodeString.Contains("; LAYER:"))
            {
                gcodeHasExplicitLayerChangeInfo = true;
            }

            GCodeFile loadedGCodeFile = new GCodeFile(gcodeHasExplicitLayerChangeInfo);

            int crCount = CountNumLines(gCodeString);
            int lineIndex = 0;
            foreach (string outputString in CustomSplit(gCodeString, '\n'))
            {
                string lineString = outputString.Trim();
                machineInstructionForLine = new PrinterMachineInstruction(lineString, machineInstructionForLine);
                // take off any comments before we check its length

                if (lineString.Length > 0)
                {
                    switch (lineString[0])
                    {
                        case 'M':
                            loadedGCodeFile.ParseMLine(lineString, machineInstructionForLine);
                            break;

                        case 'G':
                            loadedGCodeFile.ParseGLine(lineString, machineInstructionForLine);
                            break;

                        case ';':
                            if (gcodeHasExplicitLayerChangeInfo && lineString.StartsWith("; LAYER:"))
                            {
                                loadedGCodeFile.IndexOfChangeInZ.Add(loadedGCodeFile.GCodeCommandQueue.Count);
                            }
                            if(lineString.StartsWith("; layerThickness"))
                            {
                                loadedGCodeFile.layerThickness = double.Parse(lineString.Split('=')[1]);
                            }
                            else if(lineString.StartsWith("; firstLayerThickness"))
                            {
                                loadedGCodeFile.firstLayerThickness = double.Parse(lineString.Split('=')[1]);
                            }
                            break;

                        default:
                            break;
                    }
                }

                loadedGCodeFile.GCodeCommandQueue.Add(machineInstructionForLine);

                if (backgroundWorker != null)
                {
                    if (backgroundWorker.CancellationPending)
                    {
                        return;
                    }

                    if (backgroundWorker.WorkerReportsProgress && maxProgressReport.ElapsedMilliseconds > 200)
                    {
                        backgroundWorker.ReportProgress(lineIndex * 100 / crCount / 2);
                        maxProgressReport.Restart();
                    }
                }

                lineIndex++;
            }

            loadedGCodeFile.AnalyzeGCodeLines(backgroundWorker);

            doWorkEventArgs.Result = loadedGCodeFile;
        }