Date:2010-12-09 07:10:39 (13 years 3 months ago)
Author:Werner Almesberger
Commit:6f040f6a745c32adcdb2fb57095180499a1a913d
Message:drl2gp.c: implemented drill file filtering, some code restructuring

Files: drl2gp/drl2gp.c (10 diffs)

Change Details

drl2gp/drl2gp.c
3636#define MAX_STEP 0.01 /* max arc step, in mm */
3737
3838
39static const char *filter = NULL;
3940static double tool_d[MAX_TOOL+1];
4041static FILE *out = NULL;
4142static int lineno = 1;
...... 
4849#define MIL2MM(mil) IN2MM((mil)/1000)
4950
5051
52/* ----- gnuplot output ---------------------------------------------------- */
53
54
5155static void eprintf(const char *fmt, ...)
5256{
5357    va_list ap;
...... 
6165}
6266
6367
68static double arc2angle(double arc, double r)
69{
70    return acos(1-arc*arc/(r*r)/2);
71}
72
73
74static 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
95static 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
116static 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
131static 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
64140static void header(void)
65141{
66142    enum {
...... 
80156    while ((c = getchar()) != EOF) {
81157        if (out) {
82158            if (fputc(c, out) == EOF) {
83                perror("write");
159                perror(filter);
84160                exit(1);
85161            }
86162        }
...... 
178254}
179255
180256
181static double arc2angle(double arc, double r)
182{
183    return acos(1-arc*arc/(r*r)/2);
184}
257/* ----- body parsing ------------------------------------------------------ */
185258
186259
187static 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
208static 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
229static 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
244static void drill(double x, double y)
245{
246    eprintf("%f %f %f\n\n", x, y, -depth);
247}
260static int active = 0;
248261
249262
250263static void do_cmd(char cmd, double v, int nl)
251264{
252    static int active = 0;
253265    static int metric = 1;
254266    static int slotting = 0;
255267    static double x = 0, y = 0, x0 = 0, d = 0;
...... 
346358    int c, s = 0;
347359
348360    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            }
349366        if (c == '\n') {
350367            lineno++;
351368            if (cmd)
...... 
374391}
375392
376393
394/* ----- command line ------------------------------------------------------ */
395
396
377397static void usage(const char *name)
378398{
379399    fprintf(stderr,
...... 
390410
391411int main(int argc, char **argv)
392412{
393    const char *filtered = NULL;
394413    int metric = 1;
395414    double arg[MAX_ARGS];
396415    int n_arg = 0;
...... 
422441            metric = 1;
423442            break;
424443        case 'f':
425            if (filtered)
444            if (filter)
426445                usage(*argv);
427446            if (argv[i][2])
428                filtered = argv[i]+2;
447                filter = argv[i]+2;
429448            else {
430449                i++;
431450                if (i == argc)
432451                    usage(*argv);
433                filtered = argv[i];
452                filter = argv[i];
434453            }
435454            break;
436455        default:
...... 
454473        usage(*argv);
455474    }
456475
476    if (filter) {
477        out = fopen(filter, "w");
478        if (!out) {
479            perror(filter);
480            exit(1);
481        }
482    }
483
457484    header();
458485    body();
459486

Archive Download the corresponding diff file

Branches:
master



interactive