working on components using array indices

This commit is contained in:
LarsBrubaker 2022-03-22 09:22:57 -07:00
parent 8244357095
commit 088db8c30c
3 changed files with 79 additions and 20 deletions

View file

@ -38,12 +38,37 @@ namespace MatterHackers.MatterControl.DesignTools.Operations
public override void Apply(Agg.UI.UndoBuffer undoBuffer) public override void Apply(Agg.UI.UndoBuffer undoBuffer)
{ {
var indexExpansions = new (string key, int index)[]
{
("[index]", 0),
("[index0]", 0),
("[index1]", 1),
("[index2]", 2),
};
// convert [index] expressions to their constant values // convert [index] expressions to their constant values
foreach (var item in this.Descendants((item) => !(item is ArrayObject3D))) foreach (var item in this.Descendants((item) => !(item is ArrayObject3D)))
{ {
foreach(var expression in SheetObject3D.GetActiveExpressions(item, "[index]", false)) foreach (var expansion in indexExpansions)
{ {
expression.Expression = expression.Expression.Replace("[index]", SheetObject3D.RetrieveArrayIndex(item, 0).ToString()); foreach (var expression in SheetObject3D.GetActiveExpressions(item, expansion.key, false))
{
expression.Expression = expression.Expression.Replace(expansion.key, SheetObject3D.RetrieveArrayIndex(item, expansion.index).ToString());
}
// Also convert index expressions in ComponentObjects to their constants
if (item is ComponentObject3D component)
{
for (int i = 0; i < component.SurfacedEditors.Count; i++)
{
var (cellId, cellData) = component.DecodeContent(i);
if (cellId != null)
{
cellData = cellData.Replace(expansion.key, SheetObject3D.RetrieveArrayIndex(component, expansion.index).ToString());
}
}
}
} }
} }

View file

@ -135,27 +135,32 @@ namespace MatterHackers.MatterControl.DesignTools
public (string cellId, string cellData) DecodeContent(int editorIndex) public (string cellId, string cellData) DecodeContent(int editorIndex)
{ {
var cellData2 = SurfacedEditors[editorIndex].Substring(1); if (SurfacedEditors[editorIndex].StartsWith("!"))
var cellId2 = cellData2.ToLower();
// check if it has embededdata
var separator = cellData2.IndexOf(',');
if (separator != -1)
{ {
cellId2 = cellData2.Substring(0, separator).ToLower(); var cellData2 = SurfacedEditors[editorIndex].Substring(1);
cellData2 = cellData2.Substring(separator + 1); var cellId2 = cellData2.ToLower();
} // check if it has embededdata
else var separator = cellData2.IndexOf(',');
{ if (separator != -1)
var firtSheet = this.Descendants<SheetObject3D>().FirstOrDefault();
if (firtSheet != null)
{ {
// We don't have any cache of the cell content, get the current content cellId2 = cellData2.Substring(0, separator).ToLower();
double.TryParse(firtSheet.SheetData.EvaluateExpression(cellId2), out double value); cellData2 = cellData2.Substring(separator + 1);
cellData2 = value.ToString();
} }
else
{
var firtSheet = this.Descendants<SheetObject3D>().FirstOrDefault();
if (firtSheet != null)
{
// We don't have any cache of the cell content, get the current content
double.TryParse(firtSheet.SheetData.EvaluateExpression(cellId2), out double value);
cellData2 = value.ToString();
}
}
return (cellId2, cellData2);
} }
return (cellId2, cellData2); return (null, null);
} }

View file

@ -521,11 +521,40 @@ namespace MatterHackers.MatterControl.DesignTools
} }
} }
public static IEnumerable<int> GetComponentExpressions(ComponentObject3D component, string checkForString, bool startsWith)
{
for (var i = 0; i < component.SurfacedEditors.Count; i++)
{
var (cellId, cellData) = component.DecodeContent(i);
if (cellId != null)
{
if (startsWith)
{
if (cellData.StartsWith(checkForString))
{
// WIP: check if the value has actually changed, this will update every object on any cell change
yield return i;
}
}
else
{
if (cellData.Contains(checkForString))
{
yield return i;
}
}
}
}
}
public static bool HasExpressionWithString(IObject3D itemToCheck, string checkForString, bool startsWith) public static bool HasExpressionWithString(IObject3D itemToCheck, string checkForString, bool startsWith)
{ {
foreach (var item in itemToCheck.DescendantsAndSelf()) foreach (var item in itemToCheck.DescendantsAndSelf())
{ {
if (GetActiveExpressions(item, checkForString, startsWith).Any()) if (GetActiveExpressions(item, checkForString, startsWith).Any()
|| (itemToCheck is ComponentObject3D component
&& GetComponentExpressions(component, checkForString, startsWith).Any()))
{ {
// three is one so return true // three is one so return true
return true; return true;