Automatically add buttons to the toolbar when GHA is loaded?

How to automatically add buttons to the toolbar when gha is loaded?
I need to add two buttons to the toolbar. How should I write them in the gha project?
image

This is a button added after GH is loaded, and it needs a GH_Commonent to be inserted into the current GHDocument. How to add it to the GHA project? Is it automatically added to the toolbar after loading the GHA file?

I have the same question?

Hello guys,

I went over this wire and this one:
Adding a button to Canvas toolbar? - Grasshopper Developer - McNeel Forum

Adding buttons to the canvas is what interested me as well. Putting together my “coding skill”, both wires and code from this:

rhino.inside-bricscad/GhUI.cs at master · Bricsys/rhino.inside-bricscad · GitHub

I can automatically add a button to the canvas as the Grasshopper is loaded.

I use Visual Studio to create my plugin. There is class which inherits from GH_AssemblyPrioroty (normally used for checking valid license)

image

This class has PriorityLoad() method which is called before plugin is loaded.

Code looks like this:

   public override GH_LoadingInstruction PriorityLoad()
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        if (LicensedUsers.ContainsKey(userName))
        {
            //Adding event, when Canvas is Created (this must be done with Canvas not DocumentEditor (otherwise DocumentEditor is null). )
            Instances.CanvasCreated += Instances_CanvasCreated;
            return (GH_LoadingInstruction.Proceed);
        }
        else
        {
            return (GH_LoadingInstruction.Abort);
        }
    }
    private void Instances_CanvasCreated(GH_Canvas canvas)
    {
        //Copied from rhino.inside-bricscad plugin
        Instances.CanvasCreated -= Instances_CanvasCreated;
        GH_DocumentEditor editor = Instances.DocumentEditor;
        //Check if DocumentEditor was instantiated
        if (editor == null)
        {

            return;
        }

        //Finally buttons is added to the Controls
        var toolbar = Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1] as ToolStrip;
        var button = new System.Windows.Forms.ToolStripButton("Hi from Ondřej");
        toolbar.Items.Add(button);
    }

And result like this:

Hope it helps.

Ondřej

4 Likes

Hi, can I ask you a question? What are LicensedUsers? Why can’t I write this dictionary.help me !!,thank you very much.

It is my own Dictionary with licenses. You should modify that part or delete If condition.

Ondřej


Hi, I have another question. How can I add a button to the top menu bar

Hello @Bynn

I use a method which is called inside a class which inherits from “GH_AssemblyPriority”. Example bellow.

       private void Instances_CanvasCreated(GH_Canvas canvas)
       {
           Instances.CanvasCreated -= Instances_CanvasCreated;
           GH_DocumentEditor editor = Instances.DocumentEditor;
           if (editor == null)
           {
               return;
           }
           else
           {
               MenuStrip toolbar = editor.MainMenuStrip;
               toolbar.Items.Add(new EndPoints_Menu());
           }
       }

EndPoints_Menu is a class which inherits from “ToolStripMenuItem”.

Hope it helps.

Ondrej