Restore ability to attach VS to Android via debugger

- Prevent accidental construction of ProviderSQLite objects inside of ProviderSQLite constructor
 - Initialize .baseLibraryCollection during rootLibraryCollection construction to prevent null reference errors
   caused by access before assignment
 - Remove AddItem -> AddStlOrGcode -> AddItem recursion - Fixes crash due to concurrent writes to default.mcp
 - Add sqlite async methods and supporting logic to ensure disk IO operations aren’t run in parallel
 - Consolidate logic around sample part extraction from StaticData from various areas into a single method
 - Rename that method from SyncCalibrationFilesToDisk to EnsureSamplePartsExist
 - Break apart the large LINQ query into discrete and easier to understand steps
 - Add shared base class for sqlite backed library providers and resuse common code in base
 - Guard against cache deletion during one-time library initialization
 - Discard StreamReader cases in favor of ReadAllText - Ensures .Dispose is always called and is ultra succinct
 - Purge orphaned code
This commit is contained in:
John Lewin 2015-08-08 11:25:51 -07:00
parent 84253d92b8
commit 0ab547bb26
11 changed files with 308 additions and 665 deletions

View file

@ -101,6 +101,11 @@ namespace MatterHackers.MatterControl
public static void DeleteCacheData()
{
if(LibraryProviderSQLite.Instance.PreloadingCalibrationFiles)
{
return;
}
// delete everything in the GCodeOutputPath
// AppData\Local\MatterControl\data\gcode
// delete everything in the temp data that is not in use
@ -213,7 +218,8 @@ namespace MatterHackers.MatterControl
case ".STL":
case ".AMF":
case ".GCODE":
if (referencedPrintItemsFilePaths.Contains(file))
//
if (referencedPrintItemsFilePaths.Contains(file) || LibraryProviderSQLite.Instance.PreloadingCalibrationFiles && Path.GetDirectoryName(file).Contains("calibration-parts"))
{
contentCount++;
}

View file

@ -53,31 +53,6 @@ namespace MatterHackers.MatterControl.PrintHistory
}
}
private List<string> GetLibraryParts()
{
List<string> libraryFilesToPreload = new List<string>();
string setupSettingsPathAndFile = Path.Combine("OEMSettings", "PreloadedLibraryFiles.txt");
if (StaticData.Instance.FileExists(setupSettingsPathAndFile))
{
try
{
foreach (string line in StaticData.Instance.ReadAllLines(setupSettingsPathAndFile))
{
//Ignore commented lines
if (!line.StartsWith("#"))
{
string settingLine = line.Trim();
libraryFilesToPreload.Add(settingLine);
}
}
}
catch
{
}
}
return libraryFilesToPreload;
}
public static readonly int RecordLimit = 20;
public IEnumerable<DataStorage.PrintTask> GetHistoryItems(int recordCount)

View file

