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);
}
}
}
}
kitjmv
(jmv)
July 30, 2022, 9:10am
2
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
2 Likes
kitjmv
(jmv)
July 30, 2022, 9:19am
3
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:
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
kitjmv
(jmv)
July 30, 2022, 11:39am
6
Seghier khaled:
permanent button
Sorry I do not understand.
Maybe a image is better…
Fixed button like other buttons in the ttolbar
kitjmv
(jmv)
July 30, 2022, 11:55am
8
I assume you are using this model:
try
{
System.Diagnostics.Process.Start(@Path.Combine(GhBcConnection.DllPath, "Samples"));
}
catch (System.Exception) { }
};
specFold.DropDownItems.Add(samplesItem);
}
}
if (Instances.DocumentEditor.Controls[0].Controls[1] is ToolStrip toolbar)
{
var linkButton = new ToolStripButton(Properties.Resources.link)
{
ToolTipText = "Link with current BricsCAD document"
};
linkButton.Click += (s, e) =>
{
if (System.Convert.ToInt16(_BcAp.Application.GetSystemVariable("DWGTITLED")) == 0)
MessageBox.Show("Bricscad drawing must be saved before using Grasshopper");
else
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 …
kitjmv
(jmv)
July 30, 2022, 12:07pm
10
Seghier khaled:
second code?
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
kitjmv
(jmv)
July 30, 2022, 12:55pm
12
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.
kitjmv
(jmv)
July 30, 2022, 3:04pm
14
It uses a private grasshopper function LoadGHA
:
var LoadGHAProc = Grasshopper.Instances.ComponentServer.GetType().GetMethod("LoadGHA", BindingFlags.NonPublic | BindingFlags.Instance);
return false;
}
}
return true;
}
public static bool LoadGrasshopperComponents()
{
if (_grasshopperLoaded)
return true;
var LoadGHAProc = Grasshopper.Instances.ComponentServer.GetType().GetMethod("LoadGHA", BindingFlags.NonPublic | BindingFlags.Instance);
if (LoadGHAProc == null)
return false;
var bCoff = Grasshopper.Instances.Settings.GetValue("Assemblies:COFF", true);
Grasshopper.Instances.Settings.SetValue("Assemblies:COFF", false);
var rc = (bool) LoadGHAProc.Invoke
(
Grasshopper.Instances.ComponentServer,
new object[] { new Grasshopper.Kernel.GH_ExternalFile(Assembly.GetExecutingAssembly().Location), false }
To be honest, if you don’t understand the Grasshopper loading process, use two plugins.
jmv
1 Like
kitjmv
(jmv)
July 30, 2022, 3:09pm
15
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
4 Likes
Rh-3d-p
(R-d)
July 31, 2022, 9:31pm
19
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