GH_Polyline class would be nice to have

hi

There is a GH_xxxx class for all the Rhino geometry that can be output from a component, except Polyline.

I was working on a c# method that used reflection to iterate through all the fields of a class used to hold construction type geometry and create a list that can be used as an argument to DA.SetDataList. This can be solved by handling Polyline as a special case, but its not very elegant.

regards

eric

Certainly most types, for Polyline you can output a PolylineCurve instead.

The script components in fact also handle Polyline and string as special cases because they are both collection types (of Point3d and char respectively) which aren’t meant as collection types.

Can you show me the code that is currently inelegant? I’m having a hard time seeing exactly where the problem lies.

i was using Polyline because it is what got returned from creating polygons as well as inscribed polygons…

i was planning on having a Dictionary map the Rhino type into the corresponding GH_xxx type and using

Activator.CreateInstance to instantiate that object…this would have worked for all the types except Polyline…

i could of created my own type to construct a suitable object

the plugin was a learning exercise for c# and the rhino&grasshopper api’s, in the end it was easier to do what i wanted using normal grasshopper as opposed to doing a plugin, so no real code per see…

the only reason i brought it up, was that this seemed to be the only geometry type missing from the GH_xxx types

sorry for the lame response yesterday, this is the pseudo code showing what i was trying to do…since i ended up doing what i wanted in native grasshopper, i did not finalize this code, however each of the key parts to implement it were tested to see that results were what was expected,such as init of flds in the super class, and activating object creation.

the question is why Polyline is not treated the same as the other objects by having a GH_xxx wrapper???

class Super {
  ...
  // static init once
  // List of fields in sub class
  static protected FieldInfo[] flds;

  // static, init once using the dictionary below...
  // List of types to be able to construct objs
  static protected Cons[] typeObj;
  
  // dictionary of types that can be mapped
  static protected Dictionary<Type, Type> typeMap = new Dictionary<Type, Type>()
  {
    { typeof(GeometryGroup), typeof(GH_GeometryGroup) },
    { typeof(Arc), typeof(GH_Arc) },
    { typeof(Box), typeof(GH_Box) },
    { typeof(Brep), typeof(GH_Brep) },
    { typeof(Circle), typeof(GH_Circle) },
    { typeof(Curve), typeof(GH_Curve) },
    { typeof(Line), typeof(GH_Line) },
    { typeof(Rectangle), typeof(GH_Rectangle) },
    { typeof(Mesh), typeof(GH_Mesh) },
    { typeof(Plane), typeof(GH_Plane) },
    { typeof(Surface), typeof(GH_Surface) },
    { typeof(Point3d), typeof(GH_Point) },
    { typeof(ObjectWrapper), typeof(GH_ObjectWrapper) },
    { typeof(Surface), typeof(GH_Surface) },
    // the odd man out .. 
    // one could use this ?? i think
    { typeof(Polyline), typeof(Polyline ???) }
  };

  // called by client to get a result  
  public List<Object> Generate() {
    List<Object> result = new List<Object>();
      
    for each entryin in flds and each tc in typeObj {
      get the current value
      Object o = Activator.CreateInstance(tc, value);
      result.add(o);
    }
    return o;
    }
  }
}

class Client : Super {
  // geometry objects
  public Point3d point;
  public Circle circle;
  ... 
}

// user code elsewhere
  ...
  Client c = new Client();
  c.point = new Point3d(...);
  c.circle = new Circle(...);
  ...
  List<Object> r = Generate();
  DA.SetDataList(1, r);
  ...
//

Oh I see, in this case Polyline definitely isn’t the only odd one out. NurbsCurve, ArcCurve, BezierCurve, ControlPoint, BoundingBox, Cone, Cylinder, Sphere, Ellipse, … There’s loads of geometric types in RhinoCommon which do not have a parallel Grasshopper goo counterpart.

The methods in the GH_Convert class do a lot of special casing, if you’re looking to get geometric goo out of some value, try GH_Convert.ToGeometryGoo() it may do what you need without the need for a bunch of special cases on your part.