fixing component and sheet interactions

This commit is contained in:
LarsBrubaker 2022-11-26 15:28:01 -08:00
parent f88fb03979
commit 0292ccd76f
3 changed files with 72 additions and 61 deletions

View file

@ -59,38 +59,18 @@ namespace MatterHackers.MatterControl.DesignTools
{ {
lock (locker) lock (locker)
{ {
if (!tabelCalculated)
{
BuildTableConstants();
}
var cell = this[cellId]; var cell = this[cellId];
if (cell != null) if (cell != null)
{ {
var expression = cell.Expression; return GetCellValue(cell);
}
if (expression.StartsWith("=")) return "0";
{
expression = expression.Substring(1);
var evaluator = new Expression(expression.ToLower());
AddConstants(evaluator);
var value = evaluator.calculate();
return value.ToString();
}
else
{
// return the expression without evaluation
return expression;
}
}
return "0";
} }
} }
public string EvaluateExpression(string expression) public string GetCellValue(TableCell cell)
{ {
lock (locker) lock (locker)
{ {
@ -99,11 +79,42 @@ namespace MatterHackers.MatterControl.DesignTools
BuildTableConstants(); BuildTableConstants();
} }
if(expression.StartsWith("=")) var expression = cell.Expression;
if (expression.StartsWith("="))
{ {
expression = expression.Substring(1);
var evaluator = new Expression(expression.ToLower());
AddConstants(evaluator);
var value = evaluator.calculate();
return value.ToString();
}
else
{
// return the expression without evaluation
return expression;
}
}
}
public string EvaluateExpression(string inExpression)
{
lock (locker)
{
if (!tabelCalculated)
{
BuildTableConstants();
}
var expression = inExpression;
if (expression.StartsWith("="))
{
// to handle string values we first check if the expression is a cell reference
expression = expression.Substring(1).Trim(); expression = expression.Substring(1).Trim();
// if it is a direct cell reference than return that cells value // if it might be a direct cell reference check for column row data
if (expression.Length == 2) if (expression.Length == 2)
{ {
var column = (uint)expression.Substring(0, 1).ToUpper()[0] - 'A'; var column = (uint)expression.Substring(0, 1).ToUpper()[0] - 'A';
@ -113,9 +124,24 @@ namespace MatterHackers.MatterControl.DesignTools
return GetCellValue(CellId((int)column, (int)row)); return GetCellValue(CellId((int)column, (int)row));
} }
} }
}
var evaluator = new Expression(expression.ToLower()); // check if it is the exact name of a cell
foreach (var row in Rows)
{
foreach (var cell in row.Cells)
{
if (expression.Equals(cell.Name, StringComparison.OrdinalIgnoreCase))
{
return GetCellValue(cell);
}
}
}
// fall through to evaluate the expression
}
var evaluator = new Expression(expression.ToLower());
AddConstants(evaluator); AddConstants(evaluator);
var value = evaluator.calculate(); var value = evaluator.calculate();

View file

@ -507,26 +507,20 @@ namespace MatterHackers.MatterControl.DesignTools
return (T)(object)inputExpression; return (T)(object)inputExpression;
} }
if (double.TryParse(inputExpression, out var result)) double.TryParse(inputExpression, out var result);
if (typeof(T) == typeof(double))
{ {
if (typeof(T) == typeof(double)) return (T)(object)result;
{ }
return (T)(object)result; if (typeof(T) == typeof(int))
} {
if (typeof(T) == typeof(int)) return (T)(object)(int)Math.Round(result);
{
return (T)(object)(int)Math.Round(result);
}
} }
if (typeof(T) == typeof(double))
{
return (T)(object)0;
}
return (T)(object)(int)0; return (T)(object)(int)0;
} }
} }
/// <summary> /// <summary>
/// Find the sheet that the given item will reference /// Find the sheet that the given item will reference

View file

@ -595,23 +595,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
if (cell != null) if (cell != null)
{ {
// create an expresion editor // create an expresion editor
var field = new ExpressionField(theme) var field = new TextField(theme)
{ {
Name = cellId + " Field" Name = cellId + " Field",
}; };
field.Initialize(0); field.Initialize(0);
if (cellData.Contains("=")) field.SetValue(cellData, false);
{ field.ClearUndoHistory();
field.SetValue(cellData, false); field.Content.HAnchor = HAnchor.Stretch;
}
else // make sure it is formatted
{
double.TryParse(cellData, out double value);
var format = "0." + new string('#', 5);
field.SetValue(value.ToString(format), false);
}
field.ClearUndoHistory();
var doOrUndoing = false; var doOrUndoing = false;
field.ValueChanged += (s, e) => field.ValueChanged += (s, e) =>
@ -624,7 +615,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{ {
doOrUndoing = true; doOrUndoing = true;
editorList[editorIndex] = "!" + cellId + "," + oldValue; editorList[editorIndex] = "!" + cellId + "," + oldValue;
var expression = new DoubleOrExpression(oldValue); var expression = new StringOrExpression(oldValue);
cell.Expression = expression.Value(componentObject).ToString(); cell.Expression = expression.Value(componentObject).ToString();
componentObject.Invalidate(InvalidateType.SheetUpdated); componentObject.Invalidate(InvalidateType.SheetUpdated);
doOrUndoing = false; doOrUndoing = false;
@ -633,7 +624,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
{ {
doOrUndoing = true; doOrUndoing = true;
editorList[editorIndex] = "!" + cellId + "," + newValue; editorList[editorIndex] = "!" + cellId + "," + newValue;
var expression = new DoubleOrExpression(newValue); var expression = new StringOrExpression(newValue);
cell.Expression = expression.Value(componentObject).ToString(); cell.Expression = expression.Value(componentObject).ToString();
componentObject.Invalidate(InvalidateType.SheetUpdated); componentObject.Invalidate(InvalidateType.SheetUpdated);
doOrUndoing = false; doOrUndoing = false;