Grasshopper

algorithmic modeling for Rhino

Hi,

I am a little confused about how GH_Structure works.

In a compiled C# component I want load a Brep DataTree and place it in a flat list. In another list I would like to remember the paths - but I am having trouble with grabbing the data from the input and placing it into the lists.

Any help would be appreciated, thanks.

GH_Structure<GH_Brep> Tree; //  <--- When I load the component in GH I get an "must be called with the correct type" error 
DA.GetDataTree<GH_Brep>(0, out Tree);

List <int[]> paths = new List<int[]>();
List<Brep> AllBreps = new List<Brep>();
for (int i = 0; i < Tree.DataCount; i++)
{

AllBreps.Add((Brep) Tree.get_DataItem(i).Value);
paths.Add(Tree.Paths[i].Indices);
}

Views: 1875

Replies to This Discussion

Hi Tim,

when you call DA.GetDataTree the Type of the GH_Structure<T> must be identical to the type stored in the parameter. What sort of parameter have you got? Param_Brep, Param_Surface, Param_Geometry, ...?

A GH_Structure has a sorted list of paths and lists. You can convert paths to int[], but I don't see the point of that. Why not just stick to GH_Path? Grasshopper can handle that type.

You can use foreach() to iterate over all the items in a GH_Structure, this is faster than using Tree.DataItem(i). You can also foreach over the GH_Structure.AllData(bool) property which allows you to skip nulls.

There's also a FlattenData method which returns a list of all the items in all the branches. Basically it does what your for loop does, except it does it a lot faster :)

GH_Structure<GH_Brep> tree; 
DA.GetDataTree(0, out tree);

List<Brep> breps = new List<Brep>(tree.DataCount);

foreach (GH_Brep item in tree)

{

  if (item == null)

    breps.Add(null);

  else

    breps.Add(item.Value);

}

If however you want to create two lists, one containing the breps and another containing the path for each brep, I recommend creating two nested loops, one iterating over all paths, the other over all items in each branch:

GH_Structure<GH_Brep> tree; 
DA.GetDataTree(0, out tree);

List<Brep> breps = new List<Brep>(tree.DataCount);

List<int[]> paths = new List<int[]>(tree.DataCount);

for (int i = 0; i < tree.PathCount; i++)

{

  int[] path = (int[])tree.Paths[i].Indices.Clone();

  List<GH_Brep> branch = tree.Brancges[i];

  foreach (GH_Brep item in branch)

  {

    paths.Add(path);

    if (item == null)

      breps.Add(null);

    else

      breps.Add(item.Value);

  }

}

--

David Rutten

david@mcneel.com

Tirol, Austria

Hi David,

thanks for the extensive and fast reply. This makes things a lot clearer.

The parameters on DA.GetDataTree(0, out tree); come in as IGH_Goo. How can I cast these into Breps?

Best

Tim

Never-mind - just found :

Brep temp;
item.CastTo<Brep>(out temp);

Thanks a lot for your help David

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service