How to assign Components to toolbar icons?

Hi i would like to ask if someone knows how to assign exact component from my local library to an toolbar icon created with toolbar editor.
I dont know Ruby and so therefor i would like to ask for code that i could just copy paste and change the path to component when i need to create next icon with next component assigned to it.
Iam trying to create my library of most used components to be as toolbar so i just click the icon of needed component to get it .
Thanks a lot .

This is quite easy to achieve with a small Ruby snippet.

PATH = "C:/Some/Location/Your/File/Is/Located/At.skp"

def load_component(path)
  model = Sketchup.active_model
  definiton = model.definitions.load(path)
  model.place_component(definition)
end

command = UI::Command.new("Add Component") { load_component(PATH) }
command.tooltip = "Add Component"
command.status_bar_text = "Add a hard-coded component to model."
###command.small_icon = command.large_icon = something

toolbar = UI::Toolbar.new("My Script")
toolbar.add_item(command)
toolbar.restore

The question is why doing this. This aproach scales very badly. If more and more components are added as separate buttons, the toolbar gets wider and wider. The Component browser already handles the general case of inserting components from libraries.

3 Likes

You might wanna have a look at the toolbar editor, too(l):

https://extensions.sketchup.com/nl/content/toolbar-editor

:

thanks @eneroth3 for the quick respond. I am planning to have there just about 10 dynamic components. I will give it a try. might be able to paste the code to toolbar editor and wait what happens. :blush:

thanks @MikeWayzovski that’s exactly what I am trying to use. just don’t know the coding that I could drop in the tool.
unfortunately there is no place in Slovakia I could get help learning ruby. and just by tuts in English i cannot get it in my head.

10 icons is a huge toolbar. Even a full fledge BIM toolset with separate tools for drawing slabs, walls, doors, windows and the like could have a smaller toolbar.

hi @eneroth3,
thanks for the code you shared with me.
when i place it to toolbar editor tool it doesn’t load component but it creates another tool bar “My script” but no component at all.
one of the things I could think about (with zero knowledge of RUBY) is that my component is placed at USB disk under F:
the guesting is if it doesn’t need to be at hArd disk c.

Change PATH into the path of the component file.

i did as first.

Dňa pi 14. 6. 2019, 11:48 Julia Christina Eneroth via SketchUp Forum sketchup@discoursemail.com napísal(a):

Hi @eneroth3 and everyone ! as it seems that you don’t like it … sorry :wink: … but it’s exactly what i’m searching to do …

I’ve copied/paste the text on a .rb file …
I’ve changed just the PATH …

When launching SU, i’ve the new toolbar MyScript … but no component like pupoksveta :frowning: … do you have an idea ? what could be the reason ?

Thanks
David

Give me a second i will send you working path.

@djoseph here you go
S1D1V=
Sketchup.active_model.definitions.load(“D:/your directory/your directory/S1D1V.skp”)
Sketchup.active_model.place_component(S1D1V)

that is working 100% for me and it is cool fast importing component just by click.

uh ? @pupoksveta

My file.rb is like this … what must i change so that it works 100% for me too :wink:
and have you done you ruby with several component ?

PATH = “C:\Component\box.skp”

def load_component(path)
model = Sketchup.active_model
definiton = model.definitions.load(path)
model.place_component(definition)
end

command = UI::Command.new(“Add Component”) { load_component(PATH) }
command.tooltip = “Add Component”
command.status_bar_text = “Add a hard-coded component to model.”
###command.small_icon = command.large_icon = something

toolbar = UI::Toolbar.new(“My Script”)
toolbar.add_item(command)
toolbar.restore

… what’s wrong ?
Thanks pupoksveta for your answer ! :slight_smile:
David

The main reason is that the reference assignment misspells “definition” as “definiton

    def load_component(path)
      model = Sketchup.active_model
      definiton = model.definitions.load(path) # <-- misspelled reference
      model.place_component(definition) # <-- correct spelled reference
    end

HOWEVER…

The code should be inside a module with a unique name. Not at the toplevel of Ruby’s Objectspace.

This edition is properly module wrapped, and will make buttons for ALL the components in the PATH directory.

module ChangeToYourUniqueNamespace
  module LoadComponentButtons

    extend self

    PATH ||= "C:/Some/Location/Your/File/Is/Located/At"
    TOOLBAR_NAME ||= "Place Component"

    def load_component(path)
      model = Sketchup.active_model
      definition = model.definitions.load(path)
      model.place_component(definition)
    end

    if !@loaded # run once
      @loaded = true

      toolbar = UI::Toolbar.new(TOOLBAR_NAME)

      Dir.children(PATH).each do |file|
        next if File.extname(file) !~ /\.skp/i

        compname = File.basename(file,'.*')
        cmd_name = "Add #{compname}"

        command = UI::Command.new(cmd_name) {
          load_component(File.join(PATH,file))
        }
        command.tooltip = cmd_name
        command.status_bar_text = "Add #{compname} component to model."
        iconpath = File.join(PATH,'images',"#{compname}.png")
        if File.exist?(iconpath)
          command.small_icon = command.large_icon = iconpath
        end

        toolbar.add_item(command)
      end

      toolbar.restore

    end # run once block

  end # plugin submodule
end # unique toplevel namespace module

FIXED line 20 was missing the .each method call before the block.

2 Likes

Wow ! Big Thanks for your answer Dan !
I will try to test it quickly !

Best Regards
David

1 Like

DanRathbun knows everything about ruby a sketchup. You should follow his instructions .
What I am doing is picking little bits of pieces, snippets of ruby.
But anyway I am using toolbar editor to trigger the snippets. Or bits of codes. If you want to get it this way your box.skp load code would work like this … I think.
Box=
Sketchup.active_model.definitions.load(“D:/your directory/your directory/Box.skp”)
Sketchup.active_model.place_component(Box)

It should work and bring the box to sketchup. But as I said you have to create toolbar in toolbar editor, paste the code , apply.

… i’m back … and not glorious … :pensive:
I’m not Ruby developers … and it’s sometimes hard to understand all in english … so, i’m confused but i loose again …

i’ve saved a file called danscript.rb on my SU2019 plugin directory

modified like this

my path is like this with a box.png on “images” directory

file

and when i launch SU2019, i just have this

skp

Sorry, but i don’t understand … can you help me to undersantd please ? :flushed:

Thanks a lot
David

Does is the box file have the pathname "C:/Component/box.skp" ?
… and is the image file pathname "C:/Component/images/box.png" ?

What size is the png image ?

small icon should be 16 x 16 pixels

large icon or (used for both) should be 24 x 24 pixels.

@DanRathbun
“Does is the box file have the pathname "C:/Component/box.skp" ?
… and is the image file pathname "C:/Component/images/box.png" ?”
YES
… and YES

“What size is the png image ?
small icon should be 16 x 16 pixels
large icon or (used for both) should be 24 x 24 pixels”
It was 32x32 … so i changed it now to 16x16 as C:/Component/images/box.png

But my result is still the same
skp

:roll_eyes:

Don’t call it that … here I’ve fixed it and renamed it.
Also renamed the toplevel module as “DJoseph” …

DJoseph_component_toolbar.rb (1.1 KB)

It was a dumb typo error … line 20 was …

      Dir.children(PATH) do |file|

… when it should have been …

      Dir.children(PATH).each do |file|

So the do loop was never iterating.

Strange that Ruby did not raise a SyntaxError. Just ignored the block.