Preview in Grasshopper

Hi to all,
I have a question about a custom class and component I created. This component is a simple point class but with some enriched properties, you can see it from the next image:

I want that the output of my custom class have two different behaviour:

1.) Have an output as “Point3d” in order to see the Points in the Rhino 3D GUI;
2.) Override ToString() property in order to have the text (the text you see on the panel component) output that I want.

I tried to derive from Point3d but is impossible because it is marked as “sealed” class. So any suggestion how to achieve this?

Thanks
Robert

Point3d is a struct mate, you cannot derive from structs at all.

Luckily it doesn’t matter, because Grasshopper only allows you to store types in parameters that implement the IGH_Goo interface.

If you haven’t downloaded the Grasshopper SDK documentation yet you should probably start there (it’s available via the GH help menu). It has an example of how to implement a new data type.

To do what you want to do is not perhaps entirely possible. The point type already exists in Grasshopper and you cannot override its ToString() method. So you’ll have to create a new type which can have a custom ToString() and also a custom preview display, and it can automatically convert itself to (or from) a GH_Point, but as soon as you feed that type into a standard Point parameter it will be converted and all that additional information will be lost.

1 Like

The attached file has a C# component which defines a new geometric data type based on a Point3d coordinate, but overrides ToString() and the preview. It draws a circle around the point, facing the world origin. It also implements the proper casting methods, so you can feed your data into any component which expects points and it will work.

custom goo.gh (13.1 KB)

1 Like

Dear David,
Thank you for replay. Conceptually now is very clear for me. I want to ask your some additional information of how to add a custom preview display and how to convert itself automatically to (or from) a GH_Point, I mean specifically what I have to override and where. I think i have to play around the custom attribute or i’m wrong?

PS: I will see yout attached file maybe can answer to my questions :-). T

Thanks again
Robert

For making your goo type play nice with the rest of Grasshopper, you need to override the CastTo(), CastFrom() and ScriptInstance() methods. CastTo is used when Grasshopper needs to convert your data into other data. In this particular case, I handled both Point3d and GH_Point as supported types. CastFrom is used when other data needs to be converted into your custom data. Finally you are also allowed to change the way your data is exposed within script components. You don’t have to, but I chose to simplify it to just the Point3d.

Preview (and Baking) functionality is provided by implementing additional interfaces. IGH_PreviewData has been explained in the example file, IGH_BakeAwareData (or something close to that, can’t quite remember the name right now) is needed if you want to bake your points as well.

1 Like

This is a off topic question and I hope it‘s ok that I ask here. Can I ask you David how you create the Rhino viewport in your gh screenshot? Is there a component or do you „photoshop“ it?

Ctrl+Shift+Q

There’s a menu item in the Grasshopper file menu for outputting these images. The viewport isn’t “live”, it’s basically photoshop without photoshop.

3 Likes

Dear David,
Thank you for the answer. Now is very clear.

Thanks

Thank you David :slight_smile:

// Note to self...
// watch out for those darn copyright infringement suits.
Rename "Params.Input.PhotoshopTM", "Params.Input.photoshop"
1 Like