From a962527a6f5da1ff3220968f61bc7587be262cd5 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Mon, 31 May 2021 18:44:00 -0700 Subject: [PATCH] Refactoring --- .../DesignTools/Operations/ScaleObject3D_3.cs | 6 +- .../DesignTools/Primitives/CubeObject3D.cs | 6 +- .../Primitives/CylinderObject3D.cs | 10 +-- ...ntOrReference.cs => DoubleOrExpression.cs} | 26 ++++---- .../Primitives/HalfCylinderObject3D.cs | 4 +- .../Primitives/HalfWedgeObject3D.cs | 6 +- .../DesignTools/Primitives/PyramidObject3D.cs | 6 +- .../DesignTools/Primitives/SheetData.cs | 66 ++++++++++++++++--- .../DesignTools/Primitives/SheetObject3D.cs | 16 ++--- .../DesignTools/Primitives/WedgeObject3D.cs | 6 +- .../DesignTools/PublicPropertyEditor.cs | 14 ++-- MatterControlLib/MatterControlLib.csproj | 1 + .../View3D/Actions/SheetEditor.cs | 7 +- 13 files changed, 111 insertions(+), 63 deletions(-) rename MatterControlLib/DesignTools/Primitives/{DoubleConstantOrReference.cs => DoubleOrExpression.cs} (73%) diff --git a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs index 5255ea102..f19e1735e 100644 --- a/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs +++ b/MatterControlLib/DesignTools/Operations/ScaleObject3D_3.cs @@ -133,7 +133,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations [MaxDecimalPlaces(3)] [JsonIgnore] - public DoubleConstantOrReference Width + public DoubleOrExpression Width { get { @@ -157,7 +157,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations [MaxDecimalPlaces(3)] [JsonIgnore] - public DoubleConstantOrReference Depth + public DoubleOrExpression Depth { get { @@ -181,7 +181,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations [MaxDecimalPlaces(3)] [JsonIgnore] - public DoubleConstantOrReference Height + public DoubleOrExpression Height { get { diff --git a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs index d5e77f11e..a003b3afc 100644 --- a/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CubeObject3D.cs @@ -50,13 +50,13 @@ namespace MatterHackers.MatterControl.DesignTools /// This is the actual serialized with that can use expressions /// [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Width { get; set; } = 20; + public DoubleOrExpression Width { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Depth { get; set; } = 20; + public DoubleOrExpression Depth { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Height { get; set; } = 20; + public DoubleOrExpression Height { get; set; } = 20; public static async Task Create() { diff --git a/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs b/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs index a03ef3c27..4cfad7378 100644 --- a/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/CylinderObject3D.cs @@ -120,10 +120,10 @@ namespace MatterHackers.MatterControl.DesignTools [MaxDecimalPlaces(2)] [Description("The width from one side to the opposite side.")] - public DoubleConstantOrReference Diameter { get; set; } = 20; + public DoubleOrExpression Diameter { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Height { get; set; } = 20; + public DoubleOrExpression Height { get; set; } = 20; [Description("The number of segments around the perimeter.")] public int Sides { get; set; } = 40; @@ -135,13 +135,13 @@ namespace MatterHackers.MatterControl.DesignTools public string EasyModeMessage { get; set; } = "You can switch to Advanced mode to get more cylinder options."; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference StartingAngle { get; set; } = 0; + public DoubleOrExpression StartingAngle { get; set; } = 0; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference EndingAngle { get; set; } = 360; + public DoubleOrExpression EndingAngle { get; set; } = 360; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference DiameterTop { get; set; } = 20; + public DoubleOrExpression DiameterTop { get; set; } = 20; public override async void OnInvalidate(InvalidateArgs invalidateType) { diff --git a/MatterControlLib/DesignTools/Primitives/DoubleConstantOrReference.cs b/MatterControlLib/DesignTools/Primitives/DoubleOrExpression.cs similarity index 73% rename from MatterControlLib/DesignTools/Primitives/DoubleConstantOrReference.cs rename to MatterControlLib/DesignTools/Primitives/DoubleOrExpression.cs index f9fe3a8d0..0cd3609f9 100644 --- a/MatterControlLib/DesignTools/Primitives/DoubleConstantOrReference.cs +++ b/MatterControlLib/DesignTools/Primitives/DoubleOrExpression.cs @@ -32,39 +32,39 @@ using MatterHackers.DataConverters3D; namespace MatterHackers.MatterControl.DesignTools { - [TypeConverter(typeof(DoubleConstantOrReference))] - public class DoubleConstantOrReference + [TypeConverter(typeof(DoubleOrExpression))] + public class DoubleOrExpression { - public string CellId { get; set; } + public string Expression { get; set; } public double Value(IObject3D owner) { - if (double.TryParse(CellId, out double result)) + if (double.TryParse(Expression, out double result)) { return result; } - return SheetObject3D.FindTableAndValue(owner, CellId); + return SheetObject3D.EvaluateExpression(owner, Expression); } - public DoubleConstantOrReference(double value) + public DoubleOrExpression(double value) { - CellId = value.ToString(); + Expression = value.ToString(); } - public DoubleConstantOrReference(string value) + public DoubleOrExpression(string expression) { - CellId = value; + Expression = expression; } - public static implicit operator DoubleConstantOrReference(double value) + public static implicit operator DoubleOrExpression(double value) { - return new DoubleConstantOrReference(value); + return new DoubleOrExpression(value); } - public static implicit operator DoubleConstantOrReference(string value) + public static implicit operator DoubleOrExpression(string expression) { - return new DoubleConstantOrReference(value); + return new DoubleOrExpression(expression); } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs index 672ddd490..8b96296cc 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfCylinderObject3D.cs @@ -57,10 +57,10 @@ namespace MatterHackers.MatterControl.DesignTools } [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Width { get; set; } = 20; + public DoubleOrExpression Width { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Depth { get; set; } = 20; + public DoubleOrExpression Depth { get; set; } = 20; [MaxDecimalPlaces(2)] public int Sides { get; set; } = 20; diff --git a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs index 6d22ed7c6..420cf9bbe 100644 --- a/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/HalfWedgeObject3D.cs @@ -56,13 +56,13 @@ namespace MatterHackers.MatterControl.DesignTools } [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Width { get; set; } = 20; + public DoubleOrExpression Width { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Depth { get; set; } = 20; + public DoubleOrExpression Depth { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Height { get; set; } = 10; + public DoubleOrExpression Height { get; set; } = 10; public override async void OnInvalidate(InvalidateArgs invalidateType) { diff --git a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs index e3eefde44..2e6a4b928 100644 --- a/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/PyramidObject3D.cs @@ -57,13 +57,13 @@ namespace MatterHackers.MatterControl.DesignTools } [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Width { get; set; } = 20; + public DoubleOrExpression Width { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Depth { get; set; } = 20; + public DoubleOrExpression Depth { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Height { get; set; } = 20; + public DoubleOrExpression Height { get; set; } = 20; public override async void OnInvalidate(InvalidateArgs invalidateType) { diff --git a/MatterControlLib/DesignTools/Primitives/SheetData.cs b/MatterControlLib/DesignTools/Primitives/SheetData.cs index 3b9394f05..11947d775 100644 --- a/MatterControlLib/DesignTools/Primitives/SheetData.cs +++ b/MatterControlLib/DesignTools/Primitives/SheetData.cs @@ -28,7 +28,7 @@ either expressed or implied, of the FreeBSD Project. */ using System.Collections.Generic; -using System.Linq; +using org.mariuszgromada.math.mxparser; using Newtonsoft.Json; namespace MatterHackers.MatterControl.DesignTools @@ -48,18 +48,35 @@ namespace MatterHackers.MatterControl.DesignTools } } + public string EvaluateExpression(string expression) + { + if (!tabelCalculated) + { + Recalculate(); + } + + var evaluator = new Expression(expression); + AddConstants(evaluator); + var value = evaluator.calculate(); + + return value.ToString(); + } + + public string CellId(int x, int y) + { + return $"{(char)('A' + x)}{y + 1}"; + } + public TableCell this[int x, int y] { get { - var cellId = $"{(char)('A' + x)}{y + 1}"; - return this[cellId]; + return this[CellId(x, y)]; } set { - var cellId = $"{(char)('A' + x)}{y + 1}"; - this[cellId] = value; + this[CellId(x, y)] = value; } } @@ -148,11 +165,6 @@ namespace MatterHackers.MatterControl.DesignTools /// public string Expression = ""; - /// - /// The results of parsing the Expression - /// - public string Value => Expression; - /// /// How to format this data in the display /// @@ -178,5 +190,39 @@ namespace MatterHackers.MatterControl.DesignTools } public List Rows = new List(); + + Dictionary constants = new Dictionary(); + private bool tabelCalculated; + + public void Recalculate() + { + constants.Clear(); + // WIP: sort the cell by reference (needs to be DAG) + for (int y = 0; y < Rows.Count; y++) + { + for (int x = 0; x < Rows[y].Cells.Count; x++) + { + var cell = this[x, y]; + var evaluator = new Expression(cell.Expression); + AddConstants(evaluator); + var value = evaluator.calculate(); + constants.Add(CellId(x, y), value); + if (!string.IsNullOrEmpty(cell.Name)) + { + constants.Add(cell.Name, value); + } + } + } + + tabelCalculated = true; + } + + private void AddConstants(Expression evaluator) + { + foreach(var kvp in constants) + { + evaluator.defineConstant(kvp.Key, kvp.Value); + } + } } } \ No newline at end of file diff --git a/MatterControlLib/DesignTools/Primitives/SheetObject3D.cs b/MatterControlLib/DesignTools/Primitives/SheetObject3D.cs index b8b0af47a..2de78d951 100644 --- a/MatterControlLib/DesignTools/Primitives/SheetObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/SheetObject3D.cs @@ -28,7 +28,6 @@ either expressed or implied, of the FreeBSD Project. */ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; @@ -71,13 +70,15 @@ namespace MatterHackers.MatterControl.DesignTools public static async Task Create() { - var item = new SheetObject3D(); - item.SheetData = new SheetData(5, 5); + var item = new SheetObject3D + { + SheetData = new SheetData(5, 5) + }; await item.Rebuild(); return item; } - public override async void OnInvalidate(InvalidateArgs invalidateType) + public override void OnInvalidate(InvalidateArgs invalidateType) { if (invalidateType.InvalidateType.HasFlag(InvalidateType.SheetUpdated) && invalidateType.Source == this) { @@ -113,7 +114,7 @@ namespace MatterHackers.MatterControl.DesignTools item.Invalidate(new InvalidateArgs(item, InvalidateType.SheetUpdated)); } - public static T FindTableAndValue(IObject3D owner, string cellId) + public static T EvaluateExpression(IObject3D owner, string inputExpression) { // look through all the parents foreach (var parent in owner.Parents()) @@ -121,17 +122,16 @@ namespace MatterHackers.MatterControl.DesignTools // then each child of any give parent foreach (var sibling in parent.Children) { - var expression = ""; // if it is a sheet if (sibling != owner && sibling is SheetObject3D sheet) { // try to manage the cell into the correct data type - expression = sheet.SheetData[cellId]?.Expression; + string value = sheet.SheetData.EvaluateExpression(inputExpression); if (typeof(T) == typeof(double)) { - if (double.TryParse(expression, out double doubleValue)) + if (double.TryParse(value, out double doubleValue)) { return (T)(object)doubleValue; } diff --git a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs index 7842fd567..70d6ec2af 100644 --- a/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/WedgeObject3D.cs @@ -59,13 +59,13 @@ namespace MatterHackers.MatterControl.DesignTools } [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Width { get; set; } = 20; + public DoubleOrExpression Width { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Depth { get; set; } = 20; + public DoubleOrExpression Depth { get; set; } = 20; [MaxDecimalPlaces(2)] - public DoubleConstantOrReference Height { get; set; } = 20; + public DoubleOrExpression Height { get; set; } = 20; public bool Round { get; set; } = false; diff --git a/MatterControlLib/DesignTools/PublicPropertyEditor.cs b/MatterControlLib/DesignTools/PublicPropertyEditor.cs index 049b267ce..207c373bb 100644 --- a/MatterControlLib/DesignTools/PublicPropertyEditor.cs +++ b/MatterControlLib/DesignTools/PublicPropertyEditor.cs @@ -60,7 +60,7 @@ namespace MatterHackers.MatterControl.DesignTools private static readonly Type[] AllowedTypes = { typeof(double), typeof(int), typeof(char), typeof(string), typeof(bool), - typeof(DoubleConstantOrReference), + typeof(DoubleOrExpression), typeof(Color), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(DirectionVector), typeof(DirectionAxis), @@ -668,19 +668,19 @@ namespace MatterHackers.MatterControl.DesignTools (value) => { return ((bool)value) ? "1" : "0"; }); rowContainer = CreateSettingsRow(property, field, theme); } - else if (propertyValue is DoubleConstantOrReference doubleExpresion) + else if (propertyValue is DoubleOrExpression doubleExpresion) { // create a string editor var field = new TextField(theme); field.Initialize(0); - field.SetValue(doubleExpresion.CellId, false); + field.SetValue(doubleExpresion.Expression, false); field.ClearUndoHistory(); field.Content.HAnchor = HAnchor.Stretch; RegisterValueChanged(field, - (valueString) => new DoubleConstantOrReference(valueString), + (valueString) => new DoubleOrExpression(valueString), (value) => { - return ((DoubleConstantOrReference)value).CellId; + return ((DoubleOrExpression)value).Expression; }); rowContainer = CreateSettingsRow(property, field, theme); @@ -694,8 +694,8 @@ namespace MatterHackers.MatterControl.DesignTools { if (e.InvalidateType.HasFlag(InvalidateType.DisplayValues)) { - DoubleConstantOrReference newValue = (DoubleConstantOrReference)property.Value; - if (newValue.CellId != field.Value) + DoubleOrExpression newValue = (DoubleOrExpression)property.Value; + if (newValue.Expression != field.Value) { var format = "0." + new string('#', 5); if (property.PropertyInfo.GetCustomAttributes(true).OfType().FirstOrDefault() is MaxDecimalPlacesAttribute decimalPlaces) diff --git a/MatterControlLib/MatterControlLib.csproj b/MatterControlLib/MatterControlLib.csproj index 49c2e9eee..b128e1abc 100644 --- a/MatterControlLib/MatterControlLib.csproj +++ b/MatterControlLib/MatterControlLib.csproj @@ -92,6 +92,7 @@ + diff --git a/MatterControlLib/PartPreviewWindow/View3D/Actions/SheetEditor.cs b/MatterControlLib/PartPreviewWindow/View3D/Actions/SheetEditor.cs index e1abd090f..e797d4a98 100644 --- a/MatterControlLib/PartPreviewWindow/View3D/Actions/SheetEditor.cs +++ b/MatterControlLib/PartPreviewWindow/View3D/Actions/SheetEditor.cs @@ -95,8 +95,7 @@ namespace MatterHackers.MatterControl.DesignTools { topRow.AddChild(new TextWidget(((char)('A' + x)).ToString()) { - HAnchor = HAnchor.Absolute, - Width = cellWidth, + HAnchor = HAnchor.Stretch, TextColor = theme.TextColor }); } @@ -121,8 +120,9 @@ namespace MatterHackers.MatterControl.DesignTools var capturedX = x; var capturedY = y; - var edit = new MHTextEditWidget(sheetData[x, y].Expression, theme, cellWidth) + var edit = new MHTextEditWidget(sheetData[x, y].Expression, theme) { + HAnchor = HAnchor.Stretch, SelectAllOnFocus = true, }; @@ -183,6 +183,7 @@ namespace MatterHackers.MatterControl.DesignTools private void Recalculate() { + sheetData.Recalculate(); sheetObject.Invalidate(InvalidateType.SheetUpdated); } }