Getting Component Size with Ruby
-
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!
-
Getting the size of an element with Ruby can be done with the boundingbox method.
http://www.sketchup.com/intl/en/developer/docs/ourdoc/boundingboxJust 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) -
@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/boundingboxJust 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
-
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
-
@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. -
@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/boundingboxJust 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).
-
@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!
Advertisement