-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdxf2fbd.cpp
600 lines (563 loc) · 16 KB
/
dxf2fbd.cpp
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Gmsh - Copyright (C) 1997-2008 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to the public mailing list <[email protected]>.
//
// Contributor(s):
// David Colignon
//
// Modified by Suyono Nt.
// This is a simple AutoCAD DXF to CalculiX GraphiX FBD Data File Converter
//
// It was created from the AutoCAD DXF file to DKB data file converter
// written and placed in the public domain 8/13/90 by Aaron
// A. Collins (http://www.sdsc.edu/~mjb/mae152/dxf.spec.txt).
//
// It parses a limited, but useful, subset of the AutoCAD DXF file
// format. No effort has been made to handle the complete range of
// possible DXF opcodes and commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <set>
#define DEG2RAD 3.14159265359/180.
#define BUFSIZE 2048
#define GEOLINE 1
#define GEOCIRCLE 2
#define GEOTRI 3
#define GEOQUAD 4
FILE *infile, *outfile;
char inname[80], outname[80], curobj[80], linbuf[BUFSIZE];
long primitives = 0L, degenerates = 0L;
int groupcode, curcolor, ints[10], nump = 1, numc = 1;
int cpt_vert_node = 1, num_vert_node[1024];
float curthick, xcoords[10], ycoords[10], zcoords[10], floats[10], angles[10];
float max_x, max_y, max_z, min_x, min_y, min_z;
float THETOL, THEROT = 0., THETRANSX = 0., THETRANSY = 0., THETRANSZ = 0.;
int LDIV;
class Point
{
public:
int num;
float x, y, z;
void write() const
{
float xx = x;
float yy = y;
float zz = z;
xx = cos(-THEROT * DEG2RAD) * x + sin(-THEROT * DEG2RAD) * y;
yy = -sin(-THEROT * DEG2RAD) * x + cos(-THEROT * DEG2RAD) * y;
xx += THETRANSX;
yy += THETRANSY;
zz = THETRANSZ * z;
zz += THETRANSZ;
fprintf(outfile, "PNT P0%d %g %g %g\n",
num, xx, yy, zz);
}
};
class PointLessThanLexicographic{
public:
bool operator()(const Point &v1, const Point &v2) const
{
if(v1.x - v2.x > THETOL) return true;
if(v1.x - v2.x < -THETOL) return false;
if(v1.y - v2.y > THETOL) return true;
if(v1.y - v2.y < -THETOL) return false;
if(v1.z - v2.z > THETOL) return true;
return false;
}
};
class Curve
{
public:
int num, type, a, b, c;
void write() const
{
switch (type) {
case GEOLINE:
fprintf(outfile, "LINE L0%d P0%d P0%d %d\n", num, a, b, LDIV);
break;
case GEOCIRCLE:
fprintf(outfile, "LINE C0%d P0%d P0%d P0%d %d\n",
num, a, c, b, LDIV);
break;
}
}
};
class CurveLessThan{
public:
bool operator()(const Curve &c1, const Curve &c2) const
{
if(c1.num < c2.num) return true;
return false;
}
};
std::set<Point, PointLessThanLexicographic> Point_T;
std::set<Curve, CurveLessThan> Curve_T;
int addpoint(Point &p)
{
std::set<Point>::iterator it = Point_T.find(p);
if(it != Point_T.end())
return it->num;
p.num = nump++;
Point_T.insert(p);
return p.num;
}
void addcurve(Curve &c)
{
c.num = numc++;
Curve_T.insert(c);
}
int checkdegen(int a, int b, int c)
{ /* check for degenerate triangle structure */
if((xcoords[a] == xcoords[b] &&
ycoords[a] == ycoords[b] &&
zcoords[a] == zcoords[b]) ||
(xcoords[b] == xcoords[c] &&
ycoords[b] == ycoords[c] &&
zcoords[b] == zcoords[c]) ||
(xcoords[a] == xcoords[c] &&
ycoords[a] == ycoords[c] &&
zcoords[a] == zcoords[c]))
return (1);
return (0);
}
void addobj(void)
{ /* dump out current object we should have all info on */
Point p;
Curve c;
int num[10];
if(strstr(curobj, "POINT")) {
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
addpoint(p);
}
else if(strstr(curobj, "LINE") || strstr(curobj, "3DLINE")) {
if(xcoords[0] == xcoords[1] && ycoords[0] == ycoords[1]
&& zcoords[0] == zcoords[1]) {
degenerates++;
return;
}
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[0] = addpoint(p);
p.x = xcoords[1];
p.y = ycoords[1];
p.z = zcoords[1];
num[1] = addpoint(p);
c.type = GEOLINE;
c.a = num[0];
c.b = num[1];
addcurve(c);
}
else if(strstr(curobj, "CIRCLE")) {
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[0] = addpoint(p);
p.x = xcoords[0] - floats[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[1] = addpoint(p);
p.x = xcoords[0] + floats[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[2] = addpoint(p);
p.x = xcoords[0];
p.y = ycoords[0] - floats[0];
p.z = zcoords[0];
num[3] = addpoint(p);
p.x = xcoords[0];
p.y = ycoords[0] + floats[0];
p.z = zcoords[0];
num[4] = addpoint(p);
c.type = GEOCIRCLE;
c.a = num[2];
c.b = num[0];
c.c = num[4];
addcurve(c);
c.type = GEOCIRCLE;
c.a = num[4];
c.b = num[0];
c.c = num[1];
addcurve(c);
c.type = GEOCIRCLE;
c.a = num[1];
c.b = num[0];
c.c = num[3];
addcurve(c);
c.type = GEOCIRCLE;
c.a = num[3];
c.b = num[0];
c.c = num[2];
addcurve(c);
}
else if(strstr(curobj, "ARC")) {
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[0] = addpoint(p);
p.x = xcoords[0] + floats[0] * cos(angles[0] * DEG2RAD);
p.y = ycoords[0] + floats[0] * sin(angles[0] * DEG2RAD);
p.z = zcoords[0];
num[1] = addpoint(p);
p.x = xcoords[0] + floats[0] * cos(angles[1] * DEG2RAD);
p.y = ycoords[0] + floats[0] * sin(angles[1] * DEG2RAD);
p.z = zcoords[0];
num[2] = addpoint(p);
if((angles[1] - angles[0] > 0 && angles[1] - angles[0] < 180) ||
(angles[1] - angles[0] < 0 && angles[1] - angles[0] < -180)) {
c.type = GEOCIRCLE;
c.a = num[1];
c.b = num[0];
c.c = num[2];
addcurve(c);
}
else {
if(angles[1] - angles[0] > 0) {
p.x =
xcoords[0] +
floats[0] * cos((angles[1] - angles[0]) * 0.5 * DEG2RAD);
p.y =
ycoords[0] +
floats[0] * sin((angles[1] - angles[0]) * 0.5 * DEG2RAD);
}
else {
p.x =
xcoords[0] +
floats[0] * cos((angles[0] - angles[1]) * 0.5 * DEG2RAD);
p.y =
ycoords[0] +
floats[0] * sin((angles[0] - angles[1]) * 0.5 * DEG2RAD);
}
p.z = zcoords[0];
num[3] = addpoint(p);
c.type = GEOCIRCLE;
c.a = num[1];
c.b = num[0];
c.c = num[3];
addcurve(c);
c.type = GEOCIRCLE;
c.a = num[3];
c.b = num[0];
c.c = num[2];
addcurve(c);
}
}
else if(strstr(curobj, "TRACE")) { /* 2 back-to-back triangles */
if(checkdegen(0, 1, 2)) {
degenerates++;
return;
}
/* add triangle 0, 1, 2 */
if(checkdegen(0, 3, 2)) {
degenerates++;
return;
}
/* add triangle 0 3 2 */
}
else if(strstr(curobj, "SOLID")) { /* 1 or 2 triangles */
if(checkdegen(0, 1, 2)) {
degenerates++;
return;
}
/* add triangle 0, 1, 2 */
if(xcoords[2] == xcoords[3] && ycoords[2] == ycoords[3]
&& zcoords[2] == zcoords[3])
return; /* one triangle was enough... */
if(checkdegen(0, 3, 2)) {
degenerates++;
return;
}
/* add triangle 0 3 2 */
}
else if(strstr(curobj, "TEXT")) { /* not implemented for now */
}
else if(strstr(curobj, "SHAPE")) { /* these look very hard */
printf("SHAPE\n");
}
else if(strstr(curobj, "BLOCK")) { /* these look very hard */
printf("BLOCK\n");
}
else if(strstr(curobj, "ENDBLK")) { /* these look very hard */
}
else if(strstr(curobj, "INSERT")) { /* these look very hard */
}
else if(strstr(curobj, "ATTDEF")) { /* not implemented for now */
}
else if(strstr(curobj, "ATTRIB")) { /* not implemented for now */
}
else if(strstr(curobj, "POLYLINE")) { /* these look fairly hard */
printf("POLYLINE\n");
}
else if(strstr(curobj, "VERTEX")) { /* these look fairly hard */
if(ints[0] == 192) {
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
num_vert_node[cpt_vert_node] = addpoint(p);
cpt_vert_node++;
}
else if(ints[0] == 128) {
c.type = GEOLINE;
c.a = num_vert_node[ints[1]];
c.b = num_vert_node[ints[2]];
addcurve(c);
c.type = GEOLINE;
c.a = num_vert_node[ints[2]];
c.b = num_vert_node[ints[3]];
addcurve(c);
if(ints[4] == 0) {
c.type = GEOLINE;
c.a = num_vert_node[ints[3]];
c.b = num_vert_node[ints[1]];
addcurve(c);
}
else {
c.type = GEOLINE;
c.a = num_vert_node[ints[3]];
c.b = num_vert_node[ints[4]];
addcurve(c);
c.type = GEOLINE;
c.a = num_vert_node[ints[4]];
c.b = num_vert_node[ints[1]];
addcurve(c);
}
}
ints[0] = ints[1] = ints[2] = ints[3] = ints[4] = ints[5] = 0;
}
else if(strstr(curobj, "SEQEND")) { /* these look fairly hard */
}
else if(strstr(curobj, "3DFACE")) { /* 1 or 2 triangles */
#if 0 //removed by David Colignon
if(checkdegen(0, 1, 2)) {
degenerates++;
return;
}
/* add triangle 0 1 2 */
if(xcoords[2] == xcoords[3] && ycoords[2] == ycoords[3]
&& zcoords[2] == zcoords[3])
return; /* one triangle was enough... */
if(checkdegen(0, 3, 2)) {
degenerates++;
return;
}
/* add triangle 0 3 2 */
#else
p.x = xcoords[0];
p.y = ycoords[0];
p.z = zcoords[0];
num[0] = addpoint(p);
p.x = xcoords[1];
p.y = ycoords[1];
p.z = zcoords[1];
num[1] = addpoint(p);
p.x = xcoords[2];
p.y = ycoords[2];
p.z = zcoords[2];
num[2] = addpoint(p);
c.type = GEOLINE;
c.a = num[0];
c.b = num[1];
addcurve(c);
c.type = GEOLINE;
c.a = num[1];
c.b = num[2];
addcurve(c);
if(xcoords[3] == xcoords[2] &&
ycoords[3] == ycoords[2] &&
zcoords[3] == zcoords[2]) {
c.type = GEOLINE;
c.a = num[2];
c.b = num[0];
addcurve(c);
}
else {
p.x = xcoords[3];
p.y = ycoords[3];
p.z = zcoords[3];
num[3] = addpoint(p);
c.type = GEOLINE;
c.a = num[2];
c.b = num[3];
addcurve(c);
c.type = GEOLINE;
c.a = num[3];
c.b = num[0];
addcurve(c);
}
#endif
}
else if(strstr(curobj, "DIMENSION")) { /* not implemented for now */
}
else {
printf("%s\n",curobj);
}
}
int getline(void)
{ /* read a group code and the next line from infile */
fgets(linbuf, BUFSIZE, infile); /* get a line from .DXF */
if(feof(infile))
return (1);
sscanf(linbuf, "%3d", &groupcode); /* scan out group code */
fgets(linbuf, BUFSIZE, infile); /* get a line from .DXF */
if(feof(infile))
return (1);
return (0);
}
int main(int argc, char *argv[])
{
char *index;
printf("dxf2fbd, a 2D AutoCad DXF to CalculiX GraphiX FBD file translator\n");
if(argc < 3) {
printf("Usage: %s file[.dxf] Tolerance [Zoffset] Division\n",
argv[0]);
exit(1);
}
THETOL = atof(argv[2]);
if(argc > 3)
THETRANSZ = atof(argv[3]);
if(argc > 4)
LDIV = atof(argv[4]);
strcpy(inname, argv[1]); /* make copy we can mess with */
if(!strchr(inname, '.')) /* no dot present in filename? */
strcat(inname, ".dxf");
if(!(infile = fopen(inname, "r"))) {
printf("Cannot open input file %s\n", inname);
exit(1);
}
strcpy(outname, inname);
index = strchr(outname, '.'); /* find the dot */
strcpy(index, ".fbd"); /* make new ext. .fbd... */
if(!(outfile = fopen(outname, "w"))) {
printf("Cannot create output file %s\n", outname);
fclose(infile);
exit(1);
}
ints[0] = ints[1] = ints[2] = ints[3] = ints[4] = ints[5] = 0;
curobj[0] = '\0'; /* not working on any object currently */
curcolor = 7; /* and it also doesn't have a color yet... */
max_x = max_y = max_z = -10000000.0; /* init bounding limits */
min_x = min_y = min_z = 10000000.0;
find:
while(!feof(infile)) { /* run file up to the "ENTITIES" section */
if(getline())
goto stopit;
if(groupcode == 0) { /* file section mark */
if(strstr(linbuf, "EOF"))
goto stopit;
if(strstr(linbuf, "SECTION")) {
if(getline())
goto stopit;
if(groupcode != 2)
continue;
if(strstr(linbuf, "ENTITIES"))
break;
}
}
}
while(!feof(infile)) { /* scan ENTITIES section */
if(getline()) /* get a group code and a line */
break;
if(groupcode < 10) { /* cardinal group codes */
switch (groupcode) {
case 0: /* start of entity, table, file sep */
if(strstr(linbuf, "EOF")) {
addobj(); /* dump object */
goto stopit;
}
if(strstr(linbuf, "ENDSEC")) {
addobj(); /* dump object */
goto find;
}
addobj(); /* dump old object */
curobj[0] = '\0'; /* reset object */
curcolor = 7;
strcpy(curobj, linbuf); /* get new */
break;
case 1: /* primary text value for entity (?) */
break;
case 2: /* block name, attribute tag, etc */
case 3: /* other names */
case 4:
break;
case 5: /* entity handle (hex string) */
break;
case 6: /* line type name */
break;
case 7: /* text style name */
break;
case 8: /* layer name */
break;
case 9: /* variable name ID (only in header) */
break;
}
}
else if(groupcode >= 10 && groupcode < 19) { /* Some X coord */
sscanf(linbuf, "%f", &(xcoords[groupcode - 10]));
if(xcoords[groupcode - 10] > max_x)
max_x = xcoords[groupcode - 10];
if(xcoords[groupcode - 10] < min_x)
min_x = xcoords[groupcode - 10];
}
else if(groupcode >= 20 && groupcode < 29) { /* Some Y coord */
sscanf(linbuf, "%f", &(ycoords[groupcode - 20]));
if(ycoords[groupcode - 20] > max_y)
max_y = ycoords[groupcode - 20];
if(ycoords[groupcode - 20] < min_y)
min_y = ycoords[groupcode - 20];
}
else if(groupcode >= 30 && groupcode < 38) { /* Some Z coord */
sscanf(linbuf, "%f", &(zcoords[groupcode - 30]));
if(zcoords[groupcode - 30] > max_z)
max_z = zcoords[groupcode - 30];
if(zcoords[groupcode - 30] < min_z)
min_z = zcoords[groupcode - 30];
}
else if(groupcode == 38) { /* entity elevation if nonzero */
}
else if(groupcode == 39) { /* entity thickness if nonzero */
}
else if(groupcode >= 40 && groupcode < 49) { /* misc floats */
sscanf(linbuf, "%f", &(floats[groupcode - 40]));
}
else if(groupcode == 49) { /* repeated value groups */
}
else if(groupcode >= 50 && groupcode < 59) { /* misc angles */
sscanf(linbuf, "%f", &(angles[groupcode - 50]));
}
else if(groupcode == 62) { /* Color number */
sscanf(linbuf, "%6d", &curcolor);
}
else if(groupcode == 66) { /* "entities follow" flag */
}
else if(groupcode >= 70 && groupcode < 79) { /* misc ints */
sscanf(linbuf, "%d", &(ints[groupcode - 70]));
}
else if(groupcode == 210 || groupcode == 220 || groupcode == 230) {
/* X, Y, Z components of extrusion direction */
}
}
stopit:
fclose(infile);
fprintf(outfile, "/* Converted from AutoCad DXF file: %s */\n", inname);
fprintf(outfile, "/* Tolerance %g, Zoffset %g, Division %d: %d points / %d lines & curves */\n\n",
THETOL, THETRANSZ, LDIV, Point_T.size(), Curve_T.size());
// fprintf(outfile, "u = 1; \nlc = 1 ;\n\n");
for(std::set<Point>::iterator it = Point_T.begin(); it != Point_T.end(); ++it)
it->write();
fprintf(outfile, "\n");
for(std::set<Curve>::iterator it = Curve_T.begin(); it != Curve_T.end(); ++it)
it->write();
fprintf(outfile, "\n");
fflush(outfile);
fclose(outfile);
printf("Bounding Box [%g,%g] [%g,%g] [%g,%g]\n",
min_x, max_x, min_y, max_y, min_z, max_z);
printf("Tolerance %g, Zoffset %g, Division %d: %d points / %d lines & curves / %ld degenerate entities removed\n",
THETOL, THETRANSZ, LDIV, Point_T.size(), Curve_T.size(), degenerates);
exit(0);
}