@ -33,6 +33,7 @@ using MatterHackers.Agg.PlatformAbstract;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrintQueue;
using MatterHackers.PolygonMesh;
using MatterHackers.PolygonMesh.Processors;
using System;
using System.Collections.Generic;
@ -41,6 +42,158 @@ using System.Threading.Tasks;
namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
public abstract class ClassicSqliteStorageProvider : LibraryProvider
{
private string keywordFilter = string.Empty;
protected PrintItemCollection baseLibraryCollection;
protected List<PrintItemCollection> childCollections = new List<PrintItemCollection>();
public ClassicSqliteStorageProvider(LibraryProvider parentLibraryProvider)
: base(parentLibraryProvider)
{
}
protected virtual void AddStlOrGcode(string loadedFileName, string displayName)
{
string extension = Path.GetExtension(loadedFileName).ToUpper();
PrintItem printItem = new PrintItem();
printItem.Name = displayName;
printItem.FileLocation = Path.GetFullPath(loadedFileName);
printItem.PrintItemCollectionID = this.baseLibraryCollection.Id;
printItem.Commit();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension)))
{
List<MeshGroup> meshToConvertAndSave = MeshFileIo.Load(loadedFileName);
try
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, this);
SaveToLibraryFolder(printItemWrapper, meshToConvertAndSave, false);
}
catch (System.UnauthorizedAccessException)
{
UiThread.RunOnIdle(() =>
{
//Do something special when unauthorized?
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes, unauthorized access", "Unable to save");
});
}
catch
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save");
});
}
}
else // it is not a mesh so just add it
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, this);
string sourceFileName = printItem.FileLocation;
string newFileName = Path.ChangeExtension(Path.GetRandomFileName(), Path.GetExtension(printItem.FileLocation));
string destFileName = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, newFileName);
File.Copy(sourceFileName, destFileName, true);
printItemWrapper.FileLocation = destFileName;
printItemWrapper.PrintItem.Commit();
}
}
protected static void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List<MeshGroup> meshGroups, bool AbsolutePositioned)
{
string[] metaData = { "Created By", "MatterControl" };
if (AbsolutePositioned)
{
metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" };
}
if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath))
{
MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData);
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, metaData);
MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo);
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
QueueData.Instance.SaveDefaultQueue();
}
printItemWrapper.OnFileHasChanged();
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
return childCollections[collectionIndex];
}
public override bool Visible
{
get { return true; }
}
public override void Dispose()
{
}
public override string ProviderData
{
get
{
return baseLibraryCollection.Id.ToString();
}
}
public override string KeywordFilter
{
get
{
return keywordFilter;
}
set
{
keywordFilter = value;
}
}
protected IEnumerable<PrintItemCollection> GetChildCollections()
{
string query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
IEnumerable<PrintItemCollection> result = (IEnumerable<PrintItemCollection>)Datastore.Instance.dbSQLite.Query<PrintItemCollection>(query);
return result;
}
public IEnumerable<PrintItem> GetLibraryItems(string keyphrase = null)
{
string query;
if (keyphrase == null)
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
}
else
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", baseLibraryCollection.Id, keyphrase);
}
IEnumerable<PrintItem> result = (IEnumerable<PrintItem>)Datastore.Instance.dbSQLite.Query<PrintItem>(query);
return result;
}
}
public abstract class LibraryProvider : IDisposable
{
public event EventHandler DataReloaded;

View file

@ -63,15 +63,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public class LibraryProviderHistory : LibraryProvider
public class LibraryProviderHistory : ClassicSqliteStorageProvider
{
private static LibraryProviderHistory instance = null;
private PrintItemCollection baseLibraryCollection;
private List<PrintItemCollection> childCollections = new List<PrintItemCollection>();
private string keywordFilter = string.Empty;
EventHandler unregisterEvent;
public LibraryProviderHistory(PrintItemCollection baseLibraryCollection, LibraryProvider parentLibraryProvider)
: base(parentLibraryProvider)
@ -118,15 +112,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override bool Visible
{
get { return true; }
}
public override void Dispose()
{
}
public override int CollectionCount
{
get
@ -144,19 +129,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string KeywordFilter
{
get
{
return keywordFilter;
}
set
{
keywordFilter = value;
}
}
public override string Name
{
get
@ -165,14 +137,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string ProviderData
{
get
{
return baseLibraryCollection.Id.ToString();
}
}
public override string ProviderKey
{
get
@ -181,35 +145,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List<MeshGroup> meshGroups, bool AbsolutePositioned)
{
string[] metaData = { "Created By", "MatterControl" };
if (AbsolutePositioned)
{
metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" };
}
if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath))
{
MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData);
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, metaData);
MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo);
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
QueueData.Instance.SaveDefaultQueue();
}
printItemWrapper.OnFileHasChanged();
}
public override void AddCollectionToLibrary(string collectionName)
{
}
@ -252,27 +187,5 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
//PrintHistoryData.Instance.RemoveAt(itemToRemoveIndex);
OnDataReloaded(null);
}
private IEnumerable<PrintItemCollection> GetChildCollections()
{
string query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
IEnumerable<PrintItemCollection> result = (IEnumerable<PrintItemCollection>)Datastore.Instance.dbSQLite.Query<PrintItemCollection>(query);
return result;
}
public IEnumerable<PrintItem> GetLibraryItems(string keyphrase = null)
{
string query;
if (keyphrase == null)
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
}
else
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", baseLibraryCollection.Id, keyphrase);
}
IEnumerable<PrintItem> result = (IEnumerable<PrintItem>)Datastore.Instance.dbSQLite.Query<PrintItem>(query);
return result;
}
}
}

View file

