Grasshopper

algorithmic modeling for Rhino

Hi.

 

I'm trying to change my geometry based on its relationship to other, spcified, points.

As an input I have my geometry as a DataTree, and I want to swap the points that are close enough to the magnet-points with transformed ones.

My problem is that I don't know how to traverse a DataTree, and in the RhinoCommon sdk I can't find any documentation.

 

Any help would be appreciated. A link to documentation likewise.

 

Cheers, Peter.

Views: 2447

Replies to This Discussion

I found an example of how to traverse the tree, although in my example, I am not sure if it actually goes through the whole tree, a few points seems to be missing ? ...

 

Where I get an error, is where I try to replace the point (tree - branch - item) with the modified one. How can this be done?

 


private void RunScript(DataTree
endPoints, List
magnets, ref object A)
{

// for each magnet ..
foreach (Point3d magnet in magnets)
{
//Loop through all branches/geometry
for(int i = 0; i < endPoints.BranchCount; i++)
{

//The path for this index
GH_Path path = endPoints.Path(i);

//The count of elements in this index
int elementCount = endPoints.Branch(i).Count;

if(elementCount > 0)
{
//The first point in this path
Point3d firstPoint = endPoints[path, 0];
Line connect = new Line(magnet, firstPoint);

// if the geometrypoint is close to the magnet, transform
if(connect.Length < 3.5)
{
connect.Transform(Transform.Scale(magnet, 0.5));
}
returtest.Add(connect);

// change the point
endPoints[path, 0] = new Point3d(connect.ToX, connect.ToY, connect.ToZ);
}

}
}
A = endPoints;

}

Just a quick correction to the previous code, it was only set up to pick the first item on each branch...

for(int k = 0; k < elementCount; k++)
{
// The k point in this path
Point3d kPoint = endPoints[path, k];
Line connect = new Line(magnet, kPoint);

// if the geometrypoint is close to the magnet, transform
if(connect.Length < control)
{
connect.Transform(Transform.Scale(magnet, 1));
returTest.Add(connect);
}

// change the point
//endPoints[path, 0] = new Point3d(connect.ToX, connect.ToY, connect.ToZ);

The question then remains; how can I replace an item in the Tree? 

Hi peter

endpoints.Branch(path).RemoveAt(0);
endpoints.Branch(path).Insert(0,new Point3d(connect.ToX, connect.ToY, connect.ToZ);

I did it in 8 lines of code (if you don't count curly brackets as separate lines that is).

See attached.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Attachments:

Hi to]`s and David,

 

thanks a lot for your reply, it helped a lot :)

 

 

This might be useful for someone.

To traverse the path in nested loops I do it like so:

Get the max number of branches at each depth by

int depthLayerOne = tree.Paths[tree.PathCount - 1].Indices[0] + 1;

int depthLayerTwo = tree.Paths[tree.PathCount - 1].Indices[1] + 1;

It gets the last element from the list of paths, which would have the max number of branches at each depth level as indices. I add one to those, to make it easy to loop.

I can do the 0 and 1 index into the 'Indices ' array because in my case I know the number of layers in my tree.

If you don't know that in advance you can use a list to loop through.

Nested looping like so:

for(int i=0; i<depthLayerOne; i++) {

    for(int j=0; j<depthLayerTwo; j++)

        {

            // code

        }

}

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