-
Notifications
You must be signed in to change notification settings - Fork 19
/
dxf.cpp
1634 lines (1495 loc) · 38.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// dxf.cpp
// Copyright 2011, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "dxf.h"
using namespace std;
static const double Pi = 3.14159265358979323846264338327950288419716939937511;
CDxfWrite::CDxfWrite(const char* filepath)
{
// start the file
m_fail = false;
#ifdef __WXMSW__
m_ofs = new ofstream(filepath, ios::out);
#else
m_ofs = new ofstream(filepath, ios::out);
#endif
if(!(*m_ofs)){
m_fail = true;
return;
}
m_ofs->imbue(std::locale("C"));
// start
(*m_ofs) << 0 << endl;
(*m_ofs) << "SECTION" << endl;
(*m_ofs) << 2 << endl;
(*m_ofs) << "ENTITIES" << endl;
}
CDxfWrite::~CDxfWrite()
{
// end
(*m_ofs) << 0 << endl;
(*m_ofs) << "ENDSEC" << endl;
(*m_ofs) << 0 << endl;
(*m_ofs) << "EOF";
delete m_ofs;
}
void CDxfWrite::WriteLine(const double* s, const double* e, const char* layer_name)
{
(*m_ofs) << 0 << endl;
(*m_ofs) << "LINE" << endl;
(*m_ofs) << 8 << endl; // Group code for layer name
(*m_ofs) << layer_name << endl; // Layer number
(*m_ofs) << 10 << endl; // Start point of line
(*m_ofs) << s[0] << endl; // X in WCS coordinates
(*m_ofs) << 20 << endl;
(*m_ofs) << s[1] << endl; // Y in WCS coordinates
(*m_ofs) << 30 << endl;
(*m_ofs) << s[2] << endl; // Z in WCS coordinates
(*m_ofs) << 11 << endl; // End point of line
(*m_ofs) << e[0] << endl; // X in WCS coordinates
(*m_ofs) << 21 << endl;
(*m_ofs) << e[1] << endl; // Y in WCS coordinates
(*m_ofs) << 31 << endl;
(*m_ofs) << e[2] << endl; // Z in WCS coordinates
}
void CDxfWrite::WritePoint(const double* s, const char* layer_name)
{
(*m_ofs) << 0 << endl;
(*m_ofs) << "POINT" << endl;
(*m_ofs) << 8 << endl; // Group code for layer name
(*m_ofs) << layer_name << endl; // Layer number
(*m_ofs) << 10 << endl; // Start point of line
(*m_ofs) << s[0] << endl; // X in WCS coordinates
(*m_ofs) << 20 << endl;
(*m_ofs) << s[1] << endl; // Y in WCS coordinates
(*m_ofs) << 30 << endl;
(*m_ofs) << s[2] << endl; // Z in WCS coordinates
}
void CDxfWrite::WriteArc(const double* s, const double* e, const double* c, bool dir, const char* layer_name)
{
double ax = s[0] - c[0];
double ay = s[1] - c[1];
double bx = e[0] - c[0];
double by = e[1] - c[1];
double start_angle = atan2(ay, ax) * 180/Pi;
double end_angle = atan2(by, bx) * 180/Pi;
double radius = sqrt(ax*ax + ay*ay);
if(!dir){
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;
}
(*m_ofs) << 0 << endl;
(*m_ofs) << "ARC" << endl;
(*m_ofs) << 8 << endl; // Group code for layer name
(*m_ofs) << layer_name << endl; // Layer number
(*m_ofs) << 10 << endl; // Centre X
(*m_ofs) << c[0] << endl; // X in WCS coordinates
(*m_ofs) << 20 << endl;
(*m_ofs) << c[1] << endl; // Y in WCS coordinates
(*m_ofs) << 30 << endl;
(*m_ofs) << c[2] << endl; // Z in WCS coordinates
(*m_ofs) << 40 << endl; //
(*m_ofs) << radius << endl; // Radius
(*m_ofs) << 50 << endl;
(*m_ofs) << start_angle << endl; // Start angle
(*m_ofs) << 51 << endl;
(*m_ofs) << end_angle << endl; // End angle
}
void CDxfWrite::WriteCircle(const double* c, double radius, const char* layer_name)
{
(*m_ofs) << 0 << endl;
(*m_ofs) << "CIRCLE" << endl;
(*m_ofs) << 8 << endl; // Group code for layer name
(*m_ofs) << layer_name << endl; // Layer number
(*m_ofs) << 10 << endl; // Centre X
(*m_ofs) << c[0] << endl; // X in WCS coordinates
(*m_ofs) << 20 << endl;
(*m_ofs) << c[1] << endl; // Y in WCS coordinates
(*m_ofs) << 30 << endl;
(*m_ofs) << c[2] << endl; // Z in WCS coordinates
(*m_ofs) << 40 << endl; //
(*m_ofs) << radius << endl; // Radius
}
void CDxfWrite::WriteEllipse(const double* c, double major_radius, double minor_radius, double rotation, double start_angle, double end_angle, bool dir, const char* layer_name )
{
double m[3];
m[2]=0;
m[0] = major_radius * sin(rotation);
m[1] = major_radius * cos(rotation);
double ratio = minor_radius/major_radius;
if(!dir){
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;
}
(*m_ofs) << 0 << endl;
(*m_ofs) << "ELLIPSE" << endl;
(*m_ofs) << 8 << endl; // Group code for layer name
(*m_ofs) << layer_name << endl; // Layer number
(*m_ofs) << 10 << endl; // Centre X
(*m_ofs) << c[0] << endl; // X in WCS coordinates
(*m_ofs) << 20 << endl;
(*m_ofs) << c[1] << endl; // Y in WCS coordinates
(*m_ofs) << 30 << endl;
(*m_ofs) << c[2] << endl; // Z in WCS coordinates
(*m_ofs) << 40 << endl; //
(*m_ofs) << ratio << endl; // Ratio
(*m_ofs) << 11 << endl; //
(*m_ofs) << m[0] << endl; // Major X
(*m_ofs) << 21 << endl;
(*m_ofs) << m[1] << endl; // Major Y
(*m_ofs) << 31 << endl;
(*m_ofs) << m[2] << endl; // Major Z
(*m_ofs) << 41 << endl;
(*m_ofs) << start_angle << endl; // Start angle
(*m_ofs) << 42 << endl;
(*m_ofs) << end_angle << endl; // End angle
}
CDxfRead::CDxfRead(const char* filepath)
{
// start the file
memset( m_unused_line, '\0', sizeof(m_unused_line) );
m_fail = false;
m_eUnits = eMillimeters;
strcpy(m_layer_name, "0"); // Default layer name
m_ignore_errors = true;
m_ifs = new ifstream(filepath);
if(!(*m_ifs)){
m_fail = true;
return;
}
m_ifs->imbue(std::locale("C"));
}
CDxfRead::~CDxfRead()
{
delete m_ifs;
}
double CDxfRead::mm( const double & value ) const
{
switch(m_eUnits)
{
case eUnspecified: return(value * 1.0); // We don't know any better.
case eInches: return(value * 25.4);
case eFeet: return(value * 25.4 * 12);
case eMiles: return(value * 1609344.0);
case eMillimeters: return(value * 1.0);
case eCentimeters: return(value * 10.0);
case eMeters: return(value * 1000.0);
case eKilometers: return(value * 1000000.0);
case eMicroinches: return(value * 25.4 / 1000.0);
case eMils: return(value * 25.4 / 1000.0);
case eYards: return(value * 3 * 12 * 25.4);
case eAngstroms: return(value * 0.0000001);
case eNanometers: return(value * 0.000001);
case eMicrons: return(value * 0.001);
case eDecimeters: return(value * 100.0);
case eDekameters: return(value * 10000.0);
case eHectometers: return(value * 100000.0);
case eGigameters: return(value * 1000000000000.0);
case eAstronomicalUnits: return(value * 149597870690000.0);
case eLightYears: return(value * 9454254955500000000.0);
case eParsecs: return(value * 30856774879000000000.0);
default: return(value * 1.0); // We don't know any better.
} // End switch
} // End mm() method
bool CDxfRead::ReadLine()
{
double s[3] = {0, 0, 0};
double e[3] = {0, 0, 0};
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadLine() Failed to read integer from '%s'\n", m_str );
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with line
DerefACI();
OnReadLine(s, e);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// start x
get_line();
ss.str(m_str); ss >> s[0]; s[0] = mm(s[0]); if(ss.fail()) return false;
break;
case 20:
// start y
get_line();
ss.str(m_str); ss >> s[1]; s[1] = mm(s[1]); if(ss.fail()) return false;
break;
case 30:
// start z
get_line();
ss.str(m_str); ss >> s[2]; s[2] = mm(s[2]); if(ss.fail()) return false;
break;
case 11:
// end x
get_line();
ss.str(m_str); ss >> e[0]; e[0] = mm(e[0]); if(ss.fail()) return false;
break;
case 21:
// end y
get_line();
ss.str(m_str); ss >> e[1]; e[1] = mm(e[1]); if(ss.fail()) return false;
break;
case 31:
// end z
get_line();
ss.str(m_str); ss >> e[2]; e[2] = mm(e[2]); if(ss.fail()) return false;
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 39:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
try {
DerefACI();
OnReadLine(s, e);
}
catch(...)
{
if (! IgnoreErrors()) throw; // Re-throw the exception.
}
return false;
}
bool CDxfRead::ReadPoint()
{
double s[3] = {0, 0, 0};
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadPoint() Failed to read integer from '%s'\n", m_str );
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with line
DerefACI();
OnReadPoint(s);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// start x
get_line();
ss.str(m_str); ss >> s[0]; s[0] = mm(s[0]); if(ss.fail()) return false;
break;
case 20:
// start y
get_line();
ss.str(m_str); ss >> s[1]; s[1] = mm(s[1]); if(ss.fail()) return false;
break;
case 30:
// start z
get_line();
ss.str(m_str); ss >> s[2]; s[2] = mm(s[2]); if(ss.fail()) return false;
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 39:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
try {
DerefACI();
OnReadPoint(s);
}
catch(...)
{
if (! IgnoreErrors()) throw; // Re-throw the exception.
}
return false;
}
bool CDxfRead::ReadArc()
{
double start_angle = 0.0;// in degrees
double end_angle = 0.0;
double radius = 0.0;
double c[3]; // centre
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadArc() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with arc
DerefACI();
OnReadArc(start_angle, end_angle, radius, c);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// centre x
get_line();
ss.str(m_str); ss >> c[0]; c[0] = mm(c[0]); if(ss.fail()) return false;
break;
case 20:
// centre y
get_line();
ss.str(m_str); ss >> c[1]; c[1] = mm(c[1]); if(ss.fail()) return false;
break;
case 30:
// centre z
get_line();
ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false;
break;
case 40:
// radius
get_line();
ss.str(m_str); ss >> radius; radius = mm(radius); if(ss.fail()) return false;
break;
case 50:
// start angle
get_line();
ss.str(m_str); ss >> start_angle; if(ss.fail()) return false;
break;
case 51:
// end angle
get_line();
ss.str(m_str); ss >> end_angle; if(ss.fail()) return false;
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 39:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
DerefACI();
OnReadArc(start_angle, end_angle, radius, c);
return false;
}
bool CDxfRead::ReadSpline()
{
struct SplineData sd;
sd.norm[0] = 0;
sd.norm[1] = 0;
sd.norm[2] = 1;
sd.degree = 0;
sd.knots = 0;
sd.flag = 0;
sd.control_points = 0;
sd.fit_points = 0;
double temp_double;
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadSpline() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with Spline
DerefACI();
OnReadSpline(sd);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 210:
// normal x
get_line();
ss.str(m_str); ss >> sd.norm[0]; sd.norm[0] = mm(sd.norm[0]); if(ss.fail()) return false;
break;
case 220:
// normal y
get_line();
ss.str(m_str); ss >> sd.norm[1]; sd.norm[1] = mm(sd.norm[1]); if(ss.fail()) return false;
break;
case 230:
// normal z
get_line();
ss.str(m_str); ss >> sd.norm[2]; sd.norm[2] = mm(sd.norm[2]); if(ss.fail()) return false;
break;
case 70:
// flag
get_line();
ss.str(m_str); ss >> sd.flag; if(ss.fail()) return false;
break;
case 71:
// degree
get_line();
ss.str(m_str); ss >> sd.degree; if(ss.fail()) return false;
break;
case 72:
// knots
get_line();
ss.str(m_str); ss >> sd.knots; if(ss.fail()) return false;
break;
case 73:
// control points
get_line();
ss.str(m_str); ss >> sd.control_points; if(ss.fail()) return false;
break;
case 74:
// fit points
get_line();
ss.str(m_str); ss >> sd.fit_points; if(ss.fail()) return false;
break;
case 12:
// starttan x
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.starttanx.push_back(temp_double);
break;
case 22:
// starttan y
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.starttany.push_back(temp_double);
break;
case 32:
// starttan z
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.starttanz.push_back(temp_double);
break;
case 13:
// endtan x
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.endtanx.push_back(temp_double);
break;
case 23:
// endtan y
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.endtany.push_back(temp_double);
break;
case 33:
// endtan z
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.endtanz.push_back(temp_double);
break;
case 40:
// knot
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.knot.push_back(temp_double);
break;
case 41:
// weight
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.weight.push_back(temp_double);
break;
case 10:
// control x
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.controlx.push_back(temp_double);
break;
case 20:
// control y
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.controly.push_back(temp_double);
break;
case 30:
// control z
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.controlz.push_back(temp_double);
break;
case 11:
// fit x
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.fitx.push_back(temp_double);
break;
case 21:
// fit y
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.fity.push_back(temp_double);
break;
case 31:
// fit z
get_line();
ss.str(m_str); ss >> temp_double; if(ss.fail()) return false;
sd.fitz.push_back(temp_double);
break;
case 42:
case 43:
case 44:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
DerefACI();
OnReadSpline(sd);
return false;
}
bool CDxfRead::ReadCircle()
{
double radius = 0.0;
double c[3]; // centre
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadCircle() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with Circle
DerefACI();
OnReadCircle(c, radius);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// centre x
get_line();
ss.str(m_str); ss >> c[0]; c[0] = mm(c[0]); if(ss.fail()) return false;
break;
case 20:
// centre y
get_line();
ss.str(m_str); ss >> c[1]; c[1] = mm(c[1]); if(ss.fail()) return false;
break;
case 30:
// centre z
get_line();
ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false;
break;
case 40:
// radius
get_line();
ss.str(m_str); ss >> radius; radius = mm(radius); if(ss.fail()) return false;
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 39:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
DerefACI();
OnReadCircle(c, radius);
return false;
}
bool CDxfRead::ReadText()
{
double c[3]; // coordinate
double height = 0.03082;
memset( c, 0, sizeof(c) );
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadText() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
return false;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// centre x
get_line();
ss.str(m_str); ss >> c[0]; c[0] = mm(c[0]); if(ss.fail()) return false;
break;
case 20:
// centre y
get_line();
ss.str(m_str); ss >> c[1]; c[1] = mm(c[1]); if(ss.fail()) return false;
break;
case 30:
// centre z
get_line();
ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false;
break;
case 40:
// text height
get_line();
ss.str(m_str); ss >> height; height = mm(height); if(ss.fail()) return false;
break;
case 1:
// text
get_line();
DerefACI();
OnReadText(c, height * 25.4 / 72.0, m_str);
return(true);
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 39:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
return false;
}
bool CDxfRead::ReadEllipse()
{
double c[3]; // centre
double m[3]; //major axis point
double ratio=0; //ratio of major to minor axis
double start=0; //start of arc
double end=0; // end of arc
while(!((*m_ifs).eof()))
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadEllipse() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found, so finish with Ellipse
DerefACI();
OnReadEllipse(c, m, ratio, start, end);
return true;
case 8: // Layer name follows
get_line();
strcpy(m_layer_name, m_str);
break;
case 10:
// centre x
get_line();
ss.str(m_str); ss >> c[0]; c[0] = mm(c[0]); if(ss.fail()) return false;
break;
case 20:
// centre y
get_line();
ss.str(m_str); ss >> c[1]; c[1] = mm(c[1]); if(ss.fail()) return false;
break;
case 30:
// centre z
get_line();
ss.str(m_str); ss >> c[2]; c[2] = mm(c[2]); if(ss.fail()) return false;
break;
case 11:
// major x
get_line();
ss.str(m_str); ss >> m[0]; m[0] = mm(m[0]); if(ss.fail()) return false;
break;
case 21:
// major y
get_line();
ss.str(m_str); ss >> m[1]; m[1] = mm(m[1]); if(ss.fail()) return false;
break;
case 31:
// major z
get_line();
ss.str(m_str); ss >> m[2]; m[2] = mm(m[2]); if(ss.fail()) return false;
break;
case 40:
// ratio
get_line();
ss.str(m_str); ss >> ratio; if(ss.fail()) return false;
break;
case 41:
// start
get_line();
ss.str(m_str); ss >> start; if(ss.fail()) return false;
break;
case 42:
// end
get_line();
ss.str(m_str); ss >> end; if(ss.fail()) return false;
break;
case 62:
// color index
get_line();
ss.str(m_str); ss >> m_aci; if(ss.fail()) return false;
break;
case 100:
case 210:
case 220:
case 230:
// skip the next line
get_line();
break;
default:
// skip the next line
get_line();
break;
}
}
DerefACI();
OnReadEllipse(c, m, ratio, start, end);
return false;
}
static bool poly_prev_found = false;
static double poly_prev_x;
static double poly_prev_y;
static double poly_prev_z;
static double poly_prev_bulge_found;
static double poly_prev_bulge;
static bool poly_first_found = false;
static double poly_first_x;
static double poly_first_y;
static double poly_first_z;
static void AddPolyLinePoint(CDxfRead* dxf_read, double x, double y, double z, bool bulge_found, double bulge)
{
try {
if(poly_prev_found)
{
bool arc_done = false;
if(poly_prev_bulge_found)
{
double cot = 0.5 * ((1.0 / poly_prev_bulge) - poly_prev_bulge);
double cx = ((poly_prev_x + x) - ((y - poly_prev_y) * cot)) / 2.0;
double cy = ((poly_prev_y + y) + ((x - poly_prev_x) * cot)) / 2.0;
double ps[3] = {poly_prev_x, poly_prev_y, poly_prev_z};
double pe[3] = {x, y, z};
double pc[3] = {cx, cy, (poly_prev_z + z)/2.0};
dxf_read->OnReadArc(ps, pe, pc, poly_prev_bulge >= 0);
arc_done = true;
}
if(!arc_done)
{
double s[3] = {poly_prev_x, poly_prev_y, poly_prev_z};
double e[3] = {x, y, z};
dxf_read->OnReadLine(s, e);
}
}
poly_prev_found = true;
poly_prev_x = x;
poly_prev_y = y;
poly_prev_z = z;
if(!poly_first_found)
{
poly_first_x = x;
poly_first_y = y;
poly_first_z = z;
poly_first_found = true;
}
poly_prev_bulge_found = bulge_found;
poly_prev_bulge = bulge;
}
catch(...)
{
if (! dxf_read->IgnoreErrors()) throw; // Re-throw it.
}
}
static void PolyLineStart()
{
poly_prev_found = false;
poly_first_found = false;
}
bool CDxfRead::ReadLwPolyLine()
{
PolyLineStart();
bool x_found = false;
bool y_found = false;
double x = 0.0;
double y = 0.0;
double z = 0.0;
bool bulge_found = false;
double bulge = 0.0;
bool closed = false;
int flags;
bool next_item_found = false;
while(!((*m_ifs).eof()) && !next_item_found)
{
get_line();
int n;
if(sscanf(m_str, "%d", &n) != 1)
{
printf("CDxfRead::ReadLwPolyLine() Failed to read integer from '%s'\n", m_str);
return false;
}
std::istringstream ss;
ss.imbue(std::locale("C"));
switch(n){
case 0:
// next item found
DerefACI();