fixing component and sheet interactions
This commit is contained in:
parent
f88fb03979
commit
0292ccd76f
3 changed files with 72 additions and 61 deletions
|
|
@ -59,38 +59,18 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
{
|
||||
lock (locker)
|
||||
{
|
||||
if (!tabelCalculated)
|
||||
{
|
||||
BuildTableConstants();
|
||||
}
|
||||
|
||||
var cell = this[cellId];
|
||||
|
||||
if (cell != null)
|
||||
{
|
||||
var expression = cell.Expression;
|
||||
{
|
||||
return GetCellValue(cell);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return "0";
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
|
||||
public string EvaluateExpression(string expression)
|
||||
public string GetCellValue(TableCell cell)
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
|
|
@ -99,11 +79,42 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
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();
|
||||
|
||||
// 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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
var value = evaluator.calculate();
|
||||
|
||||
|
|
|
|||
|
|
@ -507,26 +507,20 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
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;
|
||||
}
|
||||
if (typeof(T) == typeof(int))
|
||||
{
|
||||
return (T)(object)(int)Math.Round(result);
|
||||
}
|
||||
return (T)(object)result;
|
||||
}
|
||||
if (typeof(T) == typeof(int))
|
||||
{
|
||||
return (T)(object)(int)Math.Round(result);
|
||||
}
|
||||
|
||||
if (typeof(T) == typeof(double))
|
||||
{
|
||||
return (T)(object)0;
|
||||
}
|
||||
|
||||
return (T)(object)(int)0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the sheet that the given item will reference
|
||||
|
|
|
|||
|
|
@ -595,23 +595,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
if (cell != null)
|
||||
{
|
||||
// create an expresion editor
|
||||
var field = new ExpressionField(theme)
|
||||
var field = new TextField(theme)
|
||||
{
|
||||
Name = cellId + " Field"
|
||||
Name = cellId + " Field",
|
||||
};
|
||||
field.Initialize(0);
|
||||
if (cellData.Contains("="))
|
||||
{
|
||||
field.SetValue(cellData, false);
|
||||
}
|
||||
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();
|
||||
field.SetValue(cellData, false);
|
||||
field.ClearUndoHistory();
|
||||
field.Content.HAnchor = HAnchor.Stretch;
|
||||
|
||||
var doOrUndoing = false;
|
||||
field.ValueChanged += (s, e) =>
|
||||
|
|
@ -624,7 +615,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
{
|
||||
doOrUndoing = true;
|
||||
editorList[editorIndex] = "!" + cellId + "," + oldValue;
|
||||
var expression = new DoubleOrExpression(oldValue);
|
||||
var expression = new StringOrExpression(oldValue);
|
||||
cell.Expression = expression.Value(componentObject).ToString();
|
||||
componentObject.Invalidate(InvalidateType.SheetUpdated);
|
||||
doOrUndoing = false;
|
||||
|
|
@ -633,7 +624,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow
|
|||
{
|
||||
doOrUndoing = true;
|
||||
editorList[editorIndex] = "!" + cellId + "," + newValue;
|
||||
var expression = new DoubleOrExpression(newValue);
|
||||
var expression = new StringOrExpression(newValue);
|
||||
cell.Expression = expression.Value(componentObject).ToString();
|
||||
componentObject.Invalidate(InvalidateType.SheetUpdated);
|
||||
doOrUndoing = false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue