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);
            }
        }
Exemple #2
0
        static void CreateSlicedPartsThread()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            while (!haltSlicingThread)
            {
                if (PrinterConnectionAndCommunication.Instance.ActivePrintItem != null && 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 (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((state) =>
                                        {
                                            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))
                            {
                                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((state) =>
                    {
                        itemToSlice.CurrentlySlicing = false;
                        itemToSlice.DoneSlicing      = true;
                    });

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

                Thread.Sleep(100);
            }
        }
Exemple #3
0
        static void CreateSlicedPartsThread()
        {
            while (!haltSlicingThread)
            {
                if (PrinterCommunication.Instance.ActivePrintItem != null && listOfSlicingItems.Count > 0)
                {
                    PrintItemWrapper itemToSlice = listOfSlicingItems[0];
                    // check that the STL file is currently on disk
                    if (File.Exists(itemToSlice.FileLocation))
                    {
                        itemToSlice.CurrentlySlicing = true;

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

                        string gcodePathAndFileName = itemToSlice.GCodePathAndFileName;
                        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 + "\" \"" + itemToSlice.PartToSlicePathAndFileName + "\"";
                                break;

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

                            case ActivePrinterProfile.SlicingEngineTypes.MatterSlice:
                            {
                                EngineMappingsMatterSlice.WriteMatterSliceSettingsFile(currentConfigurationFileAndPath);
                                commandArgs = "-v -o \"" + gcodePathAndFileName + "\" -c \"" + currentConfigurationFileAndPath + "\" \"" + itemToSlice.PartToSlicePathAndFileName + "\"";
                            }
                            break;
                            }

#if RUN_MATTER_SLICE_IN_PROCESS
                            if (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
#endif
                            {
                                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((state) =>
                                        {
                                            itemToSlice.OnSlicingOutputMessage(new StringEventArgs(message));
                                        });
                                    }
                                };

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

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

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

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

                Thread.Sleep(100);
            }
        }