HowTo: Run C# Script in Rhino using VS Code

Hello,

I like to share my approach in executing C# scripts with Rhino using VS Code.

1.) Download VS Code , open VS Code, open a new Folder

2.) Search & Load the Microsoft C# Extention

3.) Go in the terminal and write: dotnet new console
-> This opens a new console application. We don’t compile from vscode, so we only need to get a basic project setup having a .csproject file.

4.) open the cs.project file and under </propertygroup> add:

      <ItemGroup>
         <Reference Include="RhinoCommon">
            <HintPath>C:\Program Files\Rhino 6\System\RhinoCommon.dll</HintPath>
            <Private>False</Private>
         </Reference>
      </ItemGroup>

This allows us to work with Intellisense/autocompletion
You may need to change the Absolute Path pointing to the RhinoCommon.dll


5.) write in the vscode terminal : dotnet restore
we now have intellisense for Rhinocommon

6.) Remove the Program.cs file
we write our own cs files. Do not use this one, since this file is encoded with the bom header and loading
this file will return an compilation error.

7.) Copy these files into the project folder:
compileCSharp.py (1.3 KB)
test.cs (421 Bytes)

8.) Now check the compileCSharp python file if the location of the Rhinocommon lib fits, and execute this within the Rhino Python editor to execute the test.cs file. You can also use the _RunPythonScript command


9.) Start writing your cs code

18 Likes

I really like this approach. Because it gives you great VScode tooling without the need to restart Rhino after each compilation. I was doing something similar with F# scripts.

You might want to set the Target framework to .Net Fullframework (e.g.4.7) in cs.project.
If you use features from NetCore (your current set up) your app might not compile in the python script.

1 Like

In case you all like to use VSCode for C# in Grasshopper here is a possible solution:

follow steps 1 to 6, but also add Grasshopper dlls to the project file in step 4:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="RhinoCommon">
      <HintPath>C:\Program Files\Rhino 6\System\RhinoCommon.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Grasshopper">
      <HintPath>C:\Program Files\Rhino 6\Plug-ins\Grasshopper\Grasshopper.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Reference Include="GH_IO">
      <HintPath>C:\Program Files\Rhino 6\Plug-ins\Grasshopper\GH_IO.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Reference Include="GH_Util">
      <HintPath>C:\Program Files\Rhino 6\Plug-ins\Grasshopper\GH_Util.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
</Project>

use this gh file :

VSCode_GH_Example.gh (17.4 KB)

and this cs script file, which is loaded into the file component:

ScriptComponentA.cs (1.2 KB)

This is a quick solution I made this evening, but it could potentially being improved…

7 Likes

nice, one question, how to compile as rhp file?

Technically its feasible, since a .rhp is a renamed dll with a certain structure. But its not meant to be used this way. It gets compiled, but stays in memory. Its intented to be a “script”. Meaning it can be used to invoke only small functionality during Rhino-Runtime using C# instead of Python. If you want to develop a plugin, you’ll need to do it the official way, using the Visual Studio IDE and using the Rhino template.
Of course you are free to extend my approach. If you do the second, it would be nice to share it here with others.

1 Like