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();
//add to document
GH_Document GrasshopperDocument = this.OnPingDocument();
var rtnval = cs.Params.Output.VolatileData.get_Branch(0);
//remove that 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
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:
So for the Circle CNR component, I would find it by referencing the Curve.gha file.
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.