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

			Stopwatch loadTime = Stopwatch.StartNew();
			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;
			}

			GCodeFileLoaded loadedGCodeFile = new GCodeFileLoaded(gcodeHasExplicitLayerChangeInfo);

			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 && 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;

						case '@':
							break;

						default:
#if DEBUG
							throw new NotImplementedException();
#else
                            break;
#endif
					}
				}

				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;

			loadTime.Stop();
			Console.WriteLine("Time To Load Seconds: {0:0.00}".FormatWith(loadTime.Elapsed.TotalSeconds));
		}
Exemple #2
0
        public static void ParseFileContents(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string gCodeString = (string)doWorkEventArgs.Argument;

            if (gCodeString == null)
            {
                return;
            }

            Stopwatch        loadTime         = Stopwatch.StartNew();
            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;
            }

            GCodeFileLoaded loadedGCodeFile = new GCodeFileLoaded(gcodeHasExplicitLayerChangeInfo);

            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))
                        {
                            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;

                    case '@':
                        break;

                    default:
#if DEBUG
                        throw new NotImplementedException();
#else
                        break;
#endif
                    }
                }

                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;

            loadTime.Stop();
            Console.WriteLine("Time To Load Seconds: {0:0.00}".FormatWith(loadTime.Elapsed.TotalSeconds));
        }
Exemple #3
0
        public static GCodeFileLoaded ParseFileContents(string gCodeString, 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:"))
            {
                gcodeHasExplicitLayerChangeInfo = true;
            }

            GCodeFileLoaded loadedGCodeFile = new GCodeFileLoaded(gcodeHasExplicitLayerChangeInfo);

            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))
                        {
                            loadedGCodeFile.IndexOfChangeInZ.Add(loadedGCodeFile.GCodeCommandQueue.Count);
                        }
                        if (lineString.StartsWith("; layerThickness"))
                        {
                            loadedGCodeFile.layerThickness = double.Parse(lineString.Split('=')[1]);
                        }
                        else if (lineString.StartsWith("; firstLayerThickness") && loadedGCodeFile.firstLayerThickness == 0)
                        {
                            loadedGCodeFile.firstLayerThickness = double.Parse(lineString.Split('=')[1]);
                        }
                        break;

                    case '@':
                        break;

                    default:
#if DEBUG
                        throw new NotImplementedException();
#else
                        break;
#endif
                    }
                }

                loadedGCodeFile.GCodeCommandQueue.Add(machineInstructionForLine);

                if (progressReporter != null && maxProgressReport.ElapsedMilliseconds > 200)
                {
                    progressReporter((double)lineIndex / crCount / 2, "");
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }

                    maxProgressReport.Restart();
                }

                lineIndex++;
            }

            loadedGCodeFile.AnalyzeGCodeLines(CancellationToken.None, progressReporter);

            loadTime.Stop();
            Console.WriteLine("Time To Load Seconds: {0:0.00}".FormatWith(loadTime.Elapsed.TotalSeconds));

            return(loadedGCodeFile);
        }