Hi,
I am working to create a component to flip matrix for multilayer branch, this component could pick which level branch should be flip to item .i knew there is a path mapper but it should set manually. so i develop this to flip first branch by default, but something happened. this component that i made with c# scramble the data-tree structure of input and if i turn disable then enable again the output will be different.
the script:
// Grasshopper Script Instance
#region Usings
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
#endregion
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(DataTree<object> data, int swap_index, ref object A)
{
DataTree<object> result = new DataTree<object>();
// Iterate through every branch in the tree
// CHANGED: Use 'BranchCount' instead of 'PathCount'
for (int p = 0; p < data.BranchCount; p++)
{
GH_Path currentPath = data.Path(p);
System.Collections.IList branch = data.Branch(p);
// Safety check: Ensure the branch depth is sufficient for the swap_index
if (swap_index >= currentPath.Length)
{
Print("Error: Branch depth is shallower than the requested swap index.");
return;
}
// Iterate through every item in the branch (variable 'i')
for (int i = 0; i < branch.Count; i++)
{
object item = branch[i];
// Get the current path indices (e.g. {A; B; C})
int[] newIndices = currentPath.Indices;
// Replace the branch index 'A' with the item index 'i'
newIndices[swap_index] = i;
// Create the new path: {i; B; C; ...}
GH_Path newPath = new GH_Path(newIndices);
// Add data to the new tree
result.Add(item, newPath);
}
}
A = result;
}
}
here is the gh file:
C# Component Scrable Back the Datatree structure.gh (15.3 KB)

