|
SketchUp Ruby API Reference |
Bounding boxes are three-dimensional boxes (eight corners), aligned with the global axes, that surrounds entities within your model. There is a default bounding box for any new (empty) model that will surround all entities, including all groups and components, in your model. Additionally, there are bounding boxes for all components and groups (which can be seen when selecting the component or groups). Bounding boxes are only large enough to exactly bound the entities within your model, group, or component.
The BoundingBox class is used to create and manipulate a new bounding box.
Parent: Object
Methods: new, add , center, clear, contains?, corner, depth, diagonal, empty?, height, intersect , max, min, valid?, width
Example Code: boundingboxtests.rb
The new method is used to create a new, empty, bounding box.
boundingbox = Geom::BoundingBox.new
boundingbox – a BoundingBox object if successful
boundbox = new Geom::BoundingBox.new
if (boundbox)
# creation of bounding box was successful, you can use the object
else
# creation of bounding box was not successful
end
The add method is used to add a point, or other bounding boxes, to the bounding box. The size of the bounding box will increase as necessary to accommodate the new points or bounding boxes.
boundingbox = boundingbox.add point boundingbox = boundingbox.add point1, point2, ... boundingbox = boundingbox.add boundingbox boundingbox = boundingbox.add [point1, point2, ...]
point1, point2, ... – A Point3d object
boundingbox – a BoundingBox object
boundingbox – the new, resized, BoundingBox object if successful
Adding one point to an empty bounding box, does not increase the size of the bounding box. You must add at least two points before methods, such as BoundingBox.diagonal, will return a size greater than zero.
model = Sketchup.active_model# Get the bounding box associated with the model. There will not be any size to the bounding box# until you add something prior to calling model.boundsboundingbox = model.boundsbbox = boundingbox.add_point
The center method is used to retrieve the Point3d object at the center of the bounding box.
point = boundingbox.center
point – the Point3d at the center of the bounding box if successful
# There will not be any size to your bounding box until you add at least two points prior to calling model.bounds boundbox = model.bounds center = boundbox.center UI.messagebox center
The clear method is used to clear a bounding box.
boundingbox = boundingbox.clear
boundingbox - the BoundingBox object which was cleared
A cleared BoundingBox does not have a size greater than zero until you add at least two points or another bounding box.
# There will not be any size to your bounding box until you add at least two points prior to calling model.bounds boundbox = model.bounds bounding = boundbox.clear
This method is used to determine if a bounding box contains a specific Point3d or BoundingBox object.
status = boundingbox.contains? point | boundingbox
point – a Point3d object
boundbox – a BoundingBox object
status = true if successful (bounding box contains a Point3d or BoundingBox object), or false if unsuccessful.
point = Geom::Point3d.new(100,200,300)
model = Sketchup.active_model
bbox = boundingbox.add point
# should return true
status = bbox.contains? point if (status)# if true, code to do somethingelse# if false, code to do somethingend
The corner method is used to retrieve a point object at a specified corner of the bounding box.
point = boundingbox.corner corner
corner – a number (from 0 to 7) representing point at the corner you want to retrieve.
point – a Point3d object if successful
There are 8 corners to a bounding box, identified by the numbers 0 through 7. Points are returned in the currently set units (inches, by default).
# Add two points
point = Geom::Point3d.new(100,200,300)
point2 = Geom::Point3d.new(300,200,100)
model = Sketchup.active_model
bbox = boundingbox.add point
bbox = boundingbox.add point2
# Returns a point in the form of three coordinates, such as 6”, 0”, 0”
point = bbox.corner 1
depth
The depth method is used to retrieve the depth of the bounding box.
depth = boundingbox.depth
depth – the depth of the bounding box if successful
The depth is returned in the currently set units (inches, by default).
# Add two points
point = Geom::Point3d.new(100,200,300)
point2 = Geom::Point3d.new(300,200,100) model = Sketchup.active_model
bbox = boundingbox.add point
bbox = boundingbox.add point2
# Returns the depth
depth = boundingbox.depth
The diagonal method is used to get the length of the diagonal of the bounding box.
diagonal = boundingbox.diagonal
diagonal – the size of the diagonal for the bounding box if successful
The diagonal is returned in the currently set units (inches, by default).
# Add two pointspoint = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2# Returns the diagonaldiagonal = boundingbox.diagonal
The empty? method is used to determine if a bounding box is empty (such as if the bounds have not been set.)
status = boundingbox.empty?
status – true if the bounding box is empty, false if it is not empty
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2status = boundingbox.empty?if (status)# If true, bounding box is empty…else# If false, bounding box contains entitiesend
The height method is used to retrieve the height of the bounding box.
height = boundingbox.height
height – the height of the bounding box
The height is returned in the currently set units (inches, by default).
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2
The intersect method is used to retrieve a bounding box that is the result of intersecting one bounding box with another.
boundingbox = boundingbox1.intersect boundingbox2
boundingbox2 – a second boundbox which might intersect boundingbox1
boundingbox – the resulting BoundingBox object if successful, an empty BoundingBox object if unsuccessful.
Adding one point to an empty bounding box, does not increase the size of the bounding box. You must add at least two points before methods, such as BoundingBox.diagonal, will return a size greater than zero.
The max method is used to retrieve the Point3d object where x, y and z are maximum in the bounding box.
point = boundingbox.max
point – a Point3d object representing the point where x, y, and z are the maximum in the bounding box.
If you attempt to call the max method on an empty bounding box, you will receive a very large negative number.
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2point = boundingbox.maxUI.messagebox.point
The min method is used to retrieve the Point3d where x, y and z are minimum in the bounding box.
point = boundingbox.max
point – a Point3d object representing the point where x, y, and z are the maximum in the bounding box.
If you attempt to call the max method on an empty bounding box, you will receive a very large positive number.
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2point = boundingbox.minUI.messagebox.point
The valid method is used to determine if a bounding box is valid (contains points).
status = boundingbox.valid?
status – true if the bounding box is valid (not empty), false if it is not valid (empty)
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2status = boundingbox.valid?if (status)# If true, bounding box is empty…else# If false, bounding box contains entitiesend
The width method is used to retrieve the width of the bounding box.
width = boundingbox.width
width – the width of the bounding box if successful
The diagonal is returned in the currently set units (inches, by default).
point = Geom::Point3d.new(100,200,300)point2 = Geom::Point3d.new(300,200,100)model = Sketchup.active_modelbbox = boundingbox.add pointbbox = boundingbox.add point2# Returns the widthwidth = boundingbox.widthUI.messagebox.width