2019-06-08 17:10:47 -04:00
//
// NopSCADlib Copyright Chris Palmer 2018
// nop.head@gmail.com
// hydraraptor.blogspot.com
//
// This file is part of NopSCADlib.
//
// NopSCADlib is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// NopSCADlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with NopSCADlib.
// If not, see <https://www.gnu.org/licenses/>.
//
//
//! Global constants, functions and modules. This file is used directly or indirectly in every scad file.
//
include < ../../global_defs.scad >
2019-06-27 06:10:37 -04:00
function inch ( x ) = x * 25.4 ; //! Inch to mm conversion (For fractional inches, 'inch(1 + 7/8)' will work as expected.)
function foot ( x ) = x * 25.4 * 12 ; //! Foot to mm conversion
function yard ( x ) = x * 25.4 * 12 * 3 ; //! Yard to mm conversion
function mm ( x ) = x ; //! Explicit mm specified
function cm ( x ) = x * 10.0 ; //! cm to mm conversion
function m ( x ) = x * 1000.0 ; //! m to mm conversion
2020-12-24 11:04:59 -05:00
function sqr ( x ) = x * x ; //! Returns the square of `x`
2019-06-08 17:10:47 -04:00
function echoit ( x ) = echo ( x ) x ; //! Echo expression and return it, useful for debugging
2020-09-06 06:51:33 -04:00
function no_point ( str ) = chr ( [ for ( c = str ( str ) ) if ( c = = "." ) ord ( "p" ) else ord ( c ) ] ) ; //! Replace decimal point in string with 'p'
2020-12-24 11:04:59 -05:00
function in ( list , x ) = ! ! len ( [ for ( v = list ) if ( v = = x ) true ] ) ; //! Returns true if `x` is an element in the `list`
function Len ( x ) = is_list ( x ) ? len ( x ) : 0 ; //! Returns the length of a list or 0 if `x` is not a list
2019-06-08 17:10:47 -04:00
function r2sides ( r ) = $fn ? $fn : ceil ( max ( min ( 360 / $fa , r * 2 * PI / $fs ) , 5 ) ) ; //! Replicates the OpenSCAD logic to calculate the number of sides from the radius
function r2sides4n ( r ) = floor ( ( r2sides ( r ) + 3 ) / 4 ) * 4 ; //! Round up the number of sides to a multiple of 4 to ensure points land on all axes
2020-11-04 16:44:07 -05:00
function limit ( x , min , max ) = max ( min ( x , max ) , min ) ; //! Force x in range min <= x <= max
2019-06-08 17:10:47 -04:00
module translate_z ( z ) translate ( [ 0 , 0 , z ] ) children ( ) ; //! Shortcut for Z only translations
2020-11-20 03:46:17 -05:00
module vflip ( flip = true ) rotate ( [ flip ? 180 : 0 , 0 , 0 ] ) children ( ) ; //! Invert children by doing a 180° flip around the X axis
module hflip ( flip = true ) rotate ( [ 0 , flip ? 180 : 0 , 0 ] ) children ( ) ; //! Invert children by doing a 180° flip around the Y axis
2019-06-08 17:10:47 -04:00
module ellipse ( xr , yr ) scale ( [ 1 , yr / xr ] ) circle4n ( xr ) ; //! Draw an ellipse
2020-11-04 16:44:07 -05:00
function slice_str ( str , start , end , s = "" ) = start >= end ? s : slice_str ( str , start + 1 , end , str ( s , str [ start ] ) ) ; // Helper for slice()
function slice ( list , start = 0 , end = undef ) = let ( //! Slice a list or string with Python type semantics
len = len ( list ) ,
start = limit ( start < 0 ? len + start : start , 0 , len ) ,
end = is_undef ( end ) ? len : limit ( end < 0 ? len + end : end , 0 , len )
) is_string ( list ) ? slice_str ( list , start , end ) : [ for ( i = [ start : 1 : end - 1 ] ) list [ i ] ] ;
2020-12-24 11:04:59 -05:00
module render_if ( render = true , convexity = 2 ) //! Renders an object if `render` is true, otherwise leaves it unrendered
2020-12-20 03:53:42 -05:00
if ( render )
render ( convexity = convexity )
children ( ) ;
else
children ( ) ;
2020-12-24 11:04:59 -05:00
module extrude_if ( h , center = true ) //! Extrudes 2D object to 3D when `h` is nonzero, otherwise leaves it 2D
2019-06-08 17:10:47 -04:00
if ( h )
2020-03-29 15:18:57 -04:00
linear_extrude ( h , center = center , convexity = 2 ) // 3D
2019-06-08 17:10:47 -04:00
children ( ) ;
else
children ( ) ; // 2D
module circle4n ( r , d = undef ) { //! Circle with multiple of 4 vertices
R = is_undef ( d ) ? r : d / 2 ;
circle ( R , $fn = r2sides4n ( R ) ) ;
}
module semi_circle ( r , d = undef ) //! A semi circle in the positive Y domain
intersection ( ) {
R = is_undef ( d ) ? r : d / 2 ;
circle4n ( R ) ;
sq = R + 1 ;
translate ( [ - sq , 0 ] )
square ( [ 2 * sq , sq ] ) ;
}
2020-12-24 11:04:59 -05:00
module right_triangle ( width , height , h , center = true ) //! A right angled triangle with the 90° corner at the origin. 3D when `h` is nonzero, otherwise 2D
2019-07-29 15:43:03 -04:00
extrude_if ( h , center = center )
polygon ( points = [ [ 0 , 0 ] , [ width , 0 ] , [ 0 , height ] ] ) ;
2019-06-08 17:10:47 -04:00
include < sphere.scad >
include < bom.scad >
include < polyholes.scad >
include < teardrops.scad >
include < rounded_rectangle.scad >
include < clip.scad >