-
Notifications
You must be signed in to change notification settings - Fork 15
/
dxf.c
437 lines (403 loc) · 13.7 KB
/
dxf.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
Copyright by Oliver Dippel <oliver@multixmedia.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in `/usr/share/common-licenses/GPL'.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <dxf.h>
#include <font.h>
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <locale.h>
int block = 0;
double block_x = 0.0;
double block_y = 0.0;
char block_name[1024];
char LayerNames[MAX_OBJECTS][256];
double get_len (double x1, double y1, double x2, double y2);
int mtext_n = 0;
char dxf_options[256][256];
_OBJECT *myOBJECTS = NULL;
_LINE *myLINES = NULL;
char dxf_typename[TYPE_LAST][16];
int line_n = 1;
int line_last = 0;
void add_line (int type, char *layer, double x1, double y1, double x2, double y2, double opt, double cx, double cy) {
// printf("## ADD_LINE (%i %i): %f,%f -> %f,%f (%s / %f)\n", line_n, line_last, x1, y1, x2, y2, layer, opt);
if (x1 > 10000.0 || y1 > 10000.0 || x2 > 10000.0 || y2 > 10000.0) {
printf("###### LINE TO BIG; %f %f -> %f %f ######\n", x1, y1, x2, y2);
return;
}
if (line_n < MAX_LINES) {
int num = 0;
for (num = 0; num < line_last; num++) {
if (strcmp(LayerNames[num], layer) == 0) {
break;
} else if (LayerNames[num][0] == 0) {
strcpy(LayerNames[num], layer);
break;
}
}
if (myLINES == NULL) {
myLINES = (_LINE *)malloc(sizeof(_LINE) * 5);
} else {
myLINES = (_LINE *)realloc((void *)myLINES, sizeof(_LINE) * (line_last + 5));
}
if (strcmp(layer, "holding-tabs") == 0) {
myLINES[line_n].istab = 1;
myLINES[line_n].used = 0;
} else if (block == 1) {
myLINES[line_n].istab = 0;
myLINES[line_n].used = 0;
} else {
myLINES[line_n].istab = 0;
myLINES[line_n].used = 1;
}
myLINES[line_n].type = type;
strcpy(myLINES[line_n].layer, layer);
myLINES[line_n].x1 = x1;
myLINES[line_n].y1 = y1;
myLINES[line_n].x2 = x2;
myLINES[line_n].y2 = y2;
myLINES[line_n].cx = cx;
myLINES[line_n].cy = cy;
myLINES[line_n].opt = opt;
myLINES[line_n].marked = 0;
myLINES[line_n].in_object = -1;
strcpy(myLINES[line_n].block, block_name);
myLINES[line_n].blockdata = block;
line_n++;
myLINES[line_n].used = 0;
myLINES[line_n + 1].used = 0;
myLINES[line_n].type = -1;
myLINES[line_n + 1].type = -1;
line_last = line_n;
} else {
printf("### TO MANY LINES ##\n");
// exit(1);
}
}
size_t trimline (char *out, size_t len, const char *str) {
if(len == 0) {
return 0;
}
const char *end;
size_t out_size;
while (*str == ' ' || *str == '\r' || *str == '\n') {
str++;
}
if (*str == 0) {
*out = 0;
return 1;
}
end = str + strlen(str) - 1;
while(end > str && (*end == ' ' || *end == '\r' || *end == '\n')) {
end--;
}
end++;
out_size = (end - str) < len-1 ? (end - str) : len-1;
memcpy(out, str, out_size);
out[out_size] = 0;
return out_size;
}
void clear_dxfoptions (void) {
int num = 0;
for (num = 0; num < 256; num++) {
dxf_options[num][0] = 0;
}
}
void dxf_read (char *file) {
FILE *fp;
char *line = NULL;
char line2[1024];
size_t len = 0;
ssize_t read;
setlocale(LC_NUMERIC, "C");
line_last = 0;
line_n = 1;
if (myLINES != NULL) {
free(myLINES);
myLINES = NULL;
}
strcpy(dxf_typename[TYPE_NONE], "None");
strcpy(dxf_typename[TYPE_LINE], "Line");
strcpy(dxf_typename[TYPE_ARC], "Arc");
strcpy(dxf_typename[TYPE_CIRCLE], "Circle");
strcpy(dxf_typename[TYPE_MTEXT], "Text");
strcpy(dxf_typename[TYPE_POINT], "Point");
strcpy(dxf_typename[TYPE_POLYLINE], "Polyline");
strcpy(dxf_typename[TYPE_VERTEX], "Vertex");
fp = fopen(file, "r");
if (fp == NULL) {
return;
}
int num = 0;
for (num = 0; num < line_last; num++) {
LayerNames[num][0] = 0;
}
clear_dxfoptions();
char last_0[256];
strcpy(last_0, "");
int lwpl_flag = 0;
int pl_flag = 0;
int pl_closed = 0;
double pl_first_x = 0.0;
double pl_first_y = 0.0;
double pl_last_x = 0.0;
double pl_last_y = 0.0;
while ((read = getline(&line, &len, fp)) != -1) {
trimline(line2, 1024, line);
int dxfoption = atoi(line2);
if ((read = getline(&line, &len, fp)) != -1) {
trimline(line2, 1024, line);
if (dxfoption == 0) {
if (last_0[0] != 0) {
if (strcmp(last_0, "BLOCK") == 0) {
block = 1;
block_x = atof(dxf_options[OPTION_POINT_X]);
block_y = atof(dxf_options[OPTION_POINT_Y]);
strcpy(block_name, dxf_options[OPTION_BLOCKNAME]);
} else if (strcmp(last_0, "ENDBLK") == 0) {
block = 0;
block_name[0] = 0;
} else if (strcmp(last_0, "INSERT") == 0) {
block = 0;
block_name[0] = 0;
block_x = atof(dxf_options[OPTION_POINT_X]);
block_y = atof(dxf_options[OPTION_POINT_Y]);
float scale_x = atof(dxf_options[41]);
float scale_y = atof(dxf_options[42]);
if (scale_x == 0.0) {
scale_x = 1.0;
}
if (scale_y == 0.0) {
scale_y = 1.0;
}
strcpy(block_name, dxf_options[2]);
int num = 0;
int last = line_last;
for (num = 0; num < last - 1; num++) {
if (myLINES[num].blockdata == 1 && myLINES[num].block[0] != 0 && strcmp(myLINES[num].block, block_name) == 0) {
add_line(myLINES[num].type, dxf_options[OPTION_LAYERNAME], myLINES[num].x1 * scale_x + block_x, myLINES[num].y1 * scale_y + block_y, myLINES[num].x2 * scale_x + block_x, myLINES[num].y2 * scale_y + block_y, myLINES[num].opt, myLINES[num].cx * scale_x + block_x, myLINES[num].cy * scale_y + block_y);
}
}
} else if (strcmp(last_0, "LINE") == 0) {
double p_x1 = atof(dxf_options[OPTION_LINE_X1]);
double p_y1 = atof(dxf_options[OPTION_LINE_Y1]);
double p_x2 = atof(dxf_options[OPTION_LINE_X2]);
double p_y2 = atof(dxf_options[OPTION_LINE_Y2]);
add_line(TYPE_LINE, dxf_options[8], p_x1, p_y1, p_x2, p_y2, 0.0, 0.0, 0.0);
} else if (strcmp(last_0, "VERTEX") == 0) {
double p_x1 = atof(dxf_options[OPTION_LINE_X1]);
double p_y1 = atof(dxf_options[OPTION_LINE_Y1]);
double p_r1 = atof(dxf_options[42]);
if (pl_flag == 0) {
pl_first_x = p_x1;
pl_first_y = p_y1;
} else {
if (p_r1 != 0.0) {
double chord = sqrt(pow(abs(pl_last_x - p_x1), 2.0) + pow(abs(pl_last_y - p_y1), 2.0));
double s = chord / 2.0 * p_r1;
double radius = (pow(chord / 2.0, 2.0) + pow(s, 2.0)) / (2.0 * s);
double len = get_len(pl_last_x, pl_last_y, p_x1, p_y1);
if (radius * 2 < len) {
radius *= 2.0;
}
add_line(TYPE_ARC, dxf_options[8], pl_last_x, pl_last_y, p_x1, p_y1, radius, 0.0, 0.0);
} else {
add_line(TYPE_LINE, dxf_options[8], pl_last_x, pl_last_y, p_x1, p_y1, 0.0, 0.0, 0.0);
}
}
pl_last_x = p_x1;
pl_last_y = p_y1;
pl_flag = 1;
} else if (strcmp(last_0, "SEQEND") == 0) {
if (pl_closed == 1) {
add_line(TYPE_LINE, dxf_options[8], pl_last_x, pl_last_y, pl_first_x, pl_first_y, 0.0, 0.0, 0.0);
}
pl_flag = 0;
pl_closed = 0;
} else if (strcmp(last_0, "POLYLINE") == 0) {
pl_closed = atoi(dxf_options[70]);
pl_flag = 0;
} else if (strcmp(last_0, "LWPOLYLINE") == 0) {
pl_closed = atoi(dxf_options[70]);
double p_x1 = atof(dxf_options[OPTION_LINE_X1]);
double p_y1 = atof(dxf_options[OPTION_LINE_Y1]);
double p_r1 = atof(dxf_options[42]);
pl_last_x = p_x1;
pl_last_y = p_y1;
dxf_options[42][0] = 0;
if (pl_closed == 1) {
add_line(TYPE_LINE, dxf_options[8], pl_last_x, pl_last_y, pl_first_x, pl_first_y, p_r1, 0.0, 0.0);
}
lwpl_flag = 0;
pl_closed = 0;
} else if (strcmp(last_0, "POINT") == 0) {
double p_x1 = atof(dxf_options[OPTION_POINT_X]);
double p_y1 = atof(dxf_options[OPTION_POINT_Y]);
double p_x2 = atof(dxf_options[OPTION_POINT_X]);
double p_y2 = atof(dxf_options[OPTION_POINT_Y]);
add_line(TYPE_POINT, dxf_options[8], p_x1, p_y1, p_x2, p_y2, 0.0, 0.0, 0.0);
} else if (strcmp(last_0, "SPLINE") == 0) {
/* } else if (strcmp(last_0, "CIRCLE") == 0) {
double cx = atof(dxf_options[OPTION_ARC_X]);
double cy = atof(dxf_options[OPTION_ARC_Y]);
double r = atof(dxf_options[OPTION_ARC_RADIUS]);
add_line(TYPE_CIRCLE, dxf_options[8], cx - r, cy, cx + r, cy, r, cx, cy);
add_line(TYPE_CIRCLE, dxf_options[8], cx + r, cy, cx - r, cy, r, cx, cy);
*/
} else if (strcmp(last_0, "ARC") == 0 || strcmp(last_0, "CIRCLE") == 0) {
double p_x1 = atof(dxf_options[OPTION_ARC_X]);
double p_y1 = atof(dxf_options[OPTION_ARC_Y]);
double p_y2 = atof(dxf_options[OPTION_ARC_RADIUS]);
double p_a1 = atof(dxf_options[OPTION_ARC_BEGIN]);
double p_a2 = atof(dxf_options[OPTION_ARC_END]);
if (strcmp(last_0, "CIRCLE") == 0) {
p_a1 = 0.0;
p_a2 = 360.0;
}
if (p_a1 > p_a2) {
p_a2 += 360.0;
}
double r = p_y2;
double angle2 = toRad(p_a1);
double x2 = r * cos(angle2);
double y2 = r * sin(angle2);
double last_x = (p_x1 + x2);
double last_y = (p_y1 + y2);
double an = 0;
double p_rast = (p_a2 - p_a1) / 18.0; // resolução
for (an = p_a1 + p_rast; an <= p_a2 - (p_rast / 2.0); an += p_rast) {
double angle1 = toRad(an);
double x1 = r * cos(angle1);
double y1 = r * sin(angle1);
if (strcmp(last_0, "CIRCLE") == 0) {
add_line(TYPE_CIRCLE, dxf_options[8], last_x, last_y, p_x1 + x1, p_y1 + y1, r, p_x1, p_y1);
} else {
add_line(TYPE_ARC, dxf_options[8], last_x, last_y, p_x1 + x1, p_y1 + y1, r, p_x1, p_y1);
}
last_x = p_x1 + x1;
last_y = p_y1 + y1;
}
double angle3 = toRad(p_a2);
double x3 = r * cos(angle3);
double y3 = r * sin(angle3);
if (strcmp(last_0, "CIRCLE") == 0) {
add_line(TYPE_CIRCLE, dxf_options[8], last_x, last_y, p_x1 + x3, p_y1 + y3, r, p_x1, p_y1);
} else {
add_line(TYPE_ARC, dxf_options[8], last_x, last_y, p_x1 + x3, p_y1 + y3, r, p_x1, p_y1);
}
} else if (strcmp(last_0, "ELLIPSE") == 0) {
double p_x1 = atof(dxf_options[10]);
double p_y1 = atof(dxf_options[20]);
double e_x = atof(dxf_options[11]);
double e_y = atof(dxf_options[21]);
double p_a1 = 0.0;
double p_a2 = 360.0;
double ratio = atof(dxf_options[40]);
double r = e_x;
if (e_x < 0) {
e_x *= -1;
}
if (e_y < 0) {
e_y *= -1;
}
if (e_y > e_x) {
r = e_y * ratio;
ratio = 1.0 / ratio;
}
if (p_a1 > p_a2) {
p_a2 += 360.0;
}
double angle2 = toRad(p_a1);
double x2 = r * cos(angle2);
double y2 = r * ratio * sin(angle2);
double last_x = (p_x1 + x2);
double last_y = (p_y1 + y2);
double first_x = last_x;
double first_y = last_y;
double an = 0;
double p_rast = (p_a2 - p_a1) / 18.0;
for (an = p_a1 + p_rast; an <= p_a2 - (p_rast / 2.0); an += p_rast) {
double angle1 = toRad(an);
double x1 = r * cos(angle1);
double y1 = r * ratio * sin(angle1);
add_line(TYPE_ELLIPSE, dxf_options[8], last_x, last_y, p_x1 + x1, p_y1 + y1, 0.0, 0.0, 0.0);
last_x = p_x1 + x1;
last_y = p_y1 + y1;
}
add_line(TYPE_ELLIPSE, dxf_options[8], last_x, last_y, first_x, first_y, 0.0, 0.0, 0.0);
} else if (strcmp(last_0, "MTEXT") == 0) {
double p_x1 = atof(dxf_options[OPTION_MTEXT_X]);
double p_y1 = atof(dxf_options[OPTION_MTEXT_Y]);
double p_s = atof(dxf_options[OPTION_MTEXT_SIZE]);
output_text_dxf(dxf_options[OPTION_MTEXT_TEXT], dxf_options[8], p_x1, p_y1, 0.0, p_s);
mtext_n++;
} else {
pl_flag = 0;
lwpl_flag = 0;
}
clear_dxfoptions();
}
strcpy(last_0, line2);
}
// printf("## %i: %s\n", dxfoption, line2);
if (dxfoption < 256) {
strcpy(dxf_options[dxfoption], line2);
if (strcmp(last_0, "LWPOLYLINE") == 0) {
if (dxfoption == 10) {
} else if (dxfoption == 20) {
double p_x1 = atof(dxf_options[OPTION_LINE_X1]);
double p_y1 = atof(dxf_options[OPTION_LINE_Y1]);
double p_r1 = atof(dxf_options[42]);
if (lwpl_flag > 0) {
if (dxf_options[42][0] == 0) {
add_line(TYPE_LINE, dxf_options[8], pl_last_x, pl_last_y, p_x1, p_y1, 0.0, 0.0, 0.0);
} else {
double chord = sqrt(pow(abs(pl_last_x - p_x1), 2.0) + pow(abs(pl_last_y - p_y1), 2.0));
double s = chord / 2.0 * p_r1;
double radius = (pow(chord / 2.0, 2.0) + pow(s, 2.0)) / (2.0 * s);
double len = get_len(pl_last_x, pl_last_y, p_x1, p_y1);
if (radius * 2 < len) {
radius *= 2.0;
}
add_line(TYPE_ARC, dxf_options[8], pl_last_x, pl_last_y, p_x1, p_y1, radius, 0.0, 0.0);
}
} else {
pl_first_x = p_x1;
pl_first_y = p_y1;
}
dxf_options[42][0] = 0;
pl_last_x = p_x1;
pl_last_y = p_y1;
lwpl_flag++;
}
}
}
}
}
//exit(0);
fclose(fp);
}