Grasshopper

algorithmic modeling for Rhino

Hi,

I would like to sort objects by type. (Sorting only meshes and lines)

It works in c# component in grasshopper, when type hint is GeometryBase.

But when I try to input these objects in visual studio:

pManager.AddGeometryParameter("Geo", "G", "GeometryBase", GH_ParamAccess.list);

I get an error already at input part of code:

1. Invalid cast: Line » GeometryBase

I attached printscreens, gh, and code from visual studio.

Could you please give me an advice how to solve it?

Views: 1873

Attachments:

Replies to This Discussion

Rhino.Geometry.Line is a high-performance struct that does not partake in any of the interface/class hierarchies in RhinoCommon. If you want a line that is a type of GeometryBase you need to create a LineCurve class instead.

I thought to input a list System.Objects (nulls,meshes, lines) then.

It was just interested that C# in grasshopper converts line to lineCurve using type hint, but visual studio not.

But how to check the type of the object then? As with GeometryBase I just use ObjectType function.

This seems to work:

    String t = G.GetType().ToString();
    if(t == "Rhino.Geometry.Line"){
     ...
    }

A more efficient way is to compare the Type objects directly rather than their string presentations. This can be beneficial if you are processing a lot of data. To get a Type object you can use the typeof operator as follow

if (G.GetType() == typeof(Rhino.Geometry.Line))

...

I much prefer the is and as keywords (though as won't work on structs, you'll need to cast in that case):

object someGeometry = ...;

if (someGeometry is Mesh)

  Mesh mesh = someGeometry as Mesh;

else if (someGeometry is Line)

  Line line = (Line)someGeometry;

The as keyword never throws exceptions and instead assigns null to the target is the conversion is not possible, which is why it cannot be used on structs.

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