[Code] Parsing arithmetic formulas

[Code] Parsing arithmetic formulas

Postby fredo6 » Mon Apr 18, 2011 11:47 am

Here are a set of standalone methods that can parse a string as an arithmetic value or formula for Integers, Floats and Lengths.

They are actually derived from some work I have done in LibFredo6 (in module Traductor).

I enclosed the methods in a module called ArithmeString, which you can of course change if you enclose some of this code in your script.
  • string_to_integer --> parse a string as an integer
  • string_to_integer_formula --> parse a string as a formula evaluating as an integer
  • string_to_float --> parse a string as a float
  • string_to_float_formula --> parse a string as a formula evaluating as a float
  • string_to_length --> parse a string as a length
  • string_to_length_formula --> parse a string as a formula evaluating as a length

Note that all methods accepts the "." or the "," as valid decimal delimiter, regardless of the underlying locale of the operating system and SU version language.

All the formula methods accepts parentheses to manage more complex calculations.
For lengths, there is a logical order to respect for specifying units and numbers (ex: 2cm * 4, NOT 4 * 2cm)

The attached file contains the methods with some additional comments
Arithmetic parsing.rb


Fredo
1
User avatar
fredo6 
Premium Member
Premium Member
 

Re: [Code] Parsing arithmetic formulas

Postby TIG » Mon Apr 18, 2011 2:08 pm

Thanks Fredo
I knew someone must have done more on it.
The nearest I have is in my roof tools where you can specify a slope in degrees [25.0], a ridge height [^3.5] or a pitch [2:3]. The tool then spots which type it is and runs the appropriate method, e.g. using a Math.tan() on the A:B version...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Code] Parsing arithmetic formulas

Postby fredo6 » Mon Apr 18, 2011 9:46 pm

TIG wrote:Thanks Fredo
I knew someone must have done more on it.
The nearest I have is in my roof tools where you can specify a slope in degrees [25.0], a ridge height [^3.5] or a pitch [2:3]. The tool then spots which type it is and runs the appropriate method, e.g. using a Math.tan() on the A:B version...

TIG,

Thanks. I wonder why this is not embedded in Ruby (or maybe it is in a package that is not included in the Ruby distribution of Sketchup). My real problem is with the length. I have real difficulty with the architectural notation!

Fredo
0
User avatar
fredo6 
Premium Member
Premium Member
 

Re: [Code] Parsing arithmetic formulas

Postby Dan Rathbun » Mon Apr 18, 2011 10:55 pm

0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] Parsing arithmetic formulas

    Postby Dan Rathbun » Mon Apr 18, 2011 11:05 pm

    Fredo6 wrote: I wonder why this is not embedded in Ruby (or maybe it is in a package that is not included in the Ruby distribution of Sketchup).

    I checked the extended Ruby libraries, but nothing seems to be like this (or the Perl module or Java class, MathString.)

    I also did a search at RubyForge to see if anyone else had a similar project, but did not find anything.

    Fredo6 wrote:My real problem is with the length. I have real difficulty with the architectural notation!

    The Sketchup API already extended the Ruby base String class with a .to_l method.
    Can you use that method call within your module's length converter methods?

    I also wonder if this module would be easier to use as a custom String subclass?
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] Parsing arithmetic formulas

      Postby ktkoh » Thu Dec 01, 2011 2:44 am

      I used this code in my joint tools and found it very useful as the user could enter equation if they do not know the actual value. However I could not make it work if the user entered the " for inches as sketch-up does.

      Keith
      0

      ktkoh 
      PluginStore Author
      PluginStore Author
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      cron

      Who is online

      Users browsing this forum: magedalex and 12 guests