Date:2011-09-05 01:35:34 (12 years 6 months ago)
Author:Werner Almesberger
Commit:e5d8fd87c352cf9c9f4a9f21cf839476815923da
Message:fped/: added gnuplot output for line/rect/circle with -g (experimental)

Files: Makefile (1 diff)
file.c (2 diffs)
file.h (1 diff)
fped.c (5 diffs)
gnuplot.c (1 diff)
gnuplot.h (1 diff)

Change Details

Makefile
1515UPLOAD = www-data@downloads.qi-hardware.com:werner/fped/
1616
1717OBJS = fped.o expr.o coord.o obj.o delete.o inst.o util.o error.o \
18       unparse.o file.o dump.o kicad.o postscript.o meas.o \
18       unparse.o file.o dump.o kicad.o postscript.o gnuplot.o meas.o \
1919       layer.o overlap.o hole.o tsort.o bitset.o \
2020       cpp.o lex.yy.o y.tab.o \
2121       gui.o gui_util.o gui_style.o gui_inst.o gui_status.o gui_canvas.o \
file.c
2020#include "dump.h"
2121#include "kicad.h"
2222#include "postscript.h"
23#include "gnuplot.h"
2324#include "util.h"
2425#include "file.h"
2526#include "fped.h"
...... 
197198{
198199    do_write_ps(postscript_fullpage, one);
199200}
201
202
203void write_gnuplot(const char *one)
204{
205    char *name;
206
207    if (save_file_name) {
208        name = set_extension(save_file_name, "gp");
209        save_to(name, gnuplot, one);
210        free(name);
211    } else {
212        if (!gnuplot(stdout, one))
213            perror("stdout");
214    }
215}
file.h
3232void write_kicad(void);
3333void write_ps(const char *one);
3434void write_ps_fullpage(const char *one);
35void write_gnuplot(const char *one);
3536
3637#endif /* !FILE_H */
fped.c
6969    fprintf(stderr,
7070"usage: %s [batch_mode] [cpp_option ...] [in_file [out_file]]\n\n"
7171"Batch mode options:\n"
72" -g [-1 package]\n"
73" write gnuplot output, then exit\n"
7274" -k write KiCad output, then exit\n"
7375" -p write Postscript output, then exit\n"
7476" -P [-s scale] [-1 package]\n"
7577" 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"
7878" -T test mode. Load file, then exit\n"
7979" -T -T test mode. Load file, dump to stdout, then exit\n\n"
8080"Common options:\n"
81" -1 name output only the specified package\n"
82" -s scale scale factor for -P (default: auto-scale)\n"
8183" cpp_option -Idir, -Dname[=value], or -Uname\n"
8284    , name);
8385    exit(1);
...... 
9193        batch_kicad,
9294        batch_ps,
9395        batch_ps_fullpage,
96        batch_gnuplot,
9497        batch_test
9598    } batch = batch_none;
9699    char *name = *argv;
...... 
104107    const char *one = NULL;
105108    int c;
106109
107    while ((c = getopt(argc, argv, "1:kps:D:I:PTU:")) != EOF)
110    while ((c = getopt(argc, argv, "1:gkps:D:I:PTU:")) != EOF)
108111        switch (c) {
109112        case '1':
110113            one = optarg;
111114            break;
115        case 'g':
116            if (batch)
117                usage(*argv);
118            batch = batch_gnuplot;
119            break;
112120        case 'k':
113121            if (batch)
114122                usage(*argv);
...... 
146154            usage(name);
147155        }
148156
149    if (one && batch != batch_ps && batch != batch_ps_fullpage)
157    if (one && batch != batch_ps && batch != batch_ps_fullpage &&
158        batch != batch_gnuplot)
150159        usage(name);
151160
152161    if (!batch) {
...... 
200209    case batch_ps_fullpage:
201210        write_ps_fullpage(one);
202211        break;
212    case batch_gnuplot:
213        write_gnuplot(one);
214        break;
203215    case batch_test:
204216        dump(stdout, NULL);
205217        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
26static 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
35static 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
43static 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
58static 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
77static 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
102static void gnuplot_arc(FILE *file, const struct inst *inst)
103{
104abort();
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
134static 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
167static 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
188int 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}
gnuplot.h
1/*
2 * gnuplot.h - 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#ifndef GNUPLOT_H
15#define GNUPLOT_H
16
17#include <stdio.h>
18
19
20int gnuplot(FILE *file, const char *one);
21
22#endif /* !GNUPLOT_H */

Archive Download the corresponding diff file

Branches:
master



interactive