Refactoring
This commit is contained in:
parent
abe5d17012
commit
a962527a6f
13 changed files with 111 additions and 63 deletions
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -50,13 +50,13 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
/// This is the actual serialized with that can use expressions
|
||||
/// </summary>
|
||||
[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<CubeObject3D> Create()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<double>(owner, CellId);
|
||||
return SheetObject3D.EvaluateExpression<double>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
|||
/// </summary>
|
||||
public string Expression = "";
|
||||
|
||||
/// <summary>
|
||||
/// The results of parsing the Expression
|
||||
/// </summary>
|
||||
public string Value => Expression;
|
||||
|
||||
/// <summary>
|
||||
/// How to format this data in the display
|
||||
/// </summary>
|
||||
|
|
@ -178,5 +190,39 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
}
|
||||
|
||||
public List<RowData> Rows = new List<RowData>();
|
||||
|
||||
Dictionary<string, double> constants = new Dictionary<string, double>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<SheetObject3D> 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<T>(IObject3D owner, string cellId)
|
||||
public static T EvaluateExpression<T>(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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<MaxDecimalPlacesAttribute>().FirstOrDefault() is MaxDecimalPlacesAttribute decimalPlaces)
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@
|
|||
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00005" />
|
||||
<PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00005" />
|
||||
<PackageReference Include="Markdig" Version="0.15.2" />
|
||||
<PackageReference Include="MathParser.org-mXparser" Version="4.4.2" />
|
||||
<PackageReference Include="MIConvexHull" Version="1.1.19.1019" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue