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 Xamarin Studio 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
    
  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.

Setting up Debug

  1. Create a folder called .vscode in the solution directory.

    Can't see the folder?
    If you cannot see the .vscode folder, toggle hidden folders in Finder: click on your Desktop and press ⌘ ⇧ . (command + shift + period)

  2. We will need to create three new files in the .vscode new folder: settings.json, tasks.json, and launch.json

  3. Create settings.json with the following contents:

    {
            // This file will specify the solution we use to build
            "dotnet.defaultSolution": "HelloGrasshopper.sln"
    }
    
  4. Create tasks.json with the following contents:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build-plugin-netcore",
                // This will ensure the project is built before we try to debug it
                "command": "dotnet build ${workspaceFolder}/*.csproj -f net7.0",
                "type": "shell",
                "args": [],
                "problemMatcher": [
                    "$msCompile"
                ],
                "presentation": {
                    "reveal": "always"
                },
                "group": "build"
            }
        ]
    }
    
  5. Create launch.json with the following contents:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Run GH 8 (Mac)",
                "type": "coreclr",
                "request": "launch",
                "preLaunchTask": "build-plugin-netcore",
                // Launches Rhino for us
                "program": "/Applications/Rhino 8.app/Contents/MacOS/Rhinoceros",
                // See : http://docs.mcneel.com/rhino/8mac/help/en-us/index.htm#information/startingrhino.htm
                "args": ["-runscript=_Grasshopper"],
                "cwd": "${workspaceFolder}",
                "stopAtEntry": false,
                "console": "internalConsole",
                // RHINO_PACKAGE_DIRS is required for Multi-Targeted plugins
                // This is what enables Rhino to register our Plug-in
                "env": {
                    "RHINO_PACKAGE_DIRS": "${workspaceFolder}/bin/Debug/"
                } 
            },
        ],
        "compounds": []
    }
    
  6. The Folder Explorer should look like below (Note that we cannot see .vscode in the Solution Explorer)

    New Project

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 perform no action

Solution Anatomy

  1. Quit Rhinoceros. This stops the session. Go back to Visual Studio Code. Let’s 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 GH_Component if you want to ship multiple components in a single gha.
  6. HelloGrasshopperInfo.cs defines general information about this gha.

Debugging

  1. Add a semicolon to line 47 of HelloGrasshopperComponent.cs, and set a breakpoint on it. 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. Click New Model. And then run our HelloDrawLine command. Create the two points and as soon as you do, you should hit your breakpoint and rhino will pause Hit a breakpoint
  4. With Rhino 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
  5. Let’s Continue Execution in Rhino by pressing the Green Play button in the Debug Bar
  6. Control is passed back to Rhino and your command finishes. Quit Rhino or Stop the debugging session.
  7. 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

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: