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

    Getting Component Size with Ruby

    Scheduled Pinned Locked Moved Dynamic Components
    sketchup
    7 Posts 5 Posters 10.2k Views 5 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.
    • AibonewtA Offline
      Aibonewt
      last edited by

      Okay,

      I've been lurking here a while, but now it's time to raise my head above the parapet!

      Been an AutoCAD user for about 25 years and coded various widgets in AutoLISP, Visual Basic (and a bit of C/Arduino), and I'd really like to get started with Ruby.

      I've already built a few DCs from the GUI, quite happy with building parametric components and even building simple models from Ruby, but here's the problem...

      Is there any way to get dimensions (lenx/y/z) values without setting attributes?

      If I make a box, and convert it into a component called 'Box', about the only thing I can get from Ruby is it's name. Lenx for example, returns zero until I go to the GUI and add the attribute manually. Is there any way to set attributes to their default so they can be read by Ruby?

      If I use an attribute explorer, I can see real dimensions in _last_lenx/y/z so theymust be readable somehow?

      Many thanks in advance!

      1 Reply Last reply Reply Quote 0
      • K Offline
        kaas
        last edited by

        Getting the size of an element with Ruby can be done with the boundingbox method.
        http://www.sketchup.com/intl/en/developer/docs/ourdoc/boundingbox

        Just get your box definition into a variable (for instance box_def) and use the methods
        box_def = Sketchup.active_model.definitions["Box"] bbox = box_def.bounds w = bbox.width (same for depth and height)

        1 Reply Last reply Reply Quote 0
        • AibonewtA Offline
          Aibonewt
          last edited by

          @kaas said:

          Getting the size of an element with Ruby can be done with the boundingbox method.
          http://www.sketchup.com/intl/en/developer/docs/ourdoc/boundingbox

          Just get your box definition into a variable (for instance box_def) and use the methods
          box_def = Sketchup.active_model.definitions["Box"] bbox = box_def.bounds w = bbox.width (same for depth and height)

          Oh wow!

          That works brilliantly, thank you!

          The naming convention of width, depth and height seem the wrong way round but I can live with that. The code snippet below outputs the dimensions in the format 'name x y z'

          # this lists component name and bounding box dimensions
          
          model = Sketchup.active_model
          entities = model.entities
          entities.each { |entity|
            if entity.class == Sketchup;;ComponentInstance
              name = entity.definition.name
              defn = entity.definition
              bbox = defn.bounds
              w = bbox.width.to_l.to_s
              h = bbox.height.to_l.to_s
              d = bbox.depth.to_l.to_s
              puts name + " " + w + " " + h + " " + d
            end
          }
          
          

          P.S. Sorry about the thumbs down, I hit it by mistake and it wouldn't let me change it 😕

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            You spotted the longstanding weirdness.

            SketchUp, like many newer 3d modeling tools, uses the convention that you are looking through the screen at the 3d object, and the X/Red/width is left>right, Y/Green/depth is front>back and Z/Blue/height is down>up.

            Some older 3d applications went down an alternative route.
            These assume that you are looking down onto the object from above - since they evolved from simpler 2d applications where there was only X and Y to worry about - and Z was never considered at all.
            Their convention is therefore that X/width is parallel to the screen's horizontal, Y/height is parallel to the screen's vertical, and Z/depth is out of the screen !
            And that's why some import/export options in SketchUp ask if you want to flip Y and Z axes.
            For example an OBJ file uses the alternative non-SketchUp convention.

            For some reason [never explained] the guys who wrote the Bounds API code [years ago] followed the non-SketchUp regime, while the rest of the team followed the SketchUp schema... and so for evermore we have the confusion - the bb.width is X [as expected], BUT perversely the bb.height is Y and the bb.depth is Z. Authors have asked for aliases of the methods to be added to the API, but without success - if they were, bb.width ===> bb.x[_size], bb.height ===> bb.y[_size] and bb.depth ===> bb.z[_size] - then matching the other API usage...

            But for now you'll just have to get used to the weirdness 😕

            TIG

            1 Reply Last reply Reply Quote 0
            • K Offline
              kaas
              last edited by

              @aibonewt said:

              ...That works brilliantly, thank you!...P.S. Sorry about the thumbs down, I hit it by mistake and it wouldn't let me change it 😕

              Glad to be of help. Np about the thumbs... 👍
              Also, nice TIG for your clarification. Already wondered.

              1 Reply Last reply Reply Quote 0
              • T Offline
                TBoy
                last edited by

                @aibonewt said:

                @kaas said:

                Getting the size of an element with Ruby can be done with the boundingbox method.
                http://www.sketchup.com/intl/en/developer/docs/ourdoc/boundingbox

                Just get your box definition into a variable (for instance box_def) and use the methods
                box_def = Sketchup.active_model.definitions["Box"] bbox = box_def.bounds w = bbox.width (same for depth and height)

                This Bounding Box includes rotation, so X, Y , Z sizes are not object sizes.

                Is there a way to get exact sizes of objects (or groups,entities.. any kind of selection)according to local ("objects") or world coordinate system? (Sorry if my terminology is wrong).

                1 Reply Last reply Reply Quote 0
                • L Offline
                  Lê Việt Trường
                  last edited by

                  @unknownuser said:

                  This Bounding Box includes rotation, so X, Y , Z sizes are not object sizes.

                  Is there a way to get exact sizes of objects (or groups,entities.. any kind of selection)according to local ("objects") or world coordinate system? (Sorry if my terminology is wrong).

                  Here is my solution:

                  
                  b = instance.definition.bounds
                  t = instance.transformation
                  x = ((b.max.x-b.min.x) * t.xscale)
                  y = ((b.max.y-b.min.y) * t.yscale)
                  z = ((b.max.z-b.min.z) * t.zscale)
                  
                  

                  Hope it helps
                  Have fun!

                  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