I’m trying to figure how to get the bounding box of multiple entities (edges and faces) within a group. Is there a way to do this?
Ultimately I am actually only trying to the find the min and max point of this bounding box inside or within the group’s coordinate system. However, I need to include all of the geometry within the group so that I end up with the correct min and max point. For some reason I can’t seem to do this.
If you only want to include edges and faces to bounding box, you really need to add only the edges, since faces can not exists without bounding edges, but edges can be there without faces.
This snippet will return you new bounding box object containing only edges within a given group as parameter, its corners and min max will be in a global coordinates.
Let us say that the entities in question are in an array referenced as targets:
bounds = targets.map(&:bounds)
bbox = Geom::BoundingBox.new.add(*bounds)
max = bbox.max
min = bbox.min
If the active edit context is the group, the API methods return points in model coordinates. If the parent of the group is the edit context, the API methods return points in the parent’s coordinate system, and so on …
So you would need to transform the points.
active = Sketchup.active_model.active_path
if active # nil if at top-level
path = Sketchup::InstancePath.new(active)
t = path.transformation.inverse
# Use the bang method to modify the points:
max.transform!(t)
min.transform!(t)
end
model = Sketchup.active_model
active = model.active_path
if active # nil if at top-level
t = model.edit_transform.inverse
# Use the bang method to modify the points:
max.transform!(t)
min.transform!(t)
end