chiark / gitweb /
6308c49c71e19b33367af2bb5600089d0e02e758
[dl-things.git] / th-6021 / Libs.scad
1 /*      MY Libraries v1.1 - Common Components
2                 Unless stated otherwise, all modules
3                 are by Randy Young <dadick83@hotmail.com>
4                 2010, 2011.   License, GPL v2 or later
5 **************************************************/
6
7 displayMode= rezScreen();  // (resolution)
8
9 function rezPrinter()=0;
10 function rezScreen()=1;
11
12 function resolution(value) =
13         displayMode==1 ? 360 : value*2*3.141592; // ~1mm resolution for printer
14 $fn=resolution(4); // default for cylinder & sphere
15
16 function vectorDistance(v1, v2) =
17         sqrt((pow(v2[0]-v1[0],2)+pow(v2[1]-v1[1],2))+pow(v2[2]-v1[2],2));
18 // function for linear measurement from 2, 3d vectors)
19
20 ep=0.01; //small number to avoid overlaps (non-manifold models)
21 in=25.4; //inches to millimeter conversion
22
23
24 /***** ROTATION & TRANSLATION SHORTCUTS  *****
25 Used for common, easy to type single axis moves 
26 with names I can actually remember (since I never
27 get the rotation directions right on first try. :o)   */
28
29 function tighten (angle=90) =
30                 [0,0,-1*angle];
31
32 function loosen (angle=90) =
33                 [0,0,angle];
34
35 function righty (angle=90) =
36                 [0,angle,0];
37
38 function lefty (angle=90) =
39                 [0,-1*angle,0];
40
41 function backward (angle=90) =
42                 [-1*angle,0,0];
43
44 function forward (angle=90) =
45                 [angle,0,0];
46
47 function push(distance=1) =
48                 [0,distance,0]; 
49
50 function lift(distance=1) =
51                 [0,0,distance];
52
53 function slide(distance=1) =
54                 [distance,0,0]; 
55
56 /*************************************************
57 BASIC ENGINEERING SHAPES */
58
59 module hex (width=10, height=10, flats= true, center=false){
60         radius= (flats == true ? width/sin(60)/2 : width/2 );
61                 cylinder (r=radius, h=height, center=center, $fn=6);
62 }
63
64 module equilateralTriangle (side=10, height=10, center=false){
65         translate(center==true ? [-side/2,-sqrt(3)*side/6,-height/2] : [0,0,0]) 
66                 linear_extrude(height=height)
67                         polygon(points=[[0,0],[side,0],[side/2,sqrt(3)*side/2]]);
68 }
69
70 module pyramid(side=10, height=-1, square=false, centerHorizontal=true, centerVertical=false){
71 // creates a 3 or 4 sided pyramid.  -1 height= tetrahedron or Johnson's Solid square pyramid
72         mHeight= height!=-1 ? height:
73                 square == true ? (1/sqrt(2))*side:              //square
74                         sqrt(6)/3*side;                                         //tetra
75         vert= centerVertical!=true ? [0,0,0]:
76                 square==false ? [0,0,-mHeight/4] :       //inscribes a tetra inside a sphere, not 1/2 ht
77                         [0,0,-mHeight/2];       //for squares, center=false inscribes, so true is just 1/2 ht
78         horiz= centerHorizontal!= true ? [0,0,0] :
79                 square == true ? [-side/2, -side/2, 0] :        //square
80                         [-side/2,-sqrt(3)*side/6 ,0];                   //tetra
81         translate(vert+horiz){
82                 if (square == true){
83                         polyhedron (    points = [[0,0,0],[0,side,0],[side,side,0],
84                                                                 [side,0,0],[side/2,side/2,mHeight]], 
85                                                 triangles = [[1,0,2], [2,0,3], [0,4,3], 
86                                                                 [3,4,2], [2,4,1], [1,4,0]]);
87                 }
88                 if (square != true){
89                         polyhedron (    points = [[0,0,0],[side,0,0],[side/2,sqrt(3)*side/2,0],
90                                                                 [side/2,sqrt(3)*side/6,mHeight]], 
91                                                 triangles = [[0,1,2], [1,0,3], [2,1,3],[2,3,0]]);                       
92                 }
93 }       }
94
95 module tube(r=2, thickness=1, height=1, center=false, outline=false){
96         translate(lift(center == true ?  0 : 0.5*height)) 
97         difference(){
98                 cylinder(r=r, h=height, center=true, $fn=resolution(r));
99                 if (outline!=true)
100                         cylinder(r=r-thickness+ep, h=height+2*ep, center=true, $fn=resolution(r-thickness));
101 }       }
102
103 module roundRect (size=[10,10,10], round=3, center=false){
104         $fn=resolution(round);
105         radius = min(round, size[0]/2, size[1]/2);
106         union(){
107                 translate(center==true ? [-size[0]/2,-size[1]/2,-size[2]/2] : [0,0,0]){
108                         translate([radius,radius,0])
109                                 cylinder (r=radius, h=size[2]);
110                         translate([size[0]-radius,radius,0])
111                                 cylinder (r=radius, h=size[2]);
112                         translate([size[0]-radius,size[1]-radius,0])
113                                 cylinder (r=radius, h=size[2]);
114                         translate([radius,size[1]-radius,0])
115                                 cylinder (r=radius, h=size[2]);
116                         translate([0,radius,0]) 
117                                 cube(size-[0,radius*2,0]);
118                         translate([radius,0,0]) 
119                                 cube(size-[radius*2,0,0]);
120 }       }       }
121
122 module keyhole(r1, r2, length, height) {
123         translate ([0,length/2,0]) union(){
124                 translate ([0,length/2-r1,0]) 
125                         cylinder (r=r1, height, $fn=resolution(r1));
126                 translate ([0,0-(length/2-r2),0]) 
127                         cylinder (r=r2, height, $fn=resolution(r2));
128                 translate ([-r1,r1-length/2,0]) 
129                         cube ([r1*2,length-(r1*2),height]);
130         }
131 }
132
133 module slot(size=[4,10,1], startRound=true, endRound=true, centerXYZ=[1,0,0]){
134         $fn=resolution(size[0]/2);
135         radius=size[0]/2;
136         start= startRound==true ? radius : 0;
137         end= endRound==true ? radius : 0;
138         translate([-size[0]/2*centerXYZ[0],
139                         -size[1]/2*centerXYZ[1],
140                         -size[2]/2*centerXYZ[2]]) 
141         union(){
142                 translate([0,start,0]) cube (size-[0,start+end,0]);
143                 if (startRound==true) translate ([radius,radius,0])
144                         cylinder (r=size[0]/2, h=size[2]);
145                 if (endRound==true) translate ([radius,size[1]-radius,0])
146                         cylinder (r=size[0]/2, h=size[2]);
147         
148 }       }
149
150 module dovetail (width=9, height=10, male=true){
151         w= (male==true) ? width*0.750 : width;
152         translate(slide(2.4)) union(){
153                 rotate(tighten(30))
154                         equilateralTriangle(w,height, center=true);
155                 translate(slide(-4.5)) cube([4.2,width*1.5,height], center=true);}
156 }
157
158 module teardrop(radius=5, length=10, angle=90) {
159 // teardrop module by whosawhatsis <www.thingiverse.com/thing:3457>
160 // Creative Commons LGPL, 2010.  I added default values and resolution code
161         $fn=resolution(radius);
162         rotate([0, angle, 0]) union() {
163                 linear_extrude(height = length, center = true, convexity = radius, twist = 0)
164                         circle(r = radius, center = true);
165                 linear_extrude(height = length, center = true, convexity = radius, twist = 0)
166                         projection(cut = false) rotate([0, -angle, 0]) 
167                         translate([0, 0, radius * sin(45) * 1.5]) 
168                         cylinder(h = radius * sin(45), r1 = radius * sin(45), r2 = 0, 
169                                         center = true);
170         }
171 }
172
173
174 /*************************************************
175 HARDWARE COMPONENTS */
176
177 module hexNut(size="M3", center=false, outline=false, pos=[0,0,0]){
178         if (tableRow(size) == -1)       echo(str("hexNut ERROR: size of '",size,"' is undefined."));
179         width = tableEntry (size, "nutWidth");
180         height = tableEntry (size, "nutThickness");
181         hole = tableEntry (size, "nutID");
182         translate(pos+lift(center == true ?  0 : 0.5*height)) difference(){
183                 hex(width, height, center=true);
184                 if (outline!=true)
185                         cylinder(r=hole/2+0.01, h=height+0.02, center=true, $fn=resolution(hole/2));
186 }       }
187
188 module washer(size="M3", center=false, outline=false, pos=[0,0,0]){
189         if (tableRow(size) == -1)       echo(str("washer ERROR: size of '",size,"' is undefined."));
190         width = tableEntry (size, "washerOD");
191         height = tableEntry (size, "washerThickness");
192         hole = tableEntry (size, "washerID");
193         translate(pos) tube(width/2,(width-hole)/2,height,center,outline);
194 }
195
196 module capBolt (size="M3", length= 16, center=false, pos=[0,0,0])       {
197         if (tableRow(size) == -1)       echo(str("capBolt ERROR: size of '",size,"' is undefined."));
198         diameter = tableEntry (size, "headDiameter");
199         head = tableEntry (size, "headThickness");
200         stud = tableEntry (size, "studDiameter");       
201         translate(pos+[0,0,center == true ? -length/2 : 0]) union()     {
202                 translate(lift(-head)){
203                         cylinder(r=diameter/2, h=head, $fn=resolution(diameter/2));
204                         cylinder(r=stud/2, h=length+head, $fn=resolution(stud/2));
205 }       }       }
206
207 module hexBolt (size="M3", length= 16, center=false, pos=[0,0,0])       {
208         if (tableRow(size) == -1)       echo(str("hexBolt ERROR: size of '",size,"' is undefined."));
209         width = tableEntry (size, "headDiameter");
210         head = tableEntry (size, "headThickness");
211         stud = tableEntry (size, "studDiameter");       
212         translate(pos+[0,0,center == true ? -length/2 : 0]) union()     {
213                 translate(lift(-head)){
214                         hex(width, head, $fn=6);
215                         cylinder(r=stud/2, h=length+head, $fn=resolution(stud/2));
216 }       }       }
217
218 module bearing (size="Skate", center=false, outline=false, pos=[0,0,0]){
219 // The Elmom MCAD has a much better version of a bearing module.  
220 // This is a basic donut shape but gets the job done for construction.
221         if (tableRow(size) == -1)       echo(str("bearing ERROR: type of '",type,"' is undefined."));
222         hole = tableEntry (size, "bearingID");
223         thickness = tableEntry (size, "bearingThickness");
224         diameter = tableEntry (size, "bearingOD");
225         translate(pos) 
226                 tube(diameter/2, (diameter-hole)/2, thickness, center, outline);
227 }
228
229 /*************************************************
230 /****  TABLE DATA AND SUPPORT FUNCTIONS         ****
231 The following functions contain various engineering tables but you can add whatever data
232 you'd like.  I've also used it for translation landmarks.  The table itself is arranged with an array of arrays (vectors) and stored within the "tableRow" function with each row having a string identifier.  Thus, you can retrieve a vector by name without storing a bunch of variables.
233         A second array function, called "columnNames" is simply a convenient way to refer to each element within a vector by an element name.  The values overlap, depending on what data type you're working with.  For instance, "bearingWidth" = "headThickness" = z-vector element = 2.
234         You can find a specific variable within the data, using "rowElement" that uses row, column strings to identify the element desired.  Uncomment the example below to see it in action. 
235                                                 *****/
236 //testTable();
237                 module testTable(){
238                         column = "nutWidth";
239                         row =   "M3";
240                         
241                         echo (str(column," is found in column #",
242                                 columnName(column)      ));
243                         echo (str("The row named ",row," contains: ",
244                                 tableRow(row)           ));
245                         echo (str("The ",column," field of the ",row," row is: ",
246                                 tableEntry(row,column)  ));     
247                 }
248
249 function columnName(name)=
250         // Bearings
251                 name == "bearingID"             ? 0 :
252                 name == "bearingOD"             ? 1 :
253                 name == "bearingThickness"?2 :
254         // Bolts
255                 name == "studDiameter"  ? 0 :
256                 name == "headDiameter"  ? 1 :
257                 name == "headThickness"         ? 2 :
258         // Nuts
259                 name == "nutID"                 ? 0 :
260                 name == "nutWidth"              ? 3 :
261                 name == "nutThickness"  ? 4 :
262         // Washers
263                 name == "washerID"              ? 0 :
264                 name == "washerOD"              ? 5 :
265                 name == "washerThickness"? 6 :
266         -1; // Not Found.  Results in Undef fields.
267
268 function tableRow (name)=
269         // Bearing Names
270                 name == "606"           ? [6, 13, 6] :
271                 name == "608"           ? [8, 22, 7] :
272                 name == "624"           ? [4, 13, 5] :
273                 name == "Skate"                 ? [8, 22, 7] :
274         // Bolt, Nut, Washer Sizes
275         // It seems hardware size standards are loose, so these dimensions may not match your hardware.
276                 name == "M2"            ? [2, 4, 2, 4, 1.6, 5.5, 0.3] :         
277                 name == "M2.5"          ? [2.5, 5, 2.5, 5, 2, 7, 0.5] :         
278                 name == "M3"            ? [3, 5.5, 3, 5.5, 2.4, 7, 0.5] :               
279                 name == "M4"            ? [4, 7.0, 4, 7, 3.2, 9, 0.8] :
280                 name == "M5"            ? [5, 8.5, 5, 8, 4.7, 10, 1] :
281                 name == "M6"            ? [6, 10, 6, 10, 5.2, 12.5, 1.6 ] :
282                 name == "M8"            ? [8, 13, 8, 13, 6.8, 17, 1.8] :
283                 name == "#8"                    ? [0.164*in, 7.1, 0.164*in, 11/32*in, 3.05, in/2, in*0.04] :
284                 name == "1/4 Hex"       ? [in/4, in*0.505, in*11/64, in*7/16, in*5/32, in*0.734, in*0.063] :
285                 name == "5/16 Hex"      ? [in*5/16, in*0.577, in*7/32, in/2, in*3/16, in*0.875, in*0.063] :
286         -1; //Not found in Table
287
288 function tableEntry (rowName, fieldName) =
289         tableRow(rowName)[columnName(fieldName)];       
290
291
292 /*************************************************
293 OPENSCAD INTERFACE & EXAMPLES */
294
295 libEcho=true;
296 if (libEcho!=false){ 
297         echo ("****************************************************************  ");
298         echo ("  'Libs' library loaded. For a list of modules, use libHelp();            ");
299         echo ("   For help with translation/rotation functions, use libMoveHelp();");
300         echo ("   For examples of each object, use libDemo();                             ");
301         echo ("   To turn off this header, place libEcho=false; in your script        ");
302         echo ("   libs.scad is @ http://www.thingiverse.com/thing:6021             ");
303         echo ("****************** By randyy, 2011, LGPL v2+ ******************  ");
304 }
305
306 module libDemo(){
307         translate(slide(15)) pyramid();
308         translate(lift(5)) equilateralTriangle(center=true);
309         translate(slide(-15)) pyramid(square=true);
310         translate(slide(15)+push(15)) tube(5,2, height=10);
311         translate([-5,10,0]) roundRect();
312         translate([-15,15,0]) hex();
313         translate([15,-25,0]) slot([4,15,4],true,false);
314         translate([0,-25,0]) keyhole(2,4,15,4);
315         translate([-30,-10,0]) hexNut();
316         translate([-30,-20,0]) washer();
317         translate([-30,0,tableEntry("#8","headThickness")]) capBolt("M3",12);
318         translate([-30,15,tableEntry("#8","headThickness")]) hexBolt("#8",12);
319         translate([-15,-18,0]) rotate(tighten()) dovetail(9,4);
320 }
321
322 module libHelp(){
323         echo();
324         echo("  ***********************   libHelp   **************************");
325         echo("  displayMode= 1;  // 0=printer, 1=screen (resolution)  ");
326         echo("  hex (width=10, height=10, flats= true, center=false)  ");
327         echo("  equilateralTriangle (side=10, height=10, center=false)  ");
328         echo("  pyramid(side=10, height=-1, square=false, centerHorizontal=true, centerVertical=false)  ");
329         echo("  tube(r=2, thickness=1, height=1, center=false, outline=false)  ");
330         echo("  roundRect (size=[10,10,10], round=3, center=false)  ");
331         echo("  keyhole(r1, r2, length, height)  ");
332         echo("  slot(size=[4,10,1], startRound=true, endRound=true, centerXYZ=[1,0,0])  ");
333         echo("  dovetail (width=9, height=10, male=true)  ");
334         echo("  teardrop(radius=5, length=10, angle=90)  ");
335         echo("  hexNut(size='M3', center=false, outline=false, pos=[0,0,0])  ");
336         echo("  washer(size='M3', center=false, outline=false, pos=[0,0,0])  ");
337         echo("  capBolt (size='M3', length= 16, center=false, pos=[0,0,0])  ");
338         echo("  hexBolt (size='M3', length= 16, center=false, pos=[0,0,0])  ");
339         echo("  bearing (size='Skate', center=false, outline=false, pos=[0,0,0])  ");
340         echo("  TABLE FUNCTIONS  ");
341         echo("  function columnName(name)  ");
342         echo("  function tableRow (name)  ");
343         echo("  function tableEntry (rowName, fieldName)  ");
344         echo();
345 }
346
347 module libMoveHelp(){
348         echo();
349         echo("  *********************   libMoveHelp   ************************");
350         echo("  ROTATION - default is 90 degrees, but you can use any value in ().  ");
351         echo("     tighten(), clockwise, along z                                                       ");
352         echo("     loosen(), counterclockwise, along z                                            ");
353         echo("     righty(), leans right, along y axis                                                ");
354         echo("     lefty(), leans left, along y axis                                                     ");
355         echo("     backward() falls away, along x axis                                             ");
356         echo("     forward() falls toward you, along x axis                                      ");
357         echo("  TRANSLATION - negative numbers move in opposite direction      ");
358         echo("     push(distance) moves object away, along y                                 ");
359         echo("     lift(distance) moves object up, along z                                        ");
360         echo("     slide(distance) moves object right, along x                                 ");
361         echo();
362 }