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

    Sketchup.parse_length

    Scheduled Pinned Locked Moved Developers' Forum
    12 Posts 4 Posters 259 Views 4 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.
    • thomthomT Offline
      thomthom
      last edited by

      I had thought that Sketchup.parse_length was the same as String.to_l - but after having another look at it...

      ` '10cm'.to_l
      3.93700787401575

      '10foobar'.to_l
      Error: #<ArgumentError: (eval):155:in `to_l': Cannot convert "10foobar" to Length>
      (eval):155
      (eval):155

      Sketchup.parse_length( '10cm' )
      3.93700787401575

      Sketchup.parse_length( '10foobar' )
      nil

      Sketchup.parse_length( '10.0foobar' )
      nil

      Sketchup.parse_length("1.0splishysplash")
      nil`

      Seems that Sketchup.parse_length doesn't raise any errors. But on the other hand, I get nil on invalid strings...

      @adamb said:

      If I have to write a Regex to parse the string to check it valid before giving it to parse_length(), then I might as well do the last step of conversion myself.

      You could, but you'd also have to deal with the user's locale for decimal symbol...

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

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Speaking of locale...

        ` Sketchup.parse_length("1,0splishysplash")
        0.0393700787401575

        "1,0splishysplash".to_l
        0.0393700787401575

        "1,0".to_l
        0.0393700787401575`

        I'm using Norwegian locale - now I see the result you got. Whenever it fails to extract a unit identifier in the string it appear to assume the number it find is in the model units. This might be "by design" ... ?

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

        1 Reply Last reply Reply Quote 0
        • AdamBA Offline
          AdamB
          last edited by

          @adamb said:

          AdamB wrote:
          If I have to write a Regex to parse the string to check it valid before giving it to parse_length(), then I might as well do the last step of conversion myself.

          You could, but you'd also have to deal with the user's locale for decimal symbol...

          Absolutely! Really don't want to re-invent the wheel.

          BUT.. you're getting different results to me. I don't get nil back, I get 39.3700787401575 back - which really screws stuff up.
          (I've always checked for nil).

          I feel a witch hunt coming on.. it is almost Halloween..

          Ah, just saw your new post! If it is "by design", thats an extremely poor decision.

          Developer of LightUp Click for website

          1 Reply Last reply Reply Quote 0
          • AdamBA Offline
            AdamB
            last edited by

            Playing some more, I think its a straight bug in Sketchup code. As in, without a decimal separator the behaviour is as expected..

            Ultimately, I can't rely on it so I'll need to parse everything myself.. grrr..

            Adam

            Developer of LightUp Click for website

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              @adamb said:

              Absolutely! Really don't want to re-invent the wheel.

              While there is a method to parse lengths based on the user's current locale. There is no way to parse floats. Or coordinates (like you can do with the Move tool, specifying absolute or relative coords...)

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

              1 Reply Last reply Reply Quote 0
              • AdamBA Offline
                AdamB
                last edited by

                This is what I'm using in LightUp

                /([0-9]+[\,\.]*[0-9]*)([\w\"\']+)?$/

                Developer of LightUp Click for website

                1 Reply Last reply Reply Quote 0
                • thomthomT Offline
                  thomthom
                  last edited by

                  @adamb said:

                  This is what I'm using in LightUp

                  /([0-9]+[\,\.]*[0-9]*)([\w\"\']+)?$/

                  Yea, I'm using something similar. I think that when one assume that the decimal is either period or comma one cover pretty much everything. I've not heard of anyone using anything else. It's worse with the list separator. At the moment I assume that if one use comma for decimal that semi-colon is the list separator. ...though I'm less sure of this one...

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

                  1 Reply Last reply Reply Quote 0
                  • fredo6F Offline
                    fredo6
                    last edited by

                    If this can help, I have coded methods to parse lengths (as well as integers and floats) with or without formulas.
                    For the locale, I decided to accept both the dot and comma as decimal separators, so that there is no interference with the local settings.

                    You can get some inspiration by browsing the file Lib6Traductor.rb in LIBFREDO6_Dir_xx folder. Just search for methods like:
                    **** Traductor.string_to_length(s)

                    • Traductor.string_to_length_formula(s)
                    • Traductor.string_to_float(s)
                    • Traductor.string_to_float_formula(s)***
                      Fredo
                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      Somebody recently did alot of this work... ParseArthimetic rings a bell (DING!)

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • AdamBA Offline
                        AdamB
                        last edited by

                        @unknownuser said:

                        If this can help, I have coded methods to parse lengths (as well as integers and floats) with or without formulas.
                        For the locale, I decided to accept both the dot and comma as decimal separators, so that there is no interference with the local settings.

                        You can get some inspiration by browsing the file Lib6Traductor.rb in LIBFREDO6_Dir_xx folder. Just search for methods like:
                        **** Traductor.string_to_length(s)

                        • Traductor.string_to_length_formula(s)
                        • Traductor.string_to_float(s)
                        • Traductor.string_to_float_formula(s)***
                          Fredo

                        Nice work Fredo.

                        However, in your string_to_length(), I don't think SketchUp supports units of "km", "feet" and "mile" (though it will accept ' and " for foot and inch respectively)

                        What's so frustrating about stuff like this is not that software has bugs - it all does - just that for the rest of time you have to have scripts that jump through hoops to side-step this, duck to avoid that, jump to miss that... making them a right ol mess.

                        I guess there is an argument here that based on version number, you do go in and overload the core Sketchup methods to fix them. Problem is that while there are a bunch of great coders that I would totally trust to do this well, there's a lot more that don't have the experience to do this right, so it would be opening a horrible can of worms. I'm looking at you, Matchbox. 😄

                        Adam

                        Developer of LightUp Click for website

                        1 Reply Last reply Reply Quote 0
                        • fredo6F Offline
                          fredo6
                          last edited by

                          @adamb said:

                          However, in your string_to_length(), I don't think SketchUp supports units of "km", "feet" and "mile" (though it will accept ' and " for foot and inch respectively)

                          These functions are primarily geared at getting input from user (via VCB or dialog box). So, even if units are not supported by SU, the notation accepts it (like the dot and comma for the decimal separator).

                          The functions also support the architectural notation with ' and " (though I am not fully familiar with it). For instance

                          
                          s = "2'3\" + 5' 8\""                  --> 2'3" + 5' 8"
                          Traductor.string_to_length_formula(s) --> 95.0
                          
                          s = "3\" * 4"                         --> 3" * 4
                          Traductor.string_to_length_formula(s) --> 12.0
                          
                          Traductor.string_to_length("1.0splishysplash") --> nil
                          
                          

                          Fredo

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

                          Advertisement