From 088db8c30cecc7acceb2bc303fc3c920c5f46ee7 Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Tue, 22 Mar 2022 09:22:57 -0700 Subject: [PATCH] working on components using array indices --- .../DesignTools/Operations/ArrayObject3D.cs | 31 ++++++++++++++-- .../Primitives/ComponentObject3D.cs | 37 +++++++++++-------- .../DesignTools/Sheets/SheetObject3D.cs | 31 +++++++++++++++- 3 files changed, 79 insertions(+), 20 deletions(-) diff --git a/MatterControlLib/DesignTools/Operations/ArrayObject3D.cs b/MatterControlLib/DesignTools/Operations/ArrayObject3D.cs index a8a2408c6..fa658b0f3 100644 --- a/MatterControlLib/DesignTools/Operations/ArrayObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/ArrayObject3D.cs @@ -38,12 +38,37 @@ namespace MatterHackers.MatterControl.DesignTools.Operations 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 foreach (var item in this.Descendants((item) => !(item is ArrayObject3D))) { - foreach(var expression in SheetObject3D.GetActiveExpressions(item, "[index]", false)) - { - expression.Expression = expression.Expression.Replace("[index]", SheetObject3D.RetrieveArrayIndex(item, 0).ToString()); + foreach (var expansion in indexExpansions) + { + 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()); + } + } + } } } diff --git a/MatterControlLib/DesignTools/Primitives/ComponentObject3D.cs b/MatterControlLib/DesignTools/Primitives/ComponentObject3D.cs index 1b8b31dce..148041b66 100644 --- a/MatterControlLib/DesignTools/Primitives/ComponentObject3D.cs +++ b/MatterControlLib/DesignTools/Primitives/ComponentObject3D.cs @@ -135,27 +135,32 @@ namespace MatterHackers.MatterControl.DesignTools public (string cellId, string cellData) DecodeContent(int editorIndex) { - var cellData2 = SurfacedEditors[editorIndex].Substring(1); - var cellId2 = cellData2.ToLower(); - // check if it has embededdata - var separator = cellData2.IndexOf(','); - if (separator != -1) + if (SurfacedEditors[editorIndex].StartsWith("!")) { - cellId2 = cellData2.Substring(0, separator).ToLower(); - cellData2 = cellData2.Substring(separator + 1); - } - else - { - var firtSheet = this.Descendants().FirstOrDefault(); - if (firtSheet != null) + var cellData2 = SurfacedEditors[editorIndex].Substring(1); + var cellId2 = cellData2.ToLower(); + // check if it has embededdata + var separator = cellData2.IndexOf(','); + if (separator != -1) { - // 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(); + cellId2 = cellData2.Substring(0, separator).ToLower(); + cellData2 = cellData2.Substring(separator + 1); } + else + { + var firtSheet = this.Descendants().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); } diff --git a/MatterControlLib/DesignTools/Sheets/SheetObject3D.cs b/MatterControlLib/DesignTools/Sheets/SheetObject3D.cs index 3be8dd5a3..b4196e4a5 100644 --- a/MatterControlLib/DesignTools/Sheets/SheetObject3D.cs +++ b/MatterControlLib/DesignTools/Sheets/SheetObject3D.cs @@ -521,11 +521,40 @@ namespace MatterHackers.MatterControl.DesignTools } } + public static IEnumerable 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) { 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 return true;