Adding data in Grasshopper Panel using C#

Hello Rhino Community
I am trying to access the grasshopper panel by writing a c# script. in the script below i am able to open the grasshopper file and access the IGH_DocumentObject objects in it.

I need help in knowing the best way to find a specific panel and also changing its value


public class GH_ABC
{
    public static Result RunGHDef()
    {
        Object grasshopper = RhinoApp.GetPlugInObject("Grasshopper");
        if (grasshopper == null)
            return Result.Failure;

        grasshopper.GetType().GetMethod("DisableSolver").Invoke(grasshopper, null);

        string filePath = @"C:\Project_Folder\fksm\rhino_grasshopper\grasshopper_template";
        grasshopper.GetType().GetMethod("OpenDocument").Invoke(grasshopper, new object[] { filePath });

        grasshopper.GetType().GetMethod("ShowEditor").Invoke(grasshopper, null);

        return Result.Success;
    }

    GH_Document ghDoc = Grasshopper.Instances.ActiveCanvas.Document;

    private static IGH_DocumentObject FindPanelComponentByNickname(GH_Document ghDoc, string nickname)
    {
        foreach (IGH_ActiveObject obj in ghDoc.ActiveObjects())
        {
            if (obj.NickName == "Project ID")
            {
                // code here to change the value of the panel with nickname  "Project ID"
                return obj;
            }
        }
        return null;
    }
}

Hello,

You first need to cast your IGH_DocumentObject to Grasshopper.Kernel.Special.GH_Panel.
Then use the SetUserText method.

1 Like

Hello ,
Thanks a lot the tip worked

if (obj is GH_Panel panel)
                    {
                        panel.UserText = panelValue; // Replace "New Value" with the desired new value
                        ghDoc.NewSolution(true);
                        Rhino.RhinoApp.WriteLine(panelName + "Panel value changed to" + panelValue);
                    }

quick followup question , if i wanted to access a GH_Button how would i go about setting the value to trigger??

Grasshopper.Kernel.Special.GH_ButtonObject ghbo = obj as Grasshopper.Kernel.Special.GH_ButtonObject;

    ghbo.ExpressionNormal = strNormal;
    ghbo.ExpressionPressed = strPressed;