From 70be304f017503d948db3d5f983a5cdd0bdd365a Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Thu, 30 Sep 2021 01:22:43 -0700 Subject: [PATCH 1/2] Make use of builtin cube(), cylinder(), sphere(), square(), circle(). --- builtins.scad | 2 +- shapes2d.scad | 47 +++++++++++++++++++++++++++++++++++++---------- shapes3d.scad | 41 ++++++++++++++++++----------------------- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/builtins.scad b/builtins.scad index 3a08481..d4f46ee 100644 --- a/builtins.scad +++ b/builtins.scad @@ -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); diff --git a/shapes2d.scad b/shapes2d.scad index 72d5871..c5ef294 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -14,15 +14,19 @@ // include ////////////////////////////////////////////////////////////////////// +use + // 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: diff --git a/shapes3d.scad b/shapes3d.scad index ab01138..49dc28a 100644 --- a/shapes3d.scad +++ b/shapes3d.scad @@ -9,6 +9,8 @@ // include ////////////////////////////////////////////////////////////////////// +use + // 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) = From 337d51be1581897696e56b4b4c701115bdf8331b Mon Sep 17 00:00:00 2001 From: Garth Minette Date: Thu, 30 Sep 2021 01:45:53 -0700 Subject: [PATCH 2/2] Builtin sphere() module does not have a center= argument. --- builtins.scad | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtins.scad b/builtins.scad index d4f46ee..b1dd295 100644 --- a/builtins.scad +++ b/builtins.scad @@ -23,7 +23,7 @@ module _cube(size,center) cube(size,center=center); module _cylinder(h,r1,r2,center,r,d,d1,d2) cylinder(h,r=r,d=d,r1=r1,r2=r2,d1=d1,d2=d2,center=center); -module _sphere(r,center,d) sphere(r=r,d=d,center=center); +module _sphere(r,d) sphere(r=r,d=d);