Make toolbar icon using code

I wonder it is possible to make simple pixel toolbar icon using code or other way

All of my extensions have toolbar icon(png file) path /Plugins folder

And my Plugins folder seems too complicated

I don’t want to see png file in my Plugin folder

Is there a way to union rb file and png file?

On Mac you can also use a vector PDF, and SVG vector format on Windows, as well as PNG. But I’m pretty sure there’s no way to ‘draw’ them on the fly in Ruby.

But they still go in a subfolder of your extension’s folder - by default, though not by necessity, in …/Sketchup/Plugins/YourPluginName/Images.

2 Likes

Each one of your extensions MUST be in it’s OWN SUBFOLDER.
The extension’s files (.rb, .png, .svg, etc.,) do NOT belong in the “Plugins” folder itself.

Read about the SketchupExtension class.

Yes it can be done with code, however it still must be written to a diskfile because that is what the API uses to load into memory. So you won’t gain anything, and you will just slow down the SketchUp load cycle.

So, put your extension’s and their resource files in proper extension subfolders.

ONLY the extension’s registrar file goes in the “Plugins” folder.

1 Like

Can I put two extensions(makebox1.rb, makebox2.rb and two png files) into one subfolder?

image

YES. We do this all the time. * CORRECTED in next post.

Your PNG files should have a .png file extension.

Each of your .rb files should open your submodule for editing (using Ruby module blocks.)
Ie …


Your extension’s registrar file In the "Plugins" folder …
Must be same name as the extension subfolder but with a .rb file extension !

# encoding: UTF-8
# "Plugins/Agent47_BoxMaker.rb"
#
module Agent47
  module BoxMaker

    EXTENSION ||= SketchupExtension.new(
      "BoxMaker", "Agent47_BoxMaker/makebox_loader"
    ).instance_eval do
      # self is the instance object within this block:
      self.name= "Agent47's Nifty BoxMaker"
      self.description= "Allows the creation of various nifty boxes."
      self.version= '1.0.0'
      self.creator= "Agent47"
      self.copyright= "©2020 by author, All Rights Reserved."
      # Return the new object to get assigned to the constant EXTENSION
      self
    end

    Sketchup.register_extension(EXTENSION)

  end # close extension submodule
end # close author namespace module

* Changed the SketchupExtension constructor call’s path argument to omit the .rb file extension (as the documentation advises,) so that the loader file can be scrambled or RBE encrypted.


In your extension "Agent47_BoxMaker" subfolder …

# encoding: UTF-8
# "Plugins/Agent47_BoxMaker/makebox_loader.rb"
#
module Agent47
  module BoxMaker

    ['makebox1','makebox2','makebox_menu'].each do |rb_file|
      Sketchup.require( File.join(__dir__, rb_file) )
    end

  end # close extension submodule
end # close author namespace module

* NOTE: The global __dir__ method returns the full path to where the current evaluating file is located on disk. The File::join method concatenates parts of pathnames using the File::SEPARATOR character.

# encoding: UTF-8
# "Plugins/Agent47_BoxMaker/makebox1.rb"
#
module Agent47
  module BoxMaker

    def make_box_1
      # box making code
    end

  end # close extension submodule
end # close author namespace module
# encoding: UTF-8
# "Plugins/Agent47_BoxMaker/makebox2.rb"
module Agent47
  module BoxMaker

    def make_box_2
      # box making code
    end

  end # close extension submodule
end # close author namespace module
# encoding: UTF-8
# "Plugins/Agent47_BoxMaker/makebox_menu.rb"
#
module Agent47
  module BoxMaker

    extend self

    if !@loaded # Run only on startup
      @loaded = true

      # Define Commands, Menu Items and Toolbars here
      CMD ||= {}
      CMD[:one]= UI::Command.new("Make Box Type 1") { makebox1() }
      CMD[:two]= UI::Command.new("Make Box Type 2") { makebox2() }

      submenu = UI.menu("Plugins").add_submenu("Agent47")
      submenu.add_item(CMD[:one])
      submenu.add_item(CMD[:two])

    end # Run only on startup block

  end # close extension submodule
end # close author namespace module

So, an author can split their extension code up into as many files as they wish to help with code maintenance.

Sorry I misread what you said. I thought you meant 2 files for the same extension.
In Ruby (because it is a dynamic language) we can open an object multiple times, in multiple files, and modify the object. Modules and Classes are objects in Ruby, and can be reopened using the interpretive module and class blocks.

BUT NO each extension must have it’s own registrar file in the "Plugins" folder, and it’s OWN subfolder that is the same name as the registrar file.

The example I show above is for one extension that can draw two kinds of boxes.

To further clarify: this is not a matter of Ruby, it is because of how SketchUp manages extensions.

1 Like

As I understand it, to union two extension rb files [boxmaker1.rb , boxmaker2.rb] in one subfolder
It need
[one subfolder, one load rb file, main.rb or two rb files, susug file, two icons]
Is it right?

I have two more questions…

  1. To make a subfolder, Should it must be registered?

  2. How to make a susig file[pic 3]?

20200807_230419 20200807_230741 20200807_230832