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

    Length method returns unexpected value

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 4 Posters 224 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.
    • O Offline
      Orthogon
      last edited by

      Can anyone tell me why when I do

      Sketchup.active_model.entities.add_edges([0,0,0],[1,1,0]).length
      

      it returns "1" instead of the expected "1.414..."?

      It appears to return the number of edges rather than their sum length. Curve does the same.

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        Because you added 1 edge.

        In other words, add_edges returned an Array containing 1 Edge object.

        [<Edge>]

        Your example is asking the length of the Array.

        You want to ask the length of the first Edge (index 0) in the Array:

        Sketchup.active_model.entities.add_edges([0,0,0],[1,1,0])[0].length

        Hi

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

          Or:

          Sketchup.active_model.entities.add_line([0,0,0],[1,1,0]).length

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

          1 Reply Last reply Reply Quote 0
          • O Offline
            Orthogon
            last edited by

            OK, .length returns the expected length for add_line, but for add_edges and add_curve it returns the array length. Not obvious, but I can live with that.

            What I really want is to get the length of a curve, such as the Curve.length example code http://www.sketchup.com/intl/en/developer/docs/ourdoc/curve#length suggests. Stranger yet, their code returns 47.123... rather than the expected 62.652... (sum of segments) or 62.831... (ideal circle) Can anyone explain the 47.123... number? The workaround, which is working for me, is to iterate through the array and sum them:

            edge.each { |x| $total_length+=x.length }
            
            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              { SNIP }

              If you typed the example as it is, the radius will be 10 inches, regardless of the model units.
              What do you have the model units set as ?

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                I did a quick test, with a 10" radius circle, default 24 segments.
                It does not matter what the model units are set to, the API's edge.length() and circle.length() return values in inches (even though the API doc says "current units".)

                It did not matter what the number of segments were, the value return by e.curve.length is always:
                %(#004000)[62.8318530717959]
                which is the perfect circumference, not the sum of the current number of display segments.

                So your summing the edges' lengths is appropriate.
                You can do it without a global accumulator, thus:
                clen = curve.edges.inject(0) {|sum, e| sum + e.length }

                I'm not here much anymore.

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

                  @orthogon said:

                  but for add_edges and add_curve it returns the array length. Not obvious, but I can live with that.

                  The API docs is for once correct, and the methods returns exactly what it says:

                  http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#add_edges

                  @unknownuser said:

                  an array of Edge objects if successful

                  http://www.sketchup.com/intl/en/developer/docs/ourdoc/entities#add_curve

                  @unknownuser said:

                  an array of Edges that make up the curve if successful

                  And I'd say it makes more sense to return the edges, like they currently do, instead of returning the combined geometric length of the edges. With the actual edges you can get the length and any other information you might need from the entities you just added. It's much more versatile.

                  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

                    @dan rathbun said:

                    I did a quick test, with a 10" radius circle, default 24 segments.
                    It does not matter what the model units are set to, the API's edge.length() and circle.length() return values in inches (even though the API doc says "current units".)

                    "Current units" is a bit weak description, I agree. SketchUp uses inches at any given time. It's only when you convert Length objects to strings you get to see the unit in the format of the model's settings.

                    http://www.sketchup.com/intl/en/developer/docs/ourdoc/length

                    @unknownuser said:

                    Internally, all lengths in SketchUp are stored in inches. The Length class stores values in inches as well. A number of methods have been added to the Ruby Numeric class to do units conversions.

                    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

                      On the topic of units in SketchUp: http://www.thomthom.net/thoughts/2012/08/dealing-with-units-in-sketchup/

                      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
                      • First post
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement