Create a Tree using GH_Structure<T> innstead of DataTree<>

Hi,

I am looking for a way to replicate the same behavior of this code below using GH_Structure (This was done in C# Script in GH for prototype purposes) Since I am using the Grasshopper SDK and developing a small component in Visual Studio the documentation recommends to use GH_Structure

  private void RunScript(List<Brep> _Breps, ref object asDataTree)
  {
    // get edges of breps, store in DataTree
    DataTree<Curve> curvesTree = new DataTree<Curve>();
    for (int i = 0; i < _Breps.Count; i++)
    {
      GH_Path pth = new GH_Path(0, i);
      curvesTree.AddRange(_Breps[i].Curves3D, pth);
    }

    // Outputs
    asDataTree = curvesTree;


  }

My failed try to use GH_Structure

  private void RunScript(List<Brep> _Breps, ref object asGH_Structure)
  {

    // get edges of breps, store in Tree but using GH_Structure
    GH_Structure<GH_Curve> curvesTree = new GH_Structure<GH_Curve>();
    for (int i = 0; i < _Breps.Count; i++)
    {
      GH_Path pth = new GH_Path(0, i);
      curvesTree.AppendRange(_Breps[i].Curves3D, pth);
    }

    // Outputs
    asGH_Structure = curvesTree;


  }

I got the error

cannot convert from ‘Rhino.Geometry.Collections.BrepCurveList’ to ‘System.Collections.Generic.IEnumerable<Grasshopper.Kernel.Types.GH_Curve>’ (line 63)

I think my problem is in the line GH_Structure<GH_Curve> curvesTree = new GH_Structure<GH_Curve>();. I took a look on Grasshopper.Kernel.Types Namespace on the documentation to try to see if there is a data type similar like BrepCurveList.

Any comments and suggestions to point me on the right direction will be greatly appreciated.

Additional Clarification: I posted this code on a C# Script just to share here. The goal of the GH_Structure is to be used on a custom script that I am developing on visual studio.

DataTree vs GH_Structure.gh (432.3 KB)

Maybe use IGH_Goo instead of GH_Curve so all gh objects are accepted also lists

@flokart I still have

|Error|CS1503|Argument 1: cannot convert from ‘Rhino.Geometry.Collections.BrepCurveList’ to ‘System.Collections.Generic.IEnumerable<Grasshopper.Kernel.Types.IGH_Goo>’

:anguished:

Hi @rolandoavillena1

You should then iterate through the output of 3DCurves, which is the BrepCurveList each item in the BrepCurveList will be of type Curve

Since you are using GH_Structure, it only accepts Grasshopper types. This means any class which implements IGH_Goo. For this reason you then need to create GH_Curve's from each Curve One of the overloaded constructors of GH_Curve accepts a Curve as an argument.

Sorry I could not send you a quick example. But I am not with my computer at the moment.

I hope the explanation was clear enough.

1 Like
  private void RunScript(List<Brep> _Breps, ref object asGH_Structure)
  {

    // get edges of breps, store in Tree but using GH_Structure
    GH_Structure<IGH_Goo> curvesTree = new GH_Structure<IGH_Goo>();
    for (int i = 0; i < _Breps.Count; i++)
    {
      
      GH_Path pth = new GH_Path(0, i);      
      List<GH_Curve> l_gh_crv = new List<GH_Curve>();
      for (int j = 0; j < _Breps[i].Curves3D.Count;j++)
      {
        GH_Curve gh_crv = new GH_Curve();
        GH_Convert.ToGHCurve(_Breps[i].Curves3D[j], GH_Conversion.Both,ref gh_crv);
        l_gh_crv.Add(gh_crv);
      }
      curvesTree.AppendRange(l_gh_crv,pth);
    }

    // Outputs
    asGH_Structure = curvesTree;

Maybe this could work.

1 Like

@rawitscher-torres I think @flokart implemented what you recommended. Thanks for your explanation and for the time to take a look at it!

@flokart it works perfect :mechanical_arm:! Thanks

I guess @rawitscher-torres recommented something more like this.

  private void RunScript(List<Brep> _Breps, ref object asGH_Structure)
  {

    // get edges of breps, store in Tree but using GH_Structure
    GH_Structure<IGH_Goo> curvesTree = new GH_Structure<IGH_Goo>();
    for (int i = 0; i < _Breps.Count; i++)
    {
      for (int j = 0; j < _Breps[i].Curves3D.Count;j++)
      {
        GH_Path pth = new GH_Path(0,i);
        GH_Curve gh_crv = new GH_Curve(_Breps[i].Curves3D[j]);
        curvesTree.Append(gh_crv,pth);        
      }
    }
    // Outputs
    asGH_Structure = curvesTree;

no need to convert curve to gh_curve because there is already a constructor that accept curve and also the list is not necessary.
Maybe his solution is a bit faster.

1 Like

The two examples are great learning resources for me. Both work as expected. Thanks @flokart