Cast from Geometrybase to Line and general casting questions

Hi,
I have some questions about the cating of geometry in visual studio.
My example case:I want to have a component that sorts geometry based on midpoint or centroid.

I read about this topic and as far as I understood IGH_Geometry-Goo is just needed when operating on trees, or would there be an advantage compared to GeometryBase?
So I started using GeometryBase which worked fine until it came to Lines and Points, which (as I understood)cant be casted since they are structs and not nullable.
How would be the correct way to cast that?

          if (geo[i] is Line) //geo[i] is type GeometryBase
                {
                    Line line = (Line)geo[i];
                    zS.Add(line.ToNurbsCurve().PointAtNormalizedLength(0.5).Z);
                }

Thanks for any insight.

See attached (and check what types are supported in a GB collection).

Screen%20Shot%20046

GeometryBase_ToLines_V1.gh (12.4 KB)

Forgot the Point part of the story

GeometryBase_ToLines_V1A.gh (7.5 KB)

Thanks Peter,

This works fine in the script editor, but in VS it fails. So I kept trying, my cheap solution for now is using IGH_Geometric-Goo which seems to be able to harvest a BoundingBox out of everything,also accepts points.
I will maybe try to solve it later, for now it already cost a lot of time and until this solution fails I will stay with it.

Thanks again!

Well … to tell you the truth I can hardly justify the usage of VS for small or smallish coding adventures. In fact I never use VS … but anyway.

Line does not derive from GeometryBase. LineCurve does.

Usually for converting between various types the methods in the GH_Convert class are sufficient.

2 Likes