• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

No Implicit Conversion fron String in Messagebox

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 3 Posters 755 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    ArcTeo
    last edited by 18 Jan 2017, 02:19

    Hello.

    I'm trying to print out two numbers from a script where the user inputs a wall thickness and wall height. An inputbox takes the two prompts and is supposed to spit it out on the screen. However, when I try to get the messagebox to output the two numbers, no output occurs. Instead, I get "Ruby says 0". There is no messagebox that appears

    Adding a .to_sto each element in inputs gives a "no implicit conversion from string"

    Adding a .to_i to each element in inputs gives a "ruby says 0" and the messagebox does not appear

    I'm using Sketchup Make 2016. I wrote the script on the Ruby Code Editor
    By: Alexander C. Schreyer Version: 3.1 (3/6/2013)
    If you need any more information, kindly tell me.

    mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection Faces = [] f = mod.selection[0] #Create the inputbox prompts = ["Wall Thickness (in):", "Wall Height (ft):"] defaults = [8, 9] inputs = UI.inputbox( prompts, defaults ,'Length Input') #Set the wall thickness and wall height from inputbox to variables thickness = inputs[0].to_i h = inputs[1].to_i UI.messagebox(thickness, h)

    ArcTeo

    1 Reply Last reply Reply Quote 0
    • S Offline
      sdmitch
      last edited by 18 Jan 2017, 03:03

      @arcteo said:

      Hello.

      I'm trying to print out two numbers from a script where the user inputs a wall thickness and wall height. An inputbox takes the two prompts and is supposed to spit it out on the screen.

      UI.messagebox accepts 1 or 2 variables. The first is a text string. The second determines what buttons appear. Within the text string, you need to enclose the thickness and height variables in curly brackets {} preceded by a # symbol as demonstrated below

      UI.messagebox("#{thickness}, #{h}",MB_OK)
      

      Option 2 would be to convert the thickness and height variables to strings and add them

      UI.messagebox(thickness.to_s + ', ' + h.to_s)
      

      Nothing is worthless, it can always be used as a bad example.

      http://sdmitch.blogspot.com/

      1 Reply Last reply Reply Quote 0
      • A Offline
        ArcTeo
        last edited by 18 Jan 2017, 03:18

        OK! I got it to work, thanks to you!

        Let me ask you: how did you learn Ruby scripting for Sketchup? Did you use books, videos, search forums? Any tips for new scripters?

        ArcTeo

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdmitch
          last edited by 18 Jan 2017, 14:39

          @arcteo said:

          OK! I got it to work, thanks to you!

          Let me ask you: how did you learn Ruby scripting for Sketchup? Did you use books, videos, search forums? Any tips for new scripters?

          I have a Ruby programming reference manual but most of what I have learned is from studying plugins done by others and visiting the Plugins and Developers forums. The Ruby API is also an invaluable tool.
          http://ruby.sketchup.com/

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 18 Jan 2017, 14:53

            Another point is that your defaults are set to be both 'integers'.
            defaults = [8, 9]
            and this means that only whole numbers can be entered by the user - i.e. no 4.5.
            Also one default is expected in inches and the other one is in feet.
            Why not set your defaults to their proper units initially?
            defaults = [8.0.inch, 9.0.feet]
            Then the user can enter whatever they wish and your code will automatically know that the input is in those units with no further conversions needed...
            http://ruby.sketchup.com/Numeric.html
            http://ruby.sketchup.com/Length.html

            TIG

            1 Reply Last reply Reply Quote 0
            • A Offline
              ArcTeo
              last edited by 18 Jan 2017, 15:53

              TIG, I thought it said that .feet converts feet to inches? (See attachment)

              But now it worked. I wonder if it was a typo or am I missing something here.

              Thanks!!


              Feet.PNG

              ArcTeo

              1 Reply Last reply Reply Quote 0
              • T Offline
                TIG Moderator
                last edited by 18 Jan 2017, 18:38

                ALL of SketchUp's internal database lengths are stored in inches, irrespective of the units you use.
                In the API there are the numeric and length classes.
                So if you have an input value that defaults to a 'length' - e.g. 9.feet, or any other units format - then it is always 'stored' and 'processed' in inches [9*12=108"].
                However, this is flexible as the user can input in any units provided they use a units suffix: however how it's displayed in the input-box depends on the current model info > units settings...
                Otherwise you need to default to a string value and then reverse-engineer that - e.g.
                h="9" [read from the input[1]]
                which you then reset for later use as a parallel value
                hft=h.to_f.feet
                So the input is always taken as a decimal-feet value, but then used in inches internally...

                TIG

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                1 / 1
                • First post
                  1/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement