Improving variable parsing
This commit is contained in:
parent
43cb11cea5
commit
fe50c75fec
8 changed files with 108 additions and 71 deletions
|
|
@ -37,7 +37,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses
|
|||
value = value.Replace("\\n", "\n");
|
||||
|
||||
// Macro replace and restore escaped newlines
|
||||
return settings.ReplaceMacroValues(value.Replace("\n", "\\n"));
|
||||
return settings.ReplaceSettingsNamesWithValues(value.Replace("\n", "\\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +47,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration.MappingClasses
|
|||
value += "; LAYER:[layer_num]\n";
|
||||
}
|
||||
|
||||
return settings.ReplaceMacroValues(value.Replace("\n", "\\n"));
|
||||
return settings.ReplaceSettingsNamesWithValues(value.Replace("\n", "\\n"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MatterHackers.MatterControl.SlicerConfiguration
|
||||
{
|
||||
|
|
@ -236,8 +237,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
|
||||
private IObjectSlicer _slicer = null;
|
||||
|
||||
private HashSet<string> replacementTerms;
|
||||
|
||||
static PrinterSettings()
|
||||
{
|
||||
// Convert settings array into dictionary on initial load using settings key (SlicerConfigName)
|
||||
|
|
@ -252,43 +251,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
public PrinterSettings()
|
||||
{
|
||||
this.Helpers = new SettingsHelpers(this);
|
||||
|
||||
replacementTerms = new HashSet<string>()
|
||||
{
|
||||
SettingsKey.first_layer_speed,
|
||||
SettingsKey.external_perimeter_speed,
|
||||
SettingsKey.raft_print_speed,
|
||||
SettingsKey.bed_remove_part_temperature,
|
||||
SettingsKey.bridge_fan_speed,
|
||||
SettingsKey.bridge_speed,
|
||||
SettingsKey.air_gap_speed,
|
||||
SettingsKey.extruder_wipe_temperature,
|
||||
SettingsKey.filament_diameter,
|
||||
SettingsKey.first_layer_bed_temperature,
|
||||
SettingsKey.first_layer_temperature,
|
||||
SettingsKey.max_fan_speed,
|
||||
SettingsKey.min_fan_speed,
|
||||
SettingsKey.retract_length,
|
||||
SettingsKey.temperature,
|
||||
SettingsKey.bed_temperature,
|
||||
SettingsKey.temperature1,
|
||||
SettingsKey.temperature2,
|
||||
SettingsKey.temperature3,
|
||||
SettingsKey.infill_speed,
|
||||
SettingsKey.min_print_speed,
|
||||
SettingsKey.perimeter_speed,
|
||||
SettingsKey.perimeter_acceleration,
|
||||
SettingsKey.default_acceleration,
|
||||
SettingsKey.retract_speed,
|
||||
SettingsKey.support_material_speed,
|
||||
SettingsKey.travel_speed,
|
||||
SettingsKey.load_filament_speed,
|
||||
SettingsKey.trim_filament_markdown,
|
||||
SettingsKey.insert_filament_markdown2,
|
||||
SettingsKey.insert_filament_1_markdown,
|
||||
SettingsKey.running_clean_markdown2,
|
||||
SettingsKey.running_clean_1_markdown,
|
||||
};
|
||||
}
|
||||
|
||||
public static event EventHandler<StringEventArgs> AnyPrinterSettingChanged;
|
||||
|
|
@ -1042,37 +1004,57 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
return true;
|
||||
}
|
||||
|
||||
public string ReplaceMacroValues(string gcodeWithMacros)
|
||||
private static readonly Regex ConstantFinder = new Regex("(?<=\\[).+?(?=\\])", RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
private static readonly Regex SettingsFinder = new Regex("(?<=\\[).+?(?=\\])|(?<=\\{).+?(?=\\})", RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
|
||||
public string ReplaceSettingsNamesWithValues(string stringWithSettingsNames, bool includeCurlyBrackets = true)
|
||||
{
|
||||
foreach (string replacementTerm in replacementTerms)
|
||||
string Replace(string inputString, string setting)
|
||||
{
|
||||
// first check if this setting is anywhere in the line
|
||||
if (gcodeWithMacros.Contains(replacementTerm))
|
||||
var value = this.ResolveValue(setting);
|
||||
|
||||
if(string.IsNullOrEmpty(value))
|
||||
{
|
||||
// Acquire the replacement value
|
||||
string value = this.ResolveValue(replacementTerm);
|
||||
|
||||
if (ScaledSpeedFields.Contains(replacementTerm)
|
||||
&& double.TryParse(value, out double doubleValue))
|
||||
{
|
||||
doubleValue *= 60;
|
||||
value = $"{doubleValue:0.###}";
|
||||
}
|
||||
|
||||
// Use bed_temperature if the slice engine does not have first_layer_bed_temperature
|
||||
if (replacementTerm == SettingsKey.first_layer_bed_temperature
|
||||
&& !this.Slicer.Exports.ContainsKey(SettingsKey.first_layer_bed_temperature))
|
||||
{
|
||||
value = $"{this.GetValue<double>(SettingsKey.bed_temperature)}";
|
||||
}
|
||||
|
||||
// braces then brackets replacement
|
||||
gcodeWithMacros = gcodeWithMacros.Replace("{" + replacementTerm + "}", value);
|
||||
gcodeWithMacros = gcodeWithMacros.Replace("[" + replacementTerm + "]", value);
|
||||
return inputString;
|
||||
}
|
||||
|
||||
if (ScaledSpeedFields.Contains(setting)
|
||||
&& double.TryParse(value, out double doubleValue))
|
||||
{
|
||||
doubleValue *= 60;
|
||||
value = $"{doubleValue:0.###}";
|
||||
}
|
||||
|
||||
// Use bed_temperature if the slice engine does not have first_layer_bed_temperature
|
||||
if (setting == SettingsKey.first_layer_bed_temperature
|
||||
&& !this.Slicer.Exports.ContainsKey(SettingsKey.first_layer_bed_temperature))
|
||||
{
|
||||
value = $"{this.GetValue<double>(SettingsKey.bed_temperature)}";
|
||||
}
|
||||
|
||||
// braces then brackets replacement
|
||||
inputString = inputString.Replace("{" + setting + "}", value);
|
||||
inputString = inputString.Replace("[" + setting + "]", value);
|
||||
return inputString;
|
||||
}
|
||||
|
||||
return gcodeWithMacros;
|
||||
MatchCollection matches;
|
||||
if (includeCurlyBrackets)
|
||||
{
|
||||
matches = SettingsFinder.Matches(stringWithSettingsNames);
|
||||
}
|
||||
else
|
||||
{
|
||||
matches = ConstantFinder.Matches(stringWithSettingsNames);
|
||||
}
|
||||
|
||||
for (int i=0; i< matches.Count; i++)
|
||||
{
|
||||
var replacementTerm = matches[i].Value;
|
||||
stringWithSettingsNames = Replace(stringWithSettingsNames, replacementTerm);
|
||||
}
|
||||
|
||||
return stringWithSettingsNames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -32,12 +32,14 @@ using System.Collections.Generic;
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MatterHackers.Agg;
|
||||
using MatterHackers.Agg.Platform;
|
||||
using MatterHackers.Agg.UI;
|
||||
using MatterHackers.DataConverters3D;
|
||||
using MatterHackers.MatterControl.DesignTools.Operations;
|
||||
using MatterHackers.MatterControl.PartPreviewWindow;
|
||||
using MatterHackers.PolygonMesh;
|
||||
using MatterHackers.PolygonMesh.Processors;
|
||||
|
|
@ -224,8 +226,60 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
});
|
||||
}
|
||||
|
||||
private static readonly Regex ConstantFinder = new Regex("(?<=\\[).+?(?=\\])", RegexOptions.CultureInvariant | RegexOptions.Compiled);
|
||||
private static Random rand = new Random();
|
||||
|
||||
private static Dictionary<string, Func<double>> constants = new Dictionary<string, Func<double>>()
|
||||
{
|
||||
// length
|
||||
["cm"] = () => 10,
|
||||
["m"] = () => 1000,
|
||||
["inch"] = () => 25.4,
|
||||
["ft"] = () => 304.8,
|
||||
// math constant
|
||||
["pi"] = () => Math.PI,
|
||||
["tau"] = () => Math.PI * 2,
|
||||
["e"] = () => Math.E,
|
||||
// functions
|
||||
["rand"] = () => rand.NextDouble(),
|
||||
};
|
||||
|
||||
private static string ReplaceConstantsWithValues(string stringWithConstants)
|
||||
{
|
||||
string Replace(string inputString, string setting)
|
||||
{
|
||||
if (constants.ContainsKey(setting))
|
||||
{
|
||||
var value = constants[setting];
|
||||
|
||||
// braces then brackets replacement
|
||||
inputString = inputString.Replace("[" + setting + "]", value().ToString());
|
||||
}
|
||||
|
||||
return inputString;
|
||||
}
|
||||
|
||||
MatchCollection matches = ConstantFinder.Matches(stringWithConstants);
|
||||
|
||||
for (int i = 0; i < matches.Count; i++)
|
||||
{
|
||||
var replacementTerm = matches[i].Value;
|
||||
stringWithConstants = Replace(stringWithConstants, replacementTerm);
|
||||
}
|
||||
|
||||
return stringWithConstants;
|
||||
}
|
||||
|
||||
public static T EvaluateExpression<T>(IObject3D owner, string inputExpression)
|
||||
{
|
||||
var printer = owner.ContainingPrinter();
|
||||
if (printer != null)
|
||||
{
|
||||
inputExpression = printer.Settings.ReplaceSettingsNamesWithValues(inputExpression, false);
|
||||
}
|
||||
|
||||
inputExpression = ReplaceConstantsWithValues(inputExpression);
|
||||
|
||||
// check if the expression is not an equation (does not start with "=")
|
||||
if (inputExpression.Length > 0 && inputExpression[0] != '=')
|
||||
{
|
||||
|
|
@ -279,6 +333,7 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
{
|
||||
Debug.WriteLine(evaluator.getErrorMessage());
|
||||
}
|
||||
|
||||
return CastResult<T>(evaluator.calculate().ToString(), inputExpression);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -332,7 +332,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
|
||||
private void InjectPauseGCode(string codeToInject)
|
||||
{
|
||||
codeToInject = printer.Settings.ReplaceMacroValues(codeToInject);
|
||||
codeToInject = printer.Settings.ReplaceSettingsNamesWithValues(codeToInject);
|
||||
|
||||
codeToInject = codeToInject.Replace("\\n", "\n");
|
||||
string[] lines = codeToInject.Split('\n');
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
if (commandQueue.Count > 0)
|
||||
{
|
||||
lineToSend = commandQueue[0];
|
||||
lineToSend = printer.Settings.ReplaceMacroValues(lineToSend);
|
||||
lineToSend = printer.Settings.ReplaceSettingsNamesWithValues(lineToSend);
|
||||
commandQueue.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -435,7 +435,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
|
||||
if (afterGcodeToQueue.Trim().Length > 0)
|
||||
{
|
||||
gcode.Append(printer.Settings.ReplaceMacroValues(afterGcodeToQueue));
|
||||
gcode.Append(printer.Settings.ReplaceSettingsNamesWithValues(afterGcodeToQueue));
|
||||
}
|
||||
|
||||
// move to selected tool to the last tool position at the travel speed
|
||||
|
|
@ -507,7 +507,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication.Io
|
|||
var gcode = new StringBuilder();
|
||||
if (beforeGcodeToQueue.Trim().Length > 0)
|
||||
{
|
||||
gcode.Append(printer.Settings.ReplaceMacroValues(beforeGcodeToQueue));
|
||||
gcode.Append(printer.Settings.ReplaceSettingsNamesWithValues(beforeGcodeToQueue));
|
||||
}
|
||||
|
||||
gcode.Append("\n");
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ namespace MatterControl.Tests.MatterControl
|
|||
|
||||
void TestMacroReplacement(string inputText, string outputControl)
|
||||
{
|
||||
Assert.AreEqual(outputControl, settings.ReplaceMacroValues(inputText));
|
||||
Assert.AreEqual(outputControl, settings.ReplaceSettingsNamesWithValues(inputText));
|
||||
}
|
||||
|
||||
TestMacroReplacement("[temperature]", "200");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue