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); 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 void 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); var libraryProvider = new LibraryProviderSQLite(null, null, null, "Local Library"); libraryProvider.EnsureSamplePartsExist(calibrationPrintFileNames); var queueItems = QueueData.Instance.GetItemNames(); // Finally, ensure missing calibration parts are added to the queue if missing var filenamesWithoutExtensions = calibrationPrintFileNames.Select(f => Path.GetFileNameWithoutExtension(f)); foreach (string nameOnly in filenamesWithoutExtensions) { if (queueItems.Contains(nameOnly)) { continue; } // Find the first library item with the given name and add it to the queue PrintItem libraryItem = libraryProvider.GetLibraryItems(nameOnly).FirstOrDefault(); if (libraryItem != null) { QueueData.Instance.AddItem(new PrintItemWrapper(libraryItem)); } } libraryProvider.Dispose(); } }
private static void AddStlOrGcode(LibraryProviderSQLite 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 (MeshFileIo.ValidFileExtensions().Contains(extension)) { List <MeshGroup> meshToConvertAndSave = MeshFileIo.Load(loadedFileName); try { PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem); 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); 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); } } }
public void LibraryProviderSqlite_NavigationWorking() { StaticData.Instance = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null, null, "Local Library"); testProvider.DataReloaded += (sender, e) => { dataReloaded = true; }; Thread.Sleep(3000); // wait for the library to finish initializing UiThread.InvokePendingActions(); Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); Assert.IsTrue(testProvider.ItemCount == 3, "Start with a new database for these tests."); // create a collection and make sure it is on disk dataReloaded = false; // it has been loaded for the default set of parts string collectionName = "Collection1"; Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB Assert.IsTrue(dataReloaded == false); testProvider.AddCollectionToLibrary(collectionName); Assert.IsTrue(testProvider.CollectionCount == 1); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(NamedCollectionExists(collectionName)); // assert that the record does exist in the DB PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapperAsync(0).Result; // add an item works correctly dataReloaded = false; Assert.IsTrue(!NamedItemExists(collectionName)); Assert.IsTrue(dataReloaded == false); testProvider.AddFilesToLibrary(new string[] { meshPathAndFileName }); Thread.Sleep(3000); // wait for the add to finish UiThread.InvokePendingActions(); Assert.IsTrue(testProvider.ItemCount == 4); Assert.IsTrue(dataReloaded == true); string fileNameWithExtension = Path.GetFileNameWithoutExtension(meshPathAndFileName); Assert.IsTrue(NamedItemExists(fileNameWithExtension)); // make sure the provider locater is correct // remove item works dataReloaded = false; Assert.IsTrue(dataReloaded == false); testProvider.RemoveItem(0); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(!NamedItemExists(fileNameWithExtension)); // remove collection gets rid of it dataReloaded = false; Assert.IsTrue(dataReloaded == false); testProvider.RemoveCollection(0); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(testProvider.CollectionCount == 0); Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB //MatterControlUtilities.RestoreStaticDataAfterTesting(staticDataState, true); }
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); using (LibraryProviderSQLite instance = new LibraryProviderSQLite(null, null, null, "Local Library")) { await instance.EnsureSamplePartsExist(calibrationPrintFileNames); } var queueItems = QueueData.Instance.GetItemNames(); // Finally, ensure missing calibration parts are added to the queue if missing var filenamesWithoutExtensions = calibrationPrintFileNames.Select(f => Path.GetFileNameWithoutExtension(f)); foreach (string nameOnly in filenamesWithoutExtensions) { if (queueItems.Contains(nameOnly)) { continue; } // If the library item does not exist in the queue, add it IEnumerable<PrintItem> libraryItems = null; using (LibraryProviderSQLite instance = new LibraryProviderSQLite(null, null, null, "Local Library")) { libraryItems = instance.GetLibraryItems(nameOnly); } foreach (PrintItem libraryItem in libraryItems) { if (libraryItem != null) { QueueData.Instance.AddItem(new PrintItemWrapper(libraryItem)); } } } } }
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); }
public void LoadCalibrationPrints() { // Load the calibration file names string calibrationFiles = ActiveSliceSettings.Instance.GetValue("calibration_files"); if(string.IsNullOrEmpty(calibrationFiles)) { return; } string[] calibrationPrintFileNames = calibrationFiles.Split(';'); if (calibrationPrintFileNames.Length < 1) { return; } var libraryProvider = new LibraryProviderSQLite(null, null, null, "Local Library"); libraryProvider.EnsureSamplePartsExist(calibrationPrintFileNames); var queueItems = QueueData.Instance.GetItemNames(); // Finally, ensure missing calibration parts are added to the queue if missing var filenamesWithoutExtensions = calibrationPrintFileNames.Select(f => Path.GetFileNameWithoutExtension(f)); foreach (string nameOnly in filenamesWithoutExtensions) { if (queueItems.Contains(nameOnly)) { continue; } // Find the first library item with the given name and add it to the queue PrintItem libraryItem = libraryProvider.GetLibraryItems(nameOnly).FirstOrDefault(); if (libraryItem != null) { QueueData.Instance.AddItem(new PrintItemWrapper(libraryItem)); } } libraryProvider.Dispose(); }
private static void AddStlOrGcode(LibraryProviderSQLite 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 (MeshFileIo.ValidFileExtensions().Contains(extension)) { List<MeshGroup> meshToConvertAndSave = MeshFileIo.Load(loadedFileName); try { PrintItemWrapper printItemWrapper = new PrintItemWrapper(printItem); 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); 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); } } }
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); 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(); }
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; }
public void LibraryProviderSqlite_NavigationWorking() { Datastore.Instance.Initialize(); LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null); Thread.Sleep(3000); // wait for the library to finish initializing Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); Assert.IsTrue(testProvider.ItemCount == 1, "Start with a new database for these tests."); // create a collection and make sure it is on disk dataReloaded = false; // it has been loaded for the default set of parts string collectionName = "Collection1"; Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB Assert.IsTrue(dataReloaded == false); testProvider.AddCollectionToLibrary(collectionName); Assert.IsTrue(testProvider.CollectionCount == 1); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(NamedCollectionExists(collectionName)); // assert that the record does exist in the DB PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapper(0); List<ProviderLocatorNode> providerLocator = itemAtRoot.PrintItem.GetLibraryProviderLocator(); Assert.IsTrue(providerLocator.Count == 1); // add an item works correctly dataReloaded = false; Assert.IsTrue(!NamedItemExists(collectionName)); Assert.IsTrue(dataReloaded == false); testProvider.AddFilesToLibrary(new string[] { meshPathAndFileName }); Thread.Sleep(3000); // wait for the add to finihs Assert.IsTrue(testProvider.ItemCount == 2); Assert.IsTrue(dataReloaded == true); string fileNameWithExtension = Path.GetFileNameWithoutExtension(meshPathAndFileName); Assert.IsTrue(NamedItemExists(fileNameWithExtension)); // make sure the provider locator is correct // remove item works dataReloaded = false; Assert.IsTrue(dataReloaded == false); testProvider.RemoveItem(testProvider.GetPrintItemWrapper(1)); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(!NamedItemExists(fileNameWithExtension)); // remove collection gets rid of it dataReloaded = false; Assert.IsTrue(dataReloaded == false); testProvider.RemoveCollection(testProvider.GetCollectionItem(0)); Assert.IsTrue(dataReloaded == true); Assert.IsTrue(testProvider.CollectionCount == 0); Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB }