From 7aaaf8671ff6290410b9a0f2a42ef77f256d86aa Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 20 Sep 2013 18:28:19 -0300 Subject: [PATCH] cameo/gerber.c: crudely support some cases of G03 --- cameo/gerber.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cameo/gerber.c b/cameo/gerber.c index 62eaaa4..bd7a634 100644 --- a/cameo/gerber.c +++ b/cameo/gerber.c @@ -1,8 +1,8 @@ /* * gerber.c - Gerber file input * - * Written 2010 by Werner Almesberger - * Copyright 2010 Werner Almesberger + * Written 2010, 2013 by Werner Almesberger + * Copyright 2010, 2013 Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ #include #include #include +#include #include "path.h" #include "gerber.h" @@ -31,6 +32,45 @@ #define KU2MM(in) ((in)/10000.0*25.4) +/* + * @@@ Very crude implementation of G03. Tested with only one example in + * KiCad. + * + * Command definition from: + * http://www.rulabinsky.com/cavd/text/chapa.html + */ + +static void arc(struct path *path, const char *buf, double ax, double ay) +{ + int xi, yi, cxi, cyi; + double bx, by, cx, cy; + double r, a, b, t; + double x, y; + + if (sscanf(buf, "G03X%dY%dI%dJ%dD01*\n", &xi, &yi, &cxi, &cyi) != 4) + return; + bx = KU2MM(xi); + by = KU2MM(yi); + cx = KU2MM(cxi); + cy = KU2MM(cyi); + r = hypot(cx, cy); + cx += ax; + cy += ay; + a = atan2(ay-cy, ax-cx); + b = atan2(by-cy, bx-cx); + if (a > b) + b += 2*M_PI; +//fprintf(stderr, "@(%g,%g) (%g,%g)-(%g-%g) %g %g\n", +// cx, cy, ax, ay, bx, by, a/M_PI*180, b/M_PI*180); + for (t = a; t <= b; t += M_PI/180) { /* @@@ 1 deg increment */ + x = cx+r*cos(t); + y = cy+r*sin(t); + path_add(path, x, y, 0); + } + path_add(path, bx, by, 0); +} + + struct path *gerber_read(const char *name, double r_tool_default) { FILE *file; @@ -65,6 +105,15 @@ struct path *gerber_read(const char *name, double r_tool_default) } continue; } + if (!strncmp(buf, "G03", 3)) { + if (path) + abort(); + path = path_new(r_tool_default, name); + *anchor = path; + anchor = &path->next; + arc(path, buf, start_x, start_y); + continue; + } if (sscanf(buf, "X%dY%dD%d*\n", &xi, &yi, &d) != 3) continue; x = KU2MM(xi);