• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

Evaluate expression as input to input form

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 6 Posters 1.3k Views 6 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.
  • K Offline
    ktkoh
    last edited by 18 Apr 2011, 03:07

    Has anyone developed a method to allow the user to enter an equation into a input form field?

    I have rewritten my mortisetool.rb and sometimes when the input form is presented I need to make a calculation to determine the proper dimension to enter. The program accepts input thru the inputbox.
    Using win7 I can pop up the calculator and make the calculation then copy paste into the form but would be much more convenient to enter the equation in the form and have the program evaluate and use the answer. From my tests entering anything but a number results in an error and halts the program.

    Keith

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 18 Apr 2011, 08:26

      You need to set the particular inputbox field to a 'string' and then have a check for possible arithmetic symbols separating numbers.
      So if your inputbox field result is say c="12.3+3.6/3"
      You need to decide on precedence - like you will add then divide...
      So n=c.split('/')[0] and d=c.split('/')[1]
      if n=~/\+/ n=n.split('+')[0].to_f + n.split('+')[1].to_f else n=n.to_f end
      then
      tot=n / d.to_f
      This gives in order
      '12.3+3.6/3' '12.3+3.6'
      and
      '3'
      and then the addition of numbers
      12.3 + 3.6 >>> 15.9
      and then the division of numbers
      15.9 / 3.0 >>> 5.3 [the answer]
      Of course this is very simplistic as it assumes you always want to add two numbers together and then divide them by a number... and it can be achieved more 'elegantly'... BUT I am attempting here to show the principles...
      You could add - * () ** operations etc as you wish...

      So... set the input field as a string '0.0' [i.e. use quotes around the value], then use 'parsing' to work out the arithmetic and turn string numbers to floats [.to_f] and complete the calculation in code...
      Test it by setting values in the Ruby Console like n='12.3+4.6' then n=n.split('+')[0].to_f + n.split('+')[1].to_f to see what happens...

      If you always want to do some arithmetic the same way you can easily do this- if you want flexibility you need to jump through a few more hoops but it's quite possible...

      TIG

      1 Reply Last reply Reply Quote 0
      • F Offline
        fredo6
        last edited by 18 Apr 2011, 10:50

        @ktkoh said:

        Has anyone developed a method to allow the user to enter an equation into a input form field?
        Keith

        You can have a look at the code snippet I posted, based on methods which I have used for some times in LibFredo6. You can of course adapt them, if you don't need all features.

        Fredo

        1 Reply Last reply Reply Quote 0
        • K Offline
          ktkoh
          last edited by 18 Apr 2011, 17:29

          Fredo: Thanks for the code. I used as written and added a require Arithmetic parsing.rb and then added include ArithmeString inside my class MortiseTool. This almost works!!! GOOD NEWS. After loading the file the first time accessed the input form accepts an equation and makes the calculation. Now comes the BAD NEWS any time after that I get an error that the equation is improper input. I read in the SU Ruby book about modules but obviously I don't quite know the proper usage. My input box is coded within the def onLButtonDown(flags, x, y, view) part of my class. How do I get the module to be available each time thru the code where the input box is specified?

          Keith

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 18 Apr 2011, 19:56

            kt, Fredo's ArithmeString module is NOT a mixin-module, you cannot include it within your class.

            It is a library module, and you must call each method qualified with the module name in front:
            ` ret_array = inputbox( prompts, defaults, title )
            if ret_array
            user_length_1 = ArithmeString.string_to_length(ret_array[0])
            else

            user cancelled inputbox

            end`

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • F Offline
              fredo6
              last edited by 18 Apr 2011, 20:52

              @ktkoh said:

              How do I get the module to be available each time thru the code where the input box is specified?

              Keith

              Keith,

              As rightly pointed out by Dan, you have to prefix the methods with the module name (here ArithmeString). Note that you can change this module name if you wish. I used it just as an example.

              I would recommend anyway that you invest in understanding Ruby modules if you intend to write scripts for SU, because this is the right way to protect namespace and avoid clashes between all scripts running in the Sketchup environment.

              Fredo

              1 Reply Last reply Reply Quote 0
              • K Offline
                ktkoh
                last edited by 18 Apr 2011, 23:02

                I am trying to understand modules. Documentation seems sparse. I remember in VB6 you included them at a specific part of the code and then they were always available.
                The example from the Automatic Sketchup Book:

                require "Ch8/mods.rb"
                require "Ch8/house3.rb"
                # Create a subclass of House
                class New_House < House
                # Add the capabilities from Module A
                include Module_A
                def test_method
                draw
                print_hello
                print_goodbye
                end
                end
                

                This is how I used it here. Then after the input box @@todist=ArithmeString.string_to_float_formula(results[0]) and as I said it works just fine the first time but not after that. Each time I load the mortisetool.rb which requires Arithmetic parsing.rb the equation is evaluated the first time thru the tool class.

                Thanks
                Keith

                1 Reply Last reply Reply Quote 0
                • K Offline
                  ktkoh
                  last edited by 19 Apr 2011, 02:46

                  I have been doing some testing and I find I can open the win cal while the data form is up and waiting for input so any dim that needs some calculation to make the proper entry can be calculated and using a copy/paste input into the form. Since I program mostly for my own use (but offer to other woodworkers) I am going to leave it there for now. With a little more testing I will zip up the files and post it here.

                  Thanks for all the help
                  Keith

                  1 Reply Last reply Reply Quote 0
                  • J Offline
                    Jim
                    last edited by 19 Apr 2011, 21:11

                    I made this little expression evaluator that can send the result to the VCB. Works on Windows only. It is not aplugin, but a stand-alone HTA (html + VBScript.) It probably needs some work to make it user-friendly.

                    http://forums.sketchucation.com/viewtopic.php?f=323&t=30687

                    Probably Fredo's evaluator would be preferable, so maybe the ideas could be combined into a single plugin that sends the result to the VCB.

                    Hi

                    1 Reply Last reply Reply Quote 0
                    • K Offline
                      ktkoh
                      last edited by 20 Apr 2011, 23:34

                      I finally got the code to work using part of the code from Fredo. The problem was that some entries were interpreted as strings. After I added a final to_f then the program worked as I wanted. I am going to post the KK_MortiseTool.rb here so you can look at that to see how I worked that out.

                      Thanks to all for their help and reply's.

                      Keith

                      1 Reply Last reply Reply Quote 0
                      • T Offline
                        thomthom
                        last edited by 23 Apr 2011, 12:34

                        @ktkoh said:

                        The problem was that some entries were interpreted as strings.

                        UI.inputbox returns values in the same class as you feed the defaults. So if you feed it Lengths it returns lengths.

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

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

                        Advertisement