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.
This commit is contained in:
larsbrubaker 2014-10-24 14:55:27 -07:00
parent 08dc379472
commit b2643d55c4
4 changed files with 109 additions and 42 deletions

View file

@ -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))));
}

View file

@ -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)
{

View file

@ -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<MeshGroup> 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<MeshGroup> 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)
{
}
}
}

View file

@ -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);
}
}
}