From 85ceeaee95cc0c8d39f596058b2212e5b989bfde Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Fri, 1 Dec 2017 13:55:57 +0100 Subject: [PATCH] Initial Drop of Scaddy. Signed-off-by: Tony Tkacik --- 2d.scad | 13 +++++++++++++ aliases.scad | 23 +++++++++++++++++++++++ component.scad | 13 +++++++++++++ config.scad | 7 +++++++ debug.scad | 44 ++++++++++++++++++++++++++++++++++++++++++++ functions.scad | 6 ++++++ polyhedron.scad | 31 +++++++++++++++++++++++++++++++ preconditions.scad | 14 ++++++++++++++ scaddy.scad | 8 ++++++++ utils.scad | 33 +++++++++++++++++++++++++++++++++ vector.scad | 36 ++++++++++++++++++++++++++++++++++++ 11 files changed, 228 insertions(+) create mode 100644 2d.scad create mode 100644 aliases.scad create mode 100644 component.scad create mode 100644 config.scad create mode 100644 debug.scad create mode 100644 functions.scad create mode 100644 polyhedron.scad create mode 100644 preconditions.scad create mode 100644 scaddy.scad create mode 100644 utils.scad create mode 100644 vector.scad diff --git a/2d.scad b/2d.scad new file mode 100644 index 0000000..f5cccdd --- /dev/null +++ b/2d.scad @@ -0,0 +1,13 @@ +include + +module fillet(r) { + offset(r = -r,$fn=circle_details(r)) { + offset(delta = r,$fn=32,$fn=circle_details(r)) { + children(); + } + } +} + +module dcircle(r) { + circle(r,$fn=circle_details(r)); +} diff --git a/aliases.scad b/aliases.scad new file mode 100644 index 0000000..b19da3d --- /dev/null +++ b/aliases.scad @@ -0,0 +1,23 @@ +module move(vec) { + translate(vec) children(); +} + +module extrude(height = 100, center = true, convexity = 10, twist = 0, slices = 20, scale = 1.0) { + linear_extrude(height = height, center = center, convexity = convexity, twist = twist, slices = slices, scale = scale) children(); +} + +module revolve(angle = 360, convexity = 2) { + rotate_extrude(angle = angle, convexity = convexity) children(); +} + +module 2d_front() { + rotate(x(90)) children(); +} + +module 2d_left() { + rotate(z(-90)) 2d_front() children(); +} + +module 2d_right() { + rotate(z(90)) 2d_front() children(); +} diff --git a/component.scad b/component.scad new file mode 100644 index 0000000..89c8056 --- /dev/null +++ b/component.scad @@ -0,0 +1,13 @@ + +module component_get(name) { + component_display(name) children(); +} + +module component_display(name) { + $scaddy_component = name; + children(); +} + +module component(name) { + if($scaddy_component == undef || $scaddy_component == name) children(); +} diff --git a/config.scad b/config.scad new file mode 100644 index 0000000..642c19f --- /dev/null +++ b/config.scad @@ -0,0 +1,7 @@ +/* [makereal SCAD Common Configuration] */ + +// Minimum polygon count for curve +CURVE_POLYGON_MINIMUM=16; + +// Maximum length of curve polygons. +CURVE_POLYGON_LENGTH=1; // [0:100] diff --git a/debug.scad b/debug.scad new file mode 100644 index 0000000..dc8b22b --- /dev/null +++ b/debug.scad @@ -0,0 +1,44 @@ +module debug(force=false) { + if($scaddy_debug || force ) { + $scaddy_debug=true; + %children(); + } +} + +module _debug_mesh() { + %color([255,0,0,0.4]) children(); +} + +module debug_points(vector) { + _debug_mesh() + for(i = [0:1:len(vector)-1]) + translate(vector[i]) { + sphere(1); + text(str(i),size=4); + } +} + + +module xy_axis(length=20,width=0.5) { + _debug_mesh() + move((-width/2)*[1,1,0]) + extrude(width) { + square([width,length]); + square([length,width]); + move(x(length)-y(1)) + text("x",halign="right",valign="top",size=5); + move(y(length)-x(1)) + text("y",valign="top",halign="right",size=5); + } +} + +module xyz_axis(length=20,width=0.5) { + xy_axis(length,width); + _debug_mesh() + move((-width/2)*[1,1,0]) rotate(x(90)) + extrude(width) { + square([width,length]); + move(y(length)-x(1)) + text("z",valign="top",halign="right",size=5); + } +} diff --git a/functions.scad b/functions.scad new file mode 100644 index 0000000..359f07d --- /dev/null +++ b/functions.scad @@ -0,0 +1,6 @@ +function circle_y(x,r,cx=0,cy=0) = (2*cy + sqrt(8*cx*x-4*cx*cx-4*(x)*(x)+4*r*r))/2; + +function curve_r(curve_h,curve_w)=(pow(curve_h,2)+pow(curve_w,2))/(2*abs(curve_w)); + +function pow2(x)=x*x; +function pow3(x)=x*x*x; diff --git a/polyhedron.scad b/polyhedron.scad new file mode 100644 index 0000000..5e65732 --- /dev/null +++ b/polyhedron.scad @@ -0,0 +1,31 @@ +use + +function fill_layer(vec,o=0)=let(n=len(vec)) concat([ + [o+0,o+1,o+n-1]],[ for (i=[1:1:n/2-2]) [i, i+1, n-i-1, n-i ] + [o,o,o,o] ],[[o+n/2-1,o+n/2,o+n/2+1]]); + +function wall(vec,o=0)=let(n=len(vec)) + [ for (i=[0:1:n-1]) [i, n+i, n+(i+1)%n, (i+1)%n] + [o,o,o,o] ]; + +module polyhedron_layers(layers) { + lc = len(layers); + bottom=layers[0]; + top=layers[lc-1]; + points_per_layer=len(bottom); + points = flatten_vec(layers); + walls=[for(i = [0:1:lc-2]) wall(bottom,i*points_per_layer) ]; + bottom_f=fill_layer(bottom); + walls_f=flatten_vec(walls); + top_f = [for( i = fill_layer(top,(lc-1)*points_per_layer)) + reverse_vector(i)]; + + //echo(bottom_f = bottom_f); + //echo(top_f = top_f); + //echo(walls_f=walls_f); + //print_points(points); + polyhedron(points,faces=concat( + bottom_f, + top_f, + walls_f + )); + +} diff --git a/preconditions.scad b/preconditions.scad new file mode 100644 index 0000000..413b446 --- /dev/null +++ b/preconditions.scad @@ -0,0 +1,14 @@ +module precondition(test,error) { + if(!test) { + echo(str("",error,"")); + assert(false); + } else children(); +} + +module check_defined(val,error) { + precondition(val != undef,error) children(); +} + +module check_parent(parent,statement,error) { + precondition(parent_module(3) == parent,str(statement,"(): ",error)) children(); +} diff --git a/scaddy.scad b/scaddy.scad new file mode 100644 index 0000000..74be9a0 --- /dev/null +++ b/scaddy.scad @@ -0,0 +1,8 @@ +include <./debug.scad>; +include <./config.scad>; +include <./preconditions.scad>; +include <./utils.scad>; +include <./2d.scad>; +include <./vector.scad>; +include <./aliases.scad>; +include <./component.scad>; diff --git a/utils.scad b/utils.scad new file mode 100644 index 0000000..f8505c5 --- /dev/null +++ b/utils.scad @@ -0,0 +1,33 @@ +include ; + +function circle_details(r) = max(CURVE_POLYGON_MINIMUM,round(3.1415*r*2/CURVE_POLYGON_LENGTH)); + +function select(sel,vec) = [ for (i=sel) vec[i]]; + +function default(val,comp) = val != undef ? val : comp; + + +module construct() { + module construct_get(name) { + $construct_get=name; + children(); + } + difference() { + construct_get("add") children(); + construct_get("remove") children(); + } + +} + +module add() { + //check_parent("construct_get", "add", "could be used only as child of construct()"); + if($construct_get=="add") + children(); +} + +module remove() { + echo("remove",parent_module(1)); + + if($construct_get=="remove") + children(); +} diff --git a/vector.scad b/vector.scad new file mode 100644 index 0000000..77b7ac2 --- /dev/null +++ b/vector.scad @@ -0,0 +1,36 @@ +/** + * Returns [n,0,0] vector. + * + * @param n Size of vector in specified direction + */ +function x(n)=[n,0,0]; +function y(n)=[0,n,0]; +function z(n)=[0,0,n]; + +/** + * Checks if vector (list) contains specified value. + * + * @param vec Vector to be checked + * @param vec Value to be checked + */ +function contains(vec, val)=len([for (i = list) if (i==val) i]) > 0; + +/** + * Reverses supplied vector + */ +function reverse_vector(vec) = let(n=len(vec)) [for (i= [1:1:n]) vec[n-i]]; + +function scale_vec(sv,vec2)= let(l=len(sv)) + [ for(v = vec2) + [for(i = [0:1:l-1]) v[i]*sv[i] ]]; + +/** + * Moves each vector by given offset + * + * @param vec2 set of vectors + * @param mov Movement vector + * + */ +function move_vectors(vec2,mov)=[ for(v = vec2) v+mov]; + +function flatten_vec(list) = [ for (i = list, v = i) v ];