 ### Billiam

"Hey, sorry I haven't updated in a while. Life's been crazy, but I'll be back soon."

Etsy Github Itch.io Printables Mastodon Twitter

I’ve been using CascadeStudio recently for parametric modeling (like this) and needed a way to center some dynamic text, which isn’t implemented currently.

Fortunately, a user on github documented a way to get the boundary box of a solid shape here: this discussion post, which makes centered (or right-aligned) text easy.

// https://github.com/zalo/CascadeStudio/discussions/86#discussioncomment-506883
const getBounds = shape => {
const bmin = { x: Infinity, y: Infinity, z: Infinity },
bmax = { x: -Infinity, y: -Infinity, z: -Infinity };

ForEachFace(shape, (index, face) => {
ForEachVertex(face, (vertex) => {
const pnt = oc.BRep_Tool.prototype.Pnt(vertex);
const x = pnt.X(), y = pnt.Y(), z = pnt.Z();

if (x < bmin.x) bmin.x = x;
if (y < bmin.y) bmin.y = y;
if (z < bmin.z) bmin.z = z;

if (x > bmax.x) bmax.x = x;
if (y > bmax.y) bmax.y = y;
if (z > bmax.z) bmax.z = z;
});
});
return [bmin, bmax];
}

// create the text shape
const textShape = Text3D("hello!", 10, 0.1);

// get the minimum and maximum bounds for the text
const [min, max] = getBounds(textShape);
const width = max.x - min.x;
const height = max.z - min.z;

// translate the text by half the width and height
Translate(
[-width / 2, 0, -height / 2],
textShape,
false);