public void SliceLayersGeneratingCorrectSegments()
		{
			// TODO: Make tests work on Mac as well as Windows
			if (OsInformation.OperatingSystem == OSType.Mac)
			{
				return;
			}

			string meshFileName = TestContext.CurrentContext.ResolveProjectPath(4, "Tests", "TestData", "TestMeshes", "SliceLayers", "Box20x20x10.stl");
			Mesh cubeMesh = StlProcessing.Load(meshFileName);

			AxisAlignedBoundingBox bounds = cubeMesh.GetAxisAlignedBoundingBox();
			Assert.IsTrue(bounds.ZSize == 10);

			SliceLayers layers = new SliceLayers();
			layers.GetPerimetersForAllLayers(cubeMesh, .2, .2);
			Assert.IsTrue(layers.AllLayers.Count == 50);

			foreach (SliceLayer layer in layers.AllLayers)
			{
				Assert.IsTrue(layer.UnorderedSegments.Count == 8);

				// work in progress
				//Assert.IsTrue(layer.Perimeters.Count == 1);
				//Assert.IsTrue(layer.Perimeters[0].Count == 8);
			}

			layers.GetPerimetersForAllLayers(cubeMesh, .2, .1);
			Assert.IsTrue(layers.AllLayers.Count == 99);
		}
Ejemplo n.º 2
0
        public void SliceLayersGeneratingCorrectSegments()
        {
            // TODO: Make tests work on Mac as well as Windows
            if (OsInformation.OperatingSystem == OSType.Mac)
            {
                return;
            }

            string pathToMesh   = Path.Combine("..", "..", "..", "TestData", "TestMeshes", "SliceLayers");
            string meshFileName = Path.Combine(pathToMesh, "Box20x20x10.stl");
            Mesh   cubeMesh     = StlProcessing.Load(meshFileName);

            AxisAlignedBoundingBox bounds = cubeMesh.GetAxisAlignedBoundingBox();

            Assert.IsTrue(bounds.ZSize == 10);

            SliceLayers layers = new SliceLayers();

            layers.GetPerimetersForAllLayers(cubeMesh, .2, .2);
            Assert.IsTrue(layers.AllLayers.Count == 50);

            foreach (SliceLayer layer in layers.AllLayers)
            {
                Assert.IsTrue(layer.UnorderedSegments.Count == 8);

                // work in progress
                //Assert.IsTrue(layer.Perimeters.Count == 1);
                //Assert.IsTrue(layer.Perimeters[0].Count == 8);
            }

            layers.GetPerimetersForAllLayers(cubeMesh, .2, .1);
            Assert.IsTrue(layers.AllLayers.Count == 99);
        }
Ejemplo n.º 3
0
        //[Test]
        public void SliceLayersGeneratingCorrectSegments()
        {
            // TODO: Make tests work on Mac as well as Windows
            if (AggContext.OperatingSystem == OSType.Mac)
            {
                return;
            }

            string meshFileName = TestContext.CurrentContext.ResolveProjectPath(4, "Tests", "TestData", "TestMeshes", "SliceLayers", "Box20x20x10.stl");
            Mesh   cubeMesh     = StlProcessing.Load(meshFileName, CancellationToken.None);

            AxisAlignedBoundingBox bounds = cubeMesh.GetAxisAlignedBoundingBox();

            Assert.IsTrue(bounds.ZSize == 10);

            SliceLayers layers = new SliceLayers();

            layers.GetPerimetersForAllLayers(cubeMesh, .2, .2);
            Assert.IsTrue(layers.AllLayers.Count == 50);

            foreach (SliceLayer layer in layers.AllLayers)
            {
                Assert.IsTrue(layer.UnorderedSegments.Count == 8);

                // work in progress
                //Assert.IsTrue(layer.Perimeters.Count == 1);
                //Assert.IsTrue(layer.Perimeters[0].Count == 8);
            }

            layers.GetPerimetersForAllLayers(cubeMesh, .2, .1);
            Assert.IsTrue(layers.AllLayers.Count == 99);
        }
