What am I doing wrong?
-
Hello,
well, I was just trying do make an 'addition' to Chris' multi-pushpull plugin and this is what I've got so far:
model = Sketchup.active_model sel = model.selection ent = model.active_entities height = mpp_options_diag faces = [] sel.each do |e| if e.is_a? Sketchup;;Face faces << e end end faces.each do |face| face.pushpull height.to_i end def mpp_options_diag prompts = ["Insert the height for the Multi PushPull"] defaults = ["100"] input = UI.inputbox prompts, defaults, "Multi PushPull Tool Parameters" end
Thing is, it lets me run the code if I do "face.pushpull(100)", but not with "face.pushpull height.to_i"
The Ruby Console shows this error:
undefined method `to_i' for ["100"];Array C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/webconsole/webconsole.rb;31;in `run' (eval);15;in `each' (eval);15;in `run' undefined method `to_i' for ["100"];Array
What am I doing wrong then?
Thanks. -
Hi Javier,
Ruby methods will return the value of their last statement. In your code, the method mpp_options_dialog return the value of the input variable; which is a Array.
So height == ["100"]. You need to get the first element of the Array. Here is one way:
height = height[0].to_i
-
Also you might not want the height as an integer by using
.to_i
, because0.5.to_i
will become0
not1
as you might have expected, as an integer simply ignores anything to the right of the decimal-separator !
The inputbox dialog can have its 'type' set to be a 'float' etc by making the default e.g. 1.0 [rather than 1 which fixes it as an integer] - then the dialogs returned_value[0] is always as a float anyway - BUT if you really want it to be a string - so for example the user can type '1.5m
' and they'll get 1.5m irrespective of the SKP's current unit settings - then use.to_l
to convert that value into a 'length'...Rather than add your dialog into a lot of complex code to test it just make it in a one-liner in the Ruby Console...
results=UI.inputbox(["Height: "],[1.0],[""],"My Title")
for a float - shown with
answer=results[0]
, andanswer.class
results=UI.inputbox(["Height: "],[1],[""],"My Title")
for an integer - shown with
answer=results[0]
, andanswer.class
results=UI.inputbox(["Height: "],["1.0"],[""],"My Title")
for a 'string'- shown with
answer=results[0]
, andanswer.class
and then
answer.to_i answer.to_f answer.to_l
[Try different answers to see the results...]
-
@tig said:
BUT if you really want it to be a string - so for example the user can type '1.5m' and you get 1.5m - then use .to_l to convert it to a length...
If you feed the inputbox a
Length
- then it will return aLength
. No need for conversion. And the Length will even be displayed in the format of the model. -
result = UI.inputbox( ['Width: '], [10.m], 'Hello World' )
Type 50cm in the inputbox.
` > [19.6850393700787]result[0].class
Length
result[0].to_s
500mm`
-
Thank you everyone, for "clearing up" my doubts. It works fine now.
By the way, just not to make a new topic for a single question, is is possible to load/unload/reload plugins with SketchUp running? -
@javixp said:
By the way, just not to make a new topic for a single question
That is actually better to make a new topic. New topic of conversation - new thread. Makes it easier to navigate and browse. Also makes the threads easier to follow with less topics.
@javixp said:
is is possible to load/unload/reload plugins with SketchUp running?
You can load (and reload) scripts by using the Ruby Console, typing
load 'myRubyFile.rb'
-
Sorry to dig up an old thread but It's kind of relevant to what I'm trying to do with inputbox.
What I'd like to do is associate a word with a value... ie: inputbox prompts for 3 values to create a square, height, width and vertical line... height and width are pretty self explanatory. Vertical line is expressed as inches.. so a 24" x 24" square could be divided in half by typing in 12... what I'd like to add is being able to type in 'center' or 'middle' and getting the same result.
I've tried making that field of the array a string and then converting the string to an integer or a float but I just can't get it to work... also tried associating the words to a value and samething no workie...
Hope that makes sense...
Thanks in advance.
@tig said:
Also you might not want the height as an integer by using
.to_i
, because0.5.to_i
will become0
not1
as you might have expected, as an integer simply ignores anything to the right of the decimal-separator !
The inputbox dialog can have its 'type' set to be a 'float' etc by making the default e.g. 1.0 [rather than 1 which fixes it as an integer] - then the dialogs returned_value[0] is always as a float anyway - BUT if you really want it to be a string - so for example the user can type '1.5m
' and they'll get 1.5m irrespective of the SKP's current unit settings - then use.to_l
to convert that value into a 'length'...Rather than add your dialog into a lot of complex code to test it just make it in a one-liner in the Ruby Console...
results=UI.inputbox(["Height: "],[1.0],[""],"My Title")
for a float - shown with
answer=results[0]
, andanswer.class
results=UI.inputbox(["Height: "],[1],[""],"My Title")
for an integer - shown with
answer=results[0]
, andanswer.class
results=UI.inputbox(["Height: "],["1.0"],[""],"My Title")
for a 'string'- shown with
answer=results[0]
, andanswer.class
and then
answer.to_i answer.to_f answer.to_l
[Try different answers to see the results...]
-
With some trial and lots of errors I figured it out.
-
Sorry we were so useless. Glad you figured it out, though!
-
Trust me Chris you and everyone else who takes the time to help us new guys out are anything but useless.
@chris fullmer said:
Sorry we were so useless. Glad you figured it out, though!
Advertisement