public async void LoadInBackground(string gcodePathAndFileName, Action <double, string> progressReporter)
        {
            this.FileNameAndPath = gcodePathAndFileName;

            loadedGCode = await GCodeFileLoaded.LoadInBackground(gcodePathAndFileName, progressReporter);

            // backgroundWorker_RunWorkerCompleted
            SetGCodeAfterLoad(loadedGCode);

            DoneLoading?.Invoke(this, null);
        }
		private void SaveGCodeToNewLocation(string source, string dest)
		{
			try
			{
				if (ActiveSliceSettings.Instance.DoPrintLeveling)
				{
					GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(source);
					if (applyLeveling.Checked)
					{
						PrintLevelingData levelingData = ActiveSliceSettings.Instance.PrintLevelingData;
						if (levelingData != null)
						{
							for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++)
							{
								PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex);
								Vector3 currentDestination = instruction.Position;

								List<string> linesToWrite = null;
								switch (levelingData.CurrentPrinterLevelingSystem)
								{
									case PrintLevelingData.LevelingSystem.Probe2Points:
										instruction.Line = LevelWizard2Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										linesToWrite = LevelWizard2Point.ProcessCommand(instruction.Line);
										break;

									case PrintLevelingData.LevelingSystem.Probe3Points:
										instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										linesToWrite = LevelWizard3Point.ProcessCommand(instruction.Line);
										break;

									case PrintLevelingData.LevelingSystem.Probe7PointRadial:
										instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										linesToWrite = LevelWizard7PointRadial.ProcessCommand(instruction.Line);
										break;

									case PrintLevelingData.LevelingSystem.Probe13PointRadial:
										instruction.Line = LevelWizard13PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										linesToWrite = LevelWizard13PointRadial.ProcessCommand(instruction.Line);
										break;

									default:
										throw new NotImplementedException();
								}

								instruction.Line = linesToWrite[0];
								linesToWrite.RemoveAt(0);

								// now insert any new lines
								foreach (string line in linesToWrite)
								{
									PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line);
									unleveledGCode.Insert(++lineIndex, newInstruction);
								}
							}
						}
					}
					unleveledGCode.Save(dest);
				}
				else
				{
					File.Copy(source, dest, true);
				}
				ShowFileIfRequested(dest);
			}
			catch
			{
			}
		}
		private void SaveGCodeToNewLocation(string source, string dest)
		{
			if (ActivePrinterProfile.Instance.DoPrintLeveling)
			{
				GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(source);
				if (applyLeveling.Checked)
				{
					PrintLevelingPlane.Instance.ApplyLeveling(unleveledGCode);

					PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter);
					if (levelingData != null)
					{
						for (int lineIndex = 0; lineIndex < unleveledGCode.LineCount; lineIndex++)
						{
							PrinterMachineInstruction instruction = unleveledGCode.Instruction(lineIndex);

							List<string> linesToWrite = null;
							switch (levelingData.levelingSystem)
							{
								case PrintLevelingData.LevelingSystem.Probe2Points:
									linesToWrite = LevelWizard2Point.ProcessCommand(instruction.Line);
									break;

								case PrintLevelingData.LevelingSystem.Probe3Points:
									linesToWrite = LevelWizard3Point.ProcessCommand(instruction.Line);
									break;
							}

							instruction.Line = linesToWrite[0];
							linesToWrite.RemoveAt(0);

							// now insert any new lines
							foreach (string line in linesToWrite)
							{
								PrinterMachineInstruction newInstruction = new PrinterMachineInstruction(line);
								unleveledGCode.Insert(++lineIndex, newInstruction);
							}
						}
					}
				}
				unleveledGCode.Save(dest);
			}
			else
			{
				File.Copy(source, dest, true);
			}
			ShowFileIfRequested(dest);
		}
