1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 16:13:56 +02:00
cae-tools/cameo/lang.y
Werner Almesberger 3ae3d6cdd3 cameo: cleaned up and documented the tool compensation
- README, lang.l, lang.y: renamed the "tool" command to "offset"
- README: documented "offset"
- ops.h, ops.c (tool_comp_paths), cameo.c (main), lang.y: added
  parameter to tool_comp_paths to treat all paths as internal
- lang.l, lang.y: added option "inside" to the "offset" command
- lang.y: "offset" can now take multiple options in any order
2010-12-14 23:37:31 -03:00

278 lines
4.0 KiB
Plaintext

%{
/*
* lang.y - Toolpath adaptation language
*
* Written 2010 by Werner Almesberger
* Copyright 2010 by 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include "path.h"
#include "ops.h"
#include "gnuplot.h"
#include "gerber.h"
#include "y.tab.h"
static double xo = 0, yo = 0, zo = 0;
static struct path *paths = NULL;
#define MIL2MM(mil) ((mil)/1000*25.4)
static void add_paths(struct path *new)
{
struct path **anchor = &paths;
while (*anchor)
anchor = &(*anchor)->next;
*anchor = new;
}
static void translate(double x, double y, double z)
{
struct path *path;
struct point *p;
for (path = paths; path; path = path->next)
for (p = path->first; p; p = p->next) {
p->x += x;
p->y += y;
p->z += z;
}
}
static double ref_pick_1(int ref, double a, double b)
{
switch (ref) {
case 0:
return a;
case 1:
return (a+b)/2;
case 2:
return b;
default:
abort();
}
}
static void ref_pick_xy(int ref, double xa, double ya, double xb, double yb,
double *x, double *y)
{
*x = ref_pick_1((ref-1) % 3, xa, xb);
*y = ref_pick_1((ref-1)/3, ya, yb);
}
static void align(int ref, double x, double y)
{
const struct path *path;
const struct point *p;
double xa = 0, ya = 0, xb = 0, yb = 0;
double xr, yr, xd, yd;
int first = 1;
for (path = paths; path; path = path->next)
for (p = path->first; p; p = p->next) {
if (first || p->x < xa)
xa = p->x;
if (first || p->x > xb)
xb = p->x;
if (first || p->y < ya)
ya = p->y;
if (first || p->y > yb)
yb = p->y;
first = 0;
}
ref_pick_xy(ref, xa, ya, xb, yb, &xr, &yr);
xd = x-xr;
yd = y-yr;
translate(xd, yd, 0);
xo += xd;
yo += yd;
}
%}
%union {
double num;
char *str;
enum {
OO_DOG = 1 << 0,
OO_INSIDE = 1 << 1,
} oopt;
};
%token TOK_ALIGN TOK_ARRAY TOK_CLEAR TOK_RESET TOK_OFFSET
%token TOK_TRANSLATE TOK_Z
%token TOK_GERBER TOK_GNUPLOT TOK_WRITE
%token TOK_DOG TOK_INSIDE
%token <num> NUM_EXP_MIL NUM_EXP_MM NUM_IMP_MIL NUM_IMP_MM REF
%token <str> STRING
%type <str> opt_filename
%type <num> dimen number
%type <oopt> offset_options offset_option
%%
all:
| command all
;
command:
TOK_ALIGN REF dimen dimen
{
align((int) $2, $3, $4);
}
| TOK_ALIGN REF dimen dimen dimen dimen
{
int ref = $2;
double x, y;
if ($3 > $5)
yyerror("left edge > right edge");
if ($4 > $6)
yyerror("bottom > top");
ref_pick_xy(ref, $3, $4, $5, $6, &x, &y);
align(ref, x, y);
}
| TOK_ARRAY dimen dimen number number
{
double x = $2*$4;
double y = $3*$5;
translate(x, y, 0);
}
| TOK_CLEAR
{
struct path *next;
while (paths) {
next = paths->next;
path_free(paths);
paths = next;
}
}
| TOK_RESET
{
xo = yo = 0;
}
| TOK_OFFSET offset_options
{
tool_comp_paths(paths,
!!($2 & OO_DOG), !!($2 & OO_INSIDE));
}
| TOK_TRANSLATE dimen dimen
{
translate($2, $3, 0);
xo += $2;
yo += $3;
}
| TOK_Z dimen
{
zo += $2;
}
| TOK_Z dimen dimen
{
zo += $3-$2;
}
| TOK_GERBER dimen opt_filename
{
add_paths(gerber_read($3, $2/2));
translate(xo, yo, 0);
}
| TOK_GNUPLOT dimen opt_filename
{
add_paths(gnuplot_read($3, $2/2));
translate(xo, yo, 0);
}
| TOK_WRITE opt_filename
{
translate(0, 0, zo);
gnuplot_write($2, paths);
translate(0, 0, -zo);
}
;
opt_filename:
{
$$ = NULL;
}
| STRING
{
$$ = $1;
}
;
dimen:
NUM_EXP_MM
{
$$ = $1;
}
| NUM_IMP_MM
{
$$ = $1;
}
| NUM_EXP_MIL
{
$$ = MIL2MM($1);
}
| NUM_IMP_MIL
{
$$ = MIL2MM($1);
}
;
number:
NUM_IMP_MIL
{
$$ = $1;
}
| NUM_IMP_MM
{
$$ = $1;
}
;
offset_options:
{
$$ = 0;
}
| offset_option offset_options
{
$$ = $1 | $2;
}
;
offset_option:
TOK_DOG
{
$$ = OO_DOG;
}
| TOK_INSIDE
{
$$ = OO_INSIDE;
}
;