1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 05:08:59 +02:00

drl2gp.c: implemented drill file filtering, some code restructuring

This commit is contained in:
Werner Almesberger 2010-12-09 03:10:39 -03:00
parent b75611a750
commit 60002575b9

View File

@ -36,6 +36,7 @@
#define MAX_STEP 0.01 /* max arc step, in mm */ #define MAX_STEP 0.01 /* max arc step, in mm */
static const char *filter = NULL;
static double tool_d[MAX_TOOL+1]; static double tool_d[MAX_TOOL+1];
static FILE *out = NULL; static FILE *out = NULL;
static int lineno = 1; static int lineno = 1;
@ -48,6 +49,9 @@ static double depth, d0, d1;
#define MIL2MM(mil) IN2MM((mil)/1000) #define MIL2MM(mil) IN2MM((mil)/1000)
/* ----- gnuplot output ---------------------------------------------------- */
static void eprintf(const char *fmt, ...) static void eprintf(const char *fmt, ...)
{ {
va_list ap; 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) static void header(void)
{ {
enum { enum {
@ -80,7 +156,7 @@ static void header(void)
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
if (out) { if (out) {
if (fputc(c, out) == EOF) { if (fputc(c, out) == EOF) {
perror("write"); perror(filter);
exit(1); exit(1);
} }
} }
@ -178,78 +254,14 @@ static void header(void)
} }
static double arc2angle(double arc, double r) /* ----- body parsing ------------------------------------------------------ */
{
return acos(1-arc*arc/(r*r)/2);
}
static void half_circle(double cx, double cy, double rx, double ry, double s) static int active = 0;
{
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 void do_cmd(char cmd, double v, int nl) static void do_cmd(char cmd, double v, int nl)
{ {
static int active = 0;
static int metric = 1; static int metric = 1;
static int slotting = 0; static int slotting = 0;
static double x = 0, y = 0, x0 = 0, d = 0; static double x = 0, y = 0, x0 = 0, d = 0;
@ -346,6 +358,11 @@ static void body(void)
int c, s = 0; int c, s = 0;
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
if (out && (c == 'T' || cmd == 'T' || !active))
if (fputc(c, out) == EOF) {
perror(filter);
exit(1);
}
if (c == '\n') { if (c == '\n') {
lineno++; lineno++;
if (cmd) if (cmd)
@ -374,6 +391,9 @@ static void body(void)
} }
/* ----- command line ------------------------------------------------------ */
static void usage(const char *name) static void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
@ -390,7 +410,6 @@ static void usage(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
const char *filtered = NULL;
int metric = 1; int metric = 1;
double arg[MAX_ARGS]; double arg[MAX_ARGS];
int n_arg = 0; int n_arg = 0;
@ -422,15 +441,15 @@ int main(int argc, char **argv)
metric = 1; metric = 1;
break; break;
case 'f': case 'f':
if (filtered) if (filter)
usage(*argv); usage(*argv);
if (argv[i][2]) if (argv[i][2])
filtered = argv[i]+2; filter = argv[i]+2;
else { else {
i++; i++;
if (i == argc) if (i == argc)
usage(*argv); usage(*argv);
filtered = argv[i]; filter = argv[i];
} }
break; break;
default: default:
@ -454,6 +473,14 @@ int main(int argc, char **argv)
usage(*argv); usage(*argv);
} }
if (filter) {
out = fopen(filter, "w");
if (!out) {
perror(filter);
exit(1);
}
}
header(); header();
body(); body();
@ -462,5 +489,10 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
if (filter && fclose(out) == EOF) {
perror(filter);
exit(1);
}
return 0; return 0;
} }