I hate juggling with lists and trees in Grasshopper
Most times I think code is written faster and is easier to understand.
Here is a challenge a student came up with:
it seems super easy (at least to me) as code, but a vanilla grasshopper solution is already a lot of spaghetti…
do i miss something ? what ?
challenge
the original issue is more complex and handles optional geometry added to a polycurve for machining - , but Iet s look at a simplified text example - already with the solution:
I have a variable list of names. → As Example: Ada..Travis
as condition we look at the length of the word / names. if the Sum of both lengths is smaller or equal a variable threshold → as Example threshold = 9
we add 3 items inbetween.
the sum of both length is my condition, wether 3 additional items are generated and inserted.
code solution
doing this by code seams straight forward, collect the needed data piece by piece in a new list:
private void RunScript(
List<string> names,
int threshold,
ref object namesOptionalInfo)
{
// container to collect result data
List<string> strBack = new List<string>();
int lastIndex = names.Count -1;
for (int i = 0; i <= lastIndex; i++)
{
// always add the item
strBack.Add(names[i]);
// skip last item
if (i == lastIndex) break;
// process addtional items
int lengthThis = names[i].Length;
int lengthNext = names[i+1].Length;
int lengthSum = lengthThis + lengthNext;
// check if infoblock is needed
if (lengthSum <= threshold)
{
// create and add the infoblock only if its needed
strBack.Add($"before: {lengthThis}");
strBack.Add($"sum: {lengthSum}");
strBack.Add($"after: {lengthNext}");
}
}
// send back the data
namesOptionalInfo = strBack;
}
vanilla Grasshopper
i am interested in a nice “native components only” (=“vanilla”) grasshopper solution.
my solution feels so complicated and bulky - but i am looking forward to your comments:
get the text lengths
compose all Data - wether its needed or not (which is ugly if we talk about geometry)
generate a list of indices with negated data
replace negated data by null
compose (“weave”) the new list with all data including nulls
@martinsiegrist
thanks for digging in - but pick n choice will mainly make it easier to insert the null s - did i get this from your Screenshot ?
@Measure
looks like a smart solution with few components - not bad
but the last elements (after Travis) should not be added.
do you have a nice and short explanation - for me it looks like a lot of juggling, and even if there are fewer component count - not sure if it is readable for a third person.
@Jakob_v_Schirmeister
smart move to combine 3 texts with a separator and expand it later.
a pity this will not function for my overall issue: i am handling Curves / Geometry …
… i rescued Travis - see updated initial post
The Stack + Flip gives one branch per name where each branch has the three extra elements to (conditionally) follow that name.
The Divide Branches component does the filtering getting just the branches that fit the condition (they keep their original branch number, which is necessary for the following merge to work).
The Merge combines the (grafted) main list with the filtered additional branches (the elements from matching branch numbers are concatenated).
The final Shift flattens the resulting branches back to a single list.
Removing the final elements after ‘Travis’ can be done by setting Wrap=false on the Shift List. This might not work in general - not sure if there’s a better way than manually setting the last pattern item to false.
yes. but you can make several geometries one object by grouping -dont need for a seperator
and if you dont like grouping the items _calculate the indices to insert the items one by one listAddItem_dceuJVS_03.gh (35.3 KB)
the only downside - this approach will only work with text and numbers - not with curves (at least i think so). still the filter for the last one is missing.
Yeah I realized my approach was also missing something but I couldn’t spend more time on it. Something’s happening with Travis being added as last item.