Adding a button to Canvas toolbar?

Hello, i want to add button “disable/enable solver” , which loaded automatically when Grasshopper started.
How i can fix this code to make it work?

using Grasshopper;
using Grasshopper.Kernel;

namespace DisableSolver
{
    public class DisableSol
    {

        public static void CustomizeUI()
        {

            if (Instances.DocumentEditor.Controls[0].Controls[1] is System.Windows.Forms.ToolStrip toolbar)
            {
                var SolverButton = new System.Windows.Forms.ToolStripButton("Disable")
                {
                    ToolTipText = "Disable Solver"
                };
                SolverButton.Click += (s, e) =>
                {
                    if (GH_Document.EnableSolutions == true)
                    {
                        if (SolverButton.ToolTipText == "Disable Solver")
                        {
                            GH_Document.EnableSolutions = false; 
                        }
                    }
                    else if (GH_Document.EnableSolutions == false)
                    {
                        if (SolverButton.ToolTipText == "Disable Solver")
                        { 
                            GH_Document.EnableSolutions = true; 
                        }
                    }
                };
                toolbar.Items.Add(SolverButton);

            }
        }
    }
}

Hello,

At first, you can access the toolbar with Grasshopper.Instances.DocumentEditor.MainMenuStrip.
And your method should be called in Grasshopper.Instances.CanvasCreated event. This is to ensure that all UI objects have been created.

jmv

1 Like

I just saw your previous message. Automatically add buttons to the toolbar when GHA is loaded? - #4 by seghierkhaled
If it’s the same question, my answer is wrong and I don’t know.
The Grasshopper.Instances.DocumentEditor.MainMenuStrip provides access to the top level toolbar:
image

1 Like

Hi @kitjmv
Thank you, what i want is a permanent button
At this moment i use python script and need place it every time to add the button to the toolbar.
I see in rhino.inside-bricscaf source code ,they call Grasshopper as command with code of the button.
I don’t know how to call the above script when we click Grasshopper button in Rhino.

Thanks again, i just want permanent button
I don’t need the menu

Sorry I do not understand.
Maybe a image is better…

Fixed button like other buttons in the ttolbar

I assume you are using this model:

Inside a Rhino plugin:

public class Plugin_Rhino : PlugIn
{
     public Plugin_Rhino () {
          RhinoApp.Initialized += OnRhinoAppInitialized;
     }

     void OnRhinoAppInitialized (object sender, EventArgs e) {
          RhinoApp.Initialized -= OnRhinoAppInitialized;
          Grasshopper.Instances.CanvasCreated += CreateGrasshopperUI;
     }

    void CreateGrasshopperUI (GH_Canvas canvas) {
          Grasshopper.Instances.CanvasCreated -= CreateGrasshopperUI;
         ...
    }
}

Exactly
Looks like i need to create a dll with the second code?
CreateGrasshopperUI …

I don’t know what you call “second code” but you must have a Rhino plugin (like my example to initialize CanvasCreated event) and the Grasshopper plugin


(CreateGrasshopperUI is the name I chose)

Thank you , i will try it

Note that your Rhino plugin can reference your Grasshopper project and your CreateGrasshopperUI method can be placed in the GH plugin:

void OnRhinoAppInitialized (object sender, EventArgs e) {
          RhinoApp.Initialized -= OnRhinoAppInitialized;
          Grasshopper.Instances.CanvasCreated += MyGrasshopperPluginClass.CreateGrasshopperUI;
 }

I don’t understand why we need Rhino plugin (rhp)?
I see many Grasshopper addon have unique buttons loaded automatically.

It uses a private grasshopper function LoadGHA:

      var LoadGHAProc = Grasshopper.Instances.ComponentServer.GetType().GetMethod("LoadGHA", BindingFlags.NonPublic | BindingFlags.Instance);

To be honest, if you don’t understand the Grasshopper loading process, use two plugins.

jmv

1 Like

That said, I really don’t understand.
If you have a Grasshopper loaded, just call the function that creates your menu.
in your first post, an explanation of the error would be helpful

Calling the function is not the problem.
When i run the code, the button added immediately to the toolbar.
But i want the button appears automatically when Grasshopper opened, which mean -maybe- autorun the function.

Finally it works, i find a solution after checking PersistentDataEditor

4 Likes

Disable/Enable Solver, Undo, Redo

image

4 Likes

Hi @seghierkhaled can you created it icon by c# or python? Can you shearing it?

Hi @Rh-3d-p

Check this with python, you can do the same with c#
add remove button.gh (4.9 KB)

2 Likes