Ejemplo n.º 4
0
		private static void CreateSlicedPartsThread()
		{
			Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

			while (!haltSlicingThread)
			{
				if (listOfSlicingItems.Count > 0)
				{
					PrintItemWrapper itemToSlice = listOfSlicingItems[0];
					string[] stlFileLocations = GetStlFileLocations(itemToSlice.FileLocation);
					string fileToSlice = stlFileLocations[0];
					// check that the STL file is currently on disk
					if (File.Exists(fileToSlice))
					{
						itemToSlice.CurrentlySlicing = true;

						string currentConfigurationFileAndPath = Path.Combine(ApplicationDataStorage.Instance.GCodeOutputPath, "config_" + ActiveSliceSettings.Instance.GetHashCode().ToString() + ".ini");
						ActiveSliceSettings.Instance.GenerateConfigFile(currentConfigurationFileAndPath);

						string gcodePathAndFileName = itemToSlice.GetGCodePathAndFileName();
						bool gcodeFileIsComplete = itemToSlice.IsGCodeFileComplete(gcodePathAndFileName);

						if (!File.Exists(gcodePathAndFileName) || !gcodeFileIsComplete)
						{
							string commandArgs = "";

							switch (ActivePrinterProfile.Instance.ActiveSliceEngineType)
							{
								case ActivePrinterProfile.SlicingEngineTypes.Slic3r:
									commandArgs = "--load \"" + currentConfigurationFileAndPath + "\" --output \"" + gcodePathAndFileName + "\" \"" + fileToSlice + "\"";
									break;

								case ActivePrinterProfile.SlicingEngineTypes.CuraEngine:
									commandArgs = "-v -o \"" + gcodePathAndFileName + "\" " + EngineMappingCura.GetCuraCommandLineSettings() + " \"" + fileToSlice + "\"";
									break;

								case ActivePrinterProfile.SlicingEngineTypes.MatterSlice:
									{
										EngineMappingsMatterSlice.WriteMatterSliceSettingsFile(currentConfigurationFileAndPath);
										commandArgs = "-v -o \"" + gcodePathAndFileName + "\" -c \"" + currentConfigurationFileAndPath + "\"";
										foreach (string filename in stlFileLocations)
										{
											commandArgs = commandArgs + " \"" + filename + "\"";
										}
									}
									break;
							}

#if false
							Mesh loadedMesh = StlProcessing.Load(fileToSlice);
							SliceLayers layers = new SliceLayers();
							layers.GetPerimetersForAllLayers(loadedMesh, .2, .2);
							layers.DumpSegmentsToGcode("test.gcode");
#endif

							if (OsInformation.OperatingSystem == OSType.Android ||
								((OsInformation.OperatingSystem == OSType.Mac || runInProcess)
									&& ActivePrinterProfile.Instance.ActiveSliceEngineType == ActivePrinterProfile.SlicingEngineTypes.MatterSlice))
							{
								itemCurrentlySlicing = itemToSlice;
								MatterHackers.MatterSlice.LogOutput.GetLogWrites += SendProgressToItem;
								MatterSlice.MatterSlice.ProcessArgs(commandArgs);
								MatterHackers.MatterSlice.LogOutput.GetLogWrites -= SendProgressToItem;
								itemCurrentlySlicing = null;
							}
							else
							{
								slicerProcess = new Process();
								slicerProcess.StartInfo.Arguments = commandArgs;
								string slicerFullPath = getSlicerFullPath();

								slicerProcess.StartInfo.CreateNoWindow = true;
								slicerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
								slicerProcess.StartInfo.RedirectStandardError = true;
								slicerProcess.StartInfo.RedirectStandardOutput = true;

								slicerProcess.StartInfo.FileName = slicerFullPath;
								slicerProcess.StartInfo.UseShellExecute = false;

								slicerProcess.OutputDataReceived += (sender, args) =>
								{
									if (args.Data != null)
									{
										string message = args.Data;
										message = message.Replace("=>", "").Trim();
										if (message.Contains(".gcode"))
										{
											message = "Saving intermediate file";
										}
										message += "...";
										UiThread.RunOnIdle(() =>
										{
											itemToSlice.OnSlicingOutputMessage(new StringEventArgs(message));
										});
									}
								};

								slicerProcess.Start();
								slicerProcess.BeginOutputReadLine();
								string stdError = slicerProcess.StandardError.ReadToEnd();

								slicerProcess.WaitForExit();
								using (TimedLock.Lock(slicerProcess, "SlicingProcess"))
								{
									slicerProcess = null;
								}
							}
						}

						try
						{
							if (File.Exists(gcodePathAndFileName)
								&& File.Exists(currentConfigurationFileAndPath))
							{
								// make sure we have not already written the settings onto this file
								bool fileHaseSettings = false;
								int bufferSize = 32000;
								using (Stream fileStream = File.OpenRead(gcodePathAndFileName))
								{
									byte[] buffer = new byte[bufferSize];
									fileStream.Seek(Math.Max(0, fileStream.Length - bufferSize), SeekOrigin.Begin);
									int numBytesRead = fileStream.Read(buffer, 0, bufferSize);
									string fileEnd = System.Text.Encoding.UTF8.GetString(buffer);
									if (fileEnd.Contains("GCode settings used"))
									{
										fileHaseSettings = true;
									}
								}

								if (!fileHaseSettings)
								{
									using (StreamWriter gcodeWirter = File.AppendText(gcodePathAndFileName))
									{
										string oemName = "MatterControl";
										if (OemSettings.Instance.WindowTitleExtra != null && OemSettings.Instance.WindowTitleExtra.Trim().Length > 0)
										{
											oemName = oemName + " - {0}".FormatWith(OemSettings.Instance.WindowTitleExtra);
										}

										gcodeWirter.WriteLine("; {0} Version {1} Build {2} : GCode settings used".FormatWith(oemName, VersionInfo.Instance.ReleaseVersion, VersionInfo.Instance.BuildVersion));
										gcodeWirter.WriteLine("; Date {0} Time {1}:{2:00}".FormatWith(DateTime.Now.Date, DateTime.Now.Hour, DateTime.Now.Minute));

										foreach (string line in File.ReadLines(currentConfigurationFileAndPath))
										{
											gcodeWirter.WriteLine("; {0}".FormatWith(line));
										}
									}
								}
							}
						}
						catch (Exception)
						{
						}
					}

					UiThread.RunOnIdle(() =>
					{
						itemToSlice.CurrentlySlicing = false;
						itemToSlice.DoneSlicing = true;
					});

					using (TimedLock.Lock(listOfSlicingItems, "CreateSlicedPartsThread()"))
					{
						listOfSlicingItems.RemoveAt(0);
					}
				}

				Thread.Sleep(100);
			}
		}
