Re-worked generic-helpers.rounded_square

* Now behaves more like rounded_rectangle, but creates 2d or 3d items.
* Deprecated rounded_rectangle.
* Added significant input checks to ensure values are valid.
This commit is contained in:
Arthur Moore 2024-05-02 00:26:54 -04:00
parent 388cb8dbab
commit 7aebad420c
2 changed files with 51 additions and 12 deletions

View file

@ -7,15 +7,50 @@ function clp(x,a,b) = min(max(x,a),b);
function is_even(number) = (number%2)==0; function is_even(number) = (number%2)==0;
module rounded_rectangle(length, width, height, rad) { /**
linear_extrude(height) * @brief Create `square`, with rounded corners.
offset(rad) * @param size Same as `square`. See details for differences.
offset(-rad) * @param radius Radius of the corners. 0 is the same as just calling `square`
square([length,width], center = true); * @param center Same as `square`.
* @details "size" accepts both the standard number or a 2d vector the same as `square`.
* However, if passed a 3d vector, this will apply a `linear_extrude` to the resulting shape.
*/
module rounded_square(size, radius, center = true) {
assert(is_num(size) ||
(is_list(size) && (
(len(size) == 2 && is_num(size.x) && is_num(size.y)) ||
(len(size) == 3 && is_num(size.x) && is_num(size.y) && is_num(size.z))
))
);
assert(is_num(radius) && radius >= 0 && is_bool(center));
// Make sure something is produced.
if (is_num(size)) {
assert((size/2) > radius);
} else {
assert((size.x/2) > radius && (size.y/2 > radius));
if (len(size) == 3) {
assert(size.z > 0);
}
} }
module rounded_square(length, height, rad) { if (is_list(size) && len(size) == 3) {
rounded_rectangle(length, length, height, rad); linear_extrude(size.z)
offset(radius)
offset(-radius)
square([size.x, size.y], center = center);
} else {
offset(radius)
offset(-radius)
square(size, center = center);
}
}
/**
* @deprecated Use rounded_square(...)
*/
module rounded_rectangle(length, width, height, rad) {
rounded_square([length, width, height], rad);
} }
module copy_mirror(vec=[0,1,0]) { module copy_mirror(vec=[0,1,0]) {

View file

@ -219,13 +219,17 @@ module block_magnet_blank(o = 0, half = true) {
module block_base_blank(o = 0) { module block_base_blank(o = 0) {
mirror([0,0,1]) { mirror([0,0,1]) {
hull() { hull() {
rounded_square(l_grid-o-0.05-2*r_c2-2*r_c1, h_base, r_fo3); linear_extrude(h_base)
rounded_square(l_grid-o-0.05-2*r_c2, h_base-r_c1, r_fo2); rounded_square(l_grid-o-0.05-2*r_c2-2*r_c1, r_fo3);
linear_extrude(h_base-r_c1)
rounded_square(l_grid-o-0.05-2*r_c2, r_fo2);
} }
hull() { hull() {
rounded_square(l_grid-o-0.05-2*r_c2, r_c2, r_fo2); linear_extrude(r_c2)
rounded_square(l_grid-o-0.05-2*r_c2, r_fo2);
mirror([0,0,1]) mirror([0,0,1])
rounded_square(l_grid-o-0.05, d_bottom, r_fo1); linear_extrude(d_bottom)
rounded_square(l_grid-o-0.05, r_fo1);
} }
} }
} }