Get Branches from path´s values rule

Having a DataTree with GH_Paths: {0;0}, {0;1}, {1;0}, {1;1}
I want to get the branches data whose paths end with “0”: {0;0} and {1;0}

I tried converting the path values to string and I got the values. But as string values they are useless to use DataTree.Branch() method.

My try:
A = curvasBordes.Paths.Select(u => u.ToString()).Where(u => u.EndsWith(“0}”));

I tried using the string values obtained with Tree Branch component and it works, but I´m trying to do this inside my c# component

Should be simple but light is appreciated. Thank you

In GH I would do it with the split tree component, digging through the GH# Project I found a C# equivalent of this node.

Here’s the result:

Hope that helps you, cheers!

20230614_Split_Tree_C#_Response_01a.gh (9.4 KB)


 public DataTree<T> GetPaths<T>(DataTree<T> tree, int depth, int dimVal){
    DataTree<T> result = new DataTree<T>();

    List<GH_Path> paths = tree.Paths.Where(x => x.Indices[depth] == dimVal).ToList();
    if(!paths.Any()) Print("No match found");
    else{
      foreach(GH_Path path in paths) result.AddRange(tree.Branch(path), path);
    }
    return result;
  }

Where T is the Type in question

Obviously you can eleborate more on that if the tree is BIG: for instance first check if there’s indeed Paths with Depth that match depth etc etc. But in most of real-life cases LINQ has OK performance.

As a challenge add a search Interval (for the dimVal).

1 Like

I can see now how managing Paths´s properties is the way.
Thank you very much. I like LINQ for short and clear syntax. Interesting challenge BTW.

I´m using this variation to get count of branches within a depth 2 DataTree for a nested loop.
dataTree.Paths.Where(x => x.Length == 2).Select(x => x.Indices[1]).Distinct().Count();

Seems should be good idea working with classes…