From e83c5e7388eab360360bc9e5616f708500b4bc30 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 20 Dec 2016 10:43:39 -0800 Subject: [PATCH] Add LayerGCodeHasExpectedValue test - Immutable PrinterSettings.BaseLayer - Add PrinterSettings.AutoSave flag to support disabling autosave - Save to default path fails under test - Add PrinterSettings.Save(path) to support manual save - Migrate 'active_quality_key' to SettingsKey class --- .../Settings/PrinterSettings.cs | 47 +++++--------- .../Settings/ProfileMigrations.cs | 2 +- .../Settings/SettingsHelpers.cs | 1 + SlicerConfiguration/SliceSettingsOrganizer.cs | 29 +++++---- .../MatterControl/OemProfileTests.cs | 63 +++++++++++++++++-- 5 files changed, 93 insertions(+), 49 deletions(-) diff --git a/SlicerConfiguration/Settings/PrinterSettings.cs b/SlicerConfiguration/Settings/PrinterSettings.cs index 9d72b4e4c..80f9396a3 100644 --- a/SlicerConfiguration/Settings/PrinterSettings.cs +++ b/SlicerConfiguration/Settings/PrinterSettings.cs @@ -57,8 +57,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public static RootedObjectEventHandler PrintLevelingEnabledChanged = new RootedObjectEventHandler(); - private static PrinterSettingsLayer baseLayerCache; - public static PrinterSettings Empty { get; } public int DocumentVersion { get; set; } = LatestVersion; @@ -110,7 +108,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } } } - + [OnDeserialized] internal void OnDeserializedMethod(StreamingContext context) { @@ -132,7 +130,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration "layer_id", }; - if(!setLayerName) + if (!setLayerName) { skipKeys.Add(SettingsKey.layer_name); } @@ -146,9 +144,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration var sourceFilter = rawSourceFilter.Where(layer => layer != null); - var baseLayer = settingsToImport.BaseLayer; - settingsToImport.BaseLayer = new PrinterSettingsLayer(); - foreach (var keyName in PrinterSettings.KnownSettings) { if (settingsToImport.Contains(keyName)) @@ -171,8 +166,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration destinationLayer[SettingsKey.layer_name] = settingsToImport.GetValue(SettingsKey.layer_name, sourceFilter); } - settingsToImport.BaseLayer = baseLayer; - this.Save(); ApplicationController.Instance.ReloadAdvancedControlsPanel(); @@ -197,11 +190,11 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { get { - return GetValue("active_quality_key"); + return GetValue(SettingsKey.active_quality_key); } internal set { - SetValue("active_quality_key", value); + SetValue(SettingsKey.active_quality_key, value); QualityLayer = GetQualityLayer(value); Save(); } @@ -267,17 +260,25 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return SHA1; } } - + private string DocumentPath => ProfileManager.Instance.ProfilePath(this.ID); + [JsonIgnore] + public bool AutoSave { get; set; } = true; + public void Save() { // Skip save operation if on the EmptyProfile - if (!this.PrinterSelected) + if (!this.PrinterSelected || !this.AutoSave) { return; } + Save(DocumentPath); + } + + public void Save(string filePath) + { lock (writeLock) { string json = this.ToJson(); @@ -289,7 +290,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration ProfileManager.Instance.Save(); } - File.WriteAllText(DocumentPath, json); + File.WriteAllText(filePath, json); } if (ActiveSliceSettings.Instance?.ID == this.ID) @@ -510,23 +511,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration } [JsonIgnore] - public PrinterSettingsLayer BaseLayer - { - get - { - if (baseLayerCache == null) - { - baseLayerCache = SliceSettingsOrganizer.Instance.GetDefaultSettings(); - } - - return baseLayerCache; - } - - internal set - { - baseLayerCache = value; - } - } + public PrinterSettingsLayer BaseLayer { get; set; } = SliceSettingsOrganizer.Instance.GetDefaultSettings(); private IEnumerable defaultLayerCascade { diff --git a/SlicerConfiguration/Settings/ProfileMigrations.cs b/SlicerConfiguration/Settings/ProfileMigrations.cs index 14d2363a0..95b4b1cf0 100644 --- a/SlicerConfiguration/Settings/ProfileMigrations.cs +++ b/SlicerConfiguration/Settings/ProfileMigrations.cs @@ -234,7 +234,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration break; case "MatterControl.ActiveQualityKey": - layer.Add("active_quality_key", item.Value); + layer.Add(SettingsKey.active_quality_key, item.Value); break; case "MatterControl.ActiveMaterialKey": diff --git a/SlicerConfiguration/Settings/SettingsHelpers.cs b/SlicerConfiguration/Settings/SettingsHelpers.cs index b232bb4c8..e216dae52 100644 --- a/SlicerConfiguration/Settings/SettingsHelpers.cs +++ b/SlicerConfiguration/Settings/SettingsHelpers.cs @@ -112,6 +112,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public const string ip_address = nameof(ip_address); public const string ip_port = nameof(ip_port); public const string first_layer_speed = nameof(first_layer_speed); + public const string active_quality_key = nameof(active_quality_key); } public class SettingsHelpers diff --git a/SlicerConfiguration/SliceSettingsOrganizer.cs b/SlicerConfiguration/SliceSettingsOrganizer.cs index 96288292d..e0100182f 100644 --- a/SlicerConfiguration/SliceSettingsOrganizer.cs +++ b/SlicerConfiguration/SliceSettingsOrganizer.cs @@ -1,10 +1,5 @@ -using MatterHackers.Agg.PlatformAbstract; -using MatterHackers.Localizations; -using Newtonsoft.Json; -using Newtonsoft.Json.Converters; - -/* -Copyright (c) 2014, Kevin Pope +/* +Copyright (c) 2016, Kevin Pope, John Lewin All rights reserved. Redistribution and use in source and binary forms, with or without @@ -35,6 +30,10 @@ either expressed or implied, of the FreeBSD Project. using System; using System.Collections.Generic; using System.IO; +using MatterHackers.Agg.PlatformAbstract; +using MatterHackers.Localizations; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; namespace MatterHackers.MatterControl.SlicerConfiguration { @@ -210,6 +209,8 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public class SliceSettingsOrganizer { + private static Dictionary defaultSettings = null; + private Dictionary userLevels = new Dictionary(); public Dictionary UserLevels @@ -351,14 +352,18 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public PrinterSettingsLayer GetDefaultSettings() { - Dictionary settingsDictionary = new Dictionary(); - - foreach(SliceSettingData settingsData in this.SettingsData) + if (defaultSettings == null) { - settingsDictionary[settingsData.SlicerConfigName] = settingsData.DefaultValue; + var settingsDictionary = new Dictionary(); + foreach (var sliceSettingsData in this.SettingsData) + { + settingsDictionary[sliceSettingsData.SlicerConfigName] = sliceSettingsData.DefaultValue; + } + + defaultSettings = settingsDictionary; } - return new PrinterSettingsLayer(settingsDictionary); + return new PrinterSettingsLayer(defaultSettings); } } } \ No newline at end of file diff --git a/Tests/MatterControl.Tests/MatterControl/OemProfileTests.cs b/Tests/MatterControl.Tests/MatterControl/OemProfileTests.cs index 0df5e221e..2d7c533c4 100644 --- a/Tests/MatterControl.Tests/MatterControl/OemProfileTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/OemProfileTests.cs @@ -32,6 +32,43 @@ namespace MatterControl.Tests.MatterControl }).ToList(); } + [Test, RunInApplicationDomain] + public void LayerGCodeHasExpectedValue() + { + // Verifies "layer_gcode" is expected value: "; LAYER:[layer_num]" + ValidateOnAllPrinters((printer, settings) => + { + if (settings.GetValue(SettingsKey.layer_gcode) != "; LAYER:[layer_num]") + { + printer.RuleViolated = true; + + /* Fix existing invalid items... + string layerValue; + if (settings.OemLayer.TryGetValue(SettingsKey.layer_gcode, out layerValue) && layerValue == "") + { + settings.OemLayer.Remove(SettingsKey.layer_gcode); + } + + if (settings.QualityLayer?.TryGetValue(SettingsKey.layer_gcode, out layerValue) == true && layerValue == "") + { + settings.QualityLayer.Remove(SettingsKey.layer_gcode); + } + + if (settings.MaterialLayer?.TryGetValue(SettingsKey.layer_gcode, out layerValue) == true && layerValue == "") + { + settings.MaterialLayer.Remove(SettingsKey.layer_gcode); + } + + // Reset to default values + settings.UserLayer.Remove(SettingsKey.active_quality_key); + settings.MaterialSettingsKeys = new List(); + settings.StagedUserSettings = new PrinterSettingsLayer(); + + settings.Save(printer.ConfigPath); */ + } + }); + } + [Test] public void StartGCodeWithExtrudesMustFollowM109Heatup() { @@ -412,7 +449,7 @@ namespace MatterControl.Tests.MatterControl } /// - /// Calls the given delegate for each known printer, passing in a PrinterConfig object that has + /// Calls the given delegate for each printer as well as each quality/material layer, passing in a PrinterConfig object that has /// printer settings loaded into a SettingsLayer as well as state about the printer /// /// The action to invoke for each printer @@ -424,20 +461,30 @@ namespace MatterControl.Tests.MatterControl { printer.RuleViolated = false; - PrinterSettingsLayer oemLayer = printer.PrinterSettings.OemLayer; + var printerSettings = printer.PrinterSettings; + printerSettings.AutoSave = false; - action(printer, new PrinterSettings() { OemLayer = oemLayer }); + // Disable active material/quality overrides + printerSettings.SetMaterialPreset(0, ""); + printerSettings.ActiveQualityKey = ""; + + // Validate just the OemLayer + action(printer, printerSettings); if (printer.RuleViolated) { ruleViolations.Add(printer.RelativeFilePath); } + // Validate material layers foreach (var layer in printer.PrinterSettings.MaterialLayers) { printer.RuleViolated = false; - action(printer, new PrinterSettings() { BaseLayer = oemLayer, OemLayer = layer }); + printerSettings.SetMaterialPreset(0, layer.LayerID); + + // Validate the settings with this material layer active + action(printer, printerSettings); if (printer.RuleViolated) { @@ -445,11 +492,17 @@ namespace MatterControl.Tests.MatterControl } } + printerSettings.SetMaterialPreset(0, ""); + + // Validate quality layers foreach (var layer in printer.PrinterSettings.QualityLayers) { printer.RuleViolated = false; - action(printer, new PrinterSettings() { BaseLayer = oemLayer, OemLayer = layer }); + printerSettings.ActiveQualityKey = layer.LayerID; + + // Validate the settings with this quality layer active + action(printer, printerSettings); if (printer.RuleViolated) {