Grasshopper

algorithmic modeling for Rhino

Hi.

 

I get a DataTree<Point3d> as input to my c# script, and I would like to copy this DataTree to a variable, in order to treat the points and aferwards compare them.

Because of the C# value and reference types (the Class DataTree being the latter), I cannot seem to be able to create a copy of my input class. Hence, all the changes I make affect both the original and "cloned" DataTree.

 

Example:

RunScript(DataTree<Point3d> originalPoints)
  {
    // clone
    DataTree<Point3d> copyTree = originalPoints;

 

    for(int i = 0; i < copyTree.BranchCount; i++)
      {

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

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

        if(elementCount > 0)
        {
          for(int k = 0; k < elementCount; k++)
          {
            // The k point in this path
            Point3d kPoint = copyTree[path, k];

            //change

            kPoint.X += 2;

            copyTree.Branch(path).RemoveAt(k);
            copyTree.Branch(path).Insert(k, kPoint);

           }

       }

}

 

Because the my copyTree only is a pointer to the originalPoints, a comparison afterwards shows all points to be the same.

 

Is there a DataTree constructor that clones itself, or a way to create a new Datatree and fill it with the values from the original?

 

Cheers, Peter

Views: 2220

Replies to This Discussion

Hi Giulio and David,

 

thank you for your great help ! Its great so see that its finally working and i hope some more people will participate on this thread;

 

best wishes Peter

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service