Beispiel #4
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 && 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));
		}
		private void sliceItem_Done(object sender, EventArgs e)
		{
			PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

			sliceItem.SlicingDone -= sliceItem_Done;
			sliceItem.SlicingOutputMessage -= printItemWrapper_SlicingOutputMessage;

			if (File.Exists(sliceItem.FileLocation))
			{
				savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName());
			}

			itemCountBeingWorkedOn++;
			if (itemCountBeingWorkedOn < allFilesToExport.Count)
			{
				if (StartingNextPart != null)
				{
					StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
				}
			}
			else
			{
				if (UpdatePartStatus != null)
				{
					UpdatePartStatus(this, new StringEventArgs("Calculating Total cm3..."));
				}

				if (savedGCodeFileNames.Count > 0)
				{
					double total = 0;
					foreach (string gcodeFileName in savedGCodeFileNames)
					{
						string[] lines = File.ReadAllLines(gcodeFileName);
						if (lines.Length > 0)
						{
							string filamentAmountLine = lines[lines.Length - 1];
							bool foundAmountInGCode = false;
							int startPos = filamentAmountLine.IndexOf("(");
							if (startPos > 0)
							{
								int endPos = filamentAmountLine.IndexOf("cm3)", startPos);
								if (endPos > 0)
								{
									string value = filamentAmountLine.Substring(startPos + 1, endPos - (startPos + 1));
									double amountForThisFile;
									if (double.TryParse(value, out amountForThisFile))
									{
										foundAmountInGCode = true;
										total += amountForThisFile;
									}
								}
							}
						}
					}

					PrintLevelingData levelingData = PrintLevelingData.GetForPrinter(ActivePrinterProfile.Instance.ActivePrinter);

					// now copy all the gcode to the path given
					for (int i = 0; i < savedGCodeFileNames.Count; i++)
					{
						string savedGcodeFileName = savedGCodeFileNames[i];
						string originalFileName = Path.GetFileName(allFilesToExport[i].Name);
						string outputFileName = Path.ChangeExtension(originalFileName, ".gcode");
						string outputPathAndName = Path.Combine(exportPath, outputFileName);

						if (ActivePrinterProfile.Instance.DoPrintLeveling)
						{
							GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(savedGcodeFileName);

							for (int j = 0; j < unleveledGCode.LineCount; j++)
							{
								PrinterMachineInstruction instruction = unleveledGCode.Instruction(j);
								Vector3 currentDestination = instruction.Position;

								switch (levelingData.CurrentPrinterLevelingSystem)
								{
									case PrintLevelingData.LevelingSystem.Probe2Points:
										instruction.Line = LevelWizard2Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									case PrintLevelingData.LevelingSystem.Probe3Points:
										instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									case PrintLevelingData.LevelingSystem.Probe7PointRadial:
										instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									default:
										throw new NotImplementedException();
								}
							}
							unleveledGCode.Save(outputPathAndName);
						}
						else
						{
							File.Copy(savedGcodeFileName, outputPathAndName, true);
						}
					}

					if (DoneSaving != null)
					{
						DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
					}
				}
			}
		}
		private void sliceItem_Done(object sender, EventArgs e)
		{
			PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

			sliceItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents);
			sliceItem.SlicingOutputMessage.UnregisterEvent(printItemWrapper_SlicingOutputMessage, ref unregisterEvents);
			if (File.Exists(sliceItem.FileLocation))
			{
				savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName());
			}

			itemCountBeingWorkedOn++;
			if (itemCountBeingWorkedOn < allFilesToExport.Count)
			{
				if (StartingNextPart != null)
				{
					StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
				}
			}
			else
			{
				if (UpdatePartStatus != null)
				{
					UpdatePartStatus(this, new StringEventArgs("Calculating Total cm3..."));
				}

				if (savedGCodeFileNames.Count > 0)
				{
					double total = 0;
					foreach (string gcodeFileName in savedGCodeFileNames)
					{
						string[] lines = File.ReadAllLines(gcodeFileName);
						if (lines.Length > 0)
						{
							string filamentAmountLine = lines[lines.Length - 1];
							bool foundAmountInGCode = false;
							int startPos = filamentAmountLine.IndexOf("(");
							if (startPos > 0)
							{
								int endPos = filamentAmountLine.IndexOf("cm3)", startPos);
								if (endPos > 0)
								{
									string value = filamentAmountLine.Substring(startPos + 1, endPos - (startPos + 1));
									double amountForThisFile;
									if (double.TryParse(value, out amountForThisFile))
									{
										foundAmountInGCode = true;
										total += amountForThisFile;
									}
								}
							}
						}
					}

					// now copy all the gcode to the path given
					for (int i = 0; i < savedGCodeFileNames.Count; i++)
					{
						string savedGcodeFileName = savedGCodeFileNames[i];
						string originalFileName = Path.GetFileName(allFilesToExport[i].Name);
						string outputFileName = Path.ChangeExtension(originalFileName, ".gcode");
						string outputPathAndName = Path.Combine(exportPath, outputFileName);

						if (ActivePrinterProfile.Instance.DoPrintLeveling)
						{
							GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(savedGcodeFileName);
							PrintLevelingPlane.Instance.ApplyLeveling(unleveledGCode);
							unleveledGCode.Save(outputPathAndName);
						}
						else
						{
							File.Copy(savedGcodeFileName, outputPathAndName, true);
						}
					}

					if (DoneSaving != null)
					{
						DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
					}
				}
			}
		}
		private void sliceItem_Done(object sender, EventArgs e)
		{
			PrintItemWrapper sliceItem = (PrintItemWrapper)sender;

			sliceItem.SlicingDone -= sliceItem_Done;
			sliceItem.SlicingOutputMessage -= printItemWrapper_SlicingOutputMessage;

			if (File.Exists(sliceItem.FileLocation))
			{
				savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName());
			}

			itemCountBeingWorkedOn++;
			if (itemCountBeingWorkedOn < allFilesToExport.Count)
			{
				if (StartingNextPart != null)
				{
					StartingNextPart(this, new StringEventArgs(ItemNameBeingWorkedOn));
				}
			}
			else
			{
				if (UpdatePartStatus != null)
				{
					UpdatePartStatus(this, new StringEventArgs("Calculating Total fillament mm..."));
				}

				if (savedGCodeFileNames.Count > 0)
				{
					double total = 0;
					foreach (string gcodeFileName in savedGCodeFileNames)
					{
						string allContent = File.ReadAllText(gcodeFileName);
						if (allContent.Length > 0)
						{
							string searchString = "filament used =";
							int startPos = allContent.IndexOf(searchString);
							if (startPos > 0)
							{
								int endPos = Math.Min(allContent.IndexOf("\n", startPos), allContent.IndexOf("mm", startPos));
								if (endPos > 0)
								{
									string value = allContent.Substring(startPos + searchString.Length, endPos - startPos - searchString.Length);
									double amountForThisFile;
									if (double.TryParse(value, out amountForThisFile))
									{
										total += amountForThisFile;
									}
								}
							}
						}
					}

					PrintLevelingData levelingData = ActiveSliceSettings.Instance.Helpers.GetPrintLevelingData();

					// now copy all the gcode to the path given
					for (int i = 0; i < savedGCodeFileNames.Count; i++)
					{
						string savedGcodeFileName = savedGCodeFileNames[i];
						string originalFileName = Path.GetFileName(allFilesToExport[i].Name);
						string outputFileName = Path.ChangeExtension(originalFileName, ".gcode");
						string outputPathAndName = Path.Combine(exportPath, outputFileName);

						if (ActiveSliceSettings.Instance.GetValue<bool>(SettingsKey.print_leveling_enabled))
						{
							GCodeFileLoaded unleveledGCode = new GCodeFileLoaded(savedGcodeFileName);

							for (int j = 0; j < unleveledGCode.LineCount; j++)
							{
								PrinterMachineInstruction instruction = unleveledGCode.Instruction(j);
								Vector3 currentDestination = instruction.Position;

								switch (levelingData.CurrentPrinterLevelingSystem)
								{
									case PrintLevelingData.LevelingSystem.Probe3Points:
										instruction.Line = LevelWizard3Point.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									case PrintLevelingData.LevelingSystem.Probe7PointRadial:
										instruction.Line = LevelWizard7PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									case PrintLevelingData.LevelingSystem.Probe13PointRadial:
										instruction.Line = LevelWizard13PointRadial.ApplyLeveling(instruction.Line, currentDestination, instruction.movementType);
										break;

									default:
										throw new NotImplementedException();
								}
							}
							unleveledGCode.Save(outputPathAndName);
						}
						else
						{
							File.Copy(savedGcodeFileName, outputPathAndName, true);
						}
					}

					if (DoneSaving != null)
					{
						DoneSaving(this, new StringEventArgs(string.Format("{0:0.0}", total)));
					}
				}
			}
		}