SolidCode / SolidPython

A python frontend for solid modelling that compiles to OpenSCAD
1.12k stars 174 forks source link

Get a bounding box from an object #144

Closed rockstorm101 closed 4 years ago

rockstorm101 commented 4 years ago

This might be a way too crazy request or I might be missing something very obvious. Is there anyway to get the bounding box of a solidpython object? Ideally something like:

>>> import solid as sp
>>> ball = sp.sphere(10)
>>> ball.bounding_box
[20,20,20]

I know it's not that useful for objects one creates, but it'd be handy for imported "third-party" objects.

etjones commented 4 years ago

It’s not at all crazy, and I see the lack of introspectability in OpenSCAD (e.g., “How big is this object and where is it in space?”) as the fundamental limiting factor of the system. But yeah, OpenSCAD has minimal introspectability, so SolidPython also lacks a robust capability for tracking an object’s size.

However, there is a stub solid.utils.BoundingBox class I wrote years ago and then didn’t really follow up on. So I think you should be able to do something like:

from solid import *
from solid.utils import right, BoundingBox

c = right(2)(cube(10, center=True))
bb = BoundingBox(c)
print(bb)

and get something like (-3,-5,-5), (7,5,5)

I’m on mobile right now, so this might not be letter-perfect, but that much exists.

There are a bunch of problems with this, though. BoundingBox (& OpenSCAD) has no way of knowing how big an imported object is. And while we can say that the BoundingBox of a union of two objects is the union of both objects’ bonding boxes, that’s definitely not true for the intersection of two bounding boxes. Or, if we rotate an object, what happens to its bounding box?

I’m not really sure how to answer these questions without a geometry engine, which SP doesn’t have and isn’t likely to. At the point where somebody starts asking about things like bounding boxes, I usually tell people they’ve outgrown SolidPython/OpenSCAD and that might want to think about a more full-featured geometry system like Blender or commercial CAD. So...maybe you’ve graduated? Congratulations?

If you’ve got any ideas about how to solve these issues, I’d love to hear them. SP/OpenSCAD is fun and capable, and I’m always bummed out when I run into the edges of its capabilities like this.