From 6fb65f5211dfc656a6ec380c3002c023e850e4ff Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Sat, 23 Feb 2019 20:33:01 +0100 Subject: [PATCH] Resync --- detail-config.scad | 6 ++- dsl/component.scad | 69 +++++++++++++++++++++++++++ dsl/modify.test.json | 3 ++ dsl/modify.test.scad | 42 +++++++++++++++++ scad-common.scad | 95 ++++++++++++++++++++++++++++++++++---- scad-debug-customizer.scad | 4 ++ scad-views.scad | 50 ++++++++++++++++++++ scad-views.test.scad | 22 ++++++--- subdivision.json | 3 ++ subdivision.scad | 73 +++++++++++++++++++++++++++++ test/component.json | 3 ++ test/component.scad | 37 +++++++++++++++ 12 files changed, 389 insertions(+), 18 deletions(-) create mode 100644 dsl/component.scad create mode 100644 dsl/modify.test.json create mode 100644 dsl/modify.test.scad create mode 100644 scad-debug-customizer.scad create mode 100644 subdivision.json create mode 100644 subdivision.scad create mode 100644 test/component.json create mode 100644 test/component.scad diff --git a/detail-config.scad b/detail-config.scad index cbe67a6..9f0820f 100644 --- a/detail-config.scad +++ b/detail-config.scad @@ -1,3 +1,7 @@ +/* [makereal SCAD Common Configuration] */ +// Minimum polygon count for curve CURVE_POLYGON_MINIMUM=16; -CURVE_POLYGON_LENGTH=3; // Ideal for 3D Printing + +// Maximum length of curve polygons. +CURVE_POLYGON_LENGTH=1; // [0:100] diff --git a/dsl/component.scad b/dsl/component.scad new file mode 100644 index 0000000..9306bac --- /dev/null +++ b/dsl/component.scad @@ -0,0 +1,69 @@ +use <../scad-views.scad>; +use <./modify.scad>; + + + +HUGE=2000; + +function _contains(val, list)=len([for (i = list) if (i==val) i]) > 0; + +module c(name) { + if($sc_comp == name) children(); +} + +module v(name) { + if($sc_comp_view == name) children(); +} + +module mv(names) { + if(_contains($sc_comp_view,names)) children(); +} + +module mc(names) { + if(_contains($sc_comp,names)) children(); +} + + +module _v(name) { + $sc_comp_view=name; + children(); +} + +module _c(name) { + $sc_comp = name; + children(); +} + +module from_2d_views() { + intersection() { + side_view() + vmove(z=-HUGE/2) + vextrude(HUGE) + _v("side") + construct() children(); + top_view() + vmove(z=-HUGE/2) + vextrude(HUGE) + _v("top") + construct() children(); + front_view() + vmove(z=-HUGE/2) + vextrude(HUGE) + _v("front") + construct() children(); + } +} + +module c_construct() { + construct() { + add() move(1000,1000,1000) cube(10); + remove() move(995,995,995) cube(20); + _v("3d") children(); + add() from_2d_views() children(); + } +} + +module 3d_view() { + + children(); +} diff --git a/dsl/modify.test.json b/dsl/modify.test.json new file mode 100644 index 0000000..30e516c --- /dev/null +++ b/dsl/modify.test.json @@ -0,0 +1,3 @@ +{ + "fileFormatVersion": "1" +} diff --git a/dsl/modify.test.scad b/dsl/modify.test.scad new file mode 100644 index 0000000..4b8fecd --- /dev/null +++ b/dsl/modify.test.scad @@ -0,0 +1,42 @@ +use ; + +module offset_by(x=0,y=0,z=0) { + for(i=[0:1:$children-1]) { + translate(i*[x,y,z]) children(i); + } +} + +module inner() { + remove() cube(5); +} +mirror([1,0,0]) +offset_by(y=15) { + construct() { + remove() cube(10); + } + + construct() { + cube(10); + } + + construct() { + cube(10); + remove() cube(5); + } + construct() { + add() cube(10); + remove() cube(5); + } + construct() { + add() cube(10); + inner(); + } + + construct() { + add() cube(10); + remove() construct() { + add() sphere(7); + remove() cube(5); + } + } +} \ No newline at end of file diff --git a/scad-common.scad b/scad-common.scad index e15dad5..0c4c15a 100644 --- a/scad-common.scad +++ b/scad-common.scad @@ -1,22 +1,46 @@ include ; + + +function _scale(sv,vec2)=scale_vec(sv,vec2); + + + +function default(val,comp) = val != undef ? val : comp; +function ycircle(x,r,cx=0,cy=0) = (2*cy + sqrt(8*cx*x-4*cx*cx-4*(x)*(x)+4*r*r))/2; + + +function scale_vec(sv,vec2)= let(l=len(sv)) + [ for(v = vec2) + [for(i = [0:1:l-1]) v[i]*sv[i] ]]; + +function flatten_vec(list) = [ for (i = list, v = i) v ]; + + + module move(x=0,y=0,z=0) { translate([x,y,z]) children(); } -function circle_details(r) = max(CURVE_POLYGON_MINIMUM,round(3.1415*r*2/CURVE_POLYGON_LENGTH)); - -/** - * @deprecated - **/ -module rcircle(r) { - circle(r,$fn=circle_details(r)); +module flip_x() { + mirror([1,0,0]) children(); +} +module flip_y() { + mirror([0,1,0]) children(); } -module dcircle(r) { - circle(r,$fn=circle_details); +module flip_z() { + mirror([0,0,1]) children(); } + +module dcircle_cutout(r,angle) { + difference() { + dcircle(r); + rotate([0,0,180]) move(y=-r) square(2*(r)); + rotate([0,0, -(angle)]) move(x=-r) square(2*(r)); + } +} module ring(rs,rl) { difference() { rcircle(max(rs,rl)); @@ -54,10 +78,61 @@ module curved(h,w_bottom,w_top) { curve_h=h; curve_w=w_top-w_bottom; circle_y=curve_w>0 ? 0 : curve_h; - curve_r = (pow(curve_h,2)+pow(curve_w,2))/(2*abs(curve_w)); + curve_r = curve_r(curve_h,curve_w); echo(curve_w, curve_r); move(x=-min(w_bottom,w_top)) difference() { move(x=-abs(curve_w)) square([max(w_top,w_bottom),curve_h]); move(x=-curve_r,y = circle_y) rcircle(curve_r); } } + +function curve_r(curve_h,curve_w)=(pow(curve_h,2)+pow(curve_w,2))/(2*abs(curve_w)); + + +module dring_s_cutout(l,h,hf,hb) { + ct = curve_r(l,h-hb); + cb = curve_r(l,h-hf); + intersection() { + move(x=-h) square([h,l]); + difference() { + move(x=-cb) dcircle(cb); + move(x=-ct-hb) dcircle(ct); + + } + } +} + + +module concave_extrude(height,s=2,steps=20,angle=90) { + ad=angle/steps; + function z(i)=1-cos(ad*i); + function s(i)=(s-1)*sin(ad*i)+1; + + h=height/z(steps); + for(i=[0:1:steps-1]) { + _extrude_step(h*z(i),h*z(i+1),s(i),s(i+1)) children(); + } +} + +module convex_extrude(height,s=2,steps=10,angle=90) { + ad=angle/steps; + function z(i)=sin(ad*i); + function s(i)=s-(s-1)*cos(ad*i); + + h=height/z(steps); + for(i=[0:1:steps-1]) { + _extrude_step(h*z(i),h*z(i+1),s(i),s(i+1)) children(); + } +} + + + + +function shell_layers(layers)= + let(p=len(layers[0])) + let(l=len(layers)) + flatten( + [for (i = [0:1:l-2]) + [for (j = [0:1:p-1]) p*[i,i,i+1,i+1]+[j,(j+1)%p,(j+1)%p,j] ] + ] + ); diff --git a/scad-debug-customizer.scad b/scad-debug-customizer.scad new file mode 100644 index 0000000..472e610 --- /dev/null +++ b/scad-debug-customizer.scad @@ -0,0 +1,4 @@ +// Enables Display of Debug geometry +Debug=false; // [false,true] + +$debug=Debug; diff --git a/scad-views.scad b/scad-views.scad index 4d3fd30..2f20e10 100644 --- a/scad-views.scad +++ b/scad-views.scad @@ -1,3 +1,4 @@ +use ; module display(view) { $sc_view=view; @@ -9,3 +10,52 @@ module view(view) { children(); } } + +module display_if(view) { + if($sc_view == view) { + children(); + } +} + +module vmove(x=0,y=0,z=0) { + if($sc_view == undef || $sc_view == view) { + move(x,y,z) children(); + } else { + children(); + } +} + +module side_view() { + view("side") rotate([0,0,90]) rotate([90,0,0]) children(); + +} + +module top_view() { + view("top") children(); +} + +module front_view() { + view("front") rotate([90,0,0]) flip_z() children(); +} + +module vextrude(h=0) { + if($sc_view==undef) { + linear_extrude(h) children(); + } else { + children(); + } +} + +module view_axis() { + square([20,1]); + move(x=20) text("x"); + square([1,20]); + move(y=20) text("y"); + move(z=10) square([1,1]); +} + +module display_axis() { + front_view() color("blue") view_axis(); + side_view() color("red") view_axis(); + top_view() view_axis(); +} diff --git a/scad-views.test.scad b/scad-views.test.scad index 722526c..e60e2ad 100644 --- a/scad-views.test.scad +++ b/scad-views.test.scad @@ -1,17 +1,25 @@ use ; use ; -// View +/* [View Tests] */ + +// Viewing angle of views View=0; // [0:all,1:front,2:side,3:top] -module test() { - view("front") rotate([90,0,0]) color("blue") square(10,10); - view("side") rotate([0,-90,0]) color("red") square(10,10); - view("top") square(10,10); +include ; + +module test_extrude() { + move(x=-30) intersection() { + side_view() vextrude(10) dcircle(10); + front_view() flip_z() vextrude(10) dcircle(10); + top_view() vextrude(10) dcircle(10); + } } v_arr=[undef,"front","side","top"]; display(v_arr[View]) { - test(); - move(x=20) test(); + display_axis(); + test_extrude(); } + +debug() text("Debug geometry enabled."); diff --git a/subdivision.json b/subdivision.json new file mode 100644 index 0000000..30e516c --- /dev/null +++ b/subdivision.json @@ -0,0 +1,3 @@ +{ + "fileFormatVersion": "1" +} diff --git a/subdivision.scad b/subdivision.scad new file mode 100644 index 0000000..958fad0 --- /dev/null +++ b/subdivision.scad @@ -0,0 +1,73 @@ +module print_points(vector) { + color("red") + for(i = [0:1:len(vector)-1]) + translate(vector[i]) { + sphere(1); + text(str(i),size=4); + } +} + + +function smooth_closed(path,it=0)= + it == 0 ? + path : + smooth_closed(subdivide_closed(path),it-1); + + +function smooth_open(path,it=0)= + it == 0 ? + path : + smooth_open(subdivide_open(path),it-1); + + +function subdivide_closed(path)= + let(n=len(path)) + flatten(concat([for (i=[0:1:n-1])[ path[i], + interpolateClosed(path,n,i) + ]])); + +function subdivide_open(path) = + let(n = len(path)) + flatten(concat([for (i = [0 : 1 : n-1]) + i < n-1? + // Emit the current point and the one halfway between current and next. + [path[i], interpolateOpen(path, n, i)] + : + // We're at the end, so just emit the last point. + [path[i]] + ])); + +weight = [-1, 8, 8, -1] / 14; +weight0 = [6, 11, -1] / 16; +weight2 = [1, 1] / 2; + +// Interpolate on an open-ended path, with discontinuity at start and end. +// Returns a point between points i and i+1, weighted. +function interpolateOpen(path, n, i) = + i == 0? + n == 2? + path[i] * weight2[0] + + path[i + 1] * weight2[1] + : + path[i] * weight0[0] + + path[i + 1] * weight0[1] + + path[i + 2] * weight0[2] + : i < n - 2? + path[i - 1] * weight[0] + + path[i] * weight[1] + + path[i + 1] * weight[2] + + path[i + 2] * weight[3] + : i < n - 1? + path[i - 1] * weight0[2] + + path[i] * weight0[1] + + path[i + 1] * weight0[0] + : + path[i]; + +function interpolateClosed(path, n, i) = + path[(i + n - 1) % n] * weight[0] + + path[i] * weight[1] + + path[(i + 1) % n] * weight[2] + + path[(i + 2) % n] * weight[3] ; + +function flatten(list) = [ for (i = list, v = i) v ]; \ No newline at end of file diff --git a/test/component.json b/test/component.json new file mode 100644 index 0000000..30e516c --- /dev/null +++ b/test/component.json @@ -0,0 +1,3 @@ +{ + "fileFormatVersion": "1" +} diff --git a/test/component.scad b/test/component.scad new file mode 100644 index 0000000..a2d9096 --- /dev/null +++ b/test/component.scad @@ -0,0 +1,37 @@ +use <../dsl/component.scad>; +use <../dsl/modify.scad>; +use <../subdivision.scad>; +use <../scad-common.scad>; +view=0; // [0:all,1:side,2:top,3:front] + +$sc_view=[undef,"side","top","front"][view]; + +module test_components() { + c("square") square(10); + c("eq") add() circle(10); + c("eq") remove() construct() { + add() square(10); + remove() circle(5); + } +} + + + + +module def() { + mc(["def"]) mv("side","front") add() square([10,20]); + c("def") { + v("top") add() circle(10); + v("side") remove() circle(5); + } +} + + + +_c("def") c_construct() { + v("3d") { + add() sphere(5); + remove() move(z=20) sphere(7); + } + def(); +} \ No newline at end of file