Grasshopper

algorithmic modeling for Rhino

Hello all,

 

I'm trying to write a little method which should map a list of objects onto an existing tree structure.

Basically, I would like to take a tree and flatten it into a list 

List/font>IGH_Goo> treeList = tree.FlattenData();

I would then like to modify the items in the list, and map the individual items into a new tree which has the same topology as the original tree. Each modified item should take the same place as it had in the original tree.

I'm doing this as I have a lot of legacy compoenents which work on lists rather than trees, but I would like to update them to retain tree information.

 

Here's what I've got so far, but this only takes into account branches and does not retain path information.

 

GH_Structure/font>IGH_Goo> ListToTree(List/font>IGH_Goo> items, GH_Structure/font>IGH_Goo> tree)

{

GH_Structure/font>IGH_Goo> tree_fromlist = new Grasshopper.Kernel.Data.GH_Structure/font>IGH_Goo>();

 

int iItem = 0;

int iBranch = 0;

 

foreach (List/font>IGH_Goo> branch in tree.Branches)

{

List/font>IGH_Goo> objs = new List/font>IGH_Goo>();

 

foreach (IGH_Goo obj in branch)

{

objs.Add(items[iItem]);

iItem++;

}

GH_Path p = new Grasshopper.Kernel.Data.GH_Path(iBranch);

 

tree_fromlist.AppendRange(objs, p);

iBranch++;

}

return tree_fromlist;

}

 

Before I get stuck in trying to get it to work with paths (which I fear might be a touch more complicated,), I thought I'd ask if there's a much more obvious way of doing this. Any ideas?  Is there a way to access the objects of individual paths? Thanks!

Views: 485

Replies to This Discussion

ah, I think I've found a way of doing it (not particularly neat, as a whole copy of the first tree is made), but I guess for now it will do the job...

 

public GH_Structure/font>IGH_Goo> ListToTree(List/font>IGH_Goo> items, GH_Structure/font>IGH_Goo> tree)

{

GH_Structure/font>IGH_Goo> tree_fromlist = tree.Duplicate();

int k = 0;

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

{

GH_Path pS = tree.get_Path(i);

System.Collections.IList branch = tree.get_Branch(pS);

GH_Path pT = tree_fromlist.get_Path(i);

for(int j = 0; j <branch.Count; j++)

{

tree_fromlist.Insert(items[k], pT, j);

k++;

}

}

return tree_fromlist;

}

You could also maybe create a shallow duplicate of the tree. That'll be a lot faster if the tree contains things that take a long time to duplicate like Meshes or Breps.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

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