Grasshopper component for calculating sun direction written in C#, being called from a second test component

Run a Grasshopper component from within your C# code

How to call and calculate a Grasshopper component from within some C# code, and read and return the component outputs.

A Grasshopper component is essentially a visual interpretation of a method in programming. It has inputs, it does calculations, and it produces outputs. Grasshopper components are also saved within DLL libraries, and can be referenced. So, surely, it’s possible to access a component programatically from within something like a Visual Studio project? This is a quick first attempt to do so.

The code below attempts to read one of my own components (“CalcSunDirection”). The component runs with all inputs at default values, and then returns the first data item within output 2 (the third one in human-speak). This is copy-pasted from the Solve_Instance method when creating Grasshopper components in Visual Studio.

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            //Create the component and run calculations
            var cs = new MyComponents.Components.CalcSunDirection();
            cs.CreateAttributes();
            cs.ExpireSolution(true);

            //add to document
            GH_Document GrasshopperDocument = this.OnPingDocument();
            GrasshopperDocument.AddObject(cs, false);

            //read output[2]
            cs.Params.Output[2].CollectData();
            var rtnval = cs.Params.Output[2].VolatileData.get_Branch(0)[0];

            //remove that component
            GrasshopperDocument.RemoveObject(cs.Attributes, false);

            //send output
            DA.SetData(0, rtnval);
        }

Grasshopper component for calculating sun direction written in C#, being called from a second test component

As we can see, the test component produces the same output as the CalcSunDirection component it is referencing. The above image is for comparison – in reality, the user wouldn’t see the CalcSunDirection, only the Test component.

I believe it is hypothetically possible to do this trick with any component, assuming that the DLL/GHA file containing the component you want to use is referenced in your Visual Studio project.

I tried to do this with some native Grasshopper components (such as the circle from CNR component). I looked up the namespace of the component using this tool, but I had trouble locating the DLL containing the component, so I was unable to add it to my VS project. (For example, the Circle CNR component has namespace CurveComponents.Component_CircleCNR, which is not within Grasshopper.dll. Anyone have any idea where I can find it?) Edit – found them! See below…

As a first attempt, the code above also seems inefficient in that you have to physically add the component before the outputs become readable. I get around this from a user experience point of view by removing that component after, but computationally, it still feels heavy and unnecessary.

Upon reflection, what I’ve essentially created is a programmatic version of the clustering feature of Grasshopper. Is this the best way to go about it? If you have any interesting suggestions, please let me know 🙂

Update – geometry GHA files

Many thanks to Andrew Heumann for answering my question above – many of the components are not contained within Grasshopper.dll but are saved elsewhere.

It appears that since version 0.8.0012 or so, they have been kept buried within the Program Files. On various computers, I’ve found them at:

C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper\0.9.76.0\Components

C:\Users\jrams\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper\0.9.76.0\Components

If you are struggling to find them, and you don’t mind giving third party software direct access to your computer’s Master File Table (!), then UltraSearch is the fastest way to find the GHA files.

Within this folder is a collection of GHA files (which is essentially a DLL with the extension changed).

The full list is:

  • Curve.gha
  • Field.gha
  • Galapagos.dll
  • GalapagosLibrary.gha
  • IOLibrary.gha
  • LegacyScript.gha
  • Mathematics.gha
  • Script.gha
  • Surface.gha
  • Transform.gha
  • Triangulation.gha
  • Vector.gha

Grasshopper GHA extension files

So for the Circle CNR component, I would find it by referencing the Curve.gha file.

Update 2

See this post for updated code. This updated code allows you to create a dummy document, so you don’t need to paste components on the live document.

7 thoughts on “Run a Grasshopper component from within your C# code”

  1. Hi James,
    Been scratching my head over this for a few hours now until Andrew Heumann pointed me to this post, great workaround to access Grasshopper components.
    One thing that is not clear is how to add .gha files as reference libraries in visual studio? I tried adding references in one of my existing projects but VS wouldn’t have it.
    Thanks again for sharing
    Paul

    1. Hi Yijiang,
      I’ve just had a go myself. If I change the extension on the gha file back to a dll, then I can use it as a reference in a VS project. For some reason, the namespace doesn’t appear in Intellisense, but it does compile if I try to use it anyway. (I tested on VS2013.)

  2. Hi James,
    Thank you for your replying. But can you elaborate more on “change the extension on the gha file back to a dll”? I don’t understand really.
    Yijiang

    1. Ah 🙂 You know how Grasshopper extension files are called something.gha? In Windows Explorer, simply rename the file to something.dll. (You might need to make extensions visible to do this. On the Win10 ribbon, I can do this by going to Windows Explorer, then View, then ticking ‘File Name Extensions’.)

      This works essentially because a gha file is just a dll file with the extension changed. In other words, a gha file is a dll for Grasshopper. But Visual Studio doesn’t know this, and it just thinks that a gha file is something weird, so it ignores it. I think…

  3. Hi James,
    I just want to follow up for your hard time to locate Mathematics.gha etc.

    I find my life much easier with this tool on Windows: http://www.listary.com/

    It enables me to find files and apps in a really fast way and it takes me no time to locate the intial grasshopper gha files.

    Hope you enjoy this!

    Best,
    Yijiang

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: