1

On SketchUp one can request user input using a dialog window created with the UI.Inputbox method. It is a basic task to ask for the dimension or length of some geometry the script will create afterwards.

Internally SketchUp uses Inches to define geometry. The user will give the answer in his/her localized dimension idiom: '1,5m' for 1.5 Meters. The built in SketchUp method .to_l converts strings to length. See https://github.com/thomthom/SketchUp-Units-and-Locale-Helper#sketchups-shortcomings for reference.

I'm asking the user for a length as a string from UI.Inputbox and then converting it to a length value using .to_l but if the user types an invalid value I don't know how check or how to handle the error.

As my localized length inputs have ',' as the decimal separation (in Portuguese it is 1,5m not 1.5m), I'm not willing to ask for a floating point value.

prompts = ['Length']
defaults = ['1,2']
inputs = UI.inputbox( prompts, defaults ,'Length Input')
inputs[0].to_l
#try inputs[0].to_f 

How check the input string or catch .to_l failure?

villares
  • 333
  • 2
  • 12
  • 1
    can you provide some code as an example? it's hard to understand what you're asking – dax Apr 13 '15 at 14:20
  • @dax see if this looks like a better question... – villares Apr 13 '15 at 22:29
  • yeah, way easier to understand now, but i still have a question. You say you're not willing to ask for floating point - does that mean that you'll always receive whole number integer values as strings? – dax Apr 14 '15 at 08:20
  • Hi @dax, if I set the default as a float, let's say 1.5 or 1.0, I think the inputbox will return a float, but it will look inconsistent as user interface, he/she might not recognize it as a length ("1,5m") as entered on other SketchUp value input fields. – villares Apr 14 '15 at 12:44
  • is sketchup okay with receiving strings in the format of "1,5"? does `.to_l` throw an error just because of that or it's okay with commas? also, what would you consider 'invalid' input? – dax Apr 14 '15 at 12:46
  • @dax, "1,5" is OK in a Portuguese-language set up. "text" or "a1,5" throws and error. I have no clue on how to write Ruby exception handling. – villares Apr 15 '15 at 01:55
  • ah okay, i see. i'll write an answer you can tell me how it works for you – dax Apr 15 '15 at 08:29

2 Answers2

1

This should work for you, but it might need to be fine-tuned depending on what you want each error case to do.

prompts = ['Length']
defaults = ['1,2']
inputs = UI.inputbox( prompts, defaults ,'Length Input')

Assuming the above, we can make a method to handle error cases for your input.

def parse_input(inputs)
  input = normalize(inputs)

  handle_error if input.empty?
  input
end

def normalize(input)  
   input.gsub(/[a-zA-Z]/, "")
end

def handle_error
  # you could raise an error or just return the default
end

When you parse the input, it attempts to 'normalize' it - meaning that it replaces commas with dots and then calls to_f. Since it looks like you're getting this as input from a field, inputs will always be a String, so there's no need to worry about handling other types. Sample output:

normalize('1,4') #=> '1.4'
normalize('2.6') #=> '2.6'
normalize('5a.2') #=> '5.2'
normalize('text') #=> ''

Now that the input is normalized, you can handle errors (which is basically when the normalized input is an empty string. What you want to do there is up to you and what works best in your specific case.

Finally the input is returned if there are no input errors.

dax
  • 10,779
  • 8
  • 51
  • 86
1

I'm asking the user for a length as a string from UI.Inputbox and then converting it to a length value using .to_l

The question is wrong, I mean, it is not a good idea to ask for a string and to try yourself to manage the complexities of localized float values. Don't do it!

Aks for a 'length' and SketchUp will take care of everything for you. Use .m and the user will be prompted a value in his local/chosen units. The resulting input will be in 'Length - internal units'.

prompts = ['Name', 'Width', 'Height']
defaults = ['My Own Square', 5.m, 2.m]
input = UI.inputbox( prompts, defaults, 'Create Square' )
# User enters Width: 300cm, Height 4
p input
# => ["My Own Square", 118.110236220472, 157.48031496063]
p input.map { |n| n.class }
# => [String, Length, Length]
p input.map { |n| n.to_s }
# => ["My Own Square", "3000mm", "4000mm"]

Source: ThomThom http://www.thomthom.net/thoughts/2012/08/dealing-with-units-in-sketchup/

villares
  • 333
  • 2
  • 12