Apply transformation to objects in a DataTree

Hello!

I’m was looking for a way to apply a transformation to all lines in a DataTree. A simple way would be to iterate through all elements in the data tree and apply the transformation, but since the tree might contain different elements counts on each branch, this would be cumbersome. As an alternative I was exploring the AllData() method to get a list and apply the transformation to all elements. The problem is that I want to keep the elements with the Tree structure so I have to rebuild the Tree after the transformation. Is there a simpler way to do this without converting the Tree to List?

I was experimenting this but it doesn’t work.

 diagonals.AllData().ForEach(delegate (Line line)
    {
        line.Transform(transform);
    });

Why not iterate over all the branches in the tree? Each path will give you a list of items at that path to apply your transformation to.

Also, your method may not work because Line is a struct (value type), not a class (reference type), see below my code.

DataTree<Line> lines; // defined elsewhere
Transform transform; // defined elsewhere
foreach (var path in lines.Paths)
{
  List<Line> linesAtPath = lines.Branch(path);
  for (int i = 0; i < linesAtPath.Count; ++i)
  {
    // Line is a struct, so you get a copy from the list
    Line l = linesAtPath[i];
    l.Transform(transform);
    // update the list
    linesAtPath[i] = l; 
  }
}

Thanks @menno! Yes, I get my error now list is reference type and Line is value type.