Hi all,
I’ve been playing a little with recreating the bounding box displayed around the group/component being open for editing, including that nice little padding that allows you to distinguish these lines from the actual geometry.
I thought other developers could find this useful, as these perspective effects are a large part of the SketchUp UI.
This is a quite crude example that does too much stuff in the draw method, rather than caching the results, but shows how you can translate a pixel size at a given point into a physical length in the model, and then use that length to draw to the screen. The same principle is used for the Scale Tool handles, the Protractor and various other things.
Native bounds:
My recreation (with other color and stipple):
class HighlightBox
def draw(view)
bounds = Sketchup.active_model.selection.first.bounds
# Extend the bounds in every direction with the distance corresponding to
# 30 px on the screen at the bounding box center.
padding = view.pixels_to_model(30, bounds.center)
bounds.add(bounds.max.offset(Geom::Vector3d.new(padding, padding, padding)))
bounds.add(bounds.min.offset(Geom::Vector3d.new(-padding, -padding, -padding)))
view.drawing_color = "Magenta"
view.draw(GL_LINES, bound_lines(bounds))
end
private
def bound_lines(bounds)
points = Array.new(8) { |i| bounds.corner(i) }
lines = [
points[0], points[1],
points[2], points[3],
points[0], points[2],
points[1], points[3],
points[4], points[5],
points[6], points[7],
points[4], points[6],
points[5], points[7],
points[0], points[4],
points[1], points[5],
points[2], points[6],
points[3], points[7]
]
end
end
Sketchup.active_model.select_tool(HighlightBox.new)
I hope someone finds this useful.