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.