Max side size from an object

Hi

I thought I could use

selected_object = Sketchup.active_model.selection[0]

largest_side = selected_object.bounds.diagonal.to_a.max

to get the longest side of an selected object. The selected object could be a group. If I can’t get the group how can I get the items inside the selected group.

But I am wrong. I do get an error in the console. I thought that to_a creates an array with all the lengths.

What is the right way to get the longest side length from an object?

Thank you.

The #diagonal method is used to get the length of the diagonal of the bounding box.
There is no to_a method for (Length)

You need to use:
The #width method is used to retrieve the X extent of the bounding box.

The #height method is used to retrieve the Y extent of the bounding box.

The #depth method is used to retrieve the Z extents of the bounding box.

selected_object = Sketchup.active_model.selection[0]
bb = selected_object.bounds
largest_side = [bb.width,
                bb.height,
                bb.depth].max

(I moved you topic to the right category)


Edit

If you want to get longest side of bounds of all selected Drawingelement

sel = Sketchup.active_model.selection
bb = Geom::BoundingBox.new
sel.each{|e|
 next unless e.respond_to?(:bounds)
 bb.add e.bounds
}
largest_side = [bb.width,
                bb.height,
                bb.depth].max
1 Like

You need to clarify what you mean by “side”. If you mean bounding box side, then @dezmo’s code is right. But if you mean side of the object itself, the code will need to explore the faces in the object and measure each of them to find the largest. The bounding box corresponds to the sides of the object only if the object is a rectangular prism and is aligned with the axes.

2 Likes

@dezmo Thank you,
This works for just rectangular shapes, but what about other shapes; I thought I could use the diagonal.
I will check you second code and @slbaumgartner comment.

I did shorten the code very much to ask a new question.
How can I make the planes transparent?

selected_object = Sketchup.active_model.selection[0]
bounding_box = selected_object.bounds
center = bounding_box.center
largest_side = [bounding_box.width, bounding_box.height, bounding_box.depth].max


plane_size_percentage = 100
plane_size = largest_side * plane_size_percentage / 100.0

plane_transparency = 50
transparency = plane_transparency  / 100.0

# Draw the x plane
x_plane = Sketchup.active_model.entities.add_face(
  [center.x - plane_size / 2, center.y - plane_size / 2, center.z],
  [center.x + plane_size / 2, center.y - plane_size / 2, center.z],
  [center.x + plane_size / 2, center.y + plane_size / 2, center.z],
  [center.x - plane_size / 2, center.y + plane_size / 2, center.z]
)
x_plane.back_material = x_plane.material = Sketchup::Color.new(255, 0, 0, 255 * transparency)

# Draw the y plane
y_plane = Sketchup.active_model.entities.add_face(
  [center.x, center.y - plane_size / 2, center.z - plane_size / 2],
  [center.x, center.y + plane_size / 2, center.z - plane_size / 2],
  [center.x, center.y + plane_size / 2, center.z + plane_size / 2],
  [center.x, center.y - plane_size / 2, center.z + plane_size / 2]
)
y_plane.back_material = y_plane.material = Sketchup::Color.new(0, 255, 0, 255 * transparency)

# Draw the z plane
z_plane = Sketchup.active_model.entities.add_face(
  [center.x - plane_size / 2, center.y, center.z - plane_size / 2],
  [center.x + plane_size / 2, center.y, center.z - plane_size / 2],
  [center.x + plane_size / 2, center.y, center.z + plane_size / 2],
  [center.x - plane_size / 2, center.y, center.z + plane_size / 2]
)
z_plane.back_material = z_plane.material = Sketchup::Color.new(0, 0, 255, 255 * transparency)


x_plane.material.alpha = 255 * transparency
y_plane.material.alpha = 255 * transparency
z_plane.material.alpha = 255 * transparency


x_plane.back_material.alpha = 255 * transparency
y_plane.back_material.alpha = 255 * transparency
z_plane.back_material.alpha = 255 * transparency
Sketchup.active_model.rendering_options["EdgeTransparency"] = transparency

Please start a new topic for new question(s). Otherwise, this one can never be marked as solved.

I will do so.

Thank you, please delete the new question from this thread.