Your First Component (Mac)
macOS only

Prerequisites

It is presumed you already have the necessary tools installed and are ready to go. If you are not there yet, see Installing Tools (Mac).

HelloGrasshopper

We will use Visual Studio Code and the dotnet Rhino Grasshopper template to create a new, basic, Grasshopper component called HelloGrasshopper.

If you are familiar with Visual Studio Code, these step-by-step instructions may be overly detailed for you. The executive summary: create a new Solution using the Grasshopper Component dotnet template, build and run, and then make a change.

We are presuming you have never used Visual Studio Code before, so we’ll go through this one step at a time.

Download the required template

  1. Launch Visual Studio Code.
  2. Open Visual Studio Code’s Terminal via Terminal (menu entry) > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
  3. Inside Terminal, run:
    dotnet new install Rhino.Templates
    

Starting the Project

  1. Create a folder on your mac where you would like your project to live. Name the folder HelloGrasshopper.
  2. If you have not done so already, launch Visual Studio Code.
  3. Now we can open our new folder, navigate to File > Open Folder and choose the folder we just created.
  4. Open Terminal via Terminal > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
  5. Enter the following command into the Terminal:
    dotnet new grasshopper --version 8 -sample
    
  6. In our Folder explorer, we should see the project appear as Visual Studio Code discovers the files.
  7. Expand the Solution Explorer, this is the best way to interact with C# projects on Mac in Visual Studio Code.

Boilerplate Build

Build Issue?
Older Rhino Templates do not have System.Drawing.Common referenced. To add them to your project run the command dotnet add package System.Drawing.Common -v 7.0.0 in the terminal.
  1. Before we do anything, let’s Run and Debug HelloGrasshopper to make sure everything is working as expected. We’ll just build the boilerplate Plugin template. Click the Run and Debug button on the left hand side of Visual Studio Code and then the green play button in the newly opened panel.

    New Project

  2. Rhinoceros and Grasshopper launch.

  3. We will find the HelloGrasshopper Component under Category / SubCategory

Solution Anatomy

  1. Adding the component to the canvas will run the component and output some interesting geometry in the Rhino Viewport

Solution Anatomy

  1. Press Stop Debugging (⇧ F5), in Visual Studio Code, signified by the Red Square in the debug toolbar. This stops the debugging session. Now let’s n take a look at the Plugin Anatomy.

Component Anatomy

Use the Solution Explorer to expand the Solution (.sln) so that it looks like this…

Solution Anatomy

  1. The HelloGrasshopper solution (.sln)
  2. The HelloGrasshopper project (.csproj) has the same name as its parent solution…this is the project that was created for us by the template earlier.
  3. References: Just as with most projects, you will be referencing other libraries. The template added the necessary references to create a basic Grasshopper component.
  4. Grasshopper is the Rhino for Mac main grasshopper DLL. Classes in this DLL are subclassed and used by your custom component.
  5. HelloGrasshopperComponent.cs is where a custom Grasshopper.Kernal.GH_Component subclass is defined. Your project may contain multiple subclasses of GHComponent if you want to ship multiple components in a single _gha.
  6. HelloGrasshopperInfo.cs defines general information about this gha.

Debugging

  1. Add a breakpoint to line 75 of HelloGrasshopperComponent.cs. You set breakpoints in Visual Studio Code by clicking in the gutter to the left of the line numbers. Set a breakpoint
  2. Run and Debug. our project. The breakpoint will become an empty circle, this is because our code has not been loaded yet. Once we hit the breakpoint once and continue, the code will be loaded until we end our Debug session. Set a breakpoint
  3. Rhino and Grasshopper should open, if Grasshopper does not open, click “New Model” and run the Grasshopper command.
  4. Place our sample component HelloGrasshopperComponent and as soon as you do, you should hit your breakpoint and rhino/Grasshopper will pause (You may need to drag the Grasshopper window out of the way to see Visual Studio Code) Hit a breakpoint
  5. With Rhino/Grasshopper paused, in Visual Studio Code we will see Locals under Variables. You can inspect all of the values for the variables in your component. Locals panel
  6. Let’s Continue Execution in Rhino and Grasshopper by pressing the Green Play button in the Debug Bar
  7. Control is passed back to Rhino / Grasshopper and your command finishes. Now Stop (⇧ F5) the debugging session as before.
  8. Remove the breakpoint you created above by clicking on it in the gutter.

Congratulations! You have just built your first Grasshopper component for Rhino for Mac. Now what?

Next Steps

You’ve built a component library from boilerplate code, but what about putting together a new simple component “from scratch” and adding it to your project? (Component libraries are made up of multiple components after all). Next, check out the Simple Component guide.

Try debugging your new grasshopper plugin on Windows, all plugins using the new templates are now cross-platform by default.

Adding components

A single gha can contain more than one GH_Component derived class (and commonly does). Dotnet has support for adding more custom components to your project.

  1. Open Visual Studio Code’s Terminal via Terminal (menu entry) > New Terminal, or using the command palette (⌘ ⇧ P) and search for “Terminal”.
  2. Inside Terminal, run:
dotnet new ghcomponent -n "NewComponent"
  1. A new component will appear called NewComponent

This article is focused on initial setup and debugging a Grasshopper component in Rhino for Mac. For further reading on customizing your component please see: