Hello,
I am new to the Ruby API for sketchup and am having an issue with passing arguments to a ruby tool I have created. The intent is to build a set of typing savers or quick commands to use via the ruby console.
I created a move command ‘m’ that is defined by the following code: (The various ‘puts’ lines are me trying to figure out what’s going on)
# /Rubies/bst.rb -- temporary test file
require 'sketchup'
puts( 'Loading Brad''s bst' ) #test msg
load File.dirname( __FILE__ )+'/bst_classes.rb'
puts( 'Brad''s bst loaded') #test msg
......
< a bunch of other command function defs. >
......
def m (*args)
#Invoke with "m comp_name, [r,g,b], repeat_count <,optional>"
# example "m test, [10,10,30], 10"
puts '# of args ' +args.length.to_s
puts 'args[0] is ' +args[0]
puts 'args[1] is ' +args[1].to_s
puts 'Sketchup ComponentInstance is ' +args[0]
puts 'Sketchup::ComponentInstance is ' +args[0].is_a?(
unless ( args.length > 0 ) && ( args[0].is_a?(Sketchup::ComponentInstance) )
UI.messagebox( "First argument of \"m()\" must be a Sketchup::ComponentInstance.\n" +
'Use "xm()" to translate by r, g, b values.')
return
end
case args.length
when 2 then move( args ) # static move
when 3 then movie( args ) # animated move
when 1 then xmove( args[0] )
else UI.messagebox( 'm args must be: ( transformation ) or ( comp, vec ) or ( comp, vec, ntimes ).' )
end
end # of m()
…
< more commands
The code is part of an .rb file my tools are in. the file is loaded via the Ruby console with “load ‘c:/rubies/bst.rb’” , again without the quotes.
First, in SketchUp 2017 (Make), I draw a rectangle, pushpull it, make it a component called test and select it. I would then type “m ‘test’, [10,10,10]” to provide the component name, and a move vector.
Without fail, i get the following message and nothing happens. Can someone please help. I think the problem is in the name of the component but I don’t see why. The screenshot at the bottom shows the argument are passed in ok, but apparently “test” is not seen as a component name.
m ‘test’, [10,10,10]
of args 2
args[0] is test
args[1] is [10, 10, 10]
Sketchup ComponentInstance is test
Sketchup::ComponentInstance is false
If i pass the name in as just test without the single quotes, I get an argument error.
m test, [10,10,10]
Error: #<ArgumentError: wrong number of arguments (0 for 2…3)>
:in `test' :in `' SketchUp:1:in `eval'Thanks
Brad Smith