diff --git a/MatterControl.Printing/MappingClasses/ScaledSingleNumber.cs b/MatterControl.Printing/MappingClasses/AsPercentOrDirectFirst.cs similarity index 90% rename from MatterControl.Printing/MappingClasses/ScaledSingleNumber.cs rename to MatterControl.Printing/MappingClasses/AsPercentOrDirectFirst.cs index 2f2abfd14..8e1fadfc6 100644 --- a/MatterControl.Printing/MappingClasses/ScaledSingleNumber.cs +++ b/MatterControl.Printing/MappingClasses/AsPercentOrDirectFirst.cs @@ -29,19 +29,14 @@ either expressed or implied, of the FreeBSD Project. namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses { - public class ScaledSingleNumber : MapFirstValue + public class AsPercentOrDirectFirst : MapFirstValue { - private double scale; - - public ScaledSingleNumber(double scale = 1) - { - this.scale = scale; - } - public override string Resolve(string value, PrinterSettings settings) { double ratio = 0; + value = base.Resolve(value, settings); + if (value.Contains("%")) { string withoutPercent = value.Replace("%", ""); @@ -52,7 +47,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses ratio = ParseDouble(value); } - return (ratio * scale).ToString(); + return ratio.ToString(); } } } \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/GCodeForSlicer.cs b/MatterControl.Printing/MappingClasses/GCodeMapping.cs similarity index 89% rename from MatterControl.Printing/MappingClasses/GCodeForSlicer.cs rename to MatterControl.Printing/MappingClasses/GCodeMapping.cs index 82f26b83a..675a8eacc 100644 --- a/MatterControl.Printing/MappingClasses/GCodeForSlicer.cs +++ b/MatterControl.Printing/MappingClasses/GCodeMapping.cs @@ -29,18 +29,14 @@ either expressed or implied, of the FreeBSD Project. namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses { - public class GCodeForSlicer : InjectGCodeCommands + public class GCodeMapping : MappedSetting { - public GCodeForSlicer() - { - // TODO: GCodeForSlicer? - System.Diagnostics.Debugger.Break(); - } - public override string Resolve(string value, PrinterSettings settings) { - value = base.Resolve(value, settings); + // Unescape newlines + value = value.Replace("\\n", "\n"); + // Macro replace and restore escaped newlines return settings.ReplaceMacroValues(value.Replace("\n", "\\n")); } } diff --git a/MatterControl.Printing/MappingClasses/InjectGCodeCommands.cs b/MatterControl.Printing/MappingClasses/InjectGCodeCommands.cs deleted file mode 100644 index d245a159a..000000000 --- a/MatterControl.Printing/MappingClasses/InjectGCodeCommands.cs +++ /dev/null @@ -1,59 +0,0 @@ -/* -Copyright (c) 2016, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. -*/ - -using System.Collections.Generic; - -namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses -{ - public class InjectGCodeCommands : UnescapeNewlineCharacters - { - protected void AddDefaultIfNotPresent(List linesAdded, string commandToAdd, string[] lines, string comment) - { - string command = commandToAdd.Split(' ')[0].Trim(); - - if (!LineStartsWith(lines, command)) - { - linesAdded.Add(string.Format("{0} ; {1}", commandToAdd, comment)); - } - } - - protected static bool LineStartsWith(string[] lines, string command) - { - foreach (string line in lines) - { - if (line.StartsWith(command)) - { - return true; - } - } - - return false; - } - } -} \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs b/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs index 24898599f..bad38dbb4 100644 --- a/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs +++ b/MatterControl.Printing/MappingClasses/MapLayerChangeGCode.cs @@ -29,20 +29,25 @@ either expressed or implied, of the FreeBSD Project. namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses { - public class MapLayerChangeGCode : InjectGCodeCommands + public class MapLayerChangeGCode : MappedSetting { public override string Resolve(string value, PrinterSettings settings) { - string macroReplaced = base.Resolve(value, settings); - if (!macroReplaced.Contains("; LAYER:") - && !macroReplaced.Contains(";LAYER:")) + // Unescape newlines + value = value.Replace("\\n", "\n"); + + if (!value.Contains("; LAYER:") + && !value.Contains(";LAYER:")) { - macroReplaced += "; LAYER:[layer_num]\n"; + if(value.Length > 0) + { + value += "\n"; + } + + value += "; LAYER:[layer_num]\n"; } - macroReplaced = settings.ReplaceMacroValues(macroReplaced.Replace("\n", "\\n")); - - return macroReplaced; + return settings.ReplaceMacroValues(value.Replace("\n", "\\n")); } } } \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/MapStartGCode.cs b/MatterControl.Printing/MappingClasses/MapStartGCode.cs deleted file mode 100644 index 9944d3a32..000000000 --- a/MatterControl.Printing/MappingClasses/MapStartGCode.cs +++ /dev/null @@ -1,204 +0,0 @@ -/* -Copyright (c) 2016, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. -*/ - -using System; -using System.Collections.Generic; -using System.Text; -using MatterHackers.Agg; - -namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses -{ - public class MapStartGCode : InjectGCodeCommands - { - private readonly bool escapeNewlineCharacters; - - public MapStartGCode(bool escapeNewlineCharacters) - { - this.escapeNewlineCharacters = escapeNewlineCharacters; - } - - public override string Resolve(string value, PrinterSettings settings) - { - System.Diagnostics.Debugger.Break(); - - var newStartGCode = new StringBuilder(); - - //foreach (string line in PreStartGCode(Slicer.ExtrudersUsed)) - //{ - // newStartGCode.Append(line + "\n"); - //} - - value = base.Resolve(value, settings); - - newStartGCode.Append(settings.ReplaceMacroValues(value)); - - //foreach (string line in PostStartGCode(Slicer.ExtrudersUsed)) - //{ - // newStartGCode.Append("\n"); - // newStartGCode.Append(line); - //} - - if (escapeNewlineCharacters) - { - return newStartGCode.ToString().Replace("\n", "\\n"); - } - - return newStartGCode.ToString(); - } - - public List PreStartGCode(PrinterSettings settings, List extrudersUsed) - { - string startGCode = settings.GetValue(SettingsKey.start_gcode); - string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries); - - var preStartGCode = new List - { - "; automatic settings before start_gcode" - }; - AddDefaultIfNotPresent(preStartGCode, "G21", startGCodeLines, "set units to millimeters"); - AddDefaultIfNotPresent(preStartGCode, "M107", startGCodeLines, "fan off"); - double bed_temperature = settings.GetValue(SettingsKey.bed_temperature); - if (bed_temperature > 0) - { - string setBedTempString = string.Format("M140 S{0}", bed_temperature); - AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "start heating the bed"); - } - - int numberOfHeatedExtruders = settings.Helpers.HotendCount(); - - // Start heating all the extruder that we are going to use. - for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) - { - if (extrudersUsed.Count > hotendIndex - && extrudersUsed[hotendIndex]) - { - double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); - if (materialTemperature != 0) - { - string setTempString = "M104 T{0} S{1}".FormatWith(hotendIndex, materialTemperature); - AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"start heating T{hotendIndex}"); - } - } - } - - // If we need to wait for the heaters to heat up before homing then set them to M109 (heat and wait). - if (settings.GetValue(SettingsKey.heat_extruder_before_homing)) - { - for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) - { - if (extrudersUsed.Count > hotendIndex - && extrudersUsed[hotendIndex]) - { - double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); - if (materialTemperature != 0) - { - string setTempString = "M109 T{0} S{1}".FormatWith(hotendIndex, materialTemperature); - AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"wait for T{hotendIndex }"); - } - } - } - } - - // If we have bed temp and the start gcode specifies to finish heating the extruders, - // make sure we also finish heating the bed. This preserves legacy expectation. - if (bed_temperature > 0 - && startGCode.Contains("M109")) - { - string setBedTempString = string.Format("M190 S{0}", bed_temperature); - AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached"); - } - - SwitchToFirstActiveExtruder(extrudersUsed, preStartGCode); - preStartGCode.Add("; settings from start_gcode"); - - return preStartGCode; - } - - public List PostStartGCode(PrinterSettings settings, List extrudersUsed) - { - string startGCode = settings.GetValue(SettingsKey.start_gcode); - string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries); - - var postStartGCode = new List - { - "; automatic settings after start_gcode" - }; - - double bed_temperature = settings.GetValue(SettingsKey.bed_temperature); - if (bed_temperature > 0 - && !startGCode.Contains("M109")) - { - string setBedTempString = string.Format("M190 S{0}", bed_temperature); - AddDefaultIfNotPresent(postStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached"); - } - - int numberOfHeatedExtruders = settings.GetValue(SettingsKey.extruder_count); - // wait for them to finish - for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) - { - if (hotendIndex < extrudersUsed.Count - && extrudersUsed[hotendIndex]) - { - double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); - if (materialTemperature != 0) - { - if (!(hotendIndex == 0 && LineStartsWith(startGCodeLines, "M109 S")) - && !LineStartsWith(startGCodeLines, $"M109 T{hotendIndex} S")) - { - // always heat the extruders that are used beyond extruder 0 - postStartGCode.Add($"M109 T{hotendIndex} S{materialTemperature} ; Finish heating T{hotendIndex}"); - } - } - } - } - - SwitchToFirstActiveExtruder(extrudersUsed, postStartGCode); - AddDefaultIfNotPresent(postStartGCode, "G90", startGCodeLines, "use absolute coordinates"); - postStartGCode.Add(string.Format("{0} ; {1}", "G92 E0", "reset the expected extruder position")); - AddDefaultIfNotPresent(postStartGCode, "M82", startGCodeLines, "use absolute distance for extrusion"); - - return postStartGCode; - } - - private void SwitchToFirstActiveExtruder(List extrudersUsed, List preStartGCode) - { - // make sure we are on the first active extruder - for (int extruderIndex = 0; extruderIndex < extrudersUsed.Count; extruderIndex++) - { - if (extrudersUsed[extruderIndex]) - { - // set the active extruder to the first one that will be printing - preStartGCode.Add("T{0} ; {1}".FormatWith(extruderIndex, "set the active extruder to {0}".FormatWith(extruderIndex))); - // we have set the active extruder so don't set it to any other extruder - break; - } - } - } - } -} \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/MappedSetting.cs b/MatterControl.Printing/MappingClasses/MappedSetting.cs index 59a6489ac..531148c5e 100644 --- a/MatterControl.Printing/MappingClasses/MappedSetting.cs +++ b/MatterControl.Printing/MappingClasses/MappedSetting.cs @@ -35,6 +35,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses { return value; } + public double ParseDouble(string textValue, double valueOnError = 0) { if (!double.TryParse(textValue, out double value)) diff --git a/MatterControl.Printing/MappingClasses/ReplaceWithSetting.cs b/MatterControl.Printing/MappingClasses/ReplaceWithSetting.cs deleted file mode 100644 index 9de265f04..000000000 --- a/MatterControl.Printing/MappingClasses/ReplaceWithSetting.cs +++ /dev/null @@ -1,48 +0,0 @@ -/* -Copyright (c) 2016, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. -*/ - -namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses -{ - public class ReplaceWithSetting : MappedSetting - { - private string replaceSettingsName; - - public ReplaceWithSetting(string replaceSettingsName) - { - // TODO: What role does this type have? - System.Diagnostics.Debugger.Break(); - this.replaceSettingsName = replaceSettingsName; - } - - public override string Resolve(string value, PrinterSettings settings) - { - return settings.GetValue(replaceSettingsName); - } - } -} \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/RetractionLength.cs b/MatterControl.Printing/MappingClasses/RetractionLength.cs deleted file mode 100644 index 7ac81e2fb..000000000 --- a/MatterControl.Printing/MappingClasses/RetractionLength.cs +++ /dev/null @@ -1,52 +0,0 @@ -/* -Copyright (c) 2016, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. -*/ - -namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses -{ - public class RetractionLength : MappedSetting - { - public RetractionLength() - { - // TODO: lookd like conditional field - System.Diagnostics.Debugger.Break(); - } - - public override string Resolve(string value, PrinterSettings settings) - { - if (settings.GetValue(SettingsKey.enable_retractions)) - { - return value; - } - else - { - return "0"; - } - } - } -} \ No newline at end of file diff --git a/MatterControl.Printing/MappingClasses/UnescapeNewlineCharacters.cs b/MatterControl.Printing/MappingClasses/UnescapeNewlineCharacters.cs deleted file mode 100644 index e734f1d7c..000000000 --- a/MatterControl.Printing/MappingClasses/UnescapeNewlineCharacters.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* -Copyright (c) 2016, Lars Brubaker -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -The views and conclusions contained in the software and documentation are those -of the authors and should not be interpreted as representing official policies, -either expressed or implied, of the FreeBSD Project. -*/ - -namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses -{ - // Replaces escaped newline characters with unescaped newline characters - public class UnescapeNewlineCharacters : MappedSetting - { - public UnescapeNewlineCharacters() - { - // TODO: This type has no role. Collapse into utility function in consumer - System.Diagnostics.Debugger.Break(); - } - - public override string Resolve(string value, PrinterSettings settings) - { - return value.Replace("\\n", "\n"); - } - } -} \ No newline at end of file diff --git a/MatterControl.Printing/Settings/SliceSettingsFields.cs b/MatterControl.Printing/Settings/SliceSettingsFields.cs index c5c07864e..8038bdd44 100644 --- a/MatterControl.Printing/Settings/SliceSettingsFields.cs +++ b/MatterControl.Printing/Settings/SliceSettingsFields.cs @@ -371,7 +371,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration HelpText = "G-Code to be run at the end of all automatic output (the very end of the G-Code commands).".Localize(), DataEditType = DataEditTypes.MULTI_LINE_TEXT, DefaultValue = "M104 S0 ; turn off temperature\\nG28 X0 ; home X axis\\nM84 ; disable motors", - Resolver = new GCodeForSlicer(), + Resolver = new GCodeMapping(), }, new SliceSettingData() { @@ -526,7 +526,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration HelpText = "The amount of infill material to generate, expressed as a ratio or a percentage.".Localize(), DataEditType = DataEditTypes.DOUBLE_OR_PERCENT, DefaultValue = "0.4", - Resolver = new ScaledSingleNumber(100), + Resolver = new AsPercentOrDirectFirst(), }, new SliceSettingData() { @@ -1304,7 +1304,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DataEditType = DataEditTypes.POSITIVE_DOUBLE, Units = "%".Localize(), DefaultValue = "90", - Resolver = new ScaledSingleNumber(.01), + Resolver = new AsPercentOrDirect(), QuickMenuSettings = { { "Light", "20" }, { "Standard", "80" }, { "Heavy", "100" } } }, new SliceSettingData() @@ -1396,7 +1396,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DefaultValue = "1", ShowIfSet = "!sla_printer", EnableIfSet = SettingsKey.enable_retractions, - Resolver = new RetractionLength(), + Resolver = new ConditionalField(SettingsKey.enable_retractions, new MappedSetting()) }, new SliceSettingData() { @@ -1644,7 +1644,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration HelpText = "G-Code to be run immediately following the temperature setting commands. Including commands to set temperature in this section will cause them not be generated outside of this section. Will accept Custom G-Code variables.".Localize(), DataEditType = DataEditTypes.MULTI_LINE_TEXT, DefaultValue = "G28 ; home all axes\\nG1 Z5 F5000 ; lift nozzle", - Resolver = new MapStartGCode(true), + Resolver = new GCodeMapping(), }, new SliceSettingData() { @@ -2264,7 +2264,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration DataEditType = DataEditTypes.POSITIVE_DOUBLE, Units = "%".Localize(), DefaultValue = "100", - Resolver = new ScaledSingleNumber(.01), + Resolver = new AsPercentOrDirect(), }, new SliceSettingData() { diff --git a/MatterControlLib/SlicerConfiguration/EngineMappingsMatterSlice.cs b/MatterControlLib/SlicerConfiguration/EngineMappingsMatterSlice.cs index 2702051d0..d1ad6ff03 100644 --- a/MatterControlLib/SlicerConfiguration/EngineMappingsMatterSlice.cs +++ b/MatterControlLib/SlicerConfiguration/EngineMappingsMatterSlice.cs @@ -31,6 +31,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; +using MatterHackers.Agg; namespace MatterHackers.MatterControl.SlicerConfiguration { @@ -81,7 +83,17 @@ namespace MatterHackers.MatterControl.SlicerConfiguration [SettingsKey.max_acceleration] = new ExportField("maxAcceleration"), [SettingsKey.max_velocity] = new ExportField("maxVelocity"), [SettingsKey.jerk_velocity] = new ExportField("jerkVelocity"), - [SettingsKey.print_time_estimate_multiplier] = new ExportField("printTimeEstimateMultiplier"), + [SettingsKey.print_time_estimate_multiplier] = new ExportField( + "printTimeEstimateMultiplier", + (value, settings) => + { + if (double.TryParse(value, out double infillRatio)) + { + return $"{infillRatio * .01}"; + } + + return "0"; + }), // fan settings [SettingsKey.min_fan_speed] = new ExportField("fanSpeedMinPercent"), [SettingsKey.coast_at_end_distance] = new ExportField("coastAtEndDistance"), @@ -119,10 +131,35 @@ namespace MatterHackers.MatterControl.SlicerConfiguration [SettingsKey.merge_overlapping_lines] = new ExportField("MergeOverlappingLines"), [SettingsKey.fill_thin_gaps] = new ExportField("fillThinGaps"), [SettingsKey.spiral_vase] = new ExportField("continuousSpiralOuterPerimeter"), - [SettingsKey.start_gcode] = new ExportField("startCode"), + [SettingsKey.start_gcode] = new ExportField( + "startCode", + (value, settings) => + { + return StartGCodeGenerator.BuildStartGCode(settings, value); + }), [SettingsKey.layer_gcode] = new ExportField("layerChangeCode"), - [SettingsKey.fill_density] = new ExportField("infillPercent"), - [SettingsKey.perimeter_start_end_overlap] = new ExportField("perimeterStartEndOverlapRatio"), + [SettingsKey.fill_density] = new ExportField( + "infillPercent", + (value, settings) => + { + if (double.TryParse(value, out double infillRatio)) + { + return $"{infillRatio * 100}"; + } + + return "0"; + }), + [SettingsKey.perimeter_start_end_overlap] = new ExportField( + "perimeterStartEndOverlapRatio", + (value, settings) => + { + if (double.TryParse(value, out double infillRatio)) + { + return $"{infillRatio * .01}"; + } + + return "0"; + }), [SettingsKey.raft_extruder] = new ExportField("raftExtruder"), [SettingsKey.support_material_extruder] = new ExportField("supportExtruder"), [SettingsKey.support_material_interface_extruder] = new ExportField("supportInterfaceExtruder"), @@ -146,9 +183,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration foreach (var (key, exportField) in this.Exports.Select(kvp => (kvp.Key, kvp.Value))) { string result = settings.ResolveValue(key); + + // Run custom converter if defined on the field + if (exportField.Converter != null) + { + result = exportField.Converter(result, settings); + } + if (result != null) { - sliceSettingsFile.WriteLine("{0} = {1}", key, result); + sliceSettingsFile.WriteLine("{0} = {1}", exportField.OuputName, result); } } @@ -164,5 +208,184 @@ namespace MatterHackers.MatterControl.SlicerConfiguration return matterSliceSettingNames.Contains(canonicalSettingsName) || PrinterSettings.ApplicationLevelSettings.Contains(canonicalSettingsName); } + + public static class StartGCodeGenerator + { + public static string BuildStartGCode(PrinterSettings settings, string userGCode) + { + var newStartGCode = new StringBuilder(); + + foreach (string line in PreStartGCode(settings, Slicer.ExtrudersUsed)) + { + newStartGCode.Append(line + "\n"); + } + + newStartGCode.Append(userGCode); + + foreach (string line in PostStartGCode(settings, Slicer.ExtrudersUsed)) + { + newStartGCode.Append("\n"); + newStartGCode.Append(line); + } + + var result = newStartGCode.ToString(); + return result.Replace("\n", "\\n"); + } + + private static List PreStartGCode(PrinterSettings settings, List extrudersUsed) + { + string startGCode = settings.GetValue(SettingsKey.start_gcode); + string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries); + + var preStartGCode = new List + { + "; automatic settings before start_gcode" + }; + AddDefaultIfNotPresent(preStartGCode, "G21", startGCodeLines, "set units to millimeters"); + AddDefaultIfNotPresent(preStartGCode, "M107", startGCodeLines, "fan off"); + double bed_temperature = settings.GetValue(SettingsKey.bed_temperature); + if (bed_temperature > 0) + { + string setBedTempString = string.Format("M140 S{0}", bed_temperature); + AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "start heating the bed"); + } + + int numberOfHeatedExtruders = settings.Helpers.HotendCount(); + + // Start heating all the extruder that we are going to use. + for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) + { + if (extrudersUsed.Count > hotendIndex + && extrudersUsed[hotendIndex]) + { + double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); + if (materialTemperature != 0) + { + string setTempString = "M104 T{0} S{1}".FormatWith(hotendIndex, materialTemperature); + AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"start heating T{hotendIndex}"); + } + } + } + + // If we need to wait for the heaters to heat up before homing then set them to M109 (heat and wait). + if (settings.GetValue(SettingsKey.heat_extruder_before_homing)) + { + for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) + { + if (extrudersUsed.Count > hotendIndex + && extrudersUsed[hotendIndex]) + { + double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); + if (materialTemperature != 0) + { + string setTempString = "M109 T{0} S{1}".FormatWith(hotendIndex, materialTemperature); + AddDefaultIfNotPresent(preStartGCode, setTempString, startGCodeLines, $"wait for T{hotendIndex }"); + } + } + } + } + + // If we have bed temp and the start gcode specifies to finish heating the extruders, + // make sure we also finish heating the bed. This preserves legacy expectation. + if (bed_temperature > 0 + && startGCode.Contains("M109")) + { + string setBedTempString = string.Format("M190 S{0}", bed_temperature); + AddDefaultIfNotPresent(preStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached"); + } + + SwitchToFirstActiveExtruder(extrudersUsed, preStartGCode); + preStartGCode.Add("; settings from start_gcode"); + + return preStartGCode; + } + + private static List PostStartGCode(PrinterSettings settings, List extrudersUsed) + { + string startGCode = settings.GetValue(SettingsKey.start_gcode); + string[] startGCodeLines = startGCode.Split(new string[] { "\\n" }, StringSplitOptions.RemoveEmptyEntries); + + var postStartGCode = new List + { + "; automatic settings after start_gcode" + }; + + double bed_temperature = settings.GetValue(SettingsKey.bed_temperature); + if (bed_temperature > 0 + && !startGCode.Contains("M109")) + { + string setBedTempString = string.Format("M190 S{0}", bed_temperature); + AddDefaultIfNotPresent(postStartGCode, setBedTempString, startGCodeLines, "wait for bed temperature to be reached"); + } + + int numberOfHeatedExtruders = settings.GetValue(SettingsKey.extruder_count); + // wait for them to finish + for (int hotendIndex = 0; hotendIndex < numberOfHeatedExtruders; hotendIndex++) + { + if (hotendIndex < extrudersUsed.Count + && extrudersUsed[hotendIndex]) + { + double materialTemperature = settings.Helpers.ExtruderTargetTemperature(hotendIndex); + if (materialTemperature != 0) + { + if (!(hotendIndex == 0 && LineStartsWith(startGCodeLines, "M109 S")) + && !LineStartsWith(startGCodeLines, $"M109 T{hotendIndex} S")) + { + // always heat the extruders that are used beyond extruder 0 + postStartGCode.Add($"M109 T{hotendIndex} S{materialTemperature} ; Finish heating T{hotendIndex}"); + } + } + } + } + + SwitchToFirstActiveExtruder(extrudersUsed, postStartGCode); + AddDefaultIfNotPresent(postStartGCode, "G90", startGCodeLines, "use absolute coordinates"); + postStartGCode.Add(string.Format("{0} ; {1}", "G92 E0", "reset the expected extruder position")); + AddDefaultIfNotPresent(postStartGCode, "M82", startGCodeLines, "use absolute distance for extrusion"); + + return postStartGCode; + } + + private static void AddDefaultIfNotPresent(List linesAdded, string commandToAdd, string[] lines, string comment) + { + string command = commandToAdd.Split(' ')[0].Trim(); + + if (!LineStartsWith(lines, command)) + { + linesAdded.Add(string.Format("{0} ; {1}", commandToAdd, comment)); + } + } + + private static void SwitchToFirstActiveExtruder(List extrudersUsed, List preStartGCode) + { + // make sure we are on the first active extruder + for (int extruderIndex = 0; extruderIndex < extrudersUsed.Count; extruderIndex++) + { + if (extrudersUsed[extruderIndex]) + { + // set the active extruder to the first one that will be printing + preStartGCode.Add("T{0} ; {1}".FormatWith(extruderIndex, "set the active extruder to {0}".FormatWith(extruderIndex))); + // we have set the active extruder so don't set it to any other extruder + break; + } + } + } + + private static bool LineStartsWith(string[] lines, string command) + { + foreach (string line in lines) + { + if (line.StartsWith(command)) + { + return true; + } + } + + return false; + } + } + + + } } \ No newline at end of file diff --git a/MatterControlLib/SlicerConfiguration/ExportField.cs b/MatterControlLib/SlicerConfiguration/ExportField.cs index a82b2551a..5af232d96 100644 --- a/MatterControlLib/SlicerConfiguration/ExportField.cs +++ b/MatterControlLib/SlicerConfiguration/ExportField.cs @@ -28,15 +28,12 @@ either expressed or implied, of the FreeBSD Project. */ using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; namespace MatterHackers.MatterControl.SlicerConfiguration { public class ExportField { - public ExportField(string outputName, Func converter = null) + public ExportField(string outputName, Func converter = null) { this.OuputName = outputName; this.Converter = converter; @@ -44,6 +41,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration public string OuputName { get; } - public Func Converter { get; } + public Func Converter { get; } } } \ No newline at end of file