Ejemplo n.º 5
0
        private static void CreateSlicedPartsThread()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            while (!haltSlicingThread)
            {
                if (listOfSlicingItems.Count > 0)
                {
                    PrintItemWrapper itemToSlice     = listOfSlicingItems[0];
                    bool             doMergeInSlicer = false;
                    string           mergeRules      = "";
                    doMergeInSlicer = ActiveSliceSettings.Instance.ActiveSliceEngineType() == SlicingEngineTypes.MatterSlice;
                    string[] stlFileLocations = GetStlFileLocations(itemToSlice.FileLocation, doMergeInSlicer, ref mergeRules);
                    string   fileToSlice      = stlFileLocations[0];
                    // check that the STL file is currently on disk
                    if (File.Exists(fileToSlice))
                    {
                        itemToSlice.CurrentlySlicing = true;

                        string currentConfigurationFileAndPath = Path.Combine(ApplicationDataStorage.Instance.GCodeOutputPath, "config_" + ActiveSliceSettings.Instance.GetLongHashCode().ToString() + ".ini");
                        ActiveSliceSettings.Instance.GenerateConfigFile(currentConfigurationFileAndPath, true);

                        string gcodePathAndFileName = itemToSlice.GetGCodePathAndFileName();
                        bool   gcodeFileIsComplete  = itemToSlice.IsGCodeFileComplete(gcodePathAndFileName);

                        if (!File.Exists(gcodePathAndFileName) || !gcodeFileIsComplete)
                        {
                            string commandArgs = "";

                            switch (ActiveSliceSettings.Instance.ActiveSliceEngineType())
                            {
                            case SlicingEngineTypes.Slic3r:
                                commandArgs = "--load \"" + currentConfigurationFileAndPath + "\" --output \"" + gcodePathAndFileName + "\" \"" + fileToSlice + "\"";
                                break;

                            case SlicingEngineTypes.CuraEngine:
                                commandArgs = "-v -o \"" + gcodePathAndFileName + "\" " + EngineMappingCura.GetCuraCommandLineSettings() + " \"" + fileToSlice + "\"";
                                break;

                            case SlicingEngineTypes.MatterSlice:
                            {
                                EngineMappingsMatterSlice.WriteMatterSliceSettingsFile(currentConfigurationFileAndPath);
                                if (mergeRules == "")
                                {
                                    commandArgs = "-v -o \"" + gcodePathAndFileName + "\" -c \"" + currentConfigurationFileAndPath + "\"";
                                }
                                else
                                {
                                    commandArgs = "-b {0} -v -o \"".FormatWith(mergeRules) + gcodePathAndFileName + "\" -c \"" + currentConfigurationFileAndPath + "\"";
                                }
                                foreach (string filename in stlFileLocations)
                                {
                                    commandArgs = commandArgs + " \"" + filename + "\"";
                                }
                            }
                            break;
                            }

#if false
                            Mesh        loadedMesh = StlProcessing.Load(fileToSlice);
                            SliceLayers layers     = new SliceLayers();
                            layers.GetPerimetersForAllLayers(loadedMesh, .2, .2);
                            layers.DumpSegmentsToGcode("test.gcode");
#endif

                            if (OsInformation.OperatingSystem == OSType.Android ||
                                ((OsInformation.OperatingSystem == OSType.Mac || runInProcess) &&
                                 ActiveSliceSettings.Instance.ActiveSliceEngineType() == SlicingEngineTypes.MatterSlice))
                            {
                                itemCurrentlySlicing = itemToSlice;
                                MatterHackers.MatterSlice.LogOutput.GetLogWrites += SendProgressToItem;
                                MatterSlice.MatterSlice.ProcessArgs(commandArgs);
                                MatterHackers.MatterSlice.LogOutput.GetLogWrites -= SendProgressToItem;
                                itemCurrentlySlicing = null;
                            }
                            else
                            {
                                slicerProcess = new Process();
                                slicerProcess.StartInfo.Arguments = commandArgs;
                                string slicerFullPath = getSlicerFullPath();

                                slicerProcess.StartInfo.CreateNoWindow         = true;
                                slicerProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                                slicerProcess.StartInfo.RedirectStandardError  = true;
                                slicerProcess.StartInfo.RedirectStandardOutput = true;

                                slicerProcess.StartInfo.FileName        = slicerFullPath;
                                slicerProcess.StartInfo.UseShellExecute = false;

                                slicerProcess.OutputDataReceived += (sender, args) =>
                                {
                                    if (args.Data != null)
                                    {
                                        string message = args.Data;
                                        message = message.Replace("=>", "").Trim();
                                        if (message.Contains(".gcode"))
                                        {
                                            message = "Saving intermediate file";
                                        }
                                        message += "...";
                                        UiThread.RunOnIdle(() =>
                                        {
                                            itemToSlice.OnSlicingOutputMessage(new StringEventArgs(message));
                                        });
                                    }
                                };

                                slicerProcess.Start();
                                slicerProcess.BeginOutputReadLine();
                                string stdError = slicerProcess.StandardError.ReadToEnd();

                                slicerProcess.WaitForExit();
                                lock (slicerProcess)
                                {
                                    slicerProcess = null;
                                }
                            }
                        }

                        try
                        {
                            if (File.Exists(gcodePathAndFileName) &&
                                File.Exists(currentConfigurationFileAndPath))
                            {
                                // make sure we have not already written the settings onto this file
                                bool fileHaseSettings = false;
                                int  bufferSize       = 32000;
                                using (Stream fileStream = File.OpenRead(gcodePathAndFileName))
                                {
                                    byte[] buffer = new byte[bufferSize];
                                    fileStream.Seek(Math.Max(0, fileStream.Length - bufferSize), SeekOrigin.Begin);
                                    int    numBytesRead = fileStream.Read(buffer, 0, bufferSize);
                                    string fileEnd      = System.Text.Encoding.UTF8.GetString(buffer);
                                    if (fileEnd.Contains("GCode settings used"))
                                    {
                                        fileHaseSettings = true;
                                    }
                                }

                                if (!fileHaseSettings)
                                {
                                    using (StreamWriter gcodeWirter = File.AppendText(gcodePathAndFileName))
                                    {
                                        string oemName = "MatterControl";
                                        if (OemSettings.Instance.WindowTitleExtra != null && OemSettings.Instance.WindowTitleExtra.Trim().Length > 0)
                                        {
                                            oemName = oemName + " - {0}".FormatWith(OemSettings.Instance.WindowTitleExtra);
                                        }

                                        gcodeWirter.WriteLine("; {0} Version {1} Build {2} : GCode settings used".FormatWith(oemName, VersionInfo.Instance.ReleaseVersion, VersionInfo.Instance.BuildVersion));
                                        gcodeWirter.WriteLine("; Date {0} Time {1}:{2:00}".FormatWith(DateTime.Now.Date, DateTime.Now.Hour, DateTime.Now.Minute));

                                        foreach (string line in File.ReadLines(currentConfigurationFileAndPath))
                                        {
                                            gcodeWirter.WriteLine("; {0}".FormatWith(line));
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }

                    UiThread.RunOnIdle(() =>
                    {
                        itemToSlice.CurrentlySlicing = false;
                        itemToSlice.DoneSlicing      = true;
                    });

                    lock (listOfSlicingItems)
                    {
                        listOfSlicingItems.RemoveAt(0);
                    }
                }

                Thread.Sleep(100);
            }
        }