From 0eb7b1cb3c333a8ef4ee0a4e01d03639ae39ea5f Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Wed, 14 Nov 2018 14:28:29 -0800 Subject: [PATCH] remove active printer from PrintItemWrapper issue: MatterHackers/MCCentral#4574 --- MatterControlLib/AboutPage/CacheDirectory.cs | 1 - .../ApplicationView/ApplicationController.cs | 2 +- MatterControlLib/ApplicationView/BedConfig.cs | 4 +-- .../ApplicationView/EditContext.cs | 24 +++++++++++++-- .../ApplicationView/PrinterConfig.cs | 30 +++++++++++++++++-- .../Library/Export/GCodeExport.cs | 2 +- .../PartPreviewWindow/PrinterTabPage.cs | 8 ++--- .../View3D/PrinterBar/SliceButton.cs | 2 +- MatterControlLib/Queue/PrintItemWrapper.cs | 29 ++---------------- 9 files changed, 61 insertions(+), 41 deletions(-) diff --git a/MatterControlLib/AboutPage/CacheDirectory.cs b/MatterControlLib/AboutPage/CacheDirectory.cs index 6b5b30e5f..942d32d33 100644 --- a/MatterControlLib/AboutPage/CacheDirectory.cs +++ b/MatterControlLib/AboutPage/CacheDirectory.cs @@ -65,7 +65,6 @@ namespace MatterHackers.MatterControl // Add in all the stl and amf files referenced in the library. foreach (PrintItem printItem in allPrintItems) { - var printItemWrapper = new PrintItemWrapper(printItem); if (!filesToKeep.Contains(printItem.FileLocation)) { filesToKeep.Add(printItem.FileLocation); diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index fc4cb6598..65312563b 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -2035,7 +2035,7 @@ namespace MatterHackers.MatterControl public async Task PrintPart(EditContext editContext, PrinterConfig printer, IProgress reporter, CancellationToken cancellationToken, bool overrideAllowGCode = false) { var partFilePath = editContext.SourceFilePath; - var gcodeFilePath = editContext.GCodeFilePath; + var gcodeFilePath = editContext.GCodeFilePath(printer); var printItemName = editContext.SourceItem.Name; // Exit if called in a non-applicable state diff --git a/MatterControlLib/ApplicationView/BedConfig.cs b/MatterControlLib/ApplicationView/BedConfig.cs index 00697f7b9..261b8721f 100644 --- a/MatterControlLib/ApplicationView/BedConfig.cs +++ b/MatterControlLib/ApplicationView/BedConfig.cs @@ -286,11 +286,11 @@ namespace MatterHackers.MatterControl internal void EnsureGCodeLoaded() { if (this.LoadedGCode == null - && File.Exists(this.EditContext?.GCodeFilePath)) + && File.Exists(this.EditContext?.GCodeFilePath(this.Printer))) { UiThread.RunOnIdle(async () => { - using (var stream = File.OpenRead(this.EditContext.GCodeFilePath)) + using (var stream = File.OpenRead(this.EditContext.GCodeFilePath(this.Printer))) { await LoadGCodeContent(stream); } diff --git a/MatterControlLib/ApplicationView/EditContext.cs b/MatterControlLib/ApplicationView/EditContext.cs index 9ed6fda13..aabd55980 100644 --- a/MatterControlLib/ApplicationView/EditContext.cs +++ b/MatterControlLib/ApplicationView/EditContext.cs @@ -62,13 +62,31 @@ namespace MatterHackers.MatterControl } // Natural path - private string gcodePath => printItem?.GetGCodePathAndFileName(); + private string GCodePath(PrinterConfig printer) + { + if (printItem != null) + { + return printer.GetGCodePathAndFileName(printItem.FileLocation); + } + return null; + } // Override path - public string GCodeOverridePath => Path.ChangeExtension(gcodePath, GCodeFile.PostProcessedExtension); + public string GCodeOverridePath(PrinterConfig printer) + { + return Path.ChangeExtension(GCodePath(printer), GCodeFile.PostProcessedExtension); + } // Override or natural path - public string GCodeFilePath => (File.Exists(this.GCodeOverridePath)) ? this.GCodeOverridePath : gcodePath; + public string GCodeFilePath(PrinterConfig printer) + { + if (File.Exists(this.GCodeOverridePath(printer))) + { + return this.GCodeOverridePath(printer); + } + + return GCodePath(printer); + } public string SourceFilePath => printItem?.FileLocation; diff --git a/MatterControlLib/ApplicationView/PrinterConfig.cs b/MatterControlLib/ApplicationView/PrinterConfig.cs index 360c0b97c..f2f1ab6d2 100644 --- a/MatterControlLib/ApplicationView/PrinterConfig.cs +++ b/MatterControlLib/ApplicationView/PrinterConfig.cs @@ -34,9 +34,11 @@ using MatterHackers.MatterControl.SlicerConfiguration; namespace MatterHackers.MatterControl { + using System.IO; using System.Threading; using MatterHackers.Agg; using MatterHackers.Localizations; + using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.PrinterCommunication; using MatterHackers.MatterControl.SlicerConfiguration.MappingClasses; using MatterHackers.MeshVisualizer; @@ -127,7 +129,6 @@ namespace MatterHackers.MatterControl this.Connection.PrintFinished += PrintFinished; this.Disposed += (s, e) => this.Connection.PrintFinished -= PrintFinished; - if (!string.IsNullOrEmpty(this.Settings.GetValue(SettingsKey.baud_rate))) { this.Connection.BaudRate = this.Settings.GetValue(SettingsKey.baud_rate); @@ -142,6 +143,32 @@ namespace MatterHackers.MatterControl this.Connection.ReadLineReplacementString = this.Settings.GetValue(SettingsKey.read_regex); } + public string GetGCodePathAndFileName(string fileLocation) + { + if (fileLocation.Trim() != "") + { + if (Path.GetExtension(fileLocation).ToUpper() == ".GCODE") + { + return fileLocation; + } + + return GCodePath(fileLocation); + } + else + { + return null; + } + } + + public string GCodePath(string fileHashCode) + { + long settingsHashCode = this.Settings.GetLongHashCode(); + + return Path.Combine( + ApplicationDataStorage.Instance.GCodeOutputPath, + $"{fileHashCode}_{ settingsHashCode}.gcode"); + } + public string ReplaceMacroValues(string gcodeWithMacros) { foreach (MappedSetting mappedSetting in replaceWithSettingsStrings) @@ -278,7 +305,6 @@ namespace MatterHackers.MatterControl this.Connection.CommunicationStateChanged += CommunicationStateChanged; this.Disposed += (s, e) => this.Connection.CommunicationStateChanged -= CommunicationStateChanged; - void Printer_SettingChanged(object s, EventArgs e) { if (e is StringEventArgs stringArg diff --git a/MatterControlLib/Library/Export/GCodeExport.cs b/MatterControlLib/Library/Export/GCodeExport.cs index f8e163696..fc6970382 100644 --- a/MatterControlLib/Library/Export/GCodeExport.cs +++ b/MatterControlLib/Library/Export/GCodeExport.cs @@ -173,7 +173,7 @@ namespace MatterHackers.MatterControl.Library.Export string fileHashCode = Path.GetFileNameWithoutExtension(assetPath); - string gcodePath = PrintItemWrapper.GCodePath(fileHashCode); + string gcodePath = printer.GCodePath(fileHashCode); if (ApplicationSettings.ValidFileExtensions.IndexOf(sourceExtension, StringComparison.OrdinalIgnoreCase) >= 0 || string.Equals(sourceExtension, ".mcx", StringComparison.OrdinalIgnoreCase)) diff --git a/MatterControlLib/PartPreviewWindow/PrinterTabPage.cs b/MatterControlLib/PartPreviewWindow/PrinterTabPage.cs index 11c8252cb..72d0fc755 100644 --- a/MatterControlLib/PartPreviewWindow/PrinterTabPage.cs +++ b/MatterControlLib/PartPreviewWindow/PrinterTabPage.cs @@ -536,7 +536,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow activelySlicing = true; if (bottomRow.Name == null) { - bottomRow.Name = printer.Bed.EditContext.GCodeFilePath; + bottomRow.Name = printer.Bed.EditContext.GCodeFilePath(printer); } await ApplicationController.Instance.Tasks.Execute("Saving".Localize(), printer.Bed.SaveChanges); @@ -545,7 +545,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow await ApplicationController.Instance.SliceItemLoadOutput( printer, printer.Bed.Scene, - printer.Bed.EditContext.GCodeFilePath); + printer.Bed.EditContext.GCodeFilePath(printer)); // Switch to the 3D layer view if on Model view if (printer.ViewState.ViewMode == PartViewMode.Model) @@ -563,8 +563,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (printer.Connection != null && (printer.Connection.PrinterIsPrinting || printer.Connection.PrinterIsPaused)) { - printer.Connection.SwitchToGCode(printer.Bed.EditContext.GCodeFilePath); - bottomRow.Name = printer.Bed.EditContext.GCodeFilePath; + printer.Connection.SwitchToGCode(printer.Bed.EditContext.GCodeFilePath(printer)); + bottomRow.Name = printer.Bed.EditContext.GCodeFilePath(printer); } } else diff --git a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/SliceButton.cs b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/SliceButton.cs index 3173b91c9..b5da449d5 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/SliceButton.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/PrinterBar/SliceButton.cs @@ -110,7 +110,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow await ApplicationController.Instance.SliceItemLoadOutput( printer, printer.Bed.Scene, - printer.Bed.EditContext.GCodeFilePath); + printer.Bed.EditContext.GCodeFilePath(printer)); } catch (Exception ex) { diff --git a/MatterControlLib/Queue/PrintItemWrapper.cs b/MatterControlLib/Queue/PrintItemWrapper.cs index fb803e61e..ae2582811 100644 --- a/MatterControlLib/Queue/PrintItemWrapper.cs +++ b/MatterControlLib/Queue/PrintItemWrapper.cs @@ -69,9 +69,11 @@ namespace MatterHackers.MatterControl.PrintQueue public class PrintItemWrapper { private string fileType; + PrinterConfig printer; public PrintItemWrapper(PrintItem printItem, ILibraryContainer sourceLibraryProviderLocator = null) { + this.printer = printer; this.PrintItem = printItem; if (FileLocation != null) @@ -84,6 +86,7 @@ namespace MatterHackers.MatterControl.PrintQueue public PrintItemWrapper(int printItemId) { + this.printer = printer; this.PrintItem = Datastore.Instance.dbSQLite.Table().Where(v => v.Id == printItemId).Take(1).FirstOrDefault(); try { @@ -138,31 +141,5 @@ namespace MatterHackers.MatterControl.PrintQueue // result in inserts rather than update statements on a missing row this.PrintItem.Id = 0; } - - public string GetGCodePathAndFileName() - { - if (FileLocation.Trim() != "") - { - if (Path.GetExtension(FileLocation).ToUpper() == ".GCODE") - { - return FileLocation; - } - - return GCodePath(this.FileHashCode); - } - else - { - return null; - } - } - - public static string GCodePath(string fileHashCode) - { - long settingsHashCode = ApplicationController.Instance.ActivePrinter.Settings.GetLongHashCode(); - - return Path.Combine( - ApplicationDataStorage.Instance.GCodeOutputPath, - $"{fileHashCode}_{ settingsHashCode}.gcode"); - } } } \ No newline at end of file