This commit is contained in:
Tony Tkáčik 2019-02-23 20:33:01 +01:00
parent 14743f42fa
commit 6fb65f5211
12 changed files with 389 additions and 18 deletions

View file

@ -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]

69
dsl/component.scad Normal file
View file

@ -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();
}

3
dsl/modify.test.json Normal file
View file

@ -0,0 +1,3 @@
{
"fileFormatVersion": "1"
}

42
dsl/modify.test.scad Normal file
View file

@ -0,0 +1,42 @@
use <modify.scad>;
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);
}
}
}

View file

@ -1,22 +1,46 @@
include <detail-config.scad>;
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] ]
]
);

View file

@ -0,0 +1,4 @@
// Enables Display of Debug geometry
Debug=false; // [false,true]
$debug=Debug;

View file

@ -1,3 +1,4 @@
use <scad-common.scad>;
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();
}

View file

@ -1,17 +1,25 @@
use <scad-common.scad>;
use <scad-views.scad>;
// 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 <scad-debug-customizer.scad>;
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.");

3
subdivision.json Normal file
View file

@ -0,0 +1,3 @@
{
"fileFormatVersion": "1"
}

73
subdivision.scad Normal file
View file

@ -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 ];

3
test/component.json Normal file
View file

@ -0,0 +1,3 @@
{
"fileFormatVersion": "1"
}

37
test/component.scad Normal file
View file

@ -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();
}