chiark / gitweb /
fb8313c4305c4205d7c65c709c859b98790f0128
[reprap-play.git] / sewing-table.scad.m4
1 // -*- C -*-
2
3 include <funcs.scad>
4 include <commitid.scad>
5
6 ply_th = 18;
7 ply_hole_dia = 15;
8 ply_edge_min = 10;
9
10 tile_th = 3;
11 post_dia = 8;
12
13 post_shorter = 1;
14
15 screw_dia = 2.2;
16 screw_big_dia = 3.6;
17 screw_big_len = 4.0;
18
19 round_edge_rad = 2.0;
20
21 round_cnr_rad = 10;
22
23 interlock_dia = 10;
24 interlock_fine = 0.66;
25
26 interlock_fine_slope = 1.0;
27 interlock_fine_lenslop = 1.0;
28
29 demo_slop = 0.1;
30
31 // calculated
32
33 TEST = false;
34
35 ply_edge_hole_dist = ply_edge_min + ply_hole_dia/2;
36
37 echo(str("HOLES IN PLY ctr dist from PLY edge = ", ply_edge_hole_dist));
38
39 hole_slop = (ply_hole_dia - post_dia)/2;
40 tile_hard_edge_hole_dist = ply_edge_hole_dist + hole_slop;
41
42 echo(str("HOLES IN PLY ctr dist from TILE HARD edge = ",
43          tile_hard_edge_hole_dist));
44
45 echo(str("HOLES IN PLY ctr dist from TILE ROUND edge = ",
46          tile_hard_edge_hole_dist + round_edge_rad));
47
48 thehd = [ tile_hard_edge_hole_dist, tile_hard_edge_hole_dist ];
49 thehd_tr = thehd;
50 thehd_tl = [ -thehd_tr[0], thehd_tr[1] ];
51 thehd_bl = -thehd_tr;
52 thehd_br = -thehd_tl;
53
54 interlock_rad = interlock_dia/2;
55 interlock_negative_rad = interlock_rad + 0.125;
56
57 interlock_sq_adj = 0.2; // arbitrary
58
59 module Post(){
60   mirror([0,0,1]) {
61     difference(){
62       cylinder(r= post_dia/2, h= tile_th + ply_th - post_shorter);
63       translate([0,0, tile_th]) {
64         cylinder(r= screw_big_dia/2, h= screw_big_len);
65         cylinder(r= screw_dia/2, h= ply_th, $fn=20);
66       }
67     }
68   }
69 }
70
71 module Posts(posts) {
72   for (p= posts) {
73     translate(concat(p, [0]))
74       Post();
75   }
76 }
77
78 module TileBase(botleft, topright){
79   size = topright - botleft;
80   botleft_post = botleft + thehd_tr;
81   topright_post = topright + thehd_bl;
82   difference(){
83     mirror([0,0,1])
84       translate(concat(botleft, [0]))
85       cube(concat(size, [tile_th]));
86     translate( concat(botleft_post, [ TEST ? 0 : -tile_th])
87                + 0.5 * [ post_dia, post_dia, 0 ] )
88       Commitid_BestCount_M( topright_post-botleft_post
89                             + [-post_dia,-post_dia]
90                             + [0, thehd[1]]);
91   }
92 }
93
94 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars) { body; }
95 m4_define(`INREFFRAME',`
96   length_vec = ($2) - ($1);
97   length = dist2d([0,0], length_vec);
98   length_uvec = length_vec / length;
99   ortho_uvec = [ -length_uvec[1], length_uvec[0] ];
100   m = [ [ length_uvec[0],  ortho_uvec[0], 0, ($1)[0], ],
101         [ length_uvec[1],  ortho_uvec[1], 0, ($1)[1], ],
102         [ 0,              0,              1,            0, ],
103         [ 0,              0,              0,            1, ] ];
104   $3
105   multmatrix(m)
106 ')
107
108 m4_dnl  INREFFRAME(left_cnr, right_cnr, morevars)
109 m4_dnl    INREFFRAME_EDGE { body; }
110 m4_define(`INREFFRAME_EDGE',`
111   translate([0,0, -round_edge_rad])
112 ')
113
114 module RoundEdge(left_cnr, right_cnr) {
115   INREFFRAME(left_cnr, right_cnr)
116     INREFFRAME_EDGE {
117     difference(){
118       rotate([0,90,0])
119         cylinder(r= round_edge_rad, h= length, $fn=50);
120       translate([-1, 0, -20])
121         cube([length+2, 20, 20]);
122     }
123   }
124 }
125
126 module RoundCornerCut(ci) {
127   this_cnr = ci[0];
128   right_cnr = ci[1];
129   offr= round_cnr_rad - round_edge_rad;
130   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
131     difference(){
132       cube(offr*2 - 0.1, center=true);
133       translate([offr, offr, 0])
134         cylinder(center=true, h=20, r= offr);
135     }
136   }
137 }
138
139 module RoundCornerAdd(ci) {
140   this_cnr = ci[0];
141   right_cnr = ci[1];
142   bigr = round_cnr_rad - round_edge_rad;
143   INREFFRAME(this_cnr, right_cnr) INREFFRAME_EDGE {
144     intersection(){
145       cube(bigr*2 + 0.1, center=true);
146       translate([bigr, bigr, 0])
147         rotate_extrude(convexity=10, $fn=50)
148         translate([bigr, 0])
149         difference(){
150           circle(r= round_edge_rad, $fn=50);
151           mirror([1,1])
152             square([20,20]);
153         }
154     }
155   }
156 }
157
158 module InterlockLobePlan(negative) {
159   r = negative ? interlock_negative_rad : interlock_rad;
160   ymir = negative ? 0 : 1;
161
162   dx = sqrt(3) * r;
163   $fn= 80;
164   translate([thehd[0], 0]){
165     mirror([0,ymir]){
166       circle(r=r);
167       difference(){
168         translate([-dx, -0.1])
169           square([ dx*2, r/2 + 0.1 ]);
170         for (xi = [-1, 1]) {
171           translate([ xi*dx, r ])
172             circle(r=r);
173         }
174       }
175     }
176   }
177 }
178
179 module InterlockEdgePlan(negative, nlobes, length, dosquare=true) {
180   for (lobei = [ 0 : nlobes-1 ]) {
181     lobex = (length - thehd[0]*2) * (lobei ? lobei / (nlobes-1) : 0);
182     translate([lobex, 0, 0]) {
183       InterlockLobePlan(negative);
184     }
185   }
186
187   if (dosquare) {
188     iadj = 0;
189     slotshorter = negative ? -0.1 : interlock_fine_lenslop;
190     mirror([0, negative])
191       translate([slotshorter, iadj])
192       square([length - slotshorter*2, interlock_fine + iadj*2]);
193   }
194 }
195
196 module InterlockEdge(left_cnr, right_cnr, negative=0, nlobes=2) {
197   plusth = negative * 1.0;
198   protr = interlock_fine + interlock_sq_adj;
199
200   z2 = -tile_th/2;
201   z1 = -tile_th/2 - protr / interlock_fine_slope;
202   z3 = -tile_th/2 + protr / interlock_fine_slope;
203
204   negsign = negative ? -1 : +1;
205   yprotr = negsign * protr;
206
207   INREFFRAME(left_cnr, right_cnr) {
208     for (vsect = [ // zs0            zs1      ys0,            ys1
209                   [ -tile_th-plusth, plusth,  0,              0],
210                   [ z1,              z2,      0, yprotr],
211                   [ z2,              z3,      yprotr, 0],
212                   ]) {
213       zs0 = vsect[0];
214       zs1 = vsect[1];
215       zsd = zs1-zs0;
216       ys0 = vsect[2];
217       ys1 = vsect[3];
218       ysd = ys1-ys0;
219       sl = ysd/zsd;
220       m = [ [ 1,0,   0,    0 ],
221             [ 0,1, -sl, -ys0 + negsign*interlock_sq_adj ],
222             [ 0,0,   1,  zs0 ],
223             [ 0,0,   0,    1 ] ];
224       multmatrix(m)
225         linear_extrude(height=zsd, convexity=10)
226         InterlockEdgePlan(negative, nlobes, length, !!ysd);
227     }
228   }
229 }
230
231 function TestPiece_holes2corners(holes) =
232   [ holes[0] + thehd_bl,
233     holes[1] + thehd_br,
234     holes[1] + thehd_tr,
235     holes[0] + thehd_tl ];
236
237 module TestPiece1(){ ////toplevel
238   holes = [ [-100, 0],
239             [   0, 0]
240             ];
241   corners = TestPiece_holes2corners(holes);
242   difference(){
243     union(){
244       TileBase(corners[0], corners[2]);
245       Posts(holes);
246       RoundEdge(corners[0], corners[1]);
247       RoundEdge(corners[3], corners[0]);
248     }
249     InterlockEdge(corners[1], corners[2], 1, nlobes=1);
250     RoundCornerCut(corners[0], corners[1]);
251   }
252   RoundCornerAdd(corners[0], corners[1]);
253 }
254
255 module TestPiece2(){ ////toplevel
256   holes = [ [   0, 0],
257             [  50, 0]
258             ];
259   corners = TestPiece_holes2corners(holes);
260   TileBase(corners[0], corners[2]);
261   Posts(holes);
262   RoundEdge(corners[0], corners[1]);
263   InterlockEdge(corners[3], corners[0], 0, nlobes=1);
264 }
265
266 module TestDemo(){ ////toplevel
267   translate([ -thehd[0], 0 ])
268     color("blue")
269     TestPiece1();
270   translate([ +thehd[0] + demo_slop, 0 ])
271     TestPiece2();
272 }
273   
274 function Rectangle_corners(c0, sz) =
275   [ c0 + [ 0,     0     ],
276     c0 + [ sz[0], 0     ],
277     c0 + [ sz[0], sz[1] ],
278     c0 + [ 0,     sz[1] ] ];
279
280 function Rectangle_corners2posts(c) =
281   [ c[0] + thehd_tr,
282     c[1] + thehd_tl,
283     c[2] + thehd_bl,
284     c[3] + thehd_br ];
285
286 module Tile02(){ ////toplevel
287   sz = [100,170];
288   c0 = -sz;
289   c = Rectangle_corners(c0, sz);
290   posts = Rectangle_corners2posts(c);
291   rcs = [c[0], c[1]];
292   difference(){
293     union(){
294       TileBase(c[0], c[2]);
295       Posts(posts);
296       RoundEdge(c[0], c[1]);
297       RoundEdge(c[3], c[0]);
298       InterlockEdge(c[2], c[3], 0);
299     }
300     InterlockEdge(c[1], c[2], 1);
301     RoundCornerCut(rcs);
302   }
303   RoundCornerAdd(rcs);
304 }
305
306 module Tile12(){ ////toplevel
307   sz = [100,250];
308   c0 = [-sz[0], 0];
309   c = Rectangle_corners(c0, sz);
310   posts = Rectangle_corners2posts(c);
311   rcs = [c[2], c[3]];
312   difference(){
313     union(){
314       TileBase(c[0], c[2]);
315       RoundEdge(c[2], c[3]);
316       RoundEdge(c[3], c[0]);
317       Posts(posts);
318     }
319     InterlockEdge(c[0], c[1], 1);
320     RoundCornerCut(rcs);
321   }
322   RoundCornerAdd(rcs);
323 }
324
325 module Demo(){ ////toplevel
326   translate(demo_slop*[-2,1]) color("blue") Tile12();
327   translate(demo_slop*[-2,0]) color("red")  Tile02();
328 }
329   
330 //TestPiece1();
331 //TestPiece2();
332 //Demo();