Drawing object inside C# script/ C# component

Is there anyway to draw/add geometry and text through c# script in grasshopper without using output? I would be very thankful if you provide for me a short example.

1 Like

Hi @bahram.hosseini

it might help if you could tell us what you are trying to do.
Grasshopper’s logic is based on inputs and outputs. Not using them should be a well-thought-out decision.

If the purpose is display, you could use the CustomDisplay class:

or, look for examples of DisplayPipeline and DisplayConduit (mostly for compiled components and not for C# Scripts).


Giulio Piacentino
for Robert McNeel & Associates

1 Like

There’s a button at the top of the C# code editor that looks like an eye. Click on it to insert the three relevant method overrides for custom previews. There are already plenty of examples out there as Giulio mentioned, but the basic idea is usually this:

  • Define a bunch of class level variables that hold all your preview data. For example two lists, one of type Line and one of type Color. Also define a BoundingBox field.
  • Override the BeforeRunScript method (the button with the red and green arrows will insert these overrides).
  • Inside the BeforeRunScript wipe your lists and set your bounding box to Empty.
  • Inside your RunScript method (which potentially gets called a lot during every solution) populate your lists and make sure your bounding box grows enough to always contain all the geometry you wish to draw.
  • From your ClippingBox property return the bounding box you’ve been updating.
  • If you are drawing curves/points/text then from within DrawViewportWires iterate over your Line and Color lists and draw them using the pipeline object available in the IGH_PreviewArgs instance.