Your First Plugin (Windows)
This guide walks you through your first plugin for Rhino for Windows using RhinoCommon and Visual Studio.
It is presumed you already have the necessary tools installed and are ready to go. If you are not there yet, see Installing Tools (Windows).
HelloRhinoCommon
We will use the RhinoCommon Templates to create a new, basic, command plugin called HelloRhinoCommon.
If you are familiar with Visual Studio, these step-by-step instructions may be overly detailed for you. The executive summary: create a new Solution using the RhinoCommon 2015 Community Edition and C#).
- Navigate to File > New > Project…
- A New Project wizard should appear. In the left column, find the Installed > Templates > Visual C# > Rhinoceros section. In the central list, select the RhinoCommon Plug-In template…
- For the purposes of this Guide, we will name our demo plugin HelloRhinoCommon. At the bottom of the window, fill in the Name field. Browse and select a location for this plugin on your disk…
- Check Create directory for solution. Note: This is optional depending on how you want to structure your projects.
- Click the OK button. Note: You don’t have to add the project to source control for this demo.
- The New RhinoCommon Plug-In dialog appears. This dialog allows you to Configure your new plugin project, as well as select which RhinoCommon references to use and which debug version of Rhino you would like to launch…
- For the purposes of this guide, we will accept the defaults and click Finish…
- A new solution called HelloRhinoCommon should open…
Boilerplate Build
- Before we do anything, let’s build and run HelloRhinoCommon to make sure everything is working as expected. We’ll just build the boilerplate Plugin template. Click Start (play) button in toolbar corner of Visual Studio (or press F5) to Start Debugging…
- Rhinoceros launches. Create a new, empty model.
- Since this is the first time you are running the plugin, you need to “install” it. (The RhinoCommon template has set things up so when you compile the solution an .rhp file is compiled in the bin subdirectory of the project directory.) In the Rhino command prompt type the PlugInManager command. Click the Install… button.
- Browse to the HelloRhinoCommon.rhp file (in our case, this is in C:\dev\repositories\HelloRhinoCommon\HelloRhinoCommon\bin\) and click Open.
- The HelloRhinoCommon plugin is now installed. Click OK to close the Rhino Options dialog.
- Enter the HelloRhinoCommonCommand command. Notice that the command autocompletes…
- The HelloRhinoCommonCommand command begins and prompts you to Please select the start point:.
- Notice there is also a command status in Rhino’s command prompt when the command begins: The HelloRhinoCommonCommand command will add a line right now.
- Finish the command by selecting a start point and and end point.
- Also note there is a command status in Rhino’s command prompt when the command ends: The HelloRhinoCommonCommand command added one line to the document.
- Exit Rhinoceros. This stops the session. Go back to Visual Studio. Let’s take a look at the…
Plugin Anatomy
- Use the Solution Explorer to expand the Solution (.sln) so that it looks like this…
Note: Depending on your edition of Visual Studio, it may look slightly different.
- The HelloRhinoCommon project (.csproj) has the same name as its parent solution…this is the project that was created for us by the RhinoCommon Plugin template wizard earlier.
- Properties contains the AssemblyInfo.cs source file. This file contains the meta-data (author, version, etc) about the plugin.
- References: Just as with most projects, you will be referencing other libraries. The RhinoCommon Plugin template added the necessary references to create a basic RhinoCommon plugin.
- Microsoft.CSharp contains classes required for C# code compilation.
- RhinoCommon is the critical reference for our purposes here.
- System, System.Core, System.Drawing, System.Windows.Forms are .NET foundational libraries.
-’s Source Editor (if it isn’t already).
Notice that
HelloRhinoCommonCommandinherits from
Command…
public class HelloRhinoCommonCommand : Command
- If you hover over
Commandyou will notice this is actually
Rhino.Commands.Command.
HelloRhinoCommonCommandalso uses IntelliSense to create an auto-complete list of members to call. Now, feed
line1as an argument to the
doc.Objects.AddLinemethod…
doc.Objects.AddLine (line1);
- Now that we have a line of our own, let’s examine it…
Debugging
- Set a breakpoint on line1 67 of HelloRhinoCommonCommand.cs. You set breakpoints in Visual Studio by clicking in the gutter…
- Build and Run. Because you have changed the name of the command, you will need to “install” the plugin again. Follow the steps 3-5 in the Boilerplate Build section above.
- Run HelloDrawLine in Rhino. Create the two points…as soon as you do, you should hit your breakpoint and pause…
- With Rhino paused, in Visual Studio switch to the Autos tab (if it not already there). In the list, find the
line1object we authored. Click the dropdown arrow to expand the list of members on
line1. Our
line1is a
Rhino.Geometry.Linethis class has a
Lengthproperty…
- Continue in Rhino by pressing the Continue button in the upper menu of Visual Studio (or press F5)…
- Control is passed back to Rhino and your command finishes. Exit);
- Save, Build, and Run. Run
HelloDrawLinein Rhino yet again (create the two points…). Rhino now reports the length of the line you created. However, this is not very clean.
- Exit Rhino to Stop the debugging session once more.
Let’s add a unit system and be explicit about what we’re reporting…
RhinoApp.WriteLine ("The distance between the two points is {0} {1}.", line1.Length, doc.ModelUnitSystem.ToString().ToLower());
- Save, Windows. Now what?
Next Steps
You’re using RhinoCommon, so this plugin will actually run on both platforms. Check out the Your First Plugin (Cross Platform) guide.