I'm a beginner, my button doesn't work

I’m a beginner, I’m studying

I wanted to make a button to change to mm and another to cm, but I couldn’t, it changes, but then I can’t change to another one later.

toolbar = UI::Toolbar.new “teste”
cmd = UI::Command.new(“MM”) {
Sketchup.active_model.options[“UnitsOptions”][“LengthFormat”]=0
Sketchup.active_model.options[“UnitsOptions”][“LengthUnit”]=2
}
cmd.tooltip = “MM”
cmd.status_bar_text = “mm”
cmd.set_validation_proc {
if Sketchup.active_model.options[“UnitsOptions”][“LengthUnit”]=2
MF_CHECKED
else
MF_UNCHECKED
end
}
cmd.small_icon = “16.png”
cmd.large_icon = “24.png”
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.large_icon

This looks like a SketchUp bug. @colin , could you log this?

Cabarsa, in the meantime you can change formatting of the post so the code can be run. Start the code section with triple backticks and the language name (```ruby) a end it with triple backticks.

Here is the code with the smart quotes replaced. I also switched two lines, showing the toolbar before adding the cmd object gives you a button that works. I’m not sure what the button does, to know if it’s working, but the help tip and toggle of the button does work.

toolbar = UI::Toolbar.new "teste"
cmd = UI::Command.new("MM") {
Sketchup.active_model.options["UnitsOptions"]["LengthFormat"]=0
Sketchup.active_model.options["UnitsOptions"]["LengthUnit"]=2
}
cmd.tooltip = "MM"
cmd.status_bar_text = "mm"
cmd.set_validation_proc {
if Sketchup.active_model.options["UnitsOptions"]["LengthUnit"]=2
MF_CHECKED
else
MF_UNCHECKED
end
}
cmd.small_icon = "16.png"
cmd.large_icon = "24.png"
toolbar.show
toolbar = toolbar.add_item cmd
puts cmd.large_icon
1 Like

It changes the model units to decimal mm. You have to close and reopen the Model Info panel to see the change. I might have expected the panel to update on the change, but it does not.

Of course, the images are not present, so the button displays the missing image icon.

@Cabarsa you need to turn off smart quotes, Ruby doesn’t accept the closing variety as ending a string. You also need to enclose your code in a module with a unique name, else your data and functions may collide with ones from other code, causing very strange errors.

1 Like

1 - Works, thks guys!!! I have a few more questions, can you help me?

module Milimetro # botao para mostrar as unidades
    barra = UI::Toolbar.new "Teste"
    Sketchup.active_model.options["UnitsOptions"]["LengthFormat"] = 0
    botao = UI::Command.new("Milímetros") { Sketchup.active_model.options["UnitsOptions"]["LengthUnit"] = 2 }
    botao.tooltip = "Milímetros"
    botao.status_bar_text  = "Milímetros"
    botao.set_validation_proc {
      if Sketchup.active_model.options["UnitsOptions"]["LengthUnit"] == 2
        MF_CHECKED
      else
        MF_UNCHECKED
      end
    }
    botao.small_icon = "img/16.png"
    botao.large_icon = "img/24.png"
    barra.show
    barra = barra.add_item botao
  end

2 - the paste in place command does not work


botao = UI::Command.new("Colar no Local") { Sketchup.send_action "pasteInPlace:" }

3 - I studied some plugins that do this, I tried to adapt it to a simpler way, but it didn’t work … I tried to make a button to decrease the decimal places.
can I do it in a simple way like the millimeter button, without having this “def”?
I try: Sketchup.active_model.options[“UnitsOptions”][“LengthPrecision”] = -1 but its crash =/

  module Casadecimal
    def Casa_Decimal(val)
    model=Sketchup.active_model
    cd=model.options["UnitsOptions"]
	return if not [1,-1].index(val)
	dc = cd["LengthPrecision"]
	if 0 <= (dc + val) && (dc + val) <= 10
		cd["LengthPrecision"] = dc + val
	end
	model.active_view.invalidate
    end
    
    
    barra = UI::Toolbar.new "Test"
    botao = UI::Command.new("Diminuir Casas") { Casa_Decimal(-1) }
    botao.tooltip = "Diminuir Casas"
    botao.status_bar_text  = "Diminuir Casas"
    botao.small_icon = "img/16.png"
    botao.large_icon = "img/24.png"
    barra.show
    barra = barra.add_item botao
  end

4 - how do I find out the command for “delete guides”? I want a button for that. Its a send.action? active_model.options?

Regarding most of your new items, I have keyboard shortcuts assigned for them. I find that to be at least as easy as clicking a button and not requiring any Ruby code. But to each his own, particularly if you are doing this mainly as a learning exercise.

One thing you should understand about send_action is that it adds the action to a queue for execution when SketchUp decides it is safe. In general the action won’t happen until any running Ruby returns control to the GUI.

Did you put something on the clipboard before testing this send_action? It should work if there is something to paste.

Because values < 0 or > 10 are not legal, you need some logic to avoid them. So, yes, this requires a block of code to implement the logic. However, the block doesn’t necessarily have to invoke a def’d method, it could contain all the required code. Using a def is better design though. Note that by convention, method names start with a lower case letter: casa_decimal, not Casa_Decimal.

Your code fails due to name scoping. If you want to invoke a module method such as casa_decimal without a receiving object, you have to use

def self.casa_decimal
#...
end

With the self added and the capitalization fixed, your code works.

I don’t know of an action or pre-packaged API method to delete guides (technically ConstructionLine and ConstructionPoint in the Ruby API). I think you would have to search an Entities collection for them and explicitly erase! each one. That could all be handled using a grep method on the Entities collection. To get all of them throughout the model you would need to repeat for every Entities collection: the model and every ComponentDefinition.

2 Likes

is that I’m a teacher, and I’m going to use this bar in some classes, because some people don’t like to use or create shortcuts in the beginning. Or simply prefer to use the button.
I want everything together, simple, just clicking a button. It is not all that you can put shortcuts, change the unit does not have, purge does not have …

yes, i copied something and tried to use the paste in place button, it did nothing.

I thought I would have an easy way to delete the guides, as there is a command on the Edit menu.

thks thks thks

Yes, but Windows only …

Sketchup.send_action(21044)

I found it using the Object Inspect utility that comes with the Microsoft Windows SDK.

A method definition is better because you can test it and reload the file without having to close SketchUp and restart again to test the fixes.

# encoding: UTF-8

module Cabarsa
  module CasaDecimal
  
    extend self

    def casa_decimal(val)
      return unless val.is_a?(Numeric) && [1,-1].include?(val.to_i)
      model = Sketchup.active_model
      opts  = model.options["UnitsOptions"]
      prec  = opts["LengthPrecision"] + val.to_i
      if prec.between?(0,6)
        opts["LengthPrecision"]= prec
        model.active_view.invalidate
        UI.refresh_inspectors
      end
    end

    if !@loaded
      @loaded = true
      barra = UI::Toolbar.new("Test")
      botao = UI::Command.new("Diminuir Casas") { casa_decimal(-1) }
      botao.tooltip = "Diminuir Casas"
      botao.status_bar_text = "Diminuir Casas"
      botao.small_icon = "img/16.png"
      botao.large_icon = "img/24.png"
      barra.show
      barra = barra.add_item(botao)
    end

  end
end

Note that @DanRathbun likes to use “extend self” rather than putting self. In each def. The end result is similar.

Edit: And Dan inspired me to dig up my collection of undocumented send_action codes - I had forgotten about it. It turns out that what you want is ‘eraseConstructionGeometry:’

1 Like

Odd. It worked for me.

@DanRathbun nice, slbaumgartner it had already solved for me, it was the capital letter, is your code better for me to use? I have a button that increases and another that decreases.

Delete Guides works, thks =D

@slbaumgartner dont work here

  module Paste #
    barra = UI::Toolbar.new "Teste"
    botao = UI::Command.new("Colar no Local") { Sketchup.send_action "pasteInPlace:" }
    botao.tooltip = "Colar no Local"
    botao.status_bar_text  = "Colar no Local"
    botao.small_icon = "img/gear_16.png"
    botao.large_icon = "img/gear_24.png"
    barra.show
    barra = barra.add_item botao
  end

I’m on Mac, but it doesn’t seem like that should make a difference. One thing to check is when you paste in place, does the entity count in the Entity Info window change? Paste in place will exactly overlay the new object atop the previous one, so it can be hard to tell it actually worked. One thing to try is to cut (note: Edit->Cut, not delete) the object rather than copy (so it disappears) and then paste in place should make it reappear.

In this animation, first I cut the cylinder and then clicked your button, then I moved the original out of the way and did paste in place via your button again. Both times it worked.

pip

yes, I use this command a lot, but the button here doesn’t work. PC here

Very strange. When I get a chance I’ll launch a Windows VM and see what happens.

Edit: nope, it doesn’t work on Windows for me either. It may be one of those ones that needs a numeric code on Windows? If so, some Windows guru will have to figure it out (@DanRathbun?) as it is undocumented (so is “pasteInPlace:”).

This returns false on Windows platform so.

WIN = Sketchup.platform == :platform_win
Sketchup.send_action( WIN ? 21044 : "eraseConstructionGeometry:" )

For Paste in Place …

WIN = Sketchup.platform == :platform_win
Sketchup.send_action( WIN ? 21939 : "pasteInPlace:" )
2 Likes

I’ve always found it interesting the way the send_action strings reflect SketchUp’s origins: they are the names of Objective-C methods in the Mac source code.

@DanRathbun is it possible to make a button for the intersect faces? Screenshot_1

Probably you would have to use … Sketchup::Entities#intersect_with
… and get the receiver object from the selection.

The “With Model” automation ID is 21524
The “With Context” automation ID is 21526
The “With Selection” automation ID is 21527

1 Like