2019-04-17 02:16:50 +00:00
//////////////////////////////////////////////////////////////////////
// LibFile: primitives.scad
// The basic built-in shapes, reworked to integrate better with
2019-04-19 06:45:46 +00:00
// other BOSL2 library shapes and utilities.
2019-04-17 02:16:50 +00:00
// To use, add the following lines to the beginning of your file:
// ```
2019-04-19 07:25:10 +00:00
// include <BOSL2/std.scad>
2019-04-17 02:16:50 +00:00
// ```
//////////////////////////////////////////////////////////////////////
/*
BSD 2 - Clause License
2019-04-20 00:02:17 +00:00
Copyright ( c ) 2017 - 2019 , Revar Desmera
2019-04-17 02:16:50 +00:00
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
* Redistributions of source code must retain the above copyright notice , this
list of conditions and the following disclaimer .
* Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL
DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY ,
OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* /
2019-04-24 02:13:43 +00:00
// Section: 2D Primitives
// Module: square()
// Usage:
// square(size, [center], [anchor])
// Description:
// Creates a 2D square of the given size.
// Arguments:
// 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 = The side of the square to center on the origin. Default: `FRONT+LEFT`
2019-04-24 10:33:22 +00:00
// Example(2D):
// square(40);
// Example(2D): Centered
// square([40,30], center=true);
// Example(2D): Anchoring
// square([40,30], anchor=FRONT);
2019-04-24 02:13:43 +00:00
module square ( size , center = undef , anchor = FRONT + LEFT ) {
size = is_num ( size ) ? [ size , size ] : point2d ( size ) ;
s = size / 2 ;
pts = [ [ - s . x , - s . y ] , [ - s . x , s . y ] , [ s . x , s . y ] , [ s . x , - s . y ] ] ;
orient_and_anchor ( point3d ( size ) , ORIENT_Z , anchor , center , noncentered = FRONT + LEFT , two_d = true , chain = true ) {
polygon ( pts ) ;
children ( ) ;
}
}
// Module: circle()
// Usage:
// circle(r|d, [anchor])
// Description:
// Creates a 2D circle of the given size.
// Arguments:
// r = The radius of the circle to create.
// d = The diameter of the circle to create.
// anchor = The side of the circle to center on the origin. Default: `CENTER`
2019-04-24 10:33:22 +00:00
// Example(2D): By Radius
// circle(r=25);
// Example(2D): By Diameter
// circle(d=50);
// Example(2D): Anchoring
// circle(d=50, anchor=FRONT);
2019-04-24 02:13:43 +00:00
module circle ( r = undef , d = undef , anchor = CENTER ) {
r = get_radius ( r = r , d = d , dflt = 1 ) ;
sides = segs ( r ) ;
pts = [ for ( a = [ 0 : 360 / sides : 360 - EPSILON ] ) r * [ cos ( a ) , sin ( a ) ] ] ;
orient_and_anchor ( [ 2 * r , 2 * r , 0 ] , ORIENT_Z , anchor , geometry = "cylinder" , two_d = true , chain = true ) {
polygon ( pts ) ;
children ( ) ;
}
}
2019-04-17 02:16:50 +00:00
// Section: Primitive Shapes
// Module: cube()
//
// Description:
2019-04-23 03:55:03 +00:00
// Creates a cube object, with support for anchoring and attachments.
2019-04-17 02:16:50 +00:00
// This is a drop-in replacement for the built-in `cube()` module.
//
// Arguments:
// size = The size of the cube.
2019-04-23 03:55:03 +00:00
// anchor = The side of the origin to anchor to. Use constants from `constants.scad`. Default: `ALLNEG`
// center = If given, overrides `anchor`. A true value sets `anchor=CENTER`, false sets `anchor=ALLNEG`.
2019-04-17 02:16:50 +00:00
//
// Example: Simple regular cube.
// cube(40);
2019-04-22 08:08:41 +00:00
// Example: Rectangular cube.
2019-04-17 02:16:50 +00:00
// cuboid([20,40,50]);
2019-04-22 08:08:41 +00:00
// Example: Standard Connectors.
2019-04-23 03:55:03 +00:00
// cube(40, center=true) show_anchors();
module cube ( size , center = undef , anchor = ALLNEG )
2019-04-17 02:16:50 +00:00
{
size = scalar_vec3 ( size ) ;
2019-04-23 03:55:03 +00:00
orient_and_anchor ( size , ORIENT_Z , anchor , center , noncentered = ALLPOS , chain = true ) {
2019-04-17 02:16:50 +00:00
linear_extrude ( height = size . z , convexity = 2 , center = true ) {
square ( [ size . x , size . y ] , center = true ) ;
}
children ( ) ;
}
}
// Module: cylinder()
// Usage:
2019-04-23 03:55:03 +00:00
// cylinder(h, r|d, [center], [orient], [anchor]);
// cylinder(h, r1/d1, r2/d2, [center], [orient], [anchor]);
2019-04-17 02:16:50 +00:00
// Description:
2019-04-23 03:55:03 +00:00
// Creates a cylinder object, with support for anchoring and attachments.
2019-04-17 02:16:50 +00:00
// This is a drop-in replacement for the built-in `cylinder()` module.
// Arguments:
// l / h = The height of the cylinder.
// r = The radius of the cylinder.
// r1 = The bottom radius of the cylinder. (Before orientation.)
// r2 = The top radius of the cylinder. (Before orientation.)
// d = The diameter of the cylinder.
// d1 = The bottom diameter of the cylinder. (Before orientation.)
// d2 = The top diameter of the cylinder. (Before orientation.)
// orient = Orientation of the cylinder. Use the `ORIENT_` constants from `constants.scad`. Default: vertical.
2019-04-23 03:55:03 +00:00
// anchor = The side of the part to anchor to the origin. Use constants from `constants.scad`. Default: `UP`
// center = If given, overrides `anchor`. A true value sets `anchor=CENTER`, false sets `anchor=BOTTOM`.
2019-04-17 02:16:50 +00:00
// Example: By Radius
// xdistribute(30) {
// cylinder(h=40, r=10);
// cylinder(h=40, r1=10, r2=5);
// }
// Example: By Diameter
// xdistribute(30) {
// cylinder(h=40, d=25);
// cylinder(h=40, d1=25, d2=10);
// }
2019-04-22 08:08:41 +00:00
// Example: Standard Connectors
// xdistribute(40) {
2019-04-23 03:55:03 +00:00
// cylinder(h=30, d=25) show_anchors();
// cylinder(h=30, d1=25, d2=10) show_anchors();
2019-04-22 08:08:41 +00:00
// }
2019-04-23 03:55:03 +00:00
module cylinder ( r = undef , d = undef , r1 = undef , r2 = undef , d1 = undef , d2 = undef , h = undef , l = undef , center = undef , orient = ORIENT_Z , anchor = BOTTOM )
2019-04-17 02:16:50 +00:00
{
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 ] ) ;
sides = segs ( max ( r1 , r2 ) ) ;
size = [ r1 * 2 , r1 * 2 , l ] ;
2019-04-23 03:55:03 +00:00
orient_and_anchor ( size , orient , anchor , center , size2 = [ r2 * 2 , r2 * 2 ] , noncentered = BOTTOM , geometry = "cylinder" , chain = true ) {
2019-04-17 02:16:50 +00:00
linear_extrude ( height = l , scale = r2 / r1 , convexity = 2 , center = true ) {
circle ( r = r1 , $fn = sides ) ;
}
children ( ) ;
}
}
// Module: sphere()
// Usage:
2019-04-23 03:55:03 +00:00
// sphere(r|d, [orient], [anchor])
2019-04-17 02:16:50 +00:00
// Description:
2019-04-23 03:55:03 +00:00
// Creates a sphere object, with support for anchoring and attachments.
2019-04-17 02:16:50 +00:00
// This is a drop-in replacement for the built-in `sphere()` module.
// Arguments:
// r = Radius of the sphere.
// d = Diameter of the sphere.
// orient = Orientation of the sphere, if you don't like where the vertices lay. Use the `ORIENT_` constants from `constants.scad`. Default: `ORIENT_Z`.
2019-04-23 03:55:03 +00:00
// anchor = Alignment of the sphere. Use the constants from `constants.scad`. Default: `CENTER`.
2019-04-22 08:08:41 +00:00
// Example: By Radius
// sphere(r=50);
// Example: By Diameter
// sphere(d=100);
// Example: Standard Connectors
2019-04-23 03:55:03 +00:00
// sphere(d=50) show_anchors();
module sphere ( r = undef , d = undef , orient = ORIENT_Z , anchor = CENTER )
2019-04-17 02:16:50 +00:00
{
r = get_radius ( r = r , d = d , dflt = 1 ) ;
sides = segs ( r ) ;
size = [ r * 2 , r * 2 , r * 2 ] ;
2019-04-23 03:55:03 +00:00
orient_and_anchor ( size , orient , anchor , geometry = "sphere" , chain = true ) {
2019-04-17 02:16:50 +00:00
rotate_extrude ( convexity = 2 ) {
difference ( ) {
circle ( r = r , $fn = sides ) ;
left ( r + 0.1 ) square ( r * 2 + 0.2 , center = true ) ;
}
}
children ( ) ;
}
}
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap