Adding a button to Canvas toolbar?

Units

3 Likes

nice and how does convert *.(png){icon} O to FromBase64String( o)? var o =""code ?

i find solution

byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
1 Like

When you convert it copy the result to your code to avoid using image file

1 Like

3 Likes

Really really cool. Can I also add the same button to my canvas? if possible can you share the code? currently, I am learning c# and find this UI modification thing really cool so I wanted to learn this and want to add one extra button that zoom selected geometry button beside the solver.
image

I don’t Know if there is zoom selected geometry in Grasshopper SDK.
Maybe we need first find selected geometries and use their ids to zoom selected in Rhino.
If i find a way i will add it as a button, and sorry i can’t share c# code, python code is enough you can use it.

1 Like

sure sure no problem

but where can I find python code ??

maybe ill try to write that code in C#.

Python code, you already used it :slight_smile:

1 Like

hi @seghierkhaled and @Jay_vayeda
I managed to write python code but my try in c# code not successfully
Maybee @Mahdiyar can help for translation this code from python to ,C# Code

Python succeed +c# unsuccessfully code::point_down:

icon-- bar.zip (26.9 KB)

1 Like
private void RunScript(bool addBtns, bool removeBtns, List<string> icons)
{
  var toolbar = (System.Windows.Forms.ToolStrip) (Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]);
  if (addBtns)
  {
    var button = new ToolStripButton("Enable/Disable", Bitmap.FromFile(icons[0]), (sender, e) => Instances.ActiveCanvas.Document.ToggleEnabledFlags(), "Enable/Disable");
    button.Name = "Enable/Disable";
    button.CheckOnClick = false;
    button.Checked = true;
    button.DisplayStyle = ToolStripItemDisplayStyle.Image;
    toolbar.Items.Add(button);
  }
  if (removeBtns)
  {
    var buttons = new List<ToolStripItem>();
    foreach(ToolStripItem item in toolbar.Items)
      if (item.Name == "Enable/Disable")
        buttons.Add(item);
    foreach(var btn in buttons)
      toolbar.Items.Remove(btn);
  }
}

AddBtns.gh (3.3 KB)

5 Likes

After researching your famous solution on the loading process, I found the GH_AssemblyPriority class:

And indeed, it is really simpler.

2 Likes

nice @Mahdiyar, could you please help me to implement the Delete button? thank you!

sc

private void RunScript(bool addBtns, bool removeBtns, List<string> icons)
{
  var toolbar = (System.Windows.Forms.ToolStrip) (Grasshopper.Instances.DocumentEditor.Controls[0].Controls[1]);
  if (addBtns)
  {
    var button = new ToolStripButton("Delete", Bitmap.FromFile(icons[0]), onClick, "Delete");
    button.Name = "Delete";
    button.DisplayStyle = ToolStripItemDisplayStyle.Image;
    toolbar.Items.Add(button);
  }
  if (removeBtns)
  {
    var buttons = new List<ToolStripItem>();
    foreach(ToolStripItem item in toolbar.Items)
      if (item.Name == "Delete")
        buttons.Add(item);
    foreach(var btn in buttons)
      toolbar.Items.Remove(btn);
  }
}
// <Custom additional code> 
private void onClick(object sender, EventArgs e)
{
  GrasshopperDocument.UndoUtil.RecordRemoveObjectEvent("Delete", GrasshopperDocument.SelectedObjects());
  GrasshopperDocument.RemoveSelection(true);
}
// </Custom additional code>

AddBtns.gh (2.8 KB)

3 Likes
if (addBtns)
{
  var button = new ToolStripButton("InvertSelect", Bitmap.FromFile(icons[1]), (sender, e) => Instances.ActiveCanvas.Document.InvertSelection(),"InvertSelect");  // this is your code but I changed on function, because I am exproling sometings.
  button.Name = "InvertSelect";
  button.CheckOnClick = false;
  button.Checked = true;
  button.DisplayStyle = ToolStripItemDisplayStyle.Image;
  toolbar.Items.Add(button);
}

if (removeBtns)
{
  var buttons = new List<ToolStripItem>();
  foreach (ToolStripItem item in toolbar.Items)
    if (item.Name == "InvertSelect")
      buttons.Add(item);
  foreach (var btn in buttons)
    toolbar.Items.Remove(btn);
}

hello sir, @Mahdiyar really cool code really liked it

I just modify it with a

GH_Document.InvertSelection Method

but i am having a problem adding

GH_Document.Enabled Property

and

GH_Document.EnableSolutions Property

these two properly in the code.

actually, I am new to learning C# so I am not able to add this.

Because of this code I learn how to assign methods in code but did not have an idea of how to add and modify properties.

if possible can you guide me with this ??

Thank you

  • Jay

hi @Mahdiyar and if help for only which Api for Group and UnGroup Component to add bottom?

image

3 Likes

Very Very thank you for the Plugin.

but I am very curious about how to assign Property in C# code in that existing code. if possible can you tell me ?.

Check this post

1 Like

Hi, for auto loading button see this post.

Ondřej

1 Like