Make use of builtin cube(), cylinder(), sphere(), square(), circle().

This commit is contained in:
Garth Minette 2021-09-30 01:22:43 -07:00
parent 5036a84b52
commit 70be304f01
3 changed files with 56 additions and 34 deletions

View file

@ -17,7 +17,7 @@ module _text(t,size,font,halign,valign,spacing,direction,language,script)
halign=halign, valign=valign,
spacing=spacing, direction=direction,
language=language, script=script
;)
);
module _cube(size,center) cube(size,center=center);

View file

@ -14,15 +14,19 @@
// include <BOSL2/std.scad>
//////////////////////////////////////////////////////////////////////
use <BOSL2/builtins.scad>
// Section: 2D Primitives
// Function&Module: square()
// Topics: Shapes (2D), Path Generators (2D)
// Usage: As a Built-in Module
// square(size, [center]);
// Usage: As a Module
// square(size, [center], ...);
// Usage: With Attachments
// square(size, [center], ...) { attachables }
// Usage: As a Function
// path = square(size, [center]);
// path = square(size, [center], ...);
// See Also: rect()
// Description:
// When called as the builtin module, creates a 2D square or rectangle of the given size.
@ -31,8 +35,8 @@
// size = The size of the square to create. If given as a scalar, both X and Y will be the same size.
// center = If given and true, overrides `anchor` to be `CENTER`. If given and false, overrides `anchor` to be `FRONT+LEFT`.
// ---
// anchor = (Function only) Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER`
// spin = (Function only) Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER`
// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// Example(2D):
// square(40);
// Example(2D): Centered
@ -54,6 +58,16 @@ function square(size=1, center, anchor, spin=0) =
) reorient(anchor,spin, two_d=true, size=size, p=path);
module square(size=1, center, anchor, spin) {
anchor = get_anchor(anchor, center, [-1,-1], [-1,-1]);
size = is_num(size)? [size,size] : point2d(size);
attachable(anchor,spin, size=size, two_d=true) {
_square(size, center=true);
children();
}
}
// Function&Module: rect()
// Usage: As Module
@ -161,8 +175,10 @@ function rect(size=1, center, rounding=0, chamfer=0, anchor, spin=0) =
// Function&Module: circle()
// Topics: Shapes (2D), Path Generators (2D)
// Usage: As a Built-in Module
// Usage: As a Module
// circle(r|d=, ...);
// Usage: With Attachments
// circle(r|d=, ...) { attachables }
// Usage: As a Function
// path = circle(r|d=, ...);
// See Also: oval()
@ -173,8 +189,8 @@ function rect(size=1, center, rounding=0, chamfer=0, anchor, spin=0) =
// r = The radius of the circle to create.
// d = The diameter of the circle to create.
// ---
// anchor = (Function only) Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER`
// spin = (Function only) Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// anchor = Translate so anchor point is at origin (0,0,0). See [anchor](attachments.scad#anchor). Default: `CENTER`
// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#spin). Default: `0`
// Example(2D): By Radius
// circle(r=25);
// Example(2D): By Diameter
@ -188,12 +204,23 @@ function circle(r, d, anchor=CENTER, spin=0) =
path = [for (i=[0:1:sides-1]) let(a=360-i*360/sides) r*[cos(a),sin(a)]]
) reorient(anchor,spin, two_d=true, r=r, p=path);
module circle(r, d, anchor=CENTER, spin=0) {
r = get_radius(r=r, d=d, dflt=1);
attachable(anchor,spin, r=r) {
_circle(r=r);
children();
}
}
// Function&Module: oval()
// Usage:
// oval(r|d=, [realign=], [circum=])
// Usage: As a Module
// oval(r|d=, [realign=], [circum=], ...);
// Usage: With Attachments
// oval(r|d=, [realign=], [circum=], ...) { attachables }
// Usage: As a Function
// path = oval(r|d=, [realign=], [circum=], ...);
// Topics: Shapes (2D), Paths (2D), Path Generators, Attachable
// See Also: circle()
// Description:

View file

@ -9,6 +9,8 @@
// include <BOSL2/std.scad>
//////////////////////////////////////////////////////////////////////
use <builtins.scad>
// Section: Cuboids, Prismoids and Pyramids
@ -52,11 +54,7 @@ module cube(size=1, center, anchor, spin=0, orient=UP)
anchor = get_anchor(anchor, center, ALLNEG, ALLNEG);
size = scalar_vec3(size);
attachable(anchor,spin,orient, size=size) {
if (size.z > 0) {
linear_extrude(height=size.z, center=true, convexity=2) {
square([size.x,size.y], center=true);
}
}
_cube(size, center=true);
children();
}
}
@ -935,23 +933,8 @@ module cylinder(h, r1, r2, center, l, r, d, d1, d2, anchor, spin=0, orient=UP)
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=1);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=1);
l = first_defined([h, l, 1]);
sides = segs(max(r1,r2));
attachable(anchor,spin,orient, r1=r1, r2=r2, l=l) {
if (r1 > r2) {
if (l > 0) {
linear_extrude(height=l, center=true, convexity=2, scale=r2/r1) {
circle(r=r1);
}
}
} else {
zflip() {
if (l > 0) {
linear_extrude(height=l, center=true, convexity=2, scale=r1/r2) {
circle(r=r2);
}
}
}
}
_cylinder(h=l, r1=r1, r2=r2, center=true);
children();
}
}
@ -1506,8 +1489,20 @@ module pie_slice(
// Example: Called as Function
// vnf = sphere(d=100, style="icosa");
// vnf_polyhedron(vnf);
module sphere(r, d, circum=false, style="orig", anchor=CENTER, spin=0, orient=UP)
spheroid(r=r, d=d, circum=circum, style=style, anchor=anchor, spin=spin, orient=orient) children();
module sphere(r, d, circum=false, style="orig", anchor=CENTER, spin=0, orient=UP) {
r = get_radius(r=r, d=d, dflt=1);
if (!circum && style=="orig") {
attachable(anchor,spin,orient, r=r) {
_sphere(r=r);
children();
}
} else {
spheroid(
r=r, circum=circum, style=style,
anchor=anchor, spin=spin, orient=orient
) children();
}
}
function sphere(r, d, circum=false, style="orig", anchor=CENTER, spin=0, orient=UP) =