@ -62,13 +62,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public class LibraryProviderQueue : LibraryProvider
public class LibraryProviderQueue : ClassicSqliteStorageProvider
{
private static LibraryProviderQueue instance = null;
private PrintItemCollection baseLibraryCollection;
private List<PrintItemCollection> childCollections = new List<PrintItemCollection>();
private string keywordFilter = string.Empty;
EventHandler unregisterEvent;
@ -116,15 +112,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override bool Visible
{
get { return true; }
}
public override void Dispose()
{
}
public override int CollectionCount
{
get
@ -141,19 +128,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string KeywordFilter
{
get
{
return keywordFilter;
}
set
{
keywordFilter = value;
}
}
public override string Name
{
get
@ -162,14 +136,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string ProviderData
{
get
{
return baseLibraryCollection.Id.ToString();
}
}
public override string ProviderKey
{
get
@ -178,35 +144,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List<MeshGroup> meshGroups, bool AbsolutePositioned)
{
string[] metaData = { "Created By", "MatterControl" };
if (AbsolutePositioned)
{
metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" };
}
if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath))
{
MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData);
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, metaData);
MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo);
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
QueueData.Instance.SaveDefaultQueue();
}
printItemWrapper.OnFileHasChanged();
}
public override void AddCollectionToLibrary(string collectionName)
{
}
@ -221,11 +158,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
QueueData.Instance.AddItem(item, indexToInsert);
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
return childCollections[collectionIndex];
}
public async override Task<PrintItemWrapper> GetPrintItemWrapperAsync(int index)
{
return QueueData.Instance.GetPrintItemWrapper(index);
@ -245,114 +177,5 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
QueueData.Instance.RemoveAt(itemToRemoveIndex);
OnDataReloaded(null);
}
private static void AddStlOrGcode(LibraryProviderQueue libraryToAddTo, string loadedFileName, string extension)
{
PrintItem printItem = new PrintItem();
printItem.Name = Path.GetFileNameWithoutExtension(loadedFileName);
printItem.FileLocation = Path.GetFullPath(loadedFileName);
printItem.PrintItemCollectionID = libraryToAddTo.baseLibraryCollection.Id;
printItem.Commit();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension)))
{
List<MeshGroup> meshToConvertAndSave = MeshFileIo.Load(loadedFileName);
try
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, libraryToAddTo);
SaveToLibraryFolder(printItemWrapper, meshToConvertAndSave, false);
libraryToAddTo.AddItem(printItemWrapper);
}
catch (System.UnauthorizedAccessException)
{
UiThread.RunOnIdle(() =>
{
//Do something special when unauthorized?
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes, unauthorized access", "Unable to save");
});
}
catch
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save");
});
}
}
else // it is not a mesh so just add it
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, libraryToAddTo);
if (false)
{
libraryToAddTo.AddItem(printItemWrapper);
}
else // save a copy to the library and update this to point at it
{
string sourceFileName = printItem.FileLocation;
string newFileName = Path.ChangeExtension(Path.GetRandomFileName(), Path.GetExtension(printItem.FileLocation));
string destFileName = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, newFileName);
File.Copy(sourceFileName, destFileName, true);
printItemWrapper.FileLocation = destFileName;
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
libraryToAddTo.AddItem(printItemWrapper);
}
}
}
private IEnumerable<PrintItemCollection> GetChildCollections()
{
string query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
IEnumerable<PrintItemCollection> result = (IEnumerable<PrintItemCollection>)Datastore.Instance.dbSQLite.Query<PrintItemCollection>(query);
return result;
}
public IEnumerable<PrintItem> GetLibraryItems(string keyphrase = null)
{
string query;
if (keyphrase == null)
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
}
else
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", baseLibraryCollection.Id, keyphrase);
}
IEnumerable<PrintItem> result = (IEnumerable<PrintItem>)Datastore.Instance.dbSQLite.Query<PrintItem>(query);
return result;
}
private void loadFilesIntoLibraryBackgoundWorker_DoWork(IList<string> fileList)
{
foreach (string loadedFileName in fileList)
{
string extension = Path.GetExtension(loadedFileName).ToUpper();
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension))
|| extension == ".GCODE"
|| extension == ".ZIP")
{
if (extension == ".ZIP")
{
ProjectFileHandler project = new ProjectFileHandler(null);
List<PrintItem> partFiles = project.ImportFromProjectArchive(loadedFileName);
if (partFiles != null)
{
foreach (PrintItem part in partFiles)
{
AddStlOrGcode(this, part.FileLocation, Path.GetExtension(part.FileLocation).ToUpper());
}
}
}
else
{
AddStlOrGcode(this, loadedFileName, extension);
}
}
}
}
}
}

View file

