drl2gp/drl2gp.c |
36 | 36 | #define MAX_STEP 0.01 /* max arc step, in mm */ |
37 | 37 | |
38 | 38 | |
| 39 | static const char *filter = NULL; |
39 | 40 | static double tool_d[MAX_TOOL+1]; |
40 | 41 | static FILE *out = NULL; |
41 | 42 | static int lineno = 1; |
... | ... | |
48 | 49 | #define MIL2MM(mil) IN2MM((mil)/1000) |
49 | 50 | |
50 | 51 | |
| 52 | /* ----- gnuplot output ---------------------------------------------------- */ |
| 53 | |
| 54 | |
51 | 55 | static void eprintf(const char *fmt, ...) |
52 | 56 | { |
53 | 57 | va_list ap; |
... | ... | |
61 | 65 | } |
62 | 66 | |
63 | 67 | |
| 68 | static double arc2angle(double arc, double r) |
| 69 | { |
| 70 | return acos(1-arc*arc/(r*r)/2); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static void half_circle(double cx, double cy, double rx, double ry, double s) |
| 75 | { |
| 76 | double m[4]; |
| 77 | double x = rx, y = ry, tmp; |
| 78 | double a; |
| 79 | |
| 80 | m[0] = cos(s); |
| 81 | m[1] = -sin(s); |
| 82 | m[2] = -m[1]; |
| 83 | m[3] = m[0]; |
| 84 | |
| 85 | for (a = 0; a < M_PI; a += s) { |
| 86 | eprintf("%f %f %f\n", cx+x, cy+y, -depth); |
| 87 | tmp = x*m[0]+y*m[1]; |
| 88 | y = x*m[2]+y*m[3]; |
| 89 | x = tmp; |
| 90 | } |
| 91 | eprintf("%f %f %f\n", cx-rx, cy-ry, -depth); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void slot(double xa, double ya, double xb, double yb, double d) |
| 96 | { |
| 97 | double dx = xb-xa; |
| 98 | double dy = yb-ya; |
| 99 | double cr = d/2; |
| 100 | double tr = d0/2; |
| 101 | double s = arc2angle(MAX_STEP, cr); |
| 102 | double nx, ny; |
| 103 | double f; |
| 104 | |
| 105 | assert(mill); |
| 106 | f = (cr-tr)/hypot(dx, dy); |
| 107 | nx = -dy*f; |
| 108 | ny = dx*f; |
| 109 | |
| 110 | half_circle(xa, ya, nx, ny, s); |
| 111 | half_circle(xb, yb, -nx, -ny, s); |
| 112 | eprintf("%f %f %f\n\n", xa+nx, ya+ny, -depth); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static void circle(double cx, double cy, double d) |
| 117 | { |
| 118 | double cr = d/2; |
| 119 | double tr = d0/2; |
| 120 | double s = arc2angle(MAX_STEP, cr); |
| 121 | double a; |
| 122 | |
| 123 | assert(mill); |
| 124 | for (a = 0; a < 2*M_PI; a += s) |
| 125 | eprintf("%f %f %f\n", |
| 126 | cx+(cr-tr)*cos(a), cy+(cr-tr)*sin(a), -depth); |
| 127 | eprintf("%f %f %f\n\n", cx+(cr-tr), cy, -depth); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static void drill(double x, double y) |
| 132 | { |
| 133 | eprintf("%f %f %f\n\n", x, y, -depth); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* ----- header parsing ---------------------------------------------------- */ |
| 138 | |
| 139 | |
64 | 140 | static void header(void) |
65 | 141 | { |
66 | 142 | enum { |
... | ... | |
80 | 156 | while ((c = getchar()) != EOF) { |
81 | 157 | if (out) { |
82 | 158 | if (fputc(c, out) == EOF) { |
83 | | perror("write"); |
| 159 | perror(filter); |
84 | 160 | exit(1); |
85 | 161 | } |
86 | 162 | } |
... | ... | |
178 | 254 | } |
179 | 255 | |
180 | 256 | |
181 | | static double arc2angle(double arc, double r) |
182 | | { |
183 | | return acos(1-arc*arc/(r*r)/2); |
184 | | } |
| 257 | /* ----- body parsing ------------------------------------------------------ */ |
185 | 258 | |
186 | 259 | |
187 | | static void half_circle(double cx, double cy, double rx, double ry, double s) |
188 | | { |
189 | | double m[4]; |
190 | | double x = rx, y = ry, tmp; |
191 | | double a; |
192 | | |
193 | | m[0] = cos(s); |
194 | | m[1] = -sin(s); |
195 | | m[2] = -m[1]; |
196 | | m[3] = m[0]; |
197 | | |
198 | | for (a = 0; a < M_PI; a += s) { |
199 | | eprintf("%f %f %f\n", cx+x, cy+y, -depth); |
200 | | tmp = x*m[0]+y*m[1]; |
201 | | y = x*m[2]+y*m[3]; |
202 | | x = tmp; |
203 | | } |
204 | | eprintf("%f %f %f\n", cx-rx, cy-ry, -depth); |
205 | | } |
206 | | |
207 | | |
208 | | static void slot(double xa, double ya, double xb, double yb, double d) |
209 | | { |
210 | | double dx = xb-xa; |
211 | | double dy = yb-ya; |
212 | | double cr = d/2; |
213 | | double tr = d0/2; |
214 | | double s = arc2angle(MAX_STEP, cr); |
215 | | double nx, ny; |
216 | | double f; |
217 | | |
218 | | assert(mill); |
219 | | f = (cr-tr)/hypot(dx, dy); |
220 | | nx = -dy*f; |
221 | | ny = dx*f; |
222 | | |
223 | | half_circle(xa, ya, nx, ny, s); |
224 | | half_circle(xb, yb, -nx, -ny, s); |
225 | | eprintf("%f %f %f\n\n", xa+nx, ya+ny, -depth); |
226 | | } |
227 | | |
228 | | |
229 | | static void circle(double cx, double cy, double d) |
230 | | { |
231 | | double cr = d/2; |
232 | | double tr = d0/2; |
233 | | double s = arc2angle(MAX_STEP, cr); |
234 | | double a; |
235 | | |
236 | | assert(mill); |
237 | | for (a = 0; a < 2*M_PI; a += s) |
238 | | eprintf("%f %f %f\n", |
239 | | cx+(cr-tr)*cos(a), cy+(cr-tr)*sin(a), -depth); |
240 | | eprintf("%f %f %f\n\n", cx+(cr-tr), cy, -depth); |
241 | | } |
242 | | |
243 | | |
244 | | static void drill(double x, double y) |
245 | | { |
246 | | eprintf("%f %f %f\n\n", x, y, -depth); |
247 | | } |
| 260 | static int active = 0; |
248 | 261 | |
249 | 262 | |
250 | 263 | static void do_cmd(char cmd, double v, int nl) |
251 | 264 | { |
252 | | static int active = 0; |
253 | 265 | static int metric = 1; |
254 | 266 | static int slotting = 0; |
255 | 267 | static double x = 0, y = 0, x0 = 0, d = 0; |
... | ... | |
346 | 358 | int c, s = 0; |
347 | 359 | |
348 | 360 | while ((c = getchar()) != EOF) { |
| 361 | if (out && (c == 'T' || cmd == 'T' || !active)) |
| 362 | if (fputc(c, out) == EOF) { |
| 363 | perror(filter); |
| 364 | exit(1); |
| 365 | } |
349 | 366 | if (c == '\n') { |
350 | 367 | lineno++; |
351 | 368 | if (cmd) |
... | ... | |
374 | 391 | } |
375 | 392 | |
376 | 393 | |
| 394 | /* ----- command line ------------------------------------------------------ */ |
| 395 | |
| 396 | |
377 | 397 | static void usage(const char *name) |
378 | 398 | { |
379 | 399 | fprintf(stderr, |
... | ... | |
390 | 410 | |
391 | 411 | int main(int argc, char **argv) |
392 | 412 | { |
393 | | const char *filtered = NULL; |
394 | 413 | int metric = 1; |
395 | 414 | double arg[MAX_ARGS]; |
396 | 415 | int n_arg = 0; |
... | ... | |
422 | 441 | metric = 1; |
423 | 442 | break; |
424 | 443 | case 'f': |
425 | | if (filtered) |
| 444 | if (filter) |
426 | 445 | usage(*argv); |
427 | 446 | if (argv[i][2]) |
428 | | filtered = argv[i]+2; |
| 447 | filter = argv[i]+2; |
429 | 448 | else { |
430 | 449 | i++; |
431 | 450 | if (i == argc) |
432 | 451 | usage(*argv); |
433 | | filtered = argv[i]; |
| 452 | filter = argv[i]; |
434 | 453 | } |
435 | 454 | break; |
436 | 455 | default: |
... | ... | |
454 | 473 | usage(*argv); |
455 | 474 | } |
456 | 475 | |
| 476 | if (filter) { |
| 477 | out = fopen(filter, "w"); |
| 478 | if (!out) { |
| 479 | perror(filter); |
| 480 | exit(1); |
| 481 | } |
| 482 | } |
| 483 | |
457 | 484 | header(); |
458 | 485 | body(); |
459 | 486 | |