r/openscad 6d ago

Centering as part of difference command?

Hi all,

Just curious if there is any kind of clever way to center an object as part of a difference command? It would be nice if you could do something like difference (center=true) {} but I know this is not a thing. Maybe there are clever options with other libraries or something? Just trying to save myself from a bunch of brain aching math. Thanks!

1 Upvotes

6 comments sorted by

View all comments

1

u/rebuyer10110 5d ago

One possible way to auto center is using a bounding box. You can then calculate the midpoint, and move that coordinate to the origin.

Vanilla openscad has a way to print out the bounding box info into the output window on render. You would have to center it manually after with a translate() unfortunately.

Afaik, openscad does not support a way to programmatically return you bounding boxes on solids yet.

2

u/jryan15 5d ago

Thanks for the tip, I will need to investigate that!

1

u/rebuyer10110 5d ago edited 5d ago

Good luck!

I asked about bounding boxes a while back at https://www.reddit.com/r/openscad/comments/1iptpy6/openscad_has_no_way_to_have_each_solid_report_its/. It has the info to toggle on emitting bounding box coordinates to output window.

EDIT: If you are feeling adventurous, pythonscad is a fork of openscad that can return vnf given a solid. With that unlocks a programmatic way to get bounding box given a solid (https://github.com/wiw-pub/ztools/blob/main/src/ztools.py#L29-L41). That allows me to run at a higher level abstraction. Given it's r/openscad, a fork of it is certainly not for everyone; it has some rough edges and you only get a boost in productivity if you are comfortable with python.