Add dimensions in sketchup using ruby

Some tidbits …


This is the model’s top level collection of entities …
entities = model.entities

This is what entities context the user is within …
entities = model.active_entities


This is calling 2 methods, the .class() method, and then the .==() method …

if entity.class == Sketchup::ComponentInstance

This calls 1 method …

if entity.is_a?(Sketchup::ComponentInstance)


These both get the instance object’s definition’s untransformed bounding box …

bbox = defn.bounds
… and …
bbox = group.local_bounds

What you really want is the definition bounds, BUT scaled to match each instance’s scaling property from their individual transformation, but unrotated. So you’ll need to apply some math as @Aerilius spoke of in this other topic thread. And you’ll need to extract the scaling from a Geom::Transformation object. (We’ve covered this many times here in the forums. See next post …)


This gets the definition name, which is just going to be the same for all objects …
name = entity.definition.name

Group and Component Instances ALSO has a individual instance name attribute …
name = entity.name


w = bbox.width.to_l

Calling #to_l is unneeded because Geom::BoundingBox#width and #height and #depth all return Length class objects.

Calling Length#to_s on the result is also unneeded if you use String interpolation.

Ie, this statement is using String concatenation, and is inefficient as it creates 9 string objects …

puts name + " " + w + " " + h + " " + d

(You have the 3 literal String objects, and then each call to String#+() creates another new String object.)

This is better …

puts "#{name} : #{w} x #{h} x #{d}"

… or to format the lengths to 3 decimal places …

puts format( "%s : %.3d x %.3d x %.3d", name, w, h, d )

* NOTE: All internal lengths in SketchUp are in inches.