-
Notifications
You must be signed in to change notification settings - Fork 1
/
dgnread.cpp
2081 lines (1745 loc) · 73.1 KB
/
dgnread.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
/******************************************************************************
* $Id: dgnread.cpp,v 1.55 2006/01/25 19:01:40 kintel Exp $
*
* Project: Microstation DGN Access Library
* Purpose: DGN Access Library element reading code.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2000, Avenza Systems Inc, http://www.avenza.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*
* $Log: dgnread.cpp,v $
* Revision 1.55 2006/01/25 19:01:40 kintel
* bugfix: Forgot to convert KnotWeight array from int to float
*
* Revision 1.54 2006/01/25 18:43:19 kintel
* B-Spline curve and surface support
*
* Revision 1.48 2005/11/18 17:43:12 fwarmerdam
* Added text node support in DGNGetRawExtents().
*
* Revision 1.47 2005/11/18 17:16:35 fwarmerdam
* added TextNode implementation from Ilya Beylin
*
* Revision 1.46 2004/06/02 15:53:00 warmerda
* improve divide by zero error checking
*
* Revision 1.45 2004/04/06 04:24:51 warmerda
* Always make any new color table encountered the default color
* table when it is read.
*
* Revision 1.44 2004/01/23 16:15:22 warmerda
* DGN_LEVEL_SYMBOLOGY has no disphdr, DGNParseCore() wont read negative attr
*
* Revision 1.43 2003/11/24 14:17:40 warmerda
* Added a bunch more no-disphdr elements as per email from Marius.
*
* Revision 1.42 2003/11/18 21:11:40 warmerda
* Added DGNElemTypeHasDispHdr() function.
*
* Revision 1.41 2003/11/07 13:59:45 warmerda
* added DGNLoadTCB()
*
* Revision 1.40 2003/09/22 05:59:24 warmerda
* Fixed setting of offset for element index.
* Don't try to extract attribute data on TCB elements.
*
* Revision 1.39 2003/08/19 20:16:56 warmerda
* Added support for reading Cone (23), 3D surface (18) and 3D solid (19)
* elements. Added code to read transformation matrices for Cell headers
* Marius Kintel
*
* Revision 1.38 2003/06/27 14:38:26 warmerda
* avoid warnings
*
* Revision 1.37 2003/05/21 03:42:01 warmerda
* Expanded tabs
*
* Revision 1.36 2003/05/12 18:48:57 warmerda
* added preliminary 3D write support
*
* Revision 1.35 2002/11/13 21:26:32 warmerda
* added more documentation
*
* Revision 1.34 2002/11/12 23:11:52 warmerda
* fixed error with tags in DGNFreeElement()
*
* Revision 1.33 2002/11/12 19:45:27 warmerda
* added support to parse view infos in TCB
*
* Revision 1.32 2002/11/11 20:38:13 warmerda
* provide external extents call, add inverse to int function
*
* Revision 1.31 2002/10/29 19:44:08 warmerda
* fixed complex group handling with spatial queries
*
* Revision 1.30 2002/10/29 19:25:18 warmerda
* fixed serious bug in applying color table to DGNInfo structure
*
* Revision 1.29 2002/09/24 14:34:19 warmerda
* fix collection of background color
*
* Revision 1.28 2002/05/30 19:24:38 warmerda
* add partial support for tag type 5
*
* Revision 1.27 2002/04/22 20:42:40 warmerda
* fixed problem with tag set ids > 255
*
* Revision 1.26 2002/04/08 21:25:59 warmerda
* removed tagDef->id = iTag+1 assumption
*
* Revision 1.25 2002/03/14 21:40:03 warmerda
* expose DGNLoadRawElement, add max_element_count in psDGN
*
* Revision 1.24 2002/03/12 17:11:31 warmerda
* ensure tag elements are indexed
*
* Revision 1.23 2002/03/12 17:07:26 warmerda
* added tagset and tag value element support
*
* Revision 1.22 2002/02/22 22:17:42 warmerda
* Ensure that components of complex chain/shapes are spatially selected
* based on the decision made for their owner (header).
*
* Revision 1.21 2002/02/06 22:39:08 warmerda
* dont filter out non-spatial features with spatial filter
*
* Revision 1.20 2002/02/06 20:32:33 warmerda
* handle improbably large elements
*
* Revision 1.19 2002/01/21 20:53:33 warmerda
* a bunch of reorganization to support spatial filtering
*
* Revision 1.18 2002/01/15 06:38:51 warmerda
* moved some functions to dgnhelp.cpp
*
* Revision 1.17 2001/12/19 15:29:56 warmerda
* added preliminary cell header support
*
* Revision 1.16 2001/11/09 14:55:29 warmerda
* fixed 3D support for arcs
*
* Revision 1.15 2001/09/27 14:28:44 warmerda
* first hack at 3D support
*
* Revision 1.14 2001/08/28 21:27:07 warmerda
* added prototype multi-byte character support
*
* Revision 1.13 2001/08/21 03:01:39 warmerda
* added raw_data support
*
* Revision 1.12 2001/07/18 04:55:16 warmerda
* added CPL_CSVID
*
* Revision 1.11 2001/06/25 15:07:51 warmerda
* Added support for DGNElemComplexHeader
* Don't include elements with the complex bit (such as shared cell definition
* elements) in extents computation for fear they are in a different coord sys.
*
* Revision 1.10 2001/03/07 13:56:44 warmerda
* updated copyright to be held by Avenza Systems
*
* Revision 1.9 2001/03/07 13:52:15 warmerda
* Don't include deleted elements in the total extents.
* Capture attribute data.
*
* Revision 1.7 2001/02/02 22:20:29 warmerda
* compute text height/width properly
*
* Revision 1.6 2001/01/17 16:07:33 warmerda
* ensure that TCB and ColorTable side effects occur on indexing pass too
*
* Revision 1.5 2001/01/16 18:12:52 warmerda
* Added arc support, DGNLookupColor
*
* Revision 1.4 2001/01/10 16:13:45 warmerda
* added docs and extents api
*
* Revision 1.3 2000/12/28 21:28:59 warmerda
* added element index support
*
* Revision 1.2 2000/12/14 17:10:57 warmerda
* implemented TCB, Ellipse, TEXT
*
* Revision 1.1 2000/11/28 19:03:47 warmerda
* New
*
*/
#include "dgnlibp.h"
CPL_CVSID("$Id: dgnread.cpp,v 1.55 2006/01/25 19:01:40 kintel Exp $");
static DGNElemCore *DGNParseTCB( DGNInfo * );
static DGNElemCore *DGNParseColorTable( DGNInfo * );
static DGNElemCore *DGNParseTagSet( DGNInfo * );
static DGNElemCore *DGNParseEqp( DGNInfo * psDGN );
/************************************************************************/
/* DGNGotoElement() */
/************************************************************************/
/**
* Seek to indicated element.
*
* Changes what element will be read on the next call to DGNReadElement().
* Note that this function requires and index, and one will be built if
* not already available.
*
* @param hDGN the file to affect.
* @param element_id the element to seek to. These values are sequentially
* ordered starting at zero for the first element.
*
* @return returns TRUE on success or FALSE on failure.
*/
int DGNGotoElement( DGNHandle hDGN, int element_id )
{
DGNInfo *psDGN = (DGNInfo *) hDGN;
DGNBuildIndex( psDGN );
if( element_id < 0 || element_id >= psDGN->element_count )
return FALSE;
if( VSIFSeek( psDGN->fp, psDGN->element_index[element_id].offset,
SEEK_SET ) != 0 )
return FALSE;
psDGN->next_element_id = element_id;
psDGN->in_complex_group = FALSE;
return TRUE;
}
/************************************************************************/
/* DGNLoadRawElement() */
/************************************************************************/
int DGNLoadRawElement( DGNInfo *psDGN, int *pnType, int *pnLevel )
{
/* -------------------------------------------------------------------- */
/* Read the first four bytes to get the level, type, and word */
/* count. */
/* -------------------------------------------------------------------- */
int nType, nWords, nLevel;
if( VSIFRead( psDGN->abyElem, 1, 4, psDGN->fp ) != 4 )
return FALSE;
/* Is this an 0xFFFF endof file marker? */
if( psDGN->abyElem[0] == 0xff && psDGN->abyElem[1] == 0xff )
return FALSE;
nWords = psDGN->abyElem[2] + psDGN->abyElem[3]*256;
nType = psDGN->abyElem[1] & 0x7f;
nLevel = psDGN->abyElem[0] & 0x3f;
/* -------------------------------------------------------------------- */
/* Read the rest of the element data into the working buffer. */
/* -------------------------------------------------------------------- */
CPLAssert( nWords * 2 + 4 <= (int) sizeof(psDGN->abyElem) );
if( (int) VSIFRead( psDGN->abyElem + 4, 2, nWords, psDGN->fp ) != nWords )
return FALSE;
psDGN->nElemBytes = nWords * 2 + 4;
psDGN->next_element_id++;
/* -------------------------------------------------------------------- */
/* Return requested info. */
/* -------------------------------------------------------------------- */
if( pnType != NULL )
*pnType = nType;
if( pnLevel != NULL )
*pnLevel = nLevel;
return TRUE;
}
/************************************************************************/
/* DGNGetRawExtents() */
/* */
/* Returns FALSE if the element type does not have reconisable */
/* element extents, other TRUE and the extents will be updated. */
/* */
/* It is assumed the raw element data has been loaded into the */
/* working area by DGNLoadRawElement(). */
/************************************************************************/
static int
DGNGetRawExtents( DGNInfo *psDGN, int nType, unsigned char *pabyRawData,
GUInt32 *pnXMin, GUInt32 *pnYMin, GUInt32 *pnZMin,
GUInt32 *pnXMax, GUInt32 *pnYMax, GUInt32 *pnZMax )
{
if( pabyRawData == NULL )
pabyRawData = psDGN->abyElem + 0;
switch( nType )
{
case DGNT_LINE:
case DGNT_LINE_STRING:
case DGNT_SHAPE:
case DGNT_CURVE:
case DGNT_BSPLINE_POLE:
case DGNT_BSPLINE_SURFACE_HEADER:
case DGNT_BSPLINE_CURVE_HEADER:
case DGNT_ELLIPSE:
case DGNT_ARC:
case DGNT_TEXT:
case DGNT_TEXT_NODE:
case DGNT_COMPLEX_CHAIN_HEADER:
case DGNT_COMPLEX_SHAPE_HEADER:
case DGNT_CONE:
case DGNT_3DSURFACE_HEADER:
case DGNT_3DSOLID_HEADER:
*pnXMin = DGN_INT32( pabyRawData + 4 );
*pnYMin = DGN_INT32( pabyRawData + 8 );
if( pnZMin != NULL )
*pnZMin = DGN_INT32( pabyRawData + 12 );
*pnXMax = DGN_INT32( pabyRawData + 16 );
*pnYMax = DGN_INT32( pabyRawData + 20 );
if( pnZMax != NULL )
*pnZMax = DGN_INT32( pabyRawData + 24 );
return TRUE;
default:
return FALSE;
}
}
/************************************************************************/
/* DGNGetElementExtents() */
/************************************************************************/
/**
* Fetch extents of an element.
*
* This function will return the extents of the passed element if possible.
* The extents are extracted from the element header if it contains them,
* and transformed into master georeferenced format. Some element types
* do not have extents at all and will fail.
*
* This call will also fail if the extents raw data for the element is not
* available. This will occur if it was not the most recently read element,
* and if the raw_data field is not loaded.
*
* @param hDGN the handle of the file to read from.
*
* @param psElement the element to extract extents from.
*
* @param psMin structure loaded with X, Y and Z minimum values for the
* extent.
*
* @param psMax structure loaded with X, Y and Z maximum values for the
* extent.
*
* @return TRUE on success of FALSE if extracting extents fails.
*/
int DGNGetElementExtents( DGNHandle hDGN, DGNElemCore *psElement,
DGNPoint *psMin, DGNPoint *psMax )
{
DGNInfo *psDGN = (DGNInfo *) hDGN;
GUInt32 anMin[3], anMax[3];
int bResult;
/* -------------------------------------------------------------------- */
/* Get the extents if we have raw data in the element, or */
/* loaded in the file buffer. */
/* -------------------------------------------------------------------- */
if( psElement->raw_data != NULL )
bResult = DGNGetRawExtents( psDGN, psElement->type,
psElement->raw_data,
anMin + 0, anMin + 1, anMin + 2,
anMax + 0, anMax + 1, anMax + 2 );
else if( psElement->element_id == psDGN->next_element_id - 1 )
bResult = DGNGetRawExtents( psDGN, psElement->type,
psDGN->abyElem + 0,
anMin + 0, anMin + 1, anMin + 2,
anMax + 0, anMax + 1, anMax + 2 );
else
{
CPLError(CE_Warning, CPLE_AppDefined,
"DGNGetElementExtents() fails because the requested element\n"
" does not have raw data available." );
return FALSE;
}
if( !bResult )
return FALSE;
/* -------------------------------------------------------------------- */
/* Transform to user coordinate system and return. The offset */
/* is to convert from "binary offset" form to twos complement. */
/* -------------------------------------------------------------------- */
psMin->x = anMin[0] - 2147483648.0;
psMin->y = anMin[1] - 2147483648.0;
psMin->z = anMin[2] - 2147483648.0;
psMax->x = anMax[0] - 2147483648.0;
psMax->y = anMax[1] - 2147483648.0;
psMax->z = anMax[2] - 2147483648.0;
DGNTransformPoint( psDGN, psMin );
DGNTransformPoint( psDGN, psMax );
return TRUE;
}
/************************************************************************/
/* DGNProcessElement() */
/* */
/* Assumes the raw element data has already been loaded, and */
/* tries to convert it into an element structure. */
/************************************************************************/
static DGNElemCore *DGNProcessElement( DGNInfo *psDGN, int nType, int nLevel )
{
DGNElemCore *psElement = NULL;
/* -------------------------------------------------------------------- */
/* Handle based on element type. */
/* -------------------------------------------------------------------- */
switch( nType )
{
case DGNT_CELL_HEADER:
{
DGNElemCellHeader *psCell;
psCell = (DGNElemCellHeader *)
CPLCalloc(sizeof(DGNElemCellHeader),1);
psElement = (DGNElemCore *) psCell;
psElement->stype = DGNST_CELL_HEADER;
DGNParseCore( psDGN, psElement );
psCell->totlength = psDGN->abyElem[36] + psDGN->abyElem[37] * 256;
DGNRad50ToAscii( psDGN->abyElem[38] + psDGN->abyElem[39] * 256,
psCell->name + 0 );
DGNRad50ToAscii( psDGN->abyElem[40] + psDGN->abyElem[41] * 256,
psCell->name + 3 );
psCell->cclass = psDGN->abyElem[42] + psDGN->abyElem[43] * 256;
psCell->levels[0] = psDGN->abyElem[44] + psDGN->abyElem[45] * 256;
psCell->levels[1] = psDGN->abyElem[46] + psDGN->abyElem[47] * 256;
psCell->levels[2] = psDGN->abyElem[48] + psDGN->abyElem[49] * 256;
psCell->levels[3] = psDGN->abyElem[50] + psDGN->abyElem[51] * 256;
if( psDGN->dimension == 2 )
{
psCell->rnglow.x = DGN_INT32( psDGN->abyElem + 52 );
psCell->rnglow.y = DGN_INT32( psDGN->abyElem + 56 );
psCell->rnghigh.x = DGN_INT32( psDGN->abyElem + 60 );
psCell->rnghigh.y = DGN_INT32( psDGN->abyElem + 64 );
psCell->trans[0] =
1.0 * DGN_INT32( psDGN->abyElem + 68 ) / (1<<31);
psCell->trans[1] =
1.0 * DGN_INT32( psDGN->abyElem + 72 ) / (1<<31);
psCell->trans[2] =
1.0 * DGN_INT32( psDGN->abyElem + 76 ) / (1<<31);
psCell->trans[3] =
1.0 * DGN_INT32( psDGN->abyElem + 80 ) / (1<<31);
psCell->origin.x = DGN_INT32( psDGN->abyElem + 84 );
psCell->origin.y = DGN_INT32( psDGN->abyElem + 88 );
{
double a, b, c, d, a2, c2;
a = DGN_INT32( psDGN->abyElem + 68 );
b = DGN_INT32( psDGN->abyElem + 72 );
c = DGN_INT32( psDGN->abyElem + 76 );
d = DGN_INT32( psDGN->abyElem + 80 );
a2 = a * a;
c2 = c * c;
psCell->xscale = sqrt(a2 + c2) / 214748;
psCell->yscale = sqrt(b*b + d*d) / 214748;
if( (a2 + c2) <= 0.0 )
psCell->rotation = 0.0;
else
psCell->rotation = acos(a / sqrt(a2 + c2));
if (b <= 0)
psCell->rotation = psCell->rotation * 180 / PI;
else
psCell->rotation = 360 - psCell->rotation * 180 / PI;
}
}
else
{
psCell->rnglow.x = DGN_INT32( psDGN->abyElem + 52 );
psCell->rnglow.y = DGN_INT32( psDGN->abyElem + 56 );
psCell->rnglow.z = DGN_INT32( psDGN->abyElem + 60 );
psCell->rnghigh.x = DGN_INT32( psDGN->abyElem + 64 );
psCell->rnghigh.y = DGN_INT32( psDGN->abyElem + 68 );
psCell->rnghigh.z = DGN_INT32( psDGN->abyElem + 72 );
psCell->trans[0] =
1.0 * DGN_INT32( psDGN->abyElem + 76 ) / (1<<31);
psCell->trans[1] =
1.0 * DGN_INT32( psDGN->abyElem + 80 ) / (1<<31);
psCell->trans[2] =
1.0 * DGN_INT32( psDGN->abyElem + 84 ) / (1<<31);
psCell->trans[3] =
1.0 * DGN_INT32( psDGN->abyElem + 88 ) / (1<<31);
psCell->trans[4] =
1.0 * DGN_INT32( psDGN->abyElem + 92 ) / (1<<31);
psCell->trans[5] =
1.0 * DGN_INT32( psDGN->abyElem + 96 ) / (1<<31);
psCell->trans[6] =
1.0 * DGN_INT32( psDGN->abyElem + 100 ) / (1<<31);
psCell->trans[7] =
1.0 * DGN_INT32( psDGN->abyElem + 104 ) / (1<<31);
psCell->trans[8] =
1.0 * DGN_INT32( psDGN->abyElem + 108 ) / (1<<31);
psCell->origin.x = DGN_INT32( psDGN->abyElem + 112 );
psCell->origin.y = DGN_INT32( psDGN->abyElem + 116 );
psCell->origin.z = DGN_INT32( psDGN->abyElem + 120 );
}
DGNTransformPoint( psDGN, &(psCell->rnglow) );
DGNTransformPoint( psDGN, &(psCell->rnghigh) );
DGNTransformPoint( psDGN, &(psCell->origin) );
}
break;
case DGNT_CELL_LIBRARY:
{
DGNElemCellLibrary *psCell;
int iWord;
psCell = (DGNElemCellLibrary *)
CPLCalloc(sizeof(DGNElemCellLibrary),1);
psElement = (DGNElemCore *) psCell;
psElement->stype = DGNST_CELL_LIBRARY;
DGNParseCore( psDGN, psElement );
DGNRad50ToAscii( psDGN->abyElem[32] + psDGN->abyElem[33] * 256,
psCell->name + 0 );
DGNRad50ToAscii( psDGN->abyElem[34] + psDGN->abyElem[35] * 256,
psCell->name + 3 );
psElement->properties = psDGN->abyElem[38]
+ psDGN->abyElem[39] * 256;
psCell->dispsymb = psDGN->abyElem[40] + psDGN->abyElem[41] * 256;
psCell->cclass = psDGN->abyElem[42] + psDGN->abyElem[43] * 256;
psCell->levels[0] = psDGN->abyElem[44] + psDGN->abyElem[45] * 256;
psCell->levels[1] = psDGN->abyElem[46] + psDGN->abyElem[47] * 256;
psCell->levels[2] = psDGN->abyElem[48] + psDGN->abyElem[49] * 256;
psCell->levels[3] = psDGN->abyElem[50] + psDGN->abyElem[51] * 256;
psCell->numwords = psDGN->abyElem[36] + psDGN->abyElem[37] * 256;
memset( psCell->description, 0, sizeof(psCell->description) );
for( iWord = 0; iWord < 9; iWord++ )
{
int iOffset = 52 + iWord * 2;
DGNRad50ToAscii( psDGN->abyElem[iOffset]
+ psDGN->abyElem[iOffset+1] * 256,
psCell->description + iWord * 3 );
}
}
break;
case DGNT_LINE:
{
DGNElemMultiPoint *psLine;
psLine = (DGNElemMultiPoint *)
CPLCalloc(sizeof(DGNElemMultiPoint),1);
psElement = (DGNElemCore *) psLine;
psElement->stype = DGNST_MULTIPOINT;
DGNParseCore( psDGN, psElement );
psLine->num_vertices = 2;
if( psDGN->dimension == 2 )
{
psLine->vertices[0].x = DGN_INT32( psDGN->abyElem + 36 );
psLine->vertices[0].y = DGN_INT32( psDGN->abyElem + 40 );
psLine->vertices[1].x = DGN_INT32( psDGN->abyElem + 44 );
psLine->vertices[1].y = DGN_INT32( psDGN->abyElem + 48 );
}
else
{
psLine->vertices[0].x = DGN_INT32( psDGN->abyElem + 36 );
psLine->vertices[0].y = DGN_INT32( psDGN->abyElem + 40 );
psLine->vertices[0].z = DGN_INT32( psDGN->abyElem + 44 );
psLine->vertices[1].x = DGN_INT32( psDGN->abyElem + 48 );
psLine->vertices[1].y = DGN_INT32( psDGN->abyElem + 52 );
psLine->vertices[1].z = DGN_INT32( psDGN->abyElem + 56 );
}
DGNTransformPoint( psDGN, psLine->vertices + 0 );
DGNTransformPoint( psDGN, psLine->vertices + 1 );
}
break;
case DGNT_LINE_STRING:
case DGNT_SHAPE:
case DGNT_CURVE:
case DGNT_BSPLINE_POLE:
{
DGNElemMultiPoint *psLine;
int i, count;
int pntsize = psDGN->dimension * 4;
count = psDGN->abyElem[36] + psDGN->abyElem[37]*256;
psLine = (DGNElemMultiPoint *)
CPLCalloc(sizeof(DGNElemMultiPoint)+(count-2)*sizeof(DGNPoint),1);
psElement = (DGNElemCore *) psLine;
psElement->stype = DGNST_MULTIPOINT;
DGNParseCore( psDGN, psElement );
if( psDGN->nElemBytes < 38 + count * pntsize )
{
CPLError( CE_Warning, CPLE_AppDefined,
"Trimming multipoint vertices to %d from %d because\n"
"element is short.\n",
(psDGN->nElemBytes - 38) / pntsize,
count );
count = (psDGN->nElemBytes - 38) / pntsize;
}
psLine->num_vertices = count;
for( i = 0; i < psLine->num_vertices; i++ )
{
psLine->vertices[i].x =
DGN_INT32( psDGN->abyElem + 38 + i*pntsize );
psLine->vertices[i].y =
DGN_INT32( psDGN->abyElem + 42 + i*pntsize );
if( psDGN->dimension == 3 )
psLine->vertices[i].z =
DGN_INT32( psDGN->abyElem + 46 + i*pntsize );
DGNTransformPoint( psDGN, psLine->vertices + i );
}
}
break;
case DGNT_TEXT_NODE:
{
DGNElemTextNode *psNode;
psNode = (DGNElemTextNode *) CPLCalloc(sizeof(DGNElemTextNode),1);
psElement = (DGNElemCore *) psNode;
psElement->stype = DGNST_TEXT_NODE;
DGNParseCore( psDGN, psElement );
psNode->totlength = psDGN->abyElem[36] + psDGN->abyElem[37] * 256;
psNode->numelems = psDGN->abyElem[38] + psDGN->abyElem[39] * 256;
psNode->node_number = psDGN->abyElem[40] + psDGN->abyElem[41] * 256;
psNode->max_length = psDGN->abyElem[42];
psNode->max_used = psDGN->abyElem[43];
psNode->font_id = psDGN->abyElem[44];
psNode->justification = psDGN->abyElem[45];
psNode->length_mult = (DGN_INT32( psDGN->abyElem + 50 ))
* psDGN->scale * 6.0 / 1000.0;
psNode->height_mult = (DGN_INT32( psDGN->abyElem + 54 ))
* psDGN->scale * 6.0 / 1000.0;
if( psDGN->dimension == 2 )
{
psNode->rotation = DGN_INT32( psDGN->abyElem + 58 ) / 360000.0;
psNode->origin.x = DGN_INT32( psDGN->abyElem + 62 );
psNode->origin.y = DGN_INT32( psDGN->abyElem + 66 );
}
else
{
/* leave quaternion for later */
psNode->origin.x = DGN_INT32( psDGN->abyElem + 74 );
psNode->origin.y = DGN_INT32( psDGN->abyElem + 78 );
psNode->origin.z = DGN_INT32( psDGN->abyElem + 82 );
}
DGNTransformPoint( psDGN, &(psNode->origin) );
}
break;
case DGNT_GROUP_DATA:
if( nLevel == DGN_GDL_COLOR_TABLE )
{
psElement = DGNParseColorTable( psDGN );
}
else
{
psElement = (DGNElemCore *) CPLCalloc(sizeof(DGNElemCore),1);
psElement->stype = DGNST_CORE;
DGNParseCore( psDGN, psElement );
}
break;
case DGNT_ELLIPSE:
{
DGNElemArc *psEllipse;
psEllipse = (DGNElemArc *) CPLCalloc(sizeof(DGNElemArc),1);
psElement = (DGNElemCore *) psEllipse;
psElement->stype = DGNST_ARC;
DGNParseCore( psDGN, psElement );
memcpy( &(psEllipse->primary_axis), psDGN->abyElem + 36, 8 );
DGN2IEEEDouble( &(psEllipse->primary_axis) );
psEllipse->primary_axis *= psDGN->scale;
memcpy( &(psEllipse->secondary_axis), psDGN->abyElem + 44, 8 );
DGN2IEEEDouble( &(psEllipse->secondary_axis) );
psEllipse->secondary_axis *= psDGN->scale;
if( psDGN->dimension == 2 )
{
psEllipse->rotation = DGN_INT32( psDGN->abyElem + 52 );
psEllipse->rotation = psEllipse->rotation / 360000.0;
memcpy( &(psEllipse->origin.x), psDGN->abyElem + 56, 8 );
DGN2IEEEDouble( &(psEllipse->origin.x) );
memcpy( &(psEllipse->origin.y), psDGN->abyElem + 64, 8 );
DGN2IEEEDouble( &(psEllipse->origin.y) );
}
else
{
/* leave quaternion for later */
memcpy( &(psEllipse->origin.x), psDGN->abyElem + 68, 8 );
DGN2IEEEDouble( &(psEllipse->origin.x) );
memcpy( &(psEllipse->origin.y), psDGN->abyElem + 76, 8 );
DGN2IEEEDouble( &(psEllipse->origin.y) );
memcpy( &(psEllipse->origin.z), psDGN->abyElem + 84, 8 );
DGN2IEEEDouble( &(psEllipse->origin.z) );
psEllipse->quat[0] = DGN_INT32( psDGN->abyElem + 52 );
psEllipse->quat[1] = DGN_INT32( psDGN->abyElem + 56 );
psEllipse->quat[2] = DGN_INT32( psDGN->abyElem + 60 );
psEllipse->quat[3] = DGN_INT32( psDGN->abyElem + 64 );
}
DGNTransformPoint( psDGN, &(psEllipse->origin) );
psEllipse->startang = 0.0;
psEllipse->sweepang = 360.0;
}
break;
case DGNT_ARC:
{
DGNElemArc *psEllipse;
GInt32 nSweepVal;
psEllipse = (DGNElemArc *) CPLCalloc(sizeof(DGNElemArc),1);
psElement = (DGNElemCore *) psEllipse;
psElement->stype = DGNST_ARC;
DGNParseCore( psDGN, psElement );
psEllipse->startang = DGN_INT32( psDGN->abyElem + 36 );
psEllipse->startang = psEllipse->startang / 360000.0;
if( psDGN->abyElem[41] & 0x80 )
{
psDGN->abyElem[41] &= 0x7f;
nSweepVal = -1 * DGN_INT32( psDGN->abyElem + 40 );
}
else
nSweepVal = DGN_INT32( psDGN->abyElem + 40 );
if( nSweepVal == 0 )
psEllipse->sweepang = 360.0;
else
psEllipse->sweepang = nSweepVal / 360000.0;
memcpy( &(psEllipse->primary_axis), psDGN->abyElem + 44, 8 );
DGN2IEEEDouble( &(psEllipse->primary_axis) );
psEllipse->primary_axis *= psDGN->scale;
memcpy( &(psEllipse->secondary_axis), psDGN->abyElem + 52, 8 );
DGN2IEEEDouble( &(psEllipse->secondary_axis) );
psEllipse->secondary_axis *= psDGN->scale;
if( psDGN->dimension == 2 )
{
psEllipse->rotation = DGN_INT32( psDGN->abyElem + 60 );
psEllipse->rotation = psEllipse->rotation / 360000.0;
memcpy( &(psEllipse->origin.x), psDGN->abyElem + 64, 8 );
DGN2IEEEDouble( &(psEllipse->origin.x) );
memcpy( &(psEllipse->origin.y), psDGN->abyElem + 72, 8 );
DGN2IEEEDouble( &(psEllipse->origin.y) );
}
else
{
/* for now we don't try to handle quaternion */
psEllipse->rotation = 0;
memcpy( &(psEllipse->origin.x), psDGN->abyElem + 76, 8 );
DGN2IEEEDouble( &(psEllipse->origin.x) );
memcpy( &(psEllipse->origin.y), psDGN->abyElem + 84, 8 );
DGN2IEEEDouble( &(psEllipse->origin.y) );
memcpy( &(psEllipse->origin.z), psDGN->abyElem + 92, 8 );
DGN2IEEEDouble( &(psEllipse->origin.z) );
psEllipse->quat[0] = DGN_INT32( psDGN->abyElem + 60 );
psEllipse->quat[1] = DGN_INT32( psDGN->abyElem + 64 );
psEllipse->quat[2] = DGN_INT32( psDGN->abyElem + 68 );
psEllipse->quat[3] = DGN_INT32( psDGN->abyElem + 72 );
}
DGNTransformPoint( psDGN, &(psEllipse->origin) );
}
break;
case DGNT_TEXT:
{
DGNElemText *psText;
int num_chars, text_off;
if( psDGN->dimension == 2 )
num_chars = psDGN->abyElem[58];
else
num_chars = psDGN->abyElem[74];
psText = (DGNElemText *) CPLCalloc(sizeof(DGNElemText)+num_chars,1);
psElement = (DGNElemCore *) psText;
psElement->stype = DGNST_TEXT;
DGNParseCore( psDGN, psElement );
psText->font_id = psDGN->abyElem[36];
psText->justification = psDGN->abyElem[37];
psText->length_mult = (DGN_INT32( psDGN->abyElem + 38 ))
* psDGN->scale * 6.0 / 1000.0;
psText->height_mult = (DGN_INT32( psDGN->abyElem + 42 ))
* psDGN->scale * 6.0 / 1000.0;
if( psDGN->dimension == 2 )
{
psText->rotation = DGN_INT32( psDGN->abyElem + 46 );
psText->rotation = psText->rotation / 360000.0;
psText->origin.x = DGN_INT32( psDGN->abyElem + 50 );
psText->origin.y = DGN_INT32( psDGN->abyElem + 54 );
text_off = 60;
}
else
{
/* leave quaternion for later */
psText->origin.x = DGN_INT32( psDGN->abyElem + 62 );
psText->origin.y = DGN_INT32( psDGN->abyElem + 66 );
psText->origin.z = DGN_INT32( psDGN->abyElem + 70 );
text_off = 76;
}
DGNTransformPoint( psDGN, &(psText->origin) );
/* experimental multibyte support from Ason Kang ([email protected])*/
if (*(psDGN->abyElem + text_off) == 0xFF
&& *(psDGN->abyElem + text_off + 1) == 0xFD)
{
int n=0;
for (int i=0;i<num_chars/2-1;i++) {
unsigned short w;
memcpy(&w,psDGN->abyElem + text_off + 2 + i*2 ,2);
w = CPL_LSBWORD16(w);
if (w<256) { // if alpa-numeric code area : Normal character
*(psText->string + n) = (char) (w & 0xFF);
n++; // skip 1 byte;
}
else { // if extend code area : 2 byte Korean character
*(psText->string + n) = (char) (w >> 8); // hi
*(psText->string + n + 1) = (char) (w & 0xFF); // lo
n+=2; // 2 byte
}
}
psText->string[n] = '\0'; // terminate C string
}
else
{
memcpy( psText->string, psDGN->abyElem + text_off, num_chars );
psText->string[num_chars] = '\0';
}
}
break;
case DGNT_TCB:
psElement = DGNParseTCB( psDGN );
break;
case DGNT_EQP_ELEM:
psElement = DGNParseEqp( psDGN );
break;
case DGNT_COMPLEX_CHAIN_HEADER:
case DGNT_COMPLEX_SHAPE_HEADER:
{
DGNElemComplexHeader *psHdr;
psHdr = (DGNElemComplexHeader *)
CPLCalloc(sizeof(DGNElemComplexHeader),1);
psElement = (DGNElemCore *) psHdr;
psElement->stype = DGNST_COMPLEX_HEADER;
DGNParseCore( psDGN, psElement );
psHdr->totlength = psDGN->abyElem[36] + psDGN->abyElem[37] * 256;
psHdr->numelems = psDGN->abyElem[38] + psDGN->abyElem[39] * 256;
}
break;
case DGNT_TAG_VALUE:
{
DGNElemTagValue *psTag;
psTag = (DGNElemTagValue *)
CPLCalloc(sizeof(DGNElemTagValue),1);
psElement = (DGNElemCore *) psTag;
psElement->stype = DGNST_TAG_VALUE;
DGNParseCore( psDGN, psElement );
psTag->tagType = psDGN->abyElem[74] + psDGN->abyElem[75] * 256;
memcpy( &(psTag->tagSet), psDGN->abyElem + 68, 4 );
psTag->tagSet = CPL_LSBWORD32(psTag->tagSet);
psTag->tagIndex = psDGN->abyElem[72] + psDGN->abyElem[73] * 256;
psTag->tagLength = psDGN->abyElem[150] + psDGN->abyElem[151] * 256;
if( psTag->tagType == 1 )
{
psTag->tagValue.string =
CPLStrdup( (char *) psDGN->abyElem + 154 );
}
else if( psTag->tagType == 3 )
{
memcpy( &(psTag->tagValue.integer),
psDGN->abyElem + 154, 4 );
psTag->tagValue.integer =
CPL_LSBWORD32( psTag->tagValue.integer );
}
else if( psTag->tagType == 4 )
{
memcpy( &(psTag->tagValue.real),
psDGN->abyElem + 154, 8 );
DGN2IEEEDouble( &(psTag->tagValue.real) );
}
}
break;
case DGNT_APPLICATION_ELEM:
if( nLevel == 24 )
{
psElement = DGNParseTagSet( psDGN );
}
else
{
psElement = (DGNElemCore *) CPLCalloc(sizeof(DGNElemCore),1);
psElement->stype = DGNST_CORE;
DGNParseCore( psDGN, psElement );
}
break;
case DGNT_CONE:
{
DGNElemCone *psCone;
psCone = (DGNElemCone *) CPLCalloc(sizeof(DGNElemCone),1);
psElement = (DGNElemCore *) psCone;
psElement->stype = DGNST_CONE;
DGNParseCore( psDGN, psElement );
CPLAssert( psDGN->dimension == 3 );
psCone->unknown = psDGN->abyElem[36] + psDGN->abyElem[37] * 256;
psCone->quat[0] = DGN_INT32( psDGN->abyElem + 38 );
psCone->quat[1] = DGN_INT32( psDGN->abyElem + 42 );
psCone->quat[2] = DGN_INT32( psDGN->abyElem + 46 );
psCone->quat[3] = DGN_INT32( psDGN->abyElem + 50 );
memcpy( &(psCone->center_1.x), psDGN->abyElem + 54, 8 );
DGN2IEEEDouble( &(psCone->center_1.x) );
memcpy( &(psCone->center_1.y), psDGN->abyElem + 62, 8 );
DGN2IEEEDouble( &(psCone->center_1.y) );
memcpy( &(psCone->center_1.z), psDGN->abyElem + 70, 8 );
DGN2IEEEDouble( &(psCone->center_1.z) );
memcpy( &(psCone->radius_1), psDGN->abyElem + 78, 8 );
DGN2IEEEDouble( &(psCone->radius_1) );