fped.c |
69 | 69 | fprintf(stderr, |
70 | 70 | "usage: %s [batch_mode] [cpp_option ...] [in_file [out_file]]\n\n" |
71 | 71 | "Batch mode options:\n" |
| 72 | " -g [-1 package]\n" |
| 73 | " write gnuplot output, then exit\n" |
72 | 74 | " -k write KiCad output, then exit\n" |
73 | 75 | " -p write Postscript output, then exit\n" |
74 | 76 | " -P [-s scale] [-1 package]\n" |
75 | 77 | " write Postscript output (full page), then exit\n" |
76 | | " -1 name output only the specified package\n" |
77 | | " -s scale scale factor for -P (default: auto-scale)\n" |
78 | 78 | " -T test mode. Load file, then exit\n" |
79 | 79 | " -T -T test mode. Load file, dump to stdout, then exit\n\n" |
80 | 80 | "Common options:\n" |
| 81 | " -1 name output only the specified package\n" |
| 82 | " -s scale scale factor for -P (default: auto-scale)\n" |
81 | 83 | " cpp_option -Idir, -Dname[=value], or -Uname\n" |
82 | 84 | , name); |
83 | 85 | exit(1); |
... | ... | |
91 | 93 | batch_kicad, |
92 | 94 | batch_ps, |
93 | 95 | batch_ps_fullpage, |
| 96 | batch_gnuplot, |
94 | 97 | batch_test |
95 | 98 | } batch = batch_none; |
96 | 99 | char *name = *argv; |
... | ... | |
104 | 107 | const char *one = NULL; |
105 | 108 | int c; |
106 | 109 | |
107 | | while ((c = getopt(argc, argv, "1:kps:D:I:PTU:")) != EOF) |
| 110 | while ((c = getopt(argc, argv, "1:gkps:D:I:PTU:")) != EOF) |
108 | 111 | switch (c) { |
109 | 112 | case '1': |
110 | 113 | one = optarg; |
111 | 114 | break; |
| 115 | case 'g': |
| 116 | if (batch) |
| 117 | usage(*argv); |
| 118 | batch = batch_gnuplot; |
| 119 | break; |
112 | 120 | case 'k': |
113 | 121 | if (batch) |
114 | 122 | usage(*argv); |
... | ... | |
146 | 154 | usage(name); |
147 | 155 | } |
148 | 156 | |
149 | | if (one && batch != batch_ps && batch != batch_ps_fullpage) |
| 157 | if (one && batch != batch_ps && batch != batch_ps_fullpage && |
| 158 | batch != batch_gnuplot) |
150 | 159 | usage(name); |
151 | 160 | |
152 | 161 | if (!batch) { |
... | ... | |
200 | 209 | case batch_ps_fullpage: |
201 | 210 | write_ps_fullpage(one); |
202 | 211 | break; |
| 212 | case batch_gnuplot: |
| 213 | write_gnuplot(one); |
| 214 | break; |
203 | 215 | case batch_test: |
204 | 216 | dump(stdout, NULL); |
205 | 217 | break; |
gnuplot.c |
| 1 | /* |
| 2 | * gnuplot.c - Dump objects in gnuplot 2D format |
| 3 | * |
| 4 | * Written 2011 by Werner Almesberger |
| 5 | * Copyright 2011 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdlib.h> |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #include "coord.h" |
| 19 | #include "inst.h" |
| 20 | #include "gnuplot.h" |
| 21 | |
| 22 | |
| 23 | #define ARC_STEP 0.1 /* @@@ make configurable */ |
| 24 | |
| 25 | |
| 26 | static void recurse_id(FILE *file, const struct inst *inst) |
| 27 | { |
| 28 | if (inst->obj->frame->name) { |
| 29 | recurse_id(file, inst->outer); |
| 30 | fprintf(file, "/%s", inst->obj->frame->name); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | static void identify(FILE *file, const struct inst *inst) |
| 36 | { |
| 37 | fprintf(file, "#%%id="); |
| 38 | recurse_id(file, inst); |
| 39 | fprintf(file, "\n"); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | static void gnuplot_line(FILE *file, const struct inst *inst) |
| 44 | { |
| 45 | double xa, ya, xb, yb; |
| 46 | |
| 47 | xa = units_to_mm(inst->base.x); |
| 48 | ya = units_to_mm(inst->base.y); |
| 49 | xb = units_to_mm(inst->u.rect.end.x); |
| 50 | yb = units_to_mm(inst->u.rect.end.y); |
| 51 | |
| 52 | identify(file, inst); |
| 53 | fprintf(file, "#%%r=%f\n%f %f\n%f %f\n\n", |
| 54 | units_to_mm(inst->u.rect.width), xa, ya, xb, yb); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void gnuplot_rect(FILE *file, const struct inst *inst) |
| 59 | { |
| 60 | double xa, ya, xb, yb; |
| 61 | |
| 62 | xa = units_to_mm(inst->base.x); |
| 63 | ya = units_to_mm(inst->base.y); |
| 64 | xb = units_to_mm(inst->u.rect.end.x); |
| 65 | yb = units_to_mm(inst->u.rect.end.y); |
| 66 | |
| 67 | identify(file, inst); |
| 68 | fprintf(file, "#%%r=%f\n", units_to_mm(inst->u.rect.width)); |
| 69 | fprintf(file, "%f %f\n", xa, ya); |
| 70 | fprintf(file, "%f %f\n", xa, yb); |
| 71 | fprintf(file, "%f %f\n", xb, yb); |
| 72 | fprintf(file, "%f %f\n", xb, ya); |
| 73 | fprintf(file, "%f %f\n\n", xa, ya); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static void gnuplot_circ(FILE *file, const struct inst *inst) |
| 78 | { |
| 79 | double cx, cy, r; |
| 80 | double a; |
| 81 | int n, i; |
| 82 | |
| 83 | cx = units_to_mm(inst->base.x); |
| 84 | cy = units_to_mm(inst->base.y); |
| 85 | r = units_to_mm(inst->u.arc.r); |
| 86 | |
| 87 | identify(file, inst); |
| 88 | fprintf(file, "#%%r=%f\n", units_to_mm(inst->u.arc.width)); |
| 89 | |
| 90 | n = ceil(2*r*M_PI/ARC_STEP); |
| 91 | if (n < 2) |
| 92 | n = 2; |
| 93 | |
| 94 | for (i = 0; i <= n; i++) { |
| 95 | a = 2*M_PI/n*i; |
| 96 | fprintf(file, "%f %f\n", cx+r*sin(a), cy+r*cos(a)); |
| 97 | } |
| 98 | fprintf(file, "\n"); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | static void gnuplot_arc(FILE *file, const struct inst *inst) |
| 103 | { |
| 104 | abort(); |
| 105 | #if 0 |
| 106 | struct coord p; |
| 107 | double a; |
| 108 | |
| 109 | /* |
| 110 | * The documentation says: |
| 111 | * Xstart, Ystart, Xend, Yend, Angle, Width, Layer |
| 112 | * |
| 113 | * But it's really: |
| 114 | * Xcenter, Ycenter, Xend, Yend, ... |
| 115 | */ |
| 116 | p = rotate_r(inst->base, inst->u.arc.r, inst->u.arc.a2); |
| 117 | a = inst->u.arc.a2-inst->u.arc.a1; |
| 118 | while (a <= 0) |
| 119 | a += 360; |
| 120 | while (a > 360) |
| 121 | a -= 360; |
| 122 | fprintf(file, "DA %d %d %d %d %d %d %d\n", |
| 123 | units_to_kicad(inst->base.x), |
| 124 | -units_to_kicad(inst->base.y), |
| 125 | units_to_kicad(p.x), |
| 126 | -units_to_kicad(p.y), |
| 127 | (int) (a*10.0), |
| 128 | units_to_kicad(inst->u.arc.width), |
| 129 | layer_silk_top); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static void gnuplot_inst(FILE *file, enum inst_prio prio, |
| 135 | const struct inst *inst) |
| 136 | { |
| 137 | switch (prio) { |
| 138 | case ip_pad_copper: |
| 139 | case ip_pad_special: |
| 140 | /* complain ? */ |
| 141 | break; |
| 142 | case ip_hole: |
| 143 | /* complain ? */ |
| 144 | break; |
| 145 | case ip_line: |
| 146 | gnuplot_line(file, inst); |
| 147 | break; |
| 148 | case ip_rect: |
| 149 | gnuplot_rect(file, inst); |
| 150 | break; |
| 151 | case ip_circ: |
| 152 | gnuplot_circ(file, inst); |
| 153 | break; |
| 154 | case ip_arc: |
| 155 | gnuplot_arc(file, inst); |
| 156 | break; |
| 157 | default: |
| 158 | /* |
| 159 | * Don't try to export vectors, frame references, or |
| 160 | * measurements. |
| 161 | */ |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static void gnuplot_package(FILE *file, const struct pkg *pkg) |
| 168 | { |
| 169 | enum inst_prio prio; |
| 170 | const struct inst *inst; |
| 171 | |
| 172 | /* |
| 173 | * Package name |
| 174 | */ |
| 175 | fprintf(file, "# %s\n", pkg->name); |
| 176 | |
| 177 | FOR_INST_PRIOS_UP(prio) { |
| 178 | for (inst = pkgs->insts[prio]; inst; inst = inst->next) |
| 179 | gnuplot_inst(file, prio, inst); |
| 180 | for (inst = pkg->insts[prio]; inst; inst = inst->next) |
| 181 | gnuplot_inst(file, prio, inst); |
| 182 | } |
| 183 | |
| 184 | fprintf(file, "\n"); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | int gnuplot(FILE *file, const char *one) |
| 189 | { |
| 190 | const struct pkg *pkg; |
| 191 | |
| 192 | for (pkg = pkgs; pkg; pkg = pkg->next) |
| 193 | if (pkg->name) |
| 194 | if (!one || !strcmp(pkg->name, one)) |
| 195 | gnuplot_package(file, pkg); |
| 196 | |
| 197 | fflush(file); |
| 198 | return !ferror(file); |
| 199 | } |