@ -62,17 +62,15 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public class LibraryProviderSQLite : LibraryProvider
public class LibraryProviderSQLite : ClassicSqliteStorageProvider
{
private static LibraryProviderSQLite instance = null;
private PrintItemCollection baseLibraryCollection;
public bool PreloadingCalibrationFiles = false;
private List<PrintItemCollection> childCollections = new List<PrintItemCollection>();
private string keywordFilter = string.Empty;
private static LibraryProviderSQLite instance = null;
private List<PrintItemWrapper> printItems = new List<PrintItemWrapper>();
string visibleName;
private string visibleName;
public LibraryProviderSQLite(PrintItemCollection baseLibraryCollection, LibraryProvider parentLibraryProvider, string visibleName)
: base(parentLibraryProvider)
@ -81,14 +79,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
if (baseLibraryCollection == null)
{
baseLibraryCollection = GetRootLibraryCollection2(this);
baseLibraryCollection = GetRootLibraryCollection2().Result;
}
this.baseLibraryCollection = baseLibraryCollection;
LoadLibraryItems();
}
public static LibraryProvider Instance
public static LibraryProviderSQLite Instance
{
get
{
@ -128,15 +126,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override bool Visible
{
get { return true; }
}
public override void Dispose()
{
}
public override int CollectionCount
{
get
@ -153,19 +142,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string KeywordFilter
{
get
{
return keywordFilter;
}
set
{
keywordFilter = value;
}
}
public override string Name
{
get
@ -174,14 +150,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string ProviderData
{
get
{
return baseLibraryCollection.Id.ToString();
}
}
public override string ProviderKey
{
get
@ -190,94 +158,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public static IEnumerable<PrintItem> GetAllPrintItemsRecursive()
{
// NOTE: We are making the assumption that everything is reference if it does not have a 0 in eth PrintItemCollectionID.
string query = "SELECT * FROM PrintItem WHERE PrintItemCollectionID != 0;";
IEnumerable<PrintItem> result = (IEnumerable<PrintItem>)Datastore.Instance.dbSQLite.Query<PrintItem>(query);
return result;
}
static PrintItemCollection GetRootLibraryCollection2(LibraryProviderSQLite rootLibrary)
{
// Attempt to initialize the library from the Datastore if null
PrintItemCollection rootLibraryCollection = Datastore.Instance.dbSQLite.Table<PrintItemCollection>().Where(v => v.Name == "_library").Take(1).FirstOrDefault();
// If the _library collection is still missing, create and populate it with default content
if (rootLibraryCollection == null)
{
rootLibraryCollection = new PrintItemCollection();
rootLibraryCollection.Name = "_library";
rootLibraryCollection.Commit();
// Preload library with Oem supplied list of default parts
string[] itemsToAdd = SyncCalibrationFilesToDisk(OemSettings.Instance.PreloadedLibraryFiles);
if (itemsToAdd.Length > 0)
{
// Import any files sync'd to disk into the library, then add them to the queue
rootLibrary.AddFilesToLibrary(itemsToAdd);
}
}
return rootLibraryCollection;
}
static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List<MeshGroup> meshGroups, bool AbsolutePositioned)
{
string[] metaData = { "Created By", "MatterControl" };
if (AbsolutePositioned)
{
metaData = new string[] { "Created By", "MatterControl", "BedPosition", "Absolute" };
}
if (printItemWrapper.FileLocation.Contains(ApplicationDataStorage.Instance.ApplicationLibraryDataPath))
{
MeshOutputSettings outputInfo = new MeshOutputSettings(MeshOutputSettings.OutputType.Binary, metaData);
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, metaData);
MeshFileIo.Save(meshGroups, printItemWrapper.FileLocation, outputInfo);
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
QueueData.Instance.SaveDefaultQueue();
}
printItemWrapper.OnFileHasChanged();
}
public static string[] SyncCalibrationFilesToDisk(List<string> calibrationPrintFileNames)
{
// Ensure the CalibrationParts directory exists to store/import the files from disk
string tempPath = Path.Combine(ApplicationDataStorage.Instance.ApplicationUserDataPath, "data", "temp", "calibration-parts");
Directory.CreateDirectory(tempPath);
// Build a list of temporary files that should be imported into the library
return calibrationPrintFileNames.Where(fileName =>
{
// Filter out items that already exist in the library
LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null, null, "Local Library");
return rootLibrary.GetLibraryItems(Path.GetFileNameWithoutExtension(fileName)).Count() <= 0;
}).Select(fileName =>
{
// Copy calibration prints from StaticData to the filesystem before importing into the library
string tempFile = Path.Combine(tempPath, Path.GetFileName(fileName));
using (FileStream outstream = File.OpenWrite(tempFile))
using (Stream instream = StaticData.Instance.OpenSteam(Path.Combine("OEMSettings", "SampleParts", fileName)))
{
instream.CopyTo(outstream);
}
// Project the new filename to the output
return tempFile;
}).ToArray();
}
public override void AddCollectionToLibrary(string collectionName)
{
PrintItemCollection newCollection = new PrintItemCollection(collectionName, "");
@ -288,42 +168,18 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
public async override void AddItem(PrintItemWrapper itemToAdd)
{
if (itemToAdd != null && itemToAdd.FileLocation != null)
{
// create enough info to show that we have items pending (maybe use names from this file list for them)
// refresh the display to show the pending items
//LibraryProvider.OnDataReloaded(null);
await AddItemAsync(itemToAdd.Name, itemToAdd.FileLocation, fireDataReloaded: true);
await Task.Run(() => AddStlOrGcode(this, itemToAdd.FileLocation, itemToAdd.Name));
if (baseLibraryCollection != null)
{
LoadLibraryItems();
OnDataReloaded(null);
}
}
LoadLibraryItems();
OnDataReloaded(null);
}
public void AddItem(PrintItemWrapper item, int indexToInsert = -1)
public async Task AddItemAsync(string fileName, string fileLocation, bool fireDataReloaded)
{
if (indexToInsert == -1)
if (!string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(fileLocation))
{
indexToInsert = printItems.Count;
await Task.Run(() => AddStlOrGcode(fileLocation, fileName));
}
printItems.Insert(indexToInsert, item);
// Check if the collection we are adding to is the the currently visible collection.
List<ProviderLocatorNode> currentDisplayedCollection = GetProviderLocator();
if (currentDisplayedCollection.Count > 0 && currentDisplayedCollection[0].Key == LibraryProviderSQLite.StaticProviderKey)
{
//OnItemAdded(new IndexArgs(indexToInsert));
}
item.PrintItem.PrintItemCollectionID = baseLibraryCollection.Id;
item.PrintItem.Commit();
}
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
return childCollections[collectionIndex];
}
public async override Task<PrintItemWrapper> GetPrintItemWrapperAsync(int index)
@ -341,30 +197,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
return new LibraryProviderSQLite(collection, this, collection.Name);
}
void LoadLibraryItems()
{
printItems.Clear();
IEnumerable<PrintItem> partFiles = GetLibraryItems(KeywordFilter);
if (partFiles != null)
{
foreach (PrintItem part in partFiles)
{
PrintItemWrapper item = new PrintItemWrapper(part, this);
printItems.Add(item);
}
}
childCollections.Clear();
GetChildCollections();
IEnumerable<PrintItemCollection> collections = GetChildCollections();
if(collections != null)
{
childCollections.AddRange(collections);
}
OnDataReloaded(null);
}
public override void RemoveCollection(int collectionIndexToRemove)
{
childCollections[collectionIndexToRemove].Delete();
@ -389,86 +221,101 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
OnDataReloaded(null);
}
private static void AddStlOrGcode(LibraryProviderSQLite libraryToAddTo, string loadedFileName, string displayName)
public async Task EnsureSamplePartsExist(IEnumerable<string> filenamesToValidate)
{
string extension = Path.GetExtension(loadedFileName).ToUpper();
PreloadingCalibrationFiles = true;
PrintItem printItem = new PrintItem();
printItem.Name = displayName;
printItem.FileLocation = Path.GetFullPath(loadedFileName);
printItem.PrintItemCollectionID = libraryToAddTo.baseLibraryCollection.Id;
printItem.Commit();
// Ensure the CalibrationParts directory exists to store/import the files from disk
string tempPath = Path.Combine(ApplicationDataStorage.Instance.ApplicationUserDataPath, "data", "temp", "calibration-parts");
Directory.CreateDirectory(tempPath);
if ((extension != "" && MeshFileIo.ValidFileExtensions().Contains(extension)))
var existingLibaryItems = this.GetLibraryItems().Select(i => i.Name);
// Build a list of files that need to be imported into the library
var missingFiles = filenamesToValidate.Where(fileName => !existingLibaryItems.Contains(fileName, StringComparer.OrdinalIgnoreCase));
// Create temp files on disk that can be imported into the library
var tempFilesToImport = missingFiles.Select(fileName =>
{
List<MeshGroup> meshToConvertAndSave = MeshFileIo.Load(loadedFileName);
// Copy calibration prints from StaticData to the filesystem before importing into the library
string tempFilePath = Path.Combine(tempPath, Path.GetFileName(fileName));
using (FileStream outstream = File.OpenWrite(tempFilePath))
using (Stream instream = StaticData.Instance.OpenSteam(Path.Combine("OEMSettings", "SampleParts", fileName)))
{
instream.CopyTo(outstream);
}
try
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, libraryToAddTo);
SaveToLibraryFolder(printItemWrapper, meshToConvertAndSave, false);
libraryToAddTo.AddItem(printItemWrapper);
}
catch (System.UnauthorizedAccessException)
{
UiThread.RunOnIdle(() =>
{
//Do something special when unauthorized?
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes, unauthorized access", "Unable to save");
});
}
catch
{
UiThread.RunOnIdle(() =>
{
StyledMessageBox.ShowMessageBox(null, "Oops! Unable to save changes.", "Unable to save");
});
}
}
else // it is not a mesh so just add it
// Project the new filename to the output
return tempFilePath;
}).ToArray();
// Import any missing files into the library
foreach (string file in tempFilesToImport)
{
PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem, libraryToAddTo);
if (false)
{
libraryToAddTo.AddItem(printItemWrapper);
}
else // save a copy to the library and update this to point at it
{
string sourceFileName = printItem.FileLocation;
string newFileName = Path.ChangeExtension(Path.GetRandomFileName(), Path.GetExtension(printItem.FileLocation));
string destFileName = Path.Combine(ApplicationDataStorage.Instance.ApplicationLibraryDataPath, newFileName);
File.Copy(sourceFileName, destFileName, true);
printItemWrapper.FileLocation = destFileName;
printItemWrapper.PrintItem.Commit();
// let the queue know that the item has changed so it load the correct part
libraryToAddTo.AddItem(printItemWrapper);
}
// Ensure these operations run in serial rather than in parallel where they stomp on each other when writing to default.mcp
await this.AddItemAsync(Path.GetFileNameWithoutExtension(file), file, false);
}
PreloadingCalibrationFiles = false;
}
private IEnumerable<PrintItemCollection> GetChildCollections()
/// <summary>
/// Exposes all PrintItems for use in file purge code in AboutWidget
/// </summary>
/// <returns>A list of all print items</returns>
public static IEnumerable<PrintItem> GetAllPrintItemsRecursive()
{
string query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
IEnumerable<PrintItemCollection> result = (IEnumerable<PrintItemCollection>)Datastore.Instance.dbSQLite.Query<PrintItemCollection>(query);
return result;
}
public IEnumerable<PrintItem> GetLibraryItems(string keyphrase = null)
{
string query;
if (keyphrase == null)
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id);
}
else
{
query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", baseLibraryCollection.Id, keyphrase);
}
// NOTE: We are making the assumption that everything is reference if it does not have a 0 in eth PrintItemCollectionID.
string query = "SELECT * FROM PrintItem WHERE PrintItemCollectionID != 0;";
IEnumerable<PrintItem> result = (IEnumerable<PrintItem>)Datastore.Instance.dbSQLite.Query<PrintItem>(query);
return result;
}
private async Task<PrintItemCollection> GetRootLibraryCollection2()
{
// Attempt to initialize the library from the Datastore if null
PrintItemCollection rootLibraryCollection = Datastore.Instance.dbSQLite.Table<PrintItemCollection>().Where(v => v.Name == "_library").Take(1).FirstOrDefault();
// If the _library collection is still missing, create and populate it with default content
if (rootLibraryCollection == null)
{
rootLibraryCollection = new PrintItemCollection();
rootLibraryCollection.Name = "_library";
rootLibraryCollection.Commit();
// In this case we now need to update the baseLibraryCollection instance member as code that executes
// down this path will attempt to use the property and will exception if its not set
this.baseLibraryCollection = rootLibraryCollection;
// Preload library with Oem supplied list of default parts
await this.EnsureSamplePartsExist(OemSettings.Instance.PreloadedLibraryFiles);
}
return rootLibraryCollection;
}
private void LoadLibraryItems()
{
printItems.Clear();
IEnumerable<PrintItem> partFiles = GetLibraryItems(KeywordFilter);
if (partFiles != null)
{
foreach (PrintItem part in partFiles)
{
PrintItemWrapper item = new PrintItemWrapper(part, this);
printItems.Add(item);
}
}
childCollections.Clear();
GetChildCollections();
IEnumerable<PrintItemCollection> collections = GetChildCollections();
if (collections != null)
{
childCollections.AddRange(collections);
}
OnDataReloaded(null);
}
}
}

