Run Grasshopper file in Rhinocommon,

I have a sample grasshopper file witch is attached
test1.gh (9.6 KB)
I wrote this to load the file in the background, give it inputs and add the output to the RhinoDoc Or get the geometrybase.

using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Grasshopper.Kernel;
using GH_IO.Serialization;
using Grasshopper.Kernel.Special;
using Grasshopper.Kernel.Types;
using System.Linq;

namespace GHP_test1
{
    public class GHP_testCommand : Command
    {
        public GHP_testCommand()
        {
            Instance = this;
        }

        public static GHP_testCommand Instance { get; private set; }
        public override string EnglishName => "GHP_testCommand";

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            var ghDoc = new GH_Document();
            string path = @"D:\BOND\test\GHP_test1\test1.gh";

            GH_Archive archive = new GH_Archive();
            if (!archive.ReadFromFile(path))
            {
                RhinoApp.WriteLine("Failed to load Grasshopper file.");
                return Result.Failure;
            }

            if (!archive.ExtractObject(ghDoc, "Definition"))
            {
                RhinoApp.WriteLine("Failed to extract Grasshopper definition.");
                return Result.Failure;
            }

            // Modify input values
            foreach (var input in ghDoc.Objects.OfType<GH_ClusterInputHook>())
            {
                if (input.Name == "Length")
                {
                    input.ClearData(); // Clear existing data
                    input.AddVolatileData(new Grasshopper.Kernel.Data.GH_Path(0), 0, new GH_Number(5.0));
                }
                else if (input.Name == "Width")
                {
                    input.ClearData(); // Clear existing data
                    input.AddVolatileData(new Grasshopper.Kernel.Data.GH_Path(0), 0, new GH_Number(10.0));
                }
            }

            // Solve the Grasshopper definition
            ghDoc.NewSolution(true); // true means the solution will be recomputed

            // Check output values
            foreach (var output in ghDoc.Objects.OfType<GH_ClusterOutputHook>())
            {
                if (output.Name == "Rec")
                {
                    var dataTree = output.VolatileData;
                    RhinoApp.WriteLine("tree: " + dataTree.DataCount);
                }
            }

            doc.Views.Redraw();
            return Result.Success;
        }
    }
}

the problem is the output datatree is empty.

i really need this. does anyone has any ideas?

@dale
sorry to mention you, but its so important for me.
would you please check it?
thanks.

Can i Use GrasshopperPlayer in my rhinoPlugin to achieve that?

You might look at this link - https://james-ramsden.com/run-a-grasshopper-component-from-within-your-c-code/. If(output.Name == “Rec”) is always null, then go directly to the index. Per the Ramsden example - referenceGHComponent.params.Output[index number where “Rec” is located]. Just a guess.

1 Like

thanks for the reply, I checked the link and the website you sent me, but that was a bit diffrent from what i wanted, and since I’m not familiar with Grasshopper SDK, i can’t change it the way i want ,so I would be very thankful if you could give me a simple example of the rhino plugin that loads the file in the background, passes data to its inputs and gets the geometrybase of the outputs.
thanks a lot.

BTW, Can I use GrasshopperPlayer for that? if yes, how?

Unfortunately, what I think you are asking, namely a C# Rhino plugin, that can use a Grasshopper component to accept inputs and collect outputs, is kind of a holy grail. I have made a C# GH component that collects output data from a ‘standard’ GH component and then used that data in a referenced C# dll. But, this was all within the GH context. With the help of forum commenters, I originally got the idea from the Ransden post. Based on my limited experience, I can not whip up some prototype code. Both, a Rhino plugin, and, a Grasshopper regular or custom component, use Rhinocommon, but each is its own ‘island’ for code development. If you can find an example of a Rhino plugin using a GH component as a computational black box, let the forum know.

thanks a lot David for sharing your experience ,
I’ll seek more and surly will share the result if I achieved anything.