From b2643d55c4ea5a670dddd9127d17c7acf8cedfc4 Mon Sep 17 00:00:00 2001 From: larsbrubaker Date: Fri, 24 Oct 2014 14:55:27 -0700 Subject: [PATCH] Making the library always save to its own store fixed a problem when the extension mc is opened with is nothing a part would be added to the plate. --- MatterControlApplication.cs | 6 +- PartPreviewWindow/View3D/View3DWidget.cs | 19 +--- PrintLibrary/LibraryData.cs | 107 +++++++++++++++++++++-- PrintLibrary/PrintLibraryWidget.cs | 19 ++-- 4 files changed, 109 insertions(+), 42 deletions(-) diff --git a/MatterControlApplication.cs b/MatterControlApplication.cs index 8a2815218..a2cfee411 100644 --- a/MatterControlApplication.cs +++ b/MatterControlApplication.cs @@ -286,9 +286,9 @@ namespace MatterHackers.MatterControl firstDraw = false; foreach (string arg in commandLineArgs) { - if (arg.Length > 4 - && arg.Contains(".") - && MeshFileIo.ValidFileExtensions().Contains(Path.GetExtension(arg).ToUpper())) + string argExtension = Path.GetExtension(arg).ToUpper(); + if (argExtension.Length > 1 + && MeshFileIo.ValidFileExtensions().Contains(argExtension)) { QueueData.Instance.AddItem(new PrintItemWrapper(new DataStorage.PrintItem(Path.GetFileName(arg), Path.GetFullPath(arg)))); } diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index b85881da5..36060331c 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -41,6 +41,7 @@ using MatterHackers.Localizations; using MatterHackers.MatterControl.CustomWidgets; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.PrinterCommunication; +using MatterHackers.MatterControl.PrintLibrary; using MatterHackers.MatterControl.PrintQueue; using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.MeshVisualizer; @@ -1720,23 +1721,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow backgroundWorker.ReportProgress(nextPercent); } - if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) - { - MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); - MeshFileIo.Save(asynchMeshGroups, printItemWrapper.FileLocation, outputInfo); - } - else // save a copy to the library and update this to point at it - { - string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); - printItemWrapper.FileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); - - MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); - MeshFileIo.Save(asynchMeshGroups, printItemWrapper.FileLocation, outputInfo); - - printItemWrapper.PrintItem.Commit(); - } - - printItemWrapper.OnFileHasChanged(); + LibraryData.SaveToLibrary(printItemWrapper, asynchMeshGroups); } catch (System.UnauthorizedAccessException) { diff --git a/PrintLibrary/LibraryData.cs b/PrintLibrary/LibraryData.cs index 0158f0fb7..b251da941 100644 --- a/PrintLibrary/LibraryData.cs +++ b/PrintLibrary/LibraryData.cs @@ -29,18 +29,16 @@ either expressed or implied, of the FreeBSD Project. using System; using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.ComponentModel; using System.IO; - -using MatterHackers.Agg.Image; using MatterHackers.Agg; -using MatterHackers.Agg.UI; -using MatterHackers.VectorMath; -using MatterHackers.MatterControl; +using MatterHackers.Localizations; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.PrintQueue; using MatterHackers.MatterControl.SettingsManagement; +using MatterHackers.PolygonMesh; +using MatterHackers.Agg.UI; +using MatterHackers.PolygonMesh.Processors; namespace MatterHackers.MatterControl.PrintLibrary { @@ -73,6 +71,27 @@ namespace MatterHackers.MatterControl.PrintLibrary } } + static public void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroups) + { + if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath)) + { + MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); + MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); + } + else // save a copy to the library and update this to point at it + { + string fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".amf"); + printItemWrapper.FileLocation = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, fileName); + + MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, new string[] { "Created By", "MatterControl" }); + MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo); + + printItemWrapper.PrintItem.Commit(); + } + + printItemWrapper.OnFileHasChanged(); + } + string keywordFilter = ""; public string KeywordFilter { @@ -236,5 +255,77 @@ namespace MatterHackers.MatterControl.PrintLibrary return PrintItems.Count; } } - } + + ReportProgress fileLoadReportProgress = null; + public void LoadFilesIntoLibrary(string[] files, ReportProgress reportProgress = null) + { + this.fileLoadReportProgress = reportProgress; + if (files != null && files.Length > 0) + { + BackgroundWorker mergeAndSavePartsBackgroundWorker = new BackgroundWorker(); + mergeAndSavePartsBackgroundWorker.WorkerReportsProgress = true; + + mergeAndSavePartsBackgroundWorker.DoWork += new DoWorkEventHandler(mergeAndSavePartsBackgroundWorker_DoWork); + mergeAndSavePartsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(mergeAndSavePartsBackgroundWorker_RunWorkerCompleted); + + mergeAndSavePartsBackgroundWorker.RunWorkerAsync(files); + } + } + + void mergeAndSavePartsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + { + string[] fileList = e.Argument as string[]; + foreach (string loadedFileName in fileList) + { + PrintItem printItem = new PrintItem(); + printItem.Name = Path.GetFileNameWithoutExtension(loadedFileName); + printItem.FileLocation = Path.GetFullPath(loadedFileName); + printItem.PrintItemCollectionID = LibraryData.Instance.LibraryCollection.Id; + printItem.Commit(); + + LibraryData.Instance.AddItem(new PrintItemWrapper(printItem)); + } +#if false + SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroups); + + // we sent the data to the asynch lists but we will not pull it back out (only use it as a temp holder). + PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); + + Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; + BackgroundWorker backgroundWorker = (BackgroundWorker)sender; + try + { + // push all the transforms into the meshes + for (int i = 0; i < asynchMeshGroups.Count; i++) + { + asynchMeshGroups[i].Transform(asynchMeshGroupTransforms[i].TotalTransform); + + int nextPercent = (i + 1) * 40 / asynchMeshGroups.Count; + backgroundWorker.ReportProgress(nextPercent); + } + + LibraryData.SaveToLibrary(printItemWrapper, asynchMeshGroups); + } + catch (System.UnauthorizedAccessException) + { + UiThread.RunOnIdle((state) => + { + //Do something special when unauthorized? + StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save"); + }); + } + catch + { + UiThread.RunOnIdle((state) => + { + StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save"); + }); + } +#endif + } + + void mergeAndSavePartsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + { + } + } } \ No newline at end of file diff --git a/PrintLibrary/PrintLibraryWidget.cs b/PrintLibrary/PrintLibraryWidget.cs index 59ff65bec..77259546c 100644 --- a/PrintLibrary/PrintLibraryWidget.cs +++ b/PrintLibrary/PrintLibraryWidget.cs @@ -141,7 +141,7 @@ namespace MatterHackers.MatterControl.PrintLibrary addToLibraryButton = textImageButtonFactory.Generate(LocalizedString.Get("Import"), "icon_import_white_32x32.png"); buttonPanel.AddChild(addToLibraryButton); addToLibraryButton.Margin = new BorderDouble(0, 0, 3, 0); - addToLibraryButton.Click += new EventHandler(loadFile_Click); + addToLibraryButton.Click += new EventHandler(importToLibraryloadFile_Click); addToQueueButton = textImageButtonFactory.Generate("Add to Queue".Localize()); addToQueueButton.Margin = new BorderDouble(3, 0); @@ -391,12 +391,12 @@ namespace MatterHackers.MatterControl.PrintLibrary base.OnDragDrop(fileDropEventArgs); } - void loadFile_Click(object sender, EventArgs mouseEvent) + void importToLibraryloadFile_Click(object sender, EventArgs mouseEvent) { - UiThread.RunOnIdle(loadFile_ClickOnIdle); + UiThread.RunOnIdle(importToLibraryloadFile_ClickOnIdle); } - void loadFile_ClickOnIdle(object state) + void importToLibraryloadFile_ClickOnIdle(object state) { OpenFileDialogParams openParams = new OpenFileDialogParams(ApplicationSettings.OpenPrintableFileParams, multiSelect: true); FileDialog.OpenFileDialog(openParams, onLibraryLoadFileSelected); @@ -406,16 +406,7 @@ namespace MatterHackers.MatterControl.PrintLibrary { if (openParams.FileNames != null) { - foreach (string loadedFileName in openParams.FileNames) - { - PrintItem printItem = new PrintItem(); - printItem.Name = Path.GetFileNameWithoutExtension(loadedFileName); - printItem.FileLocation = Path.GetFullPath(loadedFileName); - printItem.PrintItemCollectionID = LibraryData.Instance.LibraryCollection.Id; - printItem.Commit(); - - LibraryData.Instance.AddItem(new PrintItemWrapper(printItem)); - } + LibraryData.Instance.LoadFilesIntoLibrary(openParams.FileNames); } } }