From 60002575b9b4241c488e38a833a7197af6270317 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 9 Dec 2010 03:10:39 -0300 Subject: [PATCH] drl2gp.c: implemented drill file filtering, some code restructuring --- drl2gp/drl2gp.c | 174 ++++++++++++++++++++++++++++-------------------- 1 file changed, 103 insertions(+), 71 deletions(-) diff --git a/drl2gp/drl2gp.c b/drl2gp/drl2gp.c index bd03269..dbc2d8e 100644 --- a/drl2gp/drl2gp.c +++ b/drl2gp/drl2gp.c @@ -36,6 +36,7 @@ #define MAX_STEP 0.01 /* max arc step, in mm */ +static const char *filter = NULL; static double tool_d[MAX_TOOL+1]; static FILE *out = NULL; static int lineno = 1; @@ -48,6 +49,9 @@ static double depth, d0, d1; #define MIL2MM(mil) IN2MM((mil)/1000) +/* ----- gnuplot output ---------------------------------------------------- */ + + static void eprintf(const char *fmt, ...) { va_list ap; @@ -61,6 +65,78 @@ static void eprintf(const char *fmt, ...) } +static double arc2angle(double arc, double r) +{ + return acos(1-arc*arc/(r*r)/2); +} + + +static void half_circle(double cx, double cy, double rx, double ry, double s) +{ + double m[4]; + double x = rx, y = ry, tmp; + double a; + + m[0] = cos(s); + m[1] = -sin(s); + m[2] = -m[1]; + m[3] = m[0]; + + for (a = 0; a < M_PI; a += s) { + eprintf("%f %f %f\n", cx+x, cy+y, -depth); + tmp = x*m[0]+y*m[1]; + y = x*m[2]+y*m[3]; + x = tmp; + } + eprintf("%f %f %f\n", cx-rx, cy-ry, -depth); +} + + +static void slot(double xa, double ya, double xb, double yb, double d) +{ + double dx = xb-xa; + double dy = yb-ya; + double cr = d/2; + double tr = d0/2; + double s = arc2angle(MAX_STEP, cr); + double nx, ny; + double f; + + assert(mill); + f = (cr-tr)/hypot(dx, dy); + nx = -dy*f; + ny = dx*f; + + half_circle(xa, ya, nx, ny, s); + half_circle(xb, yb, -nx, -ny, s); + eprintf("%f %f %f\n\n", xa+nx, ya+ny, -depth); +} + + +static void circle(double cx, double cy, double d) +{ + double cr = d/2; + double tr = d0/2; + double s = arc2angle(MAX_STEP, cr); + double a; + + assert(mill); + for (a = 0; a < 2*M_PI; a += s) + eprintf("%f %f %f\n", + cx+(cr-tr)*cos(a), cy+(cr-tr)*sin(a), -depth); + eprintf("%f %f %f\n\n", cx+(cr-tr), cy, -depth); +} + + +static void drill(double x, double y) +{ + eprintf("%f %f %f\n\n", x, y, -depth); +} + + +/* ----- header parsing ---------------------------------------------------- */ + + static void header(void) { enum { @@ -80,7 +156,7 @@ static void header(void) while ((c = getchar()) != EOF) { if (out) { if (fputc(c, out) == EOF) { - perror("write"); + perror(filter); exit(1); } } @@ -178,78 +254,14 @@ static void header(void) } -static double arc2angle(double arc, double r) -{ - return acos(1-arc*arc/(r*r)/2); -} +/* ----- body parsing ------------------------------------------------------ */ -static void half_circle(double cx, double cy, double rx, double ry, double s) -{ - double m[4]; - double x = rx, y = ry, tmp; - double a; - - m[0] = cos(s); - m[1] = -sin(s); - m[2] = -m[1]; - m[3] = m[0]; - - for (a = 0; a < M_PI; a += s) { - eprintf("%f %f %f\n", cx+x, cy+y, -depth); - tmp = x*m[0]+y*m[1]; - y = x*m[2]+y*m[3]; - x = tmp; - } - eprintf("%f %f %f\n", cx-rx, cy-ry, -depth); -} - - -static void slot(double xa, double ya, double xb, double yb, double d) -{ - double dx = xb-xa; - double dy = yb-ya; - double cr = d/2; - double tr = d0/2; - double s = arc2angle(MAX_STEP, cr); - double nx, ny; - double f; - - assert(mill); - f = (cr-tr)/hypot(dx, dy); - nx = -dy*f; - ny = dx*f; - - half_circle(xa, ya, nx, ny, s); - half_circle(xb, yb, -nx, -ny, s); - eprintf("%f %f %f\n\n", xa+nx, ya+ny, -depth); -} - - -static void circle(double cx, double cy, double d) -{ - double cr = d/2; - double tr = d0/2; - double s = arc2angle(MAX_STEP, cr); - double a; - - assert(mill); - for (a = 0; a < 2*M_PI; a += s) - eprintf("%f %f %f\n", - cx+(cr-tr)*cos(a), cy+(cr-tr)*sin(a), -depth); - eprintf("%f %f %f\n\n", cx+(cr-tr), cy, -depth); -} - - -static void drill(double x, double y) -{ - eprintf("%f %f %f\n\n", x, y, -depth); -} +static int active = 0; static void do_cmd(char cmd, double v, int nl) { - static int active = 0; static int metric = 1; static int slotting = 0; static double x = 0, y = 0, x0 = 0, d = 0; @@ -346,6 +358,11 @@ static void body(void) int c, s = 0; while ((c = getchar()) != EOF) { + if (out && (c == 'T' || cmd == 'T' || !active)) + if (fputc(c, out) == EOF) { + perror(filter); + exit(1); + } if (c == '\n') { lineno++; if (cmd) @@ -374,6 +391,9 @@ static void body(void) } +/* ----- command line ------------------------------------------------------ */ + + static void usage(const char *name) { fprintf(stderr, @@ -390,7 +410,6 @@ static void usage(const char *name) int main(int argc, char **argv) { - const char *filtered = NULL; int metric = 1; double arg[MAX_ARGS]; int n_arg = 0; @@ -422,15 +441,15 @@ int main(int argc, char **argv) metric = 1; break; case 'f': - if (filtered) + if (filter) usage(*argv); if (argv[i][2]) - filtered = argv[i]+2; + filter = argv[i]+2; else { i++; if (i == argc) usage(*argv); - filtered = argv[i]; + filter = argv[i]; } break; default: @@ -454,6 +473,14 @@ int main(int argc, char **argv) usage(*argv); } + if (filter) { + out = fopen(filter, "w"); + if (!out) { + perror(filter); + exit(1); + } + } + header(); body(); @@ -462,5 +489,10 @@ int main(int argc, char **argv) exit(1); } + if (filter && fclose(out) == EOF) { + perror(filter); + exit(1); + } + return 0; }