-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_svg.c
349 lines (290 loc) · 8.57 KB
/
post_svg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
post_svg.c
This is part of pcb2g - pcb bitmap to G code converter
Copyright (C) 2011- 2015 Peter Popovec, [email protected]
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 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Postprocesor, SVG file generator
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include "post.h"
#include "pgeom.h"
//SVG unit = 0.01
#define ZOOM 2.0
//measurment unit
#define MM "mm"
//0.5mm etch tool
#define SW 0.50
//drill stroke 0.1mm
#define DSW 0.10
//drill default 0.7mm holes (70+stroke)
#define DRILL 0.70
#define FD_TEST(p) {if(!p) return;if(!(p->data))return;}
#define DATA(p) ((struct svg_local_data *)((p)->data))
struct svg_local_data
{
FILE *fd;
enum POST_MACHINE_OPS op;
double etch_tool_dia;
double cut_tool_dia;
double real_x, real_y;
double last_x, last_y;
int r_flag;
double tool_dia;
//TODO use compensation
int comp; //tool radius compensation
};
static void
svg_operation (struct postprocesor *p, enum POST_MACHINE_OPS op)
{
double h;
FD_TEST (p);
if (!DATA (p)->fd)
return;
switch (op)
{
case MACHINE_IDLE:
if (DATA (p)->r_flag)
fprintf (DATA (p)->fd, "\" />\n");
DATA (p)->r_flag = 0;
fprintf (DATA (p)->fd, "</g>\n");
DATA (p)->tool_dia = 0;
break;
case MACHINE_SETUP:
h = DATA (p)->cut_tool_dia;
if (DATA (p)->etch_tool_dia > DATA (p)->cut_tool_dia)
h = DATA (p)->etch_tool_dia;
fprintf (DATA (p)->fd, "<?xml version=\"1.0\" standalone=\"no\"?>\n");
fprintf (DATA (p)->fd,
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
fprintf (DATA (p)->fd,
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
fprintf (DATA (p)->fd,
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\"\n");
fprintf (DATA (p)->fd, " width=\"%f%s\" height=\"%f%s\" viewBox=\"%f %f %f %f \" >\n", DATA (p)->real_x + h * 2.0, MM, DATA (p)->real_y + h * 2.0, MM, (-h * ZOOM), //min X
(-h * ZOOM), //min Y
((DATA (p)->real_x + h * 2.0) * ZOOM), //width
((DATA (p)->real_y + h * 2.0) * ZOOM)); //height
fprintf (DATA (p)->fd, "<title>PCB2G, debug file</title>\n");
fprintf (DATA (p)->fd,
"<desc>Picture generated by pcb2g, http://pcb2g.fei.tuke.sk</desc>\n");
fprintf (DATA (p)->fd,
"<g style=\"fill:black; stroke:black; stroke-width:1\"></g>\n");
DATA (p)->tool_dia = 0;
break;
case MACHINE_ETCH:
fprintf (DATA (p)->fd, "<g style=\"fill:#FFFFFF; fill-opacity:0.0; \n");
fprintf (DATA (p)->fd,
" stroke:#000000; stroke-linecap:round; stroke-linejoin:round; stroke-opacity:1.0; stroke-width:%f\"\n",
DATA (p)->etch_tool_dia * ZOOM);
DATA (p)->tool_dia = DATA (p)->etch_tool_dia;
fprintf (DATA (p)->fd, " transform=\"translate(0 0) scale(1 1)\">\n");
DATA (p)->r_flag = 0;
break;
case MACHINE_DRILL:
fprintf (DATA (p)->fd,
"<g style=\"fill:#FFFFFF; fill-opacity:1.0; stroke:#FF0000; stroke-linecap:round; ");
fprintf (DATA (p)->fd,
"stroke-linejoin:round; stroke-opacity:1.0; stroke-width:%f\"\n",
DSW);
fprintf (DATA (p)->fd, "transform=\"translate(0 0) scale(1 1)\">\n");
break;
case MACHINE_CUT:
fprintf (DATA (p)->fd, "<g style=\"fill:#FFFFFF; fill-opacity:0.0; \n");
fprintf (DATA (p)->fd,
" stroke:#606060; stroke-linecap:round; stroke-linejoin:round; stroke-opacity:1.0; stroke-width:%f\"\n",
DATA (p)->cut_tool_dia * ZOOM);
DATA (p)->tool_dia = DATA (p)->cut_tool_dia;
fprintf (DATA (p)->fd, " transform=\"translate(0 0) scale(1 1)\">\n");
DATA (p)->r_flag = 0;
break;
case MACHINE_END:
fprintf (DATA (p)->fd, "</svg>\n");
break;
}
DATA (p)->op = op;
}
static void
svg_open (struct postprocesor *p, char *filename)
{
FD_TEST (p);
if (filename)
{
int len;
DATA (p)->fd = NULL;
//test default extension...
len = strlen (filename);
if (len > 4)
{
if (0 == strcmp (filename + len - 4, ".svg"))
DATA (p)->fd = fopen (filename, "w");
}
if (!(DATA (p)->fd))
{
char *f;
f = calloc (sizeof (char), len + 5);
strcpy (f, filename);
strcat (f, ".svg");
DATA (p)->fd = fopen (f, "w");
free (f);
}
}
else
DATA (p)->fd = stderr;
}
static void
svg_close (struct postprocesor *p)
{
FD_TEST (p);
if (DATA (p)->fd)
fclose (DATA (p)->fd);
free (p->data);
}
static void
svg_write_comment (struct postprocesor *p, char *comment)
{
FD_TEST (p);
if (!DATA (p)->fd)
return;
}
static void
svg_set (struct postprocesor *p, enum POST_SET op, va_list ap)
{
FD_TEST (p);
if (!DATA (p)->fd)
return;
switch (op)
{
case POST_SET_X:
DATA (p)->real_x = va_arg (ap, double);
break;
case POST_SET_Y:
DATA (p)->real_y = va_arg (ap, double);
break;
case POST_SET_ETCH:
DATA (p)->etch_tool_dia = va_arg (ap, double);
va_arg (ap, double);
va_arg (ap, double);
break;
case POST_SET_CUT:
DATA (p)->cut_tool_dia = va_arg (ap, double);
va_arg (ap, double);
va_arg (ap, double);
break;
case POST_SET_ROUTE_RETRACT:
case POST_SET_HOLE_RETRACT:
case POST_SET_SAFE_TRAVERSE:
break;
case POST_SET_CUTTER_COMP:
DATA (p)->comp = va_arg (ap, int);
break;
}
}
static void
svg_route (struct postprocesor *p, double x, double y)
{
FD_TEST (p);
if (!DATA (p)->fd)
return;
DATA (p)->r_flag = 1;
DATA (p)->last_x = x;
DATA (p)->last_y = y;
fprintf (DATA (p)->fd, "L%f %f ", (x * ZOOM), (y * ZOOM));
}
static void
svg_route_arc (struct postprocesor *p, double x, double y, double cx,
double cy, int dir)
{
double radius;
double dist;
int lg;
FD_TEST (p);
if (!DATA (p)->fd)
return;
dir = dir ? 1 : 0;
//calculate if center is on left or right side of line start-end
dist =
lineToPointSDistance2D (DATA (p)->last_x, DATA (p)->last_y, x, y, cx, cy);
radius = ZOOM * sqrt ((cx - x) * (cx - x) + (cy - y) * (cy - y));
lg = dist > 0 ? 0 : 1;
lg ^= dir;
if (x == DATA (p)->last_x && y == DATA (p)->last_y) //circle
{
fprintf (DATA (p)->fd, "a%f,%f 0 0,0 +%f 0 ", radius, radius,
2.0 * radius + DATA (p)->tool_dia / 2.0);
fprintf (DATA (p)->fd, "a%f,%f 0 0,0 -%f 0 ", radius, radius,
2.0 * radius + DATA (p)->tool_dia / 2.0);
}
else
fprintf (DATA (p)->fd, "A%f,%f 0 %d,%d %f %f ", radius, radius, lg, dir,
(x * ZOOM), (y * ZOOM));
DATA (p)->r_flag = 1;
DATA (p)->last_x = x;
DATA (p)->last_y = y;
}
static void
svg_rapid (struct postprocesor *p, double x, double y)
{
FD_TEST (p);
if (!DATA (p)->fd)
return;
if (DATA (p)->r_flag)
fprintf (DATA (p)->fd, "\" />\n");
DATA (p)->r_flag = 1;
DATA (p)->last_x = x;
DATA (p)->last_y = y;
fprintf (DATA (p)->fd, "<path d=\"M%f %f ", (x * ZOOM), (y * ZOOM));
}
static void
svg_hole (struct postprocesor *p, double x, double y, double r)
{
double ri;
FD_TEST (p);
if (!DATA (p)->fd)
return;
r = r / 2.0;
ri = (r * ZOOM - DSW / 2.0);
//0.8mm drill hole is too small for SVG
if (ri < DSW / 2.0)
ri = DRILL;
fprintf (DATA (p)->fd,
"<ellipse cx=\"%f\" cy=\"%f\" rx=\"%f\" ry=\"%f\" />\n",
(x * ZOOM), (y * ZOOM), ri, ri);
}
static struct post_operations post_svg = {
.open = svg_open,
.close = svg_close,
.operation = svg_operation,
.set = svg_set,
.write_comment = svg_write_comment,
.route = svg_route, //move tool by G1 move ..
.rapid = svg_rapid, //do rapid, first move tool to level Z, them go to x,y position, move Z to 0
.hole = svg_hole,
.route_arc = svg_route_arc,
};
struct postprocesor *
init (void)
{
struct svg_local_data *data;
struct postprocesor *p;
data = calloc (sizeof (struct svg_local_data), 1);
data->etch_tool_dia = 0.5; //for etching isolation lines
data->cut_tool_dia = 1.0; //for cut board
data->r_flag = 0;
p = postprocesor_register (&post_svg, data, "svg");
if (!p)
free (data);
return p;
}