GrasshopperDocument and Component variables in Grasshopper from C# component to Visual Studio

In the C# component for Grasshopper, it is easy to perform functions on either itself or on the document you are in. For example:

Component.Params.Input[0].SourceCount;

…will give you the number of things connected to the first input of the particular C# component containing that line of code.

This is made possible by some variables that are pre-defined within all C# components. Start a new C# component and scroll to lines 49-61 and you should see:

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

This is all well and good if you want to use the C# component, but what if you’re developing something a bit more meaty, and want to move over to Visual Studio?

I often use the GrasshopperDocument and Component properties in particular, and wanted to be able to use these in VS.

Can’t I just call GrasshopperDocument and Component in Visual Studio?

Within the C# component, some magic goes on that gives these variables their abilities. So copy and paste of the above variables isn’t going to work.

Instead, what we need to do is create our own variables with the same names as above, and manually give them their functionality.

How to set up GrasshopperDocument and Component

Like pretty much everything on this blog, I write about stuff as I learn it, and there may be a better way to do things! But the method below works for me.

Open or start a new Grasshopper component project as usual. (See here for setting up.)

At the very top of your class, create some class variables:

GH_Document GrasshopperDocument;
IGH_Component Component;

Then, within Solve_Instance, we can give meaning to these variables:

Component = this;
GrasshopperDocument = this.OnPingDocument();

Or alternatively,

GrasshopperDocument = Instances.ActiveCanvas.Document;

So, your whole component class should look like:

    public class GHtestComponent : GH_Component
    {
        GH_Document GrasshopperDocument;
        IGH_Component Component;

        public GHtestComponent()
            : base("GH test", "Nickname",
                "Description",
                "Category", "Subcategory")
        {
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Component = this;
            GrasshopperDocument = this.OnPingDocument();
        }

        protected override System.Drawing.Bitmap Icon
        { //..
        }

        public override Guid ComponentGuid
        { //..
        }
    }

Notes

According to this GH forum post, it is possible for the OnPingDocument() to return null. I haven’t explored further on when this might happen, but you may wish to wrap this line in an ‘if’ or a ‘try catch’.

It seems unnecessary to me to have to assign values to the variables in the Solve_Instance, but this is a way I have working. I couldn’t get it to work when I put it in the constructor.

I still haven’t found the equivalent line for RhinoDoc, mostly because I haven’t needed to. When the time comes for me to figure it out (or if you happen to know yourself and would like to share it with the world!) I’ll update this page.

Update: RhinoDoc

Accessing the RhinoDoc variable is actually really easy – at the top of your C# file, just add a ‘using’:

using Rhino;

Then you can use the RhinoDoc variable like you would use it in the C# component. Simple!

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.