-
Notifications
You must be signed in to change notification settings - Fork 5
/
dxf.cpp
706 lines (670 loc) · 15.4 KB
/
dxf.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/******************************************************/
/* */
/* dxf.cpp - Drawing Exchange Format */
/* */
/******************************************************/
/* Copyright 2019-2021 Pierre Abbat.
* This file is part of PerfectTIN.
*
* PerfectTIN 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.
*
* PerfectTIN 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 and Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and Lesser General Public License along with PerfectTIN. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "dxf.h"
#include "binio.h"
#include "ldecimal.h"
using namespace std;
TagRange tagTable[]=
{
{0,128}, // string
{10,72}, // xyz. All xyz's are stored as three separate doubles.
{40,72}, // double
{60,2}, // 2-byte short
{80,0}, // invalid
{90,4}, // 4-byte int
{100,128},
{101,0},
{102,128},
{103,0},
{105,132}, // hex string
{106,0},
{110,72}, // 110-139 are three doubles that go together. 140-149 is a scalar.
{150,0},
{160,8}, // 8-byte long
{170,2},
{180,0},
{210,72},
{240,0},
{270,2},
{290,1}, // bool
{300,128},
{310,129}, // hex string binary chunk
{320,132}, // hex string handle
{370,2},
{390,132},
{400,2},
{410,128},
{420,4},
{430,128},
{440,4},
{450,8},
{460,72},
{470,128},
{480,132},
{482,0},
{999,128},
{1010,72},
{1060,2},
{1071,4},
{1072,0}
};
int tagFormat(int tag)
{
int lo=0,hi=sizeof(tagTable)/sizeof(tagTable[0]),mid;
while (hi-lo>1)
{
mid=(lo+hi)/2;
if (tagTable[mid].tag>tag)
hi=mid;
else
lo=mid;
}
if (tagTable[lo].tag>tag)
return 0;
else
return tagTable[lo].format;
}
GroupCode::GroupCode()
{
tag=-1;
}
GroupCode::GroupCode(int tag0)
{
tag=tag0;
switch (tagFormat(tag))
{
case 1:
flag=false;
break;
case 2:
case 4:
case 8:
case 132:
integer=0;
break;
case 72:
real=0;
break;
case 128:
case 129:
new (&str) string();
break;
}
}
GroupCode::GroupCode(const GroupCode &b)
{
tag=b.tag;
switch (tagFormat(tag))
{
case 1:
flag=b.flag;
break;
case 2:
case 4:
case 8:
case 132:
integer=b.integer;
break;
case 72:
real=b.real;
break;
case 128:
case 129:
new (&str) string(b.str);
break;
}
}
GroupCode& GroupCode::operator=(const GroupCode &b)
{
if ((tagFormat(tag)&-2)==128 && (tagFormat(b.tag)&-2)!=128)
str.~string();
if ((tagFormat(tag)&-2)!=128 && (tagFormat(b.tag)&-2)==128)
new (&str) string();
tag=b.tag;
switch (tagFormat(tag))
{
case 1:
flag=b.flag;
break;
case 2:
case 4:
case 8:
case 132:
integer=b.integer;
break;
case 72:
real=b.real;
break;
case 128:
case 129:
str=b.str;
break;
}
return *this;
}
GroupCode::~GroupCode()
{
switch (tagFormat(tag))
{
case 128:
case 129:
str.~string();
break;
}
}
long long hexDecodeInt(string str)
{
int i,ch;
long long ret=0;
for (i=0;i<str.length();i++)
{
ch=str[i];
if (ch>'_')
ch&=0xdf; // convert abcdef to ABCDEF
if (ch>'9')
ch-='A'-('9'+1);
ret=(ret<<4)+(ch-'0');
}
return ret;
}
string hexEncodeInt(long long num)
{
bool nonzero=false;
char nybble;
int i;
string ret;
for (i=28;i>=0;i-=4)
{
nybble=(num>>i)&15;
if (nybble)
nonzero=true;
if (nonzero || !i)
ret+=(nybble<=9)?(nybble+'0'):(nybble+'A'-10);
}
return ret;
}
string hexDecodeString(string str)
{
int i,ch;
char byte=0;
string ret;
for (i=0;i<str.length();i++)
{
ch=str[i];
if (ch>'_')
ch&=0xdf;
if (ch>'9')
ch-='A'-('9'+1);
byte=(byte<<4)+(ch-'0');
if (i&1)
ret+=byte;
}
return ret;
}
string hexEncodeString(string str)
{
int i,ch;
char nybble;
string ret;
for (i=0;i<str.length();i++)
{
ch=str[i];
nybble=(ch>>4)&15;
ret+=(nybble>9)?(nybble+'0'):(nybble+'A'-10);
nybble=ch&15;
ret+=(nybble>9)?(nybble+'0'):(nybble+'A'-10);
}
return ret;
}
GroupCode readDxfText(istream &file)
{
string tagstr,datastr;
GroupCode ret;
getline(file,tagstr);
getline(file,datastr);
if (file.good())
{
try
{
ret=GroupCode(stoi(tagstr));
switch (tagFormat(ret.tag))
{
case 1: // bools are stored in text as numbers
ret.flag=stoi(datastr)!=0;
break;
case 2:
case 4:
case 8:
ret.integer=stoll(datastr);
break;
case 72:
ret.real=stod(datastr);
break;
case 128:
ret.str=datastr;
break;
case 129:
ret.str=hexDecodeString(datastr);
break;
case 132:
ret.integer=hexDecodeInt(datastr);
break;
}
}
catch (...)
{
ret=GroupCode(-2);
}
}
return ret;
}
GroupCode readDxfBinary(istream &file)
{
GroupCode ret;
int tag;
tag=readleshort(file);
if (file.good())
{
ret=GroupCode(tag);
switch (tagFormat(ret.tag))
{
case 1:
ret.flag=file.get();
break;
case 2:
ret.integer=readleshort(file);
break;
case 4:
ret.integer=readleint(file);
break;
case 8:
ret.integer=readlelong(file);
break;
case 72:
ret.real=readledouble(file);
break;
case 128:
ret.str=readustring(file);
break;
case 129:
ret.str=hexDecodeString(readustring(file));
break;
case 132:
ret.integer=hexDecodeInt(readustring(file));
break;
}
}
return ret;
}
void writeDxfText(std::ostream &file,GroupCode code)
{
string tagstr,datastr;
tagstr=to_string(code.tag);
switch(tagFormat(code.tag))
{
case 1: // bools are stored in text as numbers
datastr=to_string((int)code.flag);
break;
case 2:
case 4:
case 8:
datastr=to_string(code.integer);
break;
case 72:
datastr=ldecimal(code.real);
break;
case 128:
datastr=code.str;
break;
case 129:
datastr=hexEncodeString(code.str);
break;
case 132:
datastr=hexEncodeInt(code.integer);
break;
}
file<<tagstr<<'\n'<<datastr<<'\n';
}
void writeDxfBinary(std::ostream &file,GroupCode code)
{
writeleshort(file,code.tag);
switch(tagFormat(code.tag))
{
case 1:
file.put((char)code.flag);
break;
case 2:
writeleshort(file,code.integer);
break;
case 4:
writeleint(file,code.integer);
break;
case 8:
writelelong(file,code.integer);
break;
case 72:
writeledouble(file,code.real);
break;
case 128:
writeustring(file,code.str);
break;
case 129:
writeustring(file,hexEncodeString(code.str));
break;
case 132:
writeustring(file,hexEncodeInt(code.integer));
break;
}
}
bool readDxfMagic(istream &file)
/* Looks for the seven bytes DXF^M^J^Z^@. If any control character appears before
* DXF, returns false.
*/
{
char buf[4]={" "};
while (!(buf[0]=='D' && buf[1]=='X' && buf[2]=='F') && buf[2]>=' ' && buf[2]<128 && file.good())
{
buf[0]=buf[1];
buf[1]=buf[2];
buf[2]=file.get();
}
if (!strcmp(buf,"DXF"))
file.read(buf,4);
return (file.good() && !strcmp(buf,"\r\n\032"));
}
void writeDxfMagic(ostream &file)
{
file<<"AutoCAD Binary DXF\r\n\032"<<'\0';
}
vector<GroupCode> readDxfGroups(istream &file,bool mode)
// mode is true for text.
{
GroupCode oneCode;
vector<GroupCode> ret;
bool cont=true;
if (!mode)
cont=readDxfMagic(file);
while (cont)
{
if (mode)
oneCode=readDxfText(file);
else
oneCode=readDxfBinary(file);
if (tagFormat(oneCode.tag))
ret.push_back(oneCode);
else
{
cont=false;
if (file.good() || oneCode.tag+1) // An unknown tag was read.
ret.clear();
}
}
return ret;
}
void writeDxfGroups(ostream &file,vector<GroupCode> &codes,bool mode)
{
int i;
if (!mode)
writeDxfMagic(file);
for (i=0;i<codes.size();i++)
if (mode)
writeDxfText(file,codes[i]);
else
writeDxfBinary(file,codes[i]);
}
vector<GroupCode> readDxfGroups(string filename)
{
int mode;
ifstream file;
vector<GroupCode> ret;
for (mode=0;mode<2 && ret.size()==0;mode++)
{
file.open(filename,ios::binary);
ret=readDxfGroups(file,mode);
file.close();
}
return ret;
}
vector<array<xyz,3> > extractTriangles(vector<GroupCode> dxfData)
/* Scans dxfData looking for 3DFACE objects, extracting the first three
* corners of each. The fourth corner is ignored. It should be the same as
* one of the first three. If it isn't, the 3DFACE is a quadrilateral,
* which might should be turned into two triangles.
*
* This function ignores sections; if there's a 3DFACE in the BLOCKS section,
* it will output it once, not every place the block is used.
*/
{
int i,ncorner,coord;
array<xyz,3> face;
vector<array<xyz,3> > ret;
int ncoords=16;
for (i=0;i<dxfData.size();i++)
{
if (dxfData[i].tag==0 && dxfData[i].str=="3DFACE")
ncoords=0;
if (ncoords<12 && dxfData[i].tag>=10 && dxfData[i].tag<40)
{
coord=dxfData[i].tag/10;
ncorner=dxfData[i].tag%10;
if (ncorner<3)
switch (coord)
{
case 1:
face[ncorner]=xyz(dxfData[i].real,face[ncorner].gety(),face[ncorner].getz());
break;
case 2:
face[ncorner]=xyz(face[ncorner].getx(),dxfData[i].real,face[ncorner].getz());
break;
case 3:
face[ncorner]=xyz(face[ncorner].getx(),face[ncorner].gety(),dxfData[i].real);
break;
}
if (12==++ncoords)
ret.push_back(face);
}
}
return ret;
}
void insertXy(vector<GroupCode> &dxfData,int xtag,xy pnt)
// xtag is the tag of the x-coordinate
{
GroupCode xCode(xtag),yCode(xtag+10);
xCode.real=pnt.getx();
yCode.real=pnt.gety();
dxfData.push_back(xCode);
dxfData.push_back(yCode);
}
void insertBulge(vector<GroupCode> &dxfData,int delta)
/* Bulge is the displacement of the midpoint of the arc from the chord,
* divided by half the chord length.
*/
{
GroupCode bulgeCode(42);
bulgeCode.real=tanquarter(delta);
dxfData.push_back(bulgeCode);
}
void insertXyz(vector<GroupCode> &dxfData,int xtag,xyz pnt)
// xtag is the tag of the x-coordinate
{
GroupCode xCode(xtag),yCode(xtag+10),zCode(xtag+20);
xCode.real=pnt.getx();
yCode.real=pnt.gety();
zCode.real=pnt.getz();
dxfData.push_back(xCode);
dxfData.push_back(yCode);
dxfData.push_back(zCode);
}
void dxfHeader(vector<GroupCode> &dxfData,BoundRect br)
{
GroupCode sectag(0),secname(2),paramtag(9),stringval(1);
sectag.str="SECTION";
secname.str="HEADER";
dxfData.push_back(sectag);
dxfData.push_back(secname);
paramtag.str="$ACADVER";
stringval.str="AC1006"; // ARES opens it with 1006 or 1009. Carlson opens it with 1027 but not 1006.
dxfData.push_back(paramtag);
dxfData.push_back(stringval);
paramtag.str="$INSBASE";
dxfData.push_back(paramtag);
insertXyz(dxfData,10,xyz(0,0,0));
paramtag.str="$EXTMIN";
dxfData.push_back(paramtag);
insertXyz(dxfData,10,xyz(br.left(),br.bottom(),br.low()));
paramtag.str="$EXTMAX";
dxfData.push_back(paramtag);
insertXyz(dxfData,10,xyz(br.right(),br.top(),br.high()));
sectag.str="ENDSEC";
dxfData.push_back(sectag);
}
void insertLinetype(vector<GroupCode> &dxfData,string name,int n1,string desc,int n2,int n3,double penWidth)
// I have no idea what n1, n2, and n3 mean. I'm just copying code from http://paulbourke.net/dataformats/dxf/min3d.html .
{
GroupCode ltypetag(0),ltypename(2),n1code(70),desccode(3);
GroupCode n2code(72),n3code(73),pencode(40);
ltypetag.str="LTYPE";
dxfData.push_back(ltypetag);
ltypename.str=name;
dxfData.push_back(ltypename);
n1code.integer=n1;
dxfData.push_back(n1code);
desccode.str=desc;
dxfData.push_back(desccode);
n2code.integer=n2;
dxfData.push_back(n2code);
n3code.integer=n3;
dxfData.push_back(n3code);
pencode.real=penWidth;
dxfData.push_back(pencode);
}
void linetypeTable(vector<GroupCode> &dxfData)
{
GroupCode tabletag(0),tablename(2),nLinetypes(70);
tabletag.str="TABLE";
tablename.str="LTYPE";
dxfData.push_back(tabletag);
dxfData.push_back(tablename);
nLinetypes.integer=1;
dxfData.push_back(nLinetypes);
insertLinetype(dxfData,"CONTINUOUS",64,"Solid line",65,0,0);
tabletag.str="ENDTAB";
dxfData.push_back(tabletag);
}
void insertLayer(vector<GroupCode> &dxfData,string name,int n1,int color)
{
GroupCode layertag(0),layername(2),n1code(70),colorcode(62);
GroupCode ltypecode(6),n3code(73),pencode(40);
layertag.str="LAYER";
dxfData.push_back(layertag);
layername.str=name;
dxfData.push_back(layername);
n1code.integer=n1;
dxfData.push_back(n1code);
colorcode.integer=color;
dxfData.push_back(colorcode);
ltypecode.str="CONTINUOUS";
dxfData.push_back(ltypecode);
}
void layerTable(vector<GroupCode> &dxfData,vector<DxfLayer> &layers)
{
int i;
GroupCode tabletag(0),tablename(2),nLayers(70);
tabletag.str="TABLE";
tablename.str="LAYER";
dxfData.push_back(tabletag);
dxfData.push_back(tablename);
nLayers.integer=layers.size();
dxfData.push_back(nLayers);
for (i=0;i<layers.size();i++)
insertLayer(dxfData,layers[i].name,64,layers[i].color);
tabletag.str="ENDTAB";
dxfData.push_back(tabletag);
}
void tableSection(vector<GroupCode> &dxfData,vector<DxfLayer> &layers)
{
GroupCode sectag(0),secname(2);
sectag.str="SECTION";
secname.str="TABLES";
dxfData.push_back(sectag);
dxfData.push_back(secname);
linetypeTable(dxfData);
layerTable(dxfData,layers);
sectag.str="ENDSEC";
dxfData.push_back(sectag);
}
void openEntitySection(vector<GroupCode> &dxfData)
{
GroupCode sectag(0),secname(2);
sectag.str="SECTION";
secname.str="ENTITIES";
dxfData.push_back(sectag);
dxfData.push_back(secname);
}
void closeEntitySection(vector<GroupCode> &dxfData)
{
GroupCode sectag(0);
sectag.str="ENDSEC";
dxfData.push_back(sectag);
}
void dxfEnd(vector<GroupCode> &dxfData)
{
GroupCode sectag(0);
sectag.str="EOF";
dxfData.push_back(sectag);
}
void insertTriangle(vector<GroupCode> &dxfData,triangle &tri,double outUnit)
{
GroupCode entityType(0),layerName(8),colorNumber(62);
entityType.str="3DFACE";
layerName.str="TIN";
colorNumber.integer=0;
dxfData.push_back(entityType);
dxfData.push_back(layerName);
insertXyz(dxfData,10,*tri.a/outUnit);
insertXyz(dxfData,11,*tri.b/outUnit);
insertXyz(dxfData,12,*tri.c/outUnit); // A 3DFACE always has four corners. That it's a
insertXyz(dxfData,13,*tri.c/outUnit); // triangle is indicated by repeating a corner.
}
void insertPolyline(vector<GroupCode> &dxfData,polyline &poly,DxfLayer &lay,double outUnit)
// Does not yet handle polyarcs or polyspirals.
{
GroupCode entityType(0),layerName(8),colorNumber(62);
GroupCode nVertices(90),closedFlag(70),elev(38);
int i;
entityType.str="LWPOLYLINE";
layerName.str=lay.name;
colorNumber.integer=0;
closedFlag.integer=!poly.isopen();
nVertices.integer=poly.size()+poly.isopen();
elev.real=poly.getElevation()/outUnit;
dxfData.push_back(entityType);
dxfData.push_back(layerName);
dxfData.push_back(nVertices);
dxfData.push_back(closedFlag);
dxfData.push_back(elev);
for (i=0;i<nVertices.integer;i++)
insertXy(dxfData,10,poly.getEndpoint(i)/outUnit);
}