Esempio n. 1
0
 public void ExportAsSlic3rConfig()
 {
     FileDialog.SaveFileDialog(
         new SaveFileDialogParams("Save Slice Configuration".Localize() + "|*.ini")
     {
         FileName = printerSettings.GetValue(SettingsKey.printer_name)
     },
         (saveParams) =>
     {
         try
         {
             if (!string.IsNullOrWhiteSpace(saveParams.FileName))
             {
                 Slic3rEngineMappings.WriteSliceSettingsFile(saveParams.FileName);
             }
         }
         catch (Exception e)
         {
             UiThread.RunOnIdle(() => {
                 StyledMessageBox.ShowMessageBox(null, e.Message, "Couldn't save file".Localize());
             });
         }
     });
 }
Esempio n. 2
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.Helpers.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");

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

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

                            switch (ActiveSliceSettings.Instance.Helpers.ActiveSliceEngineType())
                            {
                            case SlicingEngineTypes.Slic3r:
                                Slic3rEngineMappings.WriteSliceSettingsFile(currentConfigurationFileAndPath);
                                // if we have centering turend on and are printing a model loaded up from meshes (not gcode)
                                if (ActiveSliceSettings.Instance.GetValue <bool>(SettingsKey.center_part_on_bed))
                                {
                                    // figure out the center position of this file
                                    Vector2 bedCenter = ActiveSliceSettings.Instance.GetValue <Vector2>(SettingsKey.print_center);
                                    commandArgs = $"--print-center {bedCenter.x:0.##},{bedCenter.y:0.##} " + "--load \"" + currentConfigurationFileAndPath + "\" --output \"" + gcodePathAndFileName + "\" \"" + fileToSlice + "\"";
                                }
                                else
                                {
                                    commandArgs = "--load \"" + currentConfigurationFileAndPath + "\" --output \"" + gcodePathAndFileName + "\" \"" + fileToSlice + "\"";
                                }
                                break;

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

                            case SlicingEngineTypes.MatterSlice:
                            {
                                EngineMappingsMatterSlice.WriteSliceSettingsFile(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.Helpers.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 fileHasSettings = 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"))
                                    {
                                        fileHasSettings = true;
                                    }
                                }

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

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

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

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

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

                Thread.Sleep(100);
            }
        }