View file

@ -1779,7 +1779,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY);
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
BackgroundWorker backgroundWorker = (BackgroundWorker)sender;
try
{
// push all the transforms into the meshes

View file

@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MatterHackers.MatterControl
{
@ -48,73 +49,55 @@ namespace MatterHackers.MatterControl
return Convert.ToInt32(result);
}
public void LoadCalibrationPrints()
public async Task LoadCalibrationPrints()
{
if (this.ActivePrinter.Make != null && this.ActivePrinter.Model != null)
{
// Load the calibration file names
List<string> calibrationPrintFileNames = LoadCalibrationPartNamesForPrinter(this.ActivePrinter.Make, this.ActivePrinter.Model);
string[] itemsToAdd = LibraryProviderSQLite.SyncCalibrationFilesToDisk(calibrationPrintFileNames);
if (itemsToAdd.Length > 0)
{
// Import any files sync'd to disk into the library, then add them to the queue
LibraryProviderSQLite.Instance.AddFilesToLibrary(itemsToAdd);
AddItemsToQueue(calibrationPrintFileNames, QueueData.Instance.GetItemNames());
}
else
{
// Otherwise, just ensure the item gets into the queue
AddItemsToQueue(calibrationPrintFileNames, QueueData.Instance.GetItemNames());
}
}
}
await LibraryProviderSQLite.Instance.EnsureSamplePartsExist(calibrationPrintFileNames);
private static void AddItemsToQueue(List<string> calibrationPrintFileNames, string[] queueItems)
{
// After the import has completed, add each of the calibration items into the print queue
foreach (string fileName in calibrationPrintFileNames)
{
string nameOnly = Path.GetFileNameWithoutExtension(fileName);
if (queueItems.Contains(nameOnly))
{
continue;
}
var queueItems = QueueData.Instance.GetItemNames();
// If the library item does not exist in the queue, add it
foreach (PrintItem libraryItem in ((LibraryProviderSQLite)LibraryProviderSQLite.Instance).GetLibraryItems(nameOnly))
// Finally, ensure missing calibration parts are added to the queue if missing
foreach (string nameOnly in calibrationPrintFileNames)
{
if (libraryItem != null)
if (queueItems.Contains(nameOnly))
{
QueueData.Instance.AddItem(new PrintItemWrapper(libraryItem));
continue;
}
// TODO: We add any file named X into the queue? Isn't it possible to have bunch of files named X, in which case we copy all of them at this step?
// If the library item does not exist in the queue, add it
foreach (PrintItem libraryItem in LibraryProviderSQLite.Instance.GetLibraryItems(nameOnly))
{
if (libraryItem != null)
{
QueueData.Instance.AddItem(new PrintItemWrapper(libraryItem));
}
}
}
}
}
private List<string> LoadCalibrationPartNamesForPrinter(string make, string model)
{
List<string> calibrationFiles = new List<string>();
string setupSettingsPathAndFile = Path.Combine("PrinterSettings", make, model, "calibration.ini");
if (StaticData.Instance.FileExists(setupSettingsPathAndFile))
string filePath = Path.Combine("PrinterSettings", make, model, "calibration.ini");
if (StaticData.Instance.FileExists(filePath))
{
try
{
foreach (string line in StaticData.Instance.ReadAllLines(setupSettingsPathAndFile))
{
//Ignore commented lines
if (!line.StartsWith("#"))
{
string settingLine = line.Trim();
calibrationFiles.Add(settingLine);
}
}
return StaticData.Instance.ReadAllLines(filePath).Where(l => !l.StartsWith("#")).Select(l => l.Trim()).ToList();
}
catch
{
}
}
return calibrationFiles;
return new List<string>();
}
public void LoadSetupSettings(string make, string model)

View file

@ -255,12 +255,12 @@ namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
}
}
private void NextButton_Click(object sender, EventArgs mouseEvent)
private async void NextButton_Click(object sender, EventArgs mouseEvent)
{
bool canContinue = this.OnSave();
if (canContinue)
{
this.currentPrinterSetupStatus.LoadCalibrationPrints();
await this.currentPrinterSetupStatus.LoadCalibrationPrints();
UiThread.RunOnIdle(MoveToNextWidget);
}
}

