Convert Tree to List<List<int>>

Hello, Guys

tell me please, I have here is your data tree as in the picture below
I want to accept it in my node which I write for a grasshopper in C Sharp.

how to do it right?

and I also need these Trees in C Sharp to be represented List<List> in order to further process them there?

maybe someone has a piece of code how to do it right?

I am making node in visual studio environment

there in the node I submit that data tree as in the screenshot

I submit like this to the node

and somehow it needs to be converted into such a [List<List>

I’m not very good with C#, but I think something like this is what you’re looking for?

DataTree <int> valueTree = new DataTree<int>();

for (int i = 0; i < x.Count; i++)
{
  GH_Path myPath = new GH_Path(i);
  valueTree.Add(x[i], myPath);
  valueTree.Add(y[i], myPath);
  valueTree.Add(z[i], myPath);

}

A = valueTree;

Surely somebody else will find a more elegant way to do it :slight_smile:

1 Like

Thank you very much for the answer

excuse me, I should have asked the question more precisely, I’ll add

I am making node in visual studio environment

there in the node I submit that data tree as in the screenshot

I submit like this to the node

and somehow it needs to be converted into such a [List<List>

1 Like

Hello
I understand you work on Visual Studio and that maters as I can summarize like that
in Grasshopper with C# => use DataTree
in VisualStudio with C# => use GH_structure
Culebra has some very nice tools

Do to the opposite, it is quite trivial to change my example with a List<List<>>


        public static DataTree<int> GH_Structure_Int_to_DataTree(GH_Structure<GH_Integer> gh_int)
        {
            DataTree<int> dt = new DataTree<int>();
            IList<List<GH_Integer>> lst_lst_goo = gh_int.Branches;
            for (int i = 0; i < lst_lst_goo.Count; i++)
            {
                GH_Path path = new GH_Path(i);
                foreach (IGH_Goo goo in lst_lst_goo[i])
                {
                    int data;
                    if (goo.CastTo<int>(out data))
                    {
                        dt.Add(data, path);
                    }
                }
            }
            return dt;
        }
2 Likes

thank you very much for the answer
when I inserted this class into the project, I got the following error

Error CS0116

how to make a tree back from a List<List>, I know, but not the other way around

Hello
I gave you an example to adapt for tour project.
As it is a static method (not a class) you must add it in a static class for example in the Culebra static class GHSupport.
So add in your project the GHSupport.cs
Add the method I gave you, tweak it in order to have a list of list, something like this

public static class GHSupport
{
  public static List<List<<int>> GH_Structure_Int_to_ListList(GH_Structure<GH_Integer> gh_int)
        {
            List<List<int>> listlist = new  List<List<int>>();
            IList<List<GH_Integer>> lst_lst_goo = gh_int.Branches;
            for (int i = 0; i < lst_lst_goo.Count; i++)
            {
List<int> list = new List<int>();
                foreach (IGH_Goo goo in lst_lst_goo[i])
                {
                    int data;
                    if (goo.CastTo<int>(out data))
                    {
                        list .Add(data);
                    }
                }
listlist .Add(list);
            }
            return listlist;

You’ll find goo resource in Long Nguyen class