Convert DataTree<object> to DataTree<Geometry>

There are many ways Convert List<object> to List<Geometry> (without loop for(int i))
for exmple:

using rd = Rhino.NodeInCode;
using System.IO;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;

using Rhino.DocObjects;
using Rhino.Collections;
using GH_IO;
using GH_IO.Serialization;
  private void RunScript(List<System.Object> list, ref object A, ref object B)
  {
//method1
   var ptA = new  List<Point3d> ();
    ptA = list.ConvertAll(i => (Point3d) i);
//method2
    var ptB = list.Cast< Point3d>().ToList();
    A = ptA;
    B = ptB;}

but for Convert DataTree<object> to DataTree<Geometry>
I tested the above method but be an error?
What method is there?(without loop for(int i))

ConvertA DtataTree-objet.gh (8.6 KB)


_RS_ConvertA DataTree-object.gh (4.3 KB)
Try this.
AFAIK, you can’t ‘convert’ a DataTree.
You are also explicitly converting your data to Point3d, which will not work unless you set input type to ‘Point3d’ instead of System.Object.

Hope it helps.

1 Like

thanks yes i know your method but
if we use loop can use this method in single loop

using System.Linq;
 private void RunScript(DataTree<System.Object> data, ref object A)
  {
    var tmp = new DataTree<Point3d>();

    for(int i = 0;i < data.BranchCount;i++){
      tmp.AddRange(data.EnsurePath(new GH_Path(i)).Cast<Point3d>().ToList(), new GH_Path(i));
    }
    A = tmp;}

ConvertA DtataTree-objet2.gh (7.0 KB)

It’s questionable if this is better. In the first place, you should never have to convert a List-of-objects into a List-of-points, because you always deal with data-boxing/unboxing. A point is a struct, so it’s stack-allocated. But as soon as a point is cast into an object (or vice versa), you heap-allocate it on top. This can have a serious performance penalties on large list/trees. And just because you use AddRange or a LINQ expression you still have that second or even third loop. Putting that expression into one line makes the line-count shorter, but essentially you are stacking multi-line statements into one line, which does not improve readability in my opinion! @SashankPs is definitely the more readable version here (and probaly also better performing solution, since adding and casting will happen in one go. Although I’m not sure if that is actually working as intended, but the approach is right)

when we use node in Rhino.NodeInCode;( use grasshopper component in c#)
the output of NodeInCode is object[] or DataTree<object>
for devlopment in my code we need converAll object to geometry(point curve ,…) in fast time

so this method can be use for otherType for example:


using System.Linq;
{
 var polyline = new List<Polyline>(pts);
 var curve = polyline.ConvertAll(i => i.ToNurbsCurve());}

or

var brepList= SurfaceList.ConvertAll(i => i.ToBrep());

etc…
Good luck

Ok, this makes more sense. I see the demand for NodeInCode, but it’s essentially the n-th wrapper around some essential code. If you need performance, forget about this. Use a .NET Decompiler look into the given GH component and do similar calls, without that extra Grasshopper overhead. Unless you have an exotic component, there is absolutely no reason to do this, except simplicity from a GH users perspective. But as always, that sort of simplicity comes with many tradeoffs. It’s pointless to search for faster conversion, if the bottleneck is NodeInCode!

1 Like