Pardon me if my google-fu is weak but I couldn’t find this anywhere.
I mostly understand how to work with Datatrees in a component I’m writing in C#. However, our application (which I’m trying to replace bit by bit with custom components) utilizes mixed type Datatrees. For example, we have a tree where each branch contains a point in the first entry, and int then the second, a vector in the third, etc. How do I create such a beast in C#? Is there some sort of multi-templating paradigm I’m not aware of?
If using the SDK, rather than C# components, you’ll need to use GH_Structure
instead of DataTree
. GH_Structure
is also a generic type which has a constraint in that T
must be a type of IGH_Goo. Thus, if you’re going to store more than one type of data inside a GH_Structure, you should declare it to be of type GH_Structure<IGH_Goo>
. Since GH_Point
, GH_Integer
, etc. all implement IGH_Goo
this will work.
Thank you so much! I’m sure I’ll have more questions but I seem to have this working as I would expect.
More questions from this guy
I am writing code to populate a data tree. The following does exactly what I want and expect, populates the first entry in each branch with a point constructed from a list of doubles xyz. So my question is, simply, am I doing anything that will get me in trouble memory-wise? Also, it seems silly to jump through a Point3D to get to GH_Point but I couldn’t see a way to directly populate the GH_Point.
Thanks.
GH_Structure<IGH_Goo> output = new GH_Structure<IGH_Goo>();
for (int i = 0; i < totalLength; i++)
{
int whichPoint = path[i];
Point3d p0 = new Point3d();
GH_Point ghPoint = new GH_Point();
p0.X = xyz[whichPoint][0];
p0.Y = xyz[whichPoint][1];
p0.Z = xyz[whichPoint][2];
ghPoint.Value = p0;
output.Insert(ghPoint.Duplicate(), new GH_Path(i), 0);
}
Don’t see anything wrong. You don’t have to worry about variables inside functions or loops, the compiler is pretty good about optimising that sort of stuff. Do note that Point3d
is a ValueType, so you don’t need to invoke its constructor. Or, if you do, you may as well provide the xyz coordinates directly.
You don’t have to duplicate the GH_Point
btw. It’s known only to your loop where it goes out of scope during the next iteration.
GH_Structure<IGH_Goo> output = new GH_Structure<IGH_Goo>();
for (int i = 0; i < totalLength; i++)
{
int index = path[i];
GH_Path pointPath = new GH_Path(index);
Point3d point = new Point3d(
xyz[index][0],
xyz[index][1],
xyz[index][2]);
GH_Point ghPoint = new GH_Point(point);
output.Append(ghPoint, pointPath); // I just realised you may need Insert() if you're populating this tree from various loops in reverse order.
}