View file

@ -29,6 +29,7 @@ either expressed or implied, of the FreeBSD Project.
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.DataStorage;
using MatterHackers.MatterControl.PrintLibrary.Provider;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
@ -98,22 +99,6 @@ namespace MatterHackers.MatterControl
}
}
public void SaveAs()
//Opens Save file dialog and outputs current queue as a project
{
SaveFileDialogParams saveParams = new SaveFileDialogParams("Save Project|*.mcp");
FileDialog.SaveFileDialog(saveParams, onSaveFileSelected);
}
private void onSaveFileSelected(SaveFileDialogParams saveParams)
{
if (saveParams.FileName != null)
{
ExportToJson(saveParams.FileName);
}
}
private static string applicationDataPath = ApplicationDataStorage.Instance.ApplicationUserDataPath;
private static string defaultPathAndFileName = applicationDataPath + "/data/default.mcp";
@ -128,49 +113,31 @@ namespace MatterHackers.MatterControl
{
Directory.CreateDirectory(applicationDataPath + "/data/");
}
FileStream fs = new FileStream(savedFileName, FileMode.Create);
StreamWriter sw = new System.IO.StreamWriter(fs);
sw.Write(jsonString);
sw.Close();
File.WriteAllText(savedFileName, jsonString);
}
public void OpenFromDialog()
public List<PrintItem> ImportFromJson(string filePath = null)
{
OpenFileDialogParams openParams = new OpenFileDialogParams("Select a Project file|*.mcp");
FileDialog.OpenFileDialog(openParams, onManifestFileLoad);
}
private void onManifestFileLoad(OpenFileDialogParams openParams)
{
if (openParams.FileName != null)
if (filePath == null)
{
string loadedFileName = openParams.FileName;
List<PrintItem> printItems = ImportFromJson(loadedFileName);
}
}
public List<PrintItem> ImportFromJson(string loadedFileName = null)
{
if (loadedFileName == null)
{
loadedFileName = defaultPathAndFileName;
filePath = defaultPathAndFileName;
}
if (System.IO.File.Exists(loadedFileName))
{
StreamReader sr = new System.IO.StreamReader(loadedFileName);
ManifestFile newProject = (ManifestFile)Newtonsoft.Json.JsonConvert.DeserializeObject(sr.ReadToEnd(), typeof(ManifestFile));
sr.Close();
if (newProject == null)
{
return new List<PrintItem>();
}
return newProject.ProjectFiles;
}
else
if (!System.IO.File.Exists(filePath))
{
return null;
}
string json = File.ReadAllText(filePath);
ManifestFile newProject = JsonConvert.DeserializeObject<ManifestFile>(json);
if (newProject == null)
{
return new List<PrintItem>();
}
return newProject.ProjectFiles;
}
}
}

View file

@ -240,29 +240,6 @@ namespace MatterHackers.MatterControl
}
}
public void OpenFromDialog()
{
OpenFileDialogParams openParams = new OpenFileDialogParams("Zip file|*.zip");
FileDialog.OpenFileDialog(openParams, onProjectArchiveLoad);
}
private void onProjectArchiveLoad(OpenFileDialogParams openParams)
{
List<PrintItem> partFiles;
if (openParams.FileNames != null)
{
string loadedFileName = openParams.FileName;
partFiles = ImportFromProjectArchive(loadedFileName);
if (partFiles != null)
{
foreach (PrintItem part in partFiles)
{
QueueData.Instance.AddItem(new PrintItemWrapper(new PrintItem(part.Name, part.FileLocation)));
}
}
}
}
public List<PrintItem> ImportFromProjectArchive(string loadedFileName = null)
{
if (loadedFileName == null)
@ -270,10 +247,14 @@ namespace MatterHackers.MatterControl
loadedFileName = defaultProjectPathAndFileName;
}
if (System.IO.File.Exists(loadedFileName))
if (!System.IO.File.Exists(loadedFileName))
{
return null;
}
using (FileStream fs = File.OpenRead(loadedFileName))
using (ZipArchive zip = new ZipArchive(fs))
{
FileStream fs = File.OpenRead(loadedFileName);
ZipArchive zip = new ZipArchive(fs);
int projectHashCode = zip.GetHashCode();
//If the temp folder doesn't exist - create it, otherwise clear it
@ -352,10 +333,6 @@ namespace MatterHackers.MatterControl
return printItemList;
}
else
{
return null;
}
}
private PrintItem GetPrintItemFromFile(string fileName, string displayName)