GH_ParamAccess.tree not working as intended


(Andrew Jinho AHN) #1
    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 { }

        }
        else
        {
            if (filePath != null)
            {
                //separator
                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 code 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 from csv table 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.


(David Rutten) #2

You should define your tree as a GH_Structure<GH_String> from the start. You can only put that kind of data into a Text output parameter. And yes it’s recommended to wrap string into GH_String by using the constructor like you’re doing.

I would also heavily recommend declaring your tree variable inside the method only, do not make it a class level variable unless you absolutely must access it outside of SolveInstance().


(David Rutten) #3

This should not be legal. If you declare your output to have access of list, you should only be allowed to call DA.SetDataTree(1, treeArray). I’m not entirely sure why you’re getting away with calling SetData().