|
@@ -0,0 +1,378 @@
|
|
1
|
+/* Version 3
|
|
2
|
+ Added support for font selection (default is Letters.dxf)
|
|
3
|
+ Added WriteCube module
|
|
4
|
+ Added Rotate for text (rotates on the plane of the text)
|
|
5
|
+ Added writesphere
|
|
6
|
+ Added space= (spacing between characters in char widths) def=1
|
|
7
|
+ Added writecylinder()
|
|
8
|
+
|
|
9
|
+ By Harlan Martin
|
|
10
|
+ harlan@sutlog.com
|
|
11
|
+ January 2012
|
|
12
|
+
|
|
13
|
+ (The file TestWrite.scad gives More usage examples)
|
|
14
|
+ (This module requires the file Letters.dxf to reside in the same folder)
|
|
15
|
+ (The file Letters.dfx was created with inkscape..Each letter is in its own layer)
|
|
16
|
+ (This module seperates each letter in the string and imports it from Letters.dfx)
|
|
17
|
+
|
|
18
|
+*/
|
|
19
|
+
|
|
20
|
+ pi=3.1415926535897932384626433832795028841971693993751058209;
|
|
21
|
+ pi2=pi*2;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+// These control the default values for write() writesphere() writecube()
|
|
25
|
+// if the parameters are not included in the call. Feel free to set your own
|
|
26
|
+// defaults.
|
|
27
|
+
|
|
28
|
+//default settings
|
|
29
|
+ center=false;
|
|
30
|
+ h = 4; //mm letter height
|
|
31
|
+ t = 1; //mm letter thickness
|
|
32
|
+ space =1; //extra space between characters in (character widths)
|
|
33
|
+ rotate=0; // text rotation (clockwise)
|
|
34
|
+ font = "Letters.dxf"; //default for aditional fonts
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+// write cube defaults
|
|
38
|
+ face = "front"; // default face (top,bottom,left,right,back,front)
|
|
39
|
+ up =0; //mm up from center on face of cube
|
|
40
|
+ down=0;
|
|
41
|
+ right =0; //mm left from center on face of cube
|
|
42
|
+ left=0;
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+// write sphere defaults
|
|
46
|
+ rounded=false; //default for rounded letters on writesphere
|
|
47
|
+ north=0; // intial text position (I suggest leave these 0 defaults)
|
|
48
|
+ south=0;
|
|
49
|
+ east=0;
|
|
50
|
+ west=0;
|
|
51
|
+ spin=0;
|
|
52
|
+// writecylinder defaults
|
|
53
|
+ middle=0; //(mm toward middle of circle)
|
|
54
|
+ ccw=false; //write on top or bottom in a ccw direction
|
|
55
|
+ r1=0; //(not implimented yet)
|
|
56
|
+ r2=0; //(not implimented yet)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+// Contact me if your interested in how to make your own font files
|
|
61
|
+// Its tedious and time consuming, but not very hard
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+module writecylinder(text,where,radius,height){
|
|
65
|
+ wid=(.125* h *5.5 * space);
|
|
66
|
+ widall=wid*(len(text)-1)/2;
|
|
67
|
+ //angle that measures width of letters on sphere
|
|
68
|
+ function NAngle(radius)=(wid/(pi2*radius))*360;
|
|
69
|
+ //angle of half width of text
|
|
70
|
+ function mmangle(radius)=(widall/(pi2*radius)*360);
|
|
71
|
+
|
|
72
|
+ if ((face=="top")||(face=="bottom") ){
|
|
73
|
+ if (face=="top" ){
|
|
74
|
+ if (center==true){
|
|
75
|
+ writecircle(text,where+[0,0,height/2],radius-h,rotate=rotate,font=font,h=h,t=t,
|
|
76
|
+ space=space,east=east,west=west,middle=middle,ccw=ccw);
|
|
77
|
+ }else{
|
|
78
|
+ writecircle(text,where+[0,0,height],radius-h,rotate=rotate,font=font,h=h,t=t,
|
|
79
|
+ space=space,east=east,west=west,middle=middle,ccw=ccw);
|
|
80
|
+ }
|
|
81
|
+ }else{
|
|
82
|
+ rotate(180,[1,0,0])
|
|
83
|
+ if (center==true){
|
|
84
|
+ writecircle(text,where+[0,0,height/2],radius-h,rotate=rotate,font=font,h=h,t=t,
|
|
85
|
+ space=space,east=east,west=west,middle=middle,ccw=ccw);
|
|
86
|
+ }else{
|
|
87
|
+ writecircle(text,where+[0,0,0],radius-h,rotate=rotate,font=font,h=h,t=t,
|
|
88
|
+ space=space,east=east,west=west,middle=middle,ccw=ccw);
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ }else{
|
|
93
|
+// if (radius>0){
|
|
94
|
+ if (center==true) {
|
|
95
|
+ rotate(-mmangle(radius)*(1-abs(rotate)/90),[0,0,1])
|
|
96
|
+ translate(where)
|
|
97
|
+ writethecylinder(text,where,radius,height,r1=radius,r2=radius,h=h,
|
|
98
|
+ rotate=rotate,t=t,font=font,face=face,up=up,down=down,
|
|
99
|
+ east=east,west=west,center=center,space=space,rounded=rounded);
|
|
100
|
+ } else{
|
|
101
|
+ rotate(-mmangle(radius)*(1-abs(rotate)/90),[0,0,1])
|
|
102
|
+ translate(where+[0,0,height/2])
|
|
103
|
+ writethecylinder(text,where,radius,height,r1=radius,r2=radius,h=h,
|
|
104
|
+ rotate=rotate,t=t,font=font,face=face,up=up,down=down,
|
|
105
|
+ east=east,west=west,center=center,space=space,rounded=rounded);
|
|
106
|
+ }
|
|
107
|
+// the remarked out code is for cone shaped cylinders (not complete)
|
|
108
|
+// }else{
|
|
109
|
+// if (center==true) {
|
|
110
|
+// rotate(-mmangle(radius)*(1-abs(rotate)/90),[0,0,1])
|
|
111
|
+// translate(where)
|
|
112
|
+// writethecylinder(text,where,radius,height,r1=r1,r2=r2,h=h,
|
|
113
|
+// rotate=rotate,t=t,font=font,face=face,up=up,down=down,
|
|
114
|
+// east=east,west=west,center=center,space=space,rounded=rounded);
|
|
115
|
+// } else{
|
|
116
|
+// rotate(-mmangle(radius)*(1-abs(rotate)/90),[0,0,1])
|
|
117
|
+// translate(where+[0,0,height/2])
|
|
118
|
+// writethecylinder(text,where,radius,height,r1=r1,r2=r2,h=h,
|
|
119
|
+// rotate=rotate,t=t,font=font,face=face,up=up,down=down,
|
|
120
|
+// east=east,west=west,center=center,space=space,rounded=rounded);
|
|
121
|
+// }
|
|
122
|
+// }
|
|
123
|
+ }
|
|
124
|
+}
|
|
125
|
+module writecircle(text,where,radius){
|
|
126
|
+ wid=(.125* h *5.5 * space);
|
|
127
|
+ widall=wid*(len(text)-1)/2;
|
|
128
|
+ //angle that measures width of letters on sphere
|
|
129
|
+ function NAngle(radius)=(wid/(pi2*radius))*360;
|
|
130
|
+ //angle of half width of text
|
|
131
|
+ function mmangle(radius)=(widall/(pi2*radius)*360);
|
|
132
|
+
|
|
133
|
+ if (ccw==true){
|
|
134
|
+ rotate(-rotate+east-west,[0,0,1]){
|
|
135
|
+ rotate(-mmangle(radius-middle),[0,0,1]){
|
|
136
|
+ translate(where)
|
|
137
|
+ for (r=[0:len(text)-1]){
|
|
138
|
+ rotate(-90+r*NAngle(radius-middle),[0,0,1]) // bottom out=-270+r
|
|
139
|
+ translate([radius-middle,0,0])
|
|
140
|
+ //rotate(90,[1,0,0])
|
|
141
|
+ //rotate(90,[0,1,0])
|
|
142
|
+ rotate(-270,[0,0,1]) // flip text (botom out = -270)
|
|
143
|
+ write(text[r],center=true,h=h,t=t,font=font);
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ }else{
|
|
148
|
+ rotate(-rotate-east+west,[0,0,1]){
|
|
149
|
+ rotate(mmangle(radius-middle),[0,0,1]){
|
|
150
|
+ translate(where)
|
|
151
|
+ for (r=[0:len(text)-1]){
|
|
152
|
+ rotate(90-r*NAngle(radius-middle),[0,0,1]) // bottom out=-270+r
|
|
153
|
+ translate([radius-middle,0,0])
|
|
154
|
+ //rotate(90,[1,0,0])
|
|
155
|
+ //rotate(90,[0,1,0])
|
|
156
|
+ rotate(270,[0,0,1]) // flip text (botom out = -270)
|
|
157
|
+ write(text[r],center=true,h=h,t=t,font=font);
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+}
|
|
164
|
+module writethecylinder(text,where,radius,height,r1,r2){
|
|
165
|
+ wid=(.125* h *5.5 * space);
|
|
166
|
+ widall=wid*(len(text)-1)/2;
|
|
167
|
+ //angle that measures width of letters on sphere
|
|
168
|
+ function NAngle(radius)=(wid/(pi2*radius))*360*(1-abs(rotate)/90);
|
|
169
|
+ //angle of half width of text
|
|
170
|
+
|
|
171
|
+ function mmangle(radius)=(widall/(pi2*radius)*360);
|
|
172
|
+ translate([0,0,up-down])
|
|
173
|
+ rotate(east-west,[0,0,1])
|
|
174
|
+ for (r=[0:len(text)-1]){
|
|
175
|
+ rotate(-90+(r*NAngle(radius)),[0,0,1])
|
|
176
|
+ translate([radius,0,-r*((rotate)/90*wid)+(len(text)-1)/2*((rotate)/90*wid)])
|
|
177
|
+ rotate(90,[1,0,0])
|
|
178
|
+ rotate(90,[0,1,0])
|
|
179
|
+ write(text[r],center=true,h=h,rotate=rotate,t=t,font=font);
|
|
180
|
+ //echo("zloc=",height/2-r*((rotate)/90*wid)+(len(text)-1)/2*((rotate)/90*wid));
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+module writesphere(text,where,radius){
|
|
187
|
+ wid=(.125* h *5.5 * space);
|
|
188
|
+ widall=wid*(len(text)-1)/2;
|
|
189
|
+
|
|
190
|
+ echo("-----------------",widall,wid,mmangle(radius));
|
|
191
|
+ //angle that measures width of letters on sphere
|
|
192
|
+ function NAngle(radius)=(wid/(pi2*radius))*360;
|
|
193
|
+ //angle of half width of text
|
|
194
|
+ function mmangle(radius)=(widall/(pi2*radius)*360);
|
|
195
|
+
|
|
196
|
+ rotate(east-west,[0,0,1]){
|
|
197
|
+ rotate(south-north,[1,0,0]){
|
|
198
|
+ rotate(spin,[0,1,0]){
|
|
199
|
+ rotate(-mmangle(radius),[0,0,1]){
|
|
200
|
+ if ( rounded== false ){
|
|
201
|
+ translate(where)
|
|
202
|
+ for (r=[0:len(text)-1]){
|
|
203
|
+ rotate(-90+r*NAngle(radius),[0,0,1])
|
|
204
|
+ translate([radius,0,0])
|
|
205
|
+ rotate(90,[1,0,0])
|
|
206
|
+ rotate(90,[0,1,0])
|
|
207
|
+ write(text[r],center=true,h=h,rotate=rotate,t=t,font=font);
|
|
208
|
+ }
|
|
209
|
+ }else{
|
|
210
|
+ difference(){
|
|
211
|
+ translate(where)
|
|
212
|
+ for (r=[0:len(text)-1]){
|
|
213
|
+ rotate(-90+r*NAngle(radius),[0,0,1])
|
|
214
|
+ translate([radius,0,0])
|
|
215
|
+ rotate(90,[1,0,0])
|
|
216
|
+ rotate(90,[0,1,0])
|
|
217
|
+ write(text[r],center=true,h=h,rotate=rotate,t=t*2+h,font=font);
|
|
218
|
+ }
|
|
219
|
+ difference(){ //rounded outside
|
|
220
|
+ sphere(radius+(t*2+h)*2);
|
|
221
|
+ sphere(radius+t/2);
|
|
222
|
+ }
|
|
223
|
+ sphere(radius-t/2); // rounded inside for indented text
|
|
224
|
+ }
|
|
225
|
+ }
|
|
226
|
+ }
|
|
227
|
+}}}
|
|
228
|
+}
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+module writecube(text,where,size){
|
|
232
|
+ if (str(size)[0] != "["){
|
|
233
|
+ // its a square cube (size was not a matrix so make it one)
|
|
234
|
+ writethecube(text,where,[size,size,size],h=h,rotate=rotate,space=space,
|
|
235
|
+ t=t,font=font,face=face,up=up,down=down,right=right,left=left);
|
|
236
|
+
|
|
237
|
+ }else{
|
|
238
|
+ // its not square
|
|
239
|
+ writethecube(text,where,size,h=h,rotate=rotate,space=space,
|
|
240
|
+ t=t,font=font,face=face,up=up,down=down,right=right,left=left);
|
|
241
|
+ }
|
|
242
|
+}
|
|
243
|
+// I split the writecube module into 2 pieces.. easier to add features later
|
|
244
|
+module writethecube(text,where,size){
|
|
245
|
+ if (face=="front") {
|
|
246
|
+ translate([where[0]+right-left,where[1]-size[1]/2,where[2]+up-down])
|
|
247
|
+ rotate(90,[1,0,0])
|
|
248
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
249
|
+ }
|
|
250
|
+ if (face=="back") {
|
|
251
|
+ translate([where[0]+right-left,where[1]+size[1]/2,where[2]+up-down])
|
|
252
|
+ rotate(90,[1,0,0]) // rotate around the x axis
|
|
253
|
+ rotate(180,[0,1,0]) // rotate around the y axis (z before rotation)
|
|
254
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
255
|
+ }
|
|
256
|
+ if (face=="left") {
|
|
257
|
+ translate([where[0]-size[0]/2,where[1]-right+left,where[2]+up-down ])
|
|
258
|
+ rotate(90,[1,0,0]) // rotate around the x axis
|
|
259
|
+ rotate(90,[0,-1,0]) // rotate around the y axis (z before rotation)
|
|
260
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
261
|
+ }
|
|
262
|
+ if (face=="right") {
|
|
263
|
+ translate([where[0]+size[0]/2,where[1]+right-left,where[2] +up-down])
|
|
264
|
+ rotate(90,[1,0,0]) // rotate around the x axis
|
|
265
|
+ rotate(90,[0,1,0]) // rotate around the y axis (z before rotation)
|
|
266
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
267
|
+ }
|
|
268
|
+ if (face=="top") {
|
|
269
|
+ translate([where[0]+right-left,where[1]+up-down,where[2]+size[2]/2 ])
|
|
270
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
271
|
+ }
|
|
272
|
+ if (face=="bottom") {
|
|
273
|
+ translate([where[0]+right-left,where[1]-up+down,where[2]-size[2]/2 ])
|
|
274
|
+ rotate(180,[1,0,0])
|
|
275
|
+ write(text,center=true,h=h,rotate=rotate,t=t,font=font);
|
|
276
|
+ }
|
|
277
|
+}
|
|
278
|
+
|
|
279
|
+module write(word){
|
|
280
|
+
|
|
281
|
+ echo (h);
|
|
282
|
+ echo (word);
|
|
283
|
+ echo ("There are " ,len(word) ," letters in this string");
|
|
284
|
+// echo ("The second letter is ",word[1]);
|
|
285
|
+// echo (str(word[0],"_"));
|
|
286
|
+rotate(rotate,[0,0,-1]){
|
|
287
|
+ for (r = [0:len(word)]){ // count off each character
|
|
288
|
+ // if the letter is lower case, add an underscore to the end for file lookup
|
|
289
|
+ if ((word[r] == "a" ) || (word[r]== "b") || (word[r]== "c")
|
|
290
|
+ || (word[r]== "d") || (word[r]== "e") || (word[r]== "f")
|
|
291
|
+ || (word[r]== "g") || (word[r]== "h") || (word[r]== "i")
|
|
292
|
+ || (word[r]== "j") || (word[r]== "k") || (word[r]== "l")
|
|
293
|
+ || (word[r]== "m") || (word[r]== "n") || (word[r]== "o")
|
|
294
|
+ || (word[r]== "p") || (word[r]== "q") || (word[r]== "r")
|
|
295
|
+ || (word[r]== "s") || (word[r]== "t") || (word[r]== "u")
|
|
296
|
+ || (word[r]== "v") || (word[r]== "w") || (word[r]== "x")
|
|
297
|
+ || (word[r]== "y" )|| (word[r]== "z")){
|
|
298
|
+ if (center == true) {
|
|
299
|
+ translate([0,-h/2,0]){
|
|
300
|
+ scale([.125*h,.125*h,t]){
|
|
301
|
+ translate([ (-len(word)*5.5*space/2) + (r*5.5*space),0,0])
|
|
302
|
+ linear_extrude(height=1,convexity=10,center=true){
|
|
303
|
+ import(file = font,layer=str(word[r],"_"));
|
|
304
|
+ }
|
|
305
|
+ }
|
|
306
|
+ }
|
|
307
|
+ }else{
|
|
308
|
+ translate([0,0,t/2]){
|
|
309
|
+ scale([.125*h,.125*h,t]){
|
|
310
|
+ translate([r*5.5*space,0,0])
|
|
311
|
+ linear_extrude(height=1,convexity=10,center=true){
|
|
312
|
+ import(file = font,layer=str(word[r],"_"));
|
|
313
|
+ }
|
|
314
|
+ }
|
|
315
|
+ }
|
|
316
|
+ }
|
|
317
|
+
|
|
318
|
+ }else{
|
|
319
|
+ if (center == true) {
|
|
320
|
+ translate([0,-h/2,0]){
|
|
321
|
+ scale([.125*h,.125*h,t]){
|
|
322
|
+ translate([ (-len(word)*5.5*space/2) + (r*5.5*space),0,0])
|
|
323
|
+ linear_extrude(height=1,convexity=10,center=true){
|
|
324
|
+ import(file = font,layer=str(word[r]));
|
|
325
|
+ }
|
|
326
|
+ }
|
|
327
|
+ }
|
|
328
|
+ }else{
|
|
329
|
+ translate([0,0,t/2]){
|
|
330
|
+ scale([.125*h,.125*h,t]){
|
|
331
|
+ translate([r*5.5*space,0,0])
|
|
332
|
+ linear_extrude(height=1,convexity=10,center=true){
|
|
333
|
+ import(file = font,layer=str(word[r]));
|
|
334
|
+ }
|
|
335
|
+ }
|
|
336
|
+ }
|
|
337
|
+ }
|
|
338
|
+ }
|
|
339
|
+ }
|
|
340
|
+}
|
|
341
|
+}
|
|
342
|
+
|
|
343
|
+/*writecylinder test
|
|
344
|
+translate([0,0,0])
|
|
345
|
+%cylinder(r=20,h=40,center=true);
|
|
346
|
+color([1,0,0])
|
|
347
|
+writecylinder("rotate=90",[0,0,0],20,40,center=true,down=0,rotate=90);
|
|
348
|
+writecylinder("rotate = 30,east = 90",[0,0,0],20,40,center=true,down=0,rotate=30,east=90);
|
|
349
|
+writecylinder("ccw = true",[0,0,0],20,40,center=true,down=0,face="top",ccw=true);
|
|
350
|
+writecylinder("middle = 8",[0,0,0],20,40,h=3,center=true,down=0,face="top",middle=8);
|
|
351
|
+writecylinder("face = top",[0,0,0],20,40,center=true,down=0,face="top");
|
|
352
|
+writecylinder("east=90",[0,0,0],20,40,h=3,center=true,down=0,face="top",east=90);
|
|
353
|
+writecylinder("west=90",[0,0,0],20,40,h=3,center=true,down=0,face="top",ccw=true,west=90);
|
|
354
|
+writecylinder("face = bottom",[0,0,0],20,40,center=true,down=0,face="bottom");
|
|
355
|
+*/
|
|
356
|
+/*writesphere test
|
|
357
|
+sphere(20);
|
|
358
|
+color([1,0,0])
|
|
359
|
+writesphere("Hello World",[0,0,0],20,t=1,h=6);
|
|
360
|
+*/
|
|
361
|
+/* writecube test
|
|
362
|
+translate([30,30,30])
|
|
363
|
+cube([10,15,30],center=true);
|
|
364
|
+write("hello",center=true,rotate =30);
|
|
365
|
+color([1,0,0])
|
|
366
|
+writecube( "front",[30,30,30],[10,15,30],h=5,rotate=-90);
|
|
367
|
+color([0,1,0])
|
|
368
|
+writecube( "back",[30,30,30],size=[10,15,30],h=5,face="back",rotate=90,t=4);
|
|
369
|
+color([0,0,1])
|
|
370
|
+writecube( "left",[30,30,30],[10,15,30],h=5,face="left",up=5);
|
|
371
|
+color([1,1,0])
|
|
372
|
+writecube( "right",where=[30,30,30],size=[10,15,30],h=5,face="right",rotate=55);
|
|
373
|
+color([1,0,1])
|
|
374
|
+writecube( "top",where=[30,30,30],size=[10,15,30],h=5,face="top");
|
|
375
|
+color([1,1,1])
|
|
376
|
+writecube( "bttm",where=[30,30,30],size=[10,15,30],h=5,face="bottom",rotate=90);
|
|
377
|
+*/
|
|
378
|
+
|