Grasshopper

algorithmic modeling for Rhino

Grabbing Points from Layer and Casting Objects to Point3d in C#

Hello,

I am attempting to grab various geometries from layers in Rhino and using them as input in a C# component. I used code provided here on the forums in VB and translated it to C# to grab objects from layers:

String layer = "Points";

Int32 layer_index = doc.Layers.Find(layer, true);
if ((layer_index < 0))
return;

Rhino.DocObjects.Layer lay = doc.Layers[layer_index];
Rhino.DocObjects.RhinoObject[] objs = doc.Objects.FindByLayer(lay);
if ((objs == null))
return;

List<GeometryBase> geo = new List<GeometryBase>();
foreach (Rhino.DocObjects.RhinoObject obj in objs) {
geo.Add(obj.Geometry);
}

A = geo;

My issue here is that the grabbed "objects" should be casted to Point3d, and I can't figure out how this is done. Can someone help me with:

  • How to cast the grabbed objects to different geometry types (Point3d, curves, surfaces) Or,
  • How to grab geometry types in a certain layer using entirely different code that grabs Point3d, etc... rather than "objects"?

Thanks!

Tarek

Views: 1311

Replies to This Discussion

Hi Tarek,

You can use the Grasshopper Types to cast from GeometryBase to Rhinocommon types.

1. Method:

    List<Point3d> geo = new List<Point3d>();

foreach (Rhino.DocObjects.RhinoObject obj in objs) {
GH_Point p = new GH_Point();
p.CastFrom(obj.Geometry);
geo.Add(p.Value);
}

2. Method:

    List<Point3d> geo = new List<Point3d>();

foreach (Rhino.DocObjects.RhinoObject obj in objs) {
GH_Point p = new GH_Point(obj.Id);
p.LoadGeometry(doc);
geo.Add(p.Value);
}



Cheers FF

Thanks a lot Florian!

See I am not sure where I can get this kind of information (specifically about GH Types and casts), this forum helps a lot and I am very grateful. 

Quick fix just in case someone looks this up:

List<Point3d> geo = new List<Point3d>();

foreach (Rhino.DocObjects.RhinoObject obj in objs) {
GH_Point p = new GH_Point();
p.CastFrom(obj.Geometry);
geo.Add(p.Value);

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service