GH_Structure to Tree output from Visual Studio c#

    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        pManager.AddBooleanParameter("open", "open", "connect button to open file", GH_ParamAccess.item, false);
        pManager.AddTextParameter("separator", "separator", "sepatator character", GH_ParamAccess.item, "\t");
    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        pManager.AddTextParameter("file path", "file path","current file path", GH_ParamAccess.item);
        pManager.AddTextParameter("data", "data", "data", GH_ParamAccess.tree);
   protected override void SolveInstance(IGH_DataAccess DA)
        bool open = false;
        string separator = null;

        DA.GetData(0, ref open);
        DA.GetData(1, ref separator);

        //get file path
        if (open)
           System.Windows.Forms. OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Title = "Open CSV or TXT";
            openDialog.Filter = "TXT file|*.txt|CSV file|*.csv";
            openDialog.FilterIndex = 2;

            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                filePath = openDialog.FileName.ToString();
            else { }

            if (filePath != null)
                char sep = Convert.ToChar(separator);

                **//This is where I start to get confused. Since  DataTree is not allowed in VS, GH_Structure is used.**
                treeArray = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo>();

                //read file line by line
                string[] rows = System.IO.File.ReadAllLines(filePath);
                int arrayCount = rows[0].Split(sep).Length;

                foreach (string row in rows)
                    for (int i = 0; i < arrayCount; i++)
                        **//types used in GH_Structure have to implement IGH_Goo. I'm converting string to GH_String. but is this the right way to do it?**
                        Grasshopper.Kernel.Data.GH_Path ghpath = new Grasshopper.Kernel.Data.GH_Path(i);
                        string[] cells = row.Split(sep);
                        Grasshopper.Kernel.Types.GH_String cell = new Grasshopper.Kernel.Types.GH_String(cells[i]);
                        treeArray.Append(cell, ghpath);
                DA.SetData(1, treeArray);
            else { }
        DA.SetData(0, filePath);
    public string filePath = null;
    public Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo> treeArray = new Grasshopper.Kernel.Data.GH_Structure<Grasshopper.Kernel.Types.IGH_Goo>();

This component has 2 problems.

  1. When I use button to provoke OpenFileDialog, openfiledialog opens and as i select file, the window is provoked again and I select the file again, then the window closes. I don’t know why it does this. (Worked fine when coded inside grasshopper scripting window.)

  2. As an output of this component, each column of csv being each branch of tree should be returned(also, worked fine when coded inside grasshopper) but instead " structure {10;10;10} " returns. what am I doing wrong?

I have attached grasshopper scripting version of this for comparison.

Hello, for the “structure” issue, you should change the quoted line by :

DA.SetDataTree(1, treeArray);


1 Like