-
Notifications
You must be signed in to change notification settings - Fork 4
/
dgnwrite.cpp
2579 lines (2180 loc) · 98 KB
/
dgnwrite.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: dgnwrite.cpp,v 1.33 2006/01/25 18:43:19 kintel Exp $
*
* Project: Microstation DGN Access Library
* Purpose: DGN Access functions related to writing DGN elements.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 2002, Frank Warmerdam <[email protected]>
*
* 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: dgnwrite.cpp,v $
* Revision 1.33 2006/01/25 18:43:19 kintel
* B-Spline curve and surface support
*
* Revision 1.27 2005/12/19 15:42:17 fwarmerdam
* Fixed maximum number of points test in DGNCreateMultiPointElem()
* per email from Chris @ melbpc.
*
* Revision 1.26 2005/12/19 15:39:00 fwarmerdam
* Two fixes for uor handling in DGNCreate() as per Chris @melbpc's email.
*
* Revision 1.25 2005/12/18 22:12:49 kintel
* Fix for cloning DGNST_CONE
*
* Revision 1.24 2005/11/18 22:45:01 fwarmerdam
* Avoid use of round(), not available on win32.
*
* Revision 1.23 2005/11/18 17:35:48 fwarmerdam
* Use rounding to improve calculation of text parameters in
* DGNCreateTextElem() as per bug:
* http://bugzilla.maptools.org/show_bug.cgi?id=1203
*
* Revision 1.22 2005/11/18 17:16:35 fwarmerdam
* added TextNode implementation from Ilya Beylin
*
* Revision 1.21 2005/06/22 16:18:38 fwarmerdam
* Fixed from JeffGIS for cloning DGNST_TAG_SET properly.
*
* Revision 1.20 2004/03/23 16:17:12 warmerda
* Corrected level setting of cell header groups, and also fixed total length
* of cell groups.
*
* Revision 1.19 2004/03/23 16:06:27 warmerda
* Fixed up complex header attribute handling as complained about by EDG.
*
* Revision 1.18 2003/11/25 15:47:56 warmerda
* Added surface type for complex headers: Marius
*
* Revision 1.17 2003/11/21 16:17:33 warmerda
* fix missing handling of min/max Z in DGNCreateMultiPointElem()
*
* Revision 1.16 2003/11/21 14:43:59 warmerda
* removed extra bounds transformation in DGNCreateCellHeaderFromGroup()
*
* Revision 1.15 2003/11/19 17:37:00 warmerda
* Fixes from Marius: use ElemTypeHasDispHdr, and cosmetic
*
* Revision 1.14 2003/11/05 17:17:38 warmerda
* use DGNLoadTCB() to ensure scaling setup
*
* Revision 1.13 2003/08/19 20:17:15 warmerda
* Implemented DGNCreateConeElem(). Extended DGNCreateComplex*() to
* support 3D surface and 3D solid elements.
* Marius Kintel
*
* Revision 1.12 2003/06/27 14:50:53 warmerda
* avoid warnings
*
* Revision 1.11 2003/06/12 17:33:17 warmerda
* improved DNGO_CAPTURE_RAW_DATA flag documnentation
*
* Revision 1.10 2003/05/21 03:42:01 warmerda
* Expanded tabs
*
* Revision 1.9 2003/05/15 14:47:24 warmerda
* implement quaternion support on write
*
* Revision 1.8 2003/05/12 18:48:57 warmerda
* added preliminary 3D write support
*
* Revision 1.7 2003/02/06 16:53:48 warmerda
* Improved text bounding box calcuation from Mart Kelder.
*
* Revision 1.6 2003/01/20 20:06:55 warmerda
* added cell header creation
*
* Revision 1.5 2002/11/13 21:26:32 warmerda
* added more documentation
*
* Revision 1.4 2002/11/12 19:45:54 warmerda
* added support to update TCB in DGNCreate
*
* Revision 1.3 2002/11/11 20:38:43 warmerda
* implemented write api
*
* Revision 1.2 2002/10/20 01:50:20 warmerda
* added new write prototypes
*
* Revision 1.1 2002/03/14 21:40:43 warmerda
* New
*
*/
#include "dgnlibp.h"
CPL_CVSID("$Id: dgnwrite.cpp,v 1.33 2006/01/25 18:43:19 kintel Exp $");
static void DGNPointToInt( DGNInfo *psDGN, DGNPoint *psPoint,
unsigned char *pabyTarget );
/************************************************************************/
/* DGNResizeElement() */
/************************************************************************/
/**
* Resize an existing element.
*
* If the new size is the same as the old nothing happens.
*
* Otherwise, the old element in the file is marked as deleted, and the
* DGNElemCore.offset and element_id are set to -1 indicating that the
* element should be written to the end of file when next written by
* DGNWriteElement(). The internal raw data buffer is updated to the new
* size.
*
* Only elements with "raw_data" loaded may be moved.
*
* In normal use the DGNResizeElement() call would be called on a previously
* loaded element, and afterwards the raw_data would be updated before calling
* DGNWriteElement(). If DGNWriteElement() isn't called after
* DGNResizeElement() then the element will be lost having been marked as
* deleted in it's old position but never written at the new location.
*
* @param hDGN the DGN file on which the element lives.
* @param psElement the element to alter.
* @param nNewSize the desired new size of the element in bytes. Must be
* a multiple of 2.
*
* @return TRUE on success, or FALSE on error.
*/
int DGNResizeElement( DGNHandle hDGN, DGNElemCore *psElement, int nNewSize )
{
DGNInfo *psDGN = (DGNInfo *) hDGN;
/* -------------------------------------------------------------------- */
/* Check various conditions. */
/* -------------------------------------------------------------------- */
if( psElement->raw_bytes == 0
|| psElement->raw_bytes != psElement->size )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Raw bytes not loaded, or not matching element size." );
return FALSE;
}
if( nNewSize % 2 == 1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"DGNResizeElement(%d): "
"can't change to odd (not divisible by two) size.",
nNewSize );
return FALSE;
}
if( nNewSize == psElement->raw_bytes )
return TRUE;
/* -------------------------------------------------------------------- */
/* Mark the existing element as deleted if the element has to */
/* move to the end of the file. */
/* -------------------------------------------------------------------- */
if( psElement->offset != -1 )
{
int nOldFLoc = VSIFTell( psDGN->fp );
unsigned char abyLeader[2];
if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0
|| VSIFRead( abyLeader, sizeof(abyLeader), 1, psDGN->fp ) != 1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Failed seek or read when trying to mark existing\n"
"element as deleted in DGNResizeElement()\n" );
return FALSE;
}
abyLeader[1] |= 0x80;
if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0
|| VSIFWrite( abyLeader, sizeof(abyLeader), 1, psDGN->fp ) != 1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Failed seek or write when trying to mark existing\n"
"element as deleted in DGNResizeElement()\n" );
return FALSE;
}
VSIFSeek( psDGN->fp, SEEK_SET, nOldFLoc );
if( psElement->element_id != -1 && psDGN->index_built )
psDGN->element_index[psElement->element_id].flags
|= DGNEIF_DELETED;
}
psElement->offset = -1; /* move to end of file. */
psElement->element_id = -1;
/* -------------------------------------------------------------------- */
/* Set the new size information, and realloc the raw data buffer. */
/* -------------------------------------------------------------------- */
psElement->size = nNewSize;
psElement->raw_data = (unsigned char *)
CPLRealloc( psElement->raw_data, nNewSize );
psElement->raw_bytes = nNewSize;
/* -------------------------------------------------------------------- */
/* Update the size information within the raw buffer. */
/* -------------------------------------------------------------------- */
int nWords = (nNewSize / 2) - 2;
psElement->raw_data[2] = (unsigned char) (nWords % 256);
psElement->raw_data[3] = (unsigned char) (nWords / 256);
return TRUE;
}
/************************************************************************/
/* DGNWriteElement() */
/************************************************************************/
/**
* Write element to file.
*
* Only elements with "raw_data" loaded may be written. This should
* include elements created with the various DGNCreate*() functions, and
* those read from the file with the DGNO_CAPTURE_RAW_DATA flag turned on
* with DGNSetOptions().
*
* The passed element is written to the indicated file. If the
* DGNElemCore.offset field is -1 then the element is written at the end of
* the file (and offset/element are reset properly) otherwise the element
* is written back to the location indicated by DGNElemCore.offset.
*
* If the element is added at the end of the file, and if an element index
* has already been built, it will be updated to reference the new element.
*
* This function takes care of ensuring that the end-of-file marker is
* maintained after the last element.
*
* @param hDGN the file to write the element to.
* @param psElement the element to write.
*
* @return TRUE on success or FALSE in case of failure.
*/
int DGNWriteElement( DGNHandle hDGN, DGNElemCore *psElement )
{
DGNInfo *psDGN = (DGNInfo *) hDGN;
/* ==================================================================== */
/* If this element hasn't been positioned yet, place it at the */
/* end of the file. */
/* ==================================================================== */
if( psElement->offset == -1 )
{
int nJunk;
// We must have an index, in order to properly assign the
// element id of the newly written element. Ensure it is built.
if( !psDGN->index_built )
DGNBuildIndex( psDGN );
// Read the current "last" element.
if( !DGNGotoElement( hDGN, psDGN->element_count-1 ) )
return FALSE;
if( !DGNLoadRawElement( psDGN, &nJunk, &nJunk ) )
return FALSE;
// Establish the position of the new element.
psElement->offset = VSIFTell( psDGN->fp );
psElement->element_id = psDGN->element_count;
// Grow element buffer if needed.
if( psDGN->element_count == psDGN->max_element_count )
{
psDGN->max_element_count += 500;
psDGN->element_index = (DGNElementInfo *)
CPLRealloc( psDGN->element_index,
psDGN->max_element_count * sizeof(DGNElementInfo));
}
// Set up the element info
DGNElementInfo *psInfo;
psInfo = psDGN->element_index + psDGN->element_count;
psInfo->level = (unsigned char) psElement->level;
psInfo->type = (unsigned char) psElement->type;
psInfo->stype = (unsigned char) psElement->stype;
psInfo->offset = psElement->offset;
if( psElement->complex )
psInfo->flags = DGNEIF_COMPLEX;
else
psInfo->flags = 0;
psDGN->element_count++;
}
/* -------------------------------------------------------------------- */
/* Write out the element. */
/* -------------------------------------------------------------------- */
if( VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ) != 0
|| VSIFWrite( psElement->raw_data, psElement->raw_bytes,
1, psDGN->fp) != 1 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Error seeking or writing new element of %d bytes at %d.",
psElement->offset,
psElement->raw_bytes );
printf("\n\nthe seek = %d\n\n\nwr = %d\n\n",VSIFSeek( psDGN->fp, psElement->offset, SEEK_SET ),VSIFWrite( psElement->raw_data, psElement->raw_bytes, 1, psDGN->fp));
return FALSE;
}
psDGN->next_element_id = psElement->element_id + 1;
/* -------------------------------------------------------------------- */
/* Write out the end of file 0xffff marker (if we were */
/* extending the file), but push the file pointer back before */
/* this EOF when done. */
/* -------------------------------------------------------------------- */
if( psDGN->next_element_id == psDGN->element_count )
{
unsigned char abyEOF[2];
abyEOF[0] = 0xff;
abyEOF[1] = 0xff;
VSIFWrite( abyEOF, 2, 1, psDGN->fp );
VSIFSeek( psDGN->fp, -2, SEEK_CUR );
}
return TRUE;
}
/************************************************************************/
/* DGNCreate() */
/************************************************************************/
/**
* Create new DGN file.
*
* This function will create a new DGN file based on the provided seed
* file, and return a handle on which elements may be read and written.
*
* The following creation flags may be passed:
* <ul>
* <li> DGNCF_USE_SEED_UNITS: The master and subunit resolutions and names
* from the seed file will be used in the new file. The nMasterUnitPerSubUnit,
* nUORPerSubUnit, pszMasterUnits, and pszSubUnits arguments will be ignored.
* <li> DGNCF_USE_SEED_ORIGIN: The origin from the seed file will be used
* and the X, Y and Z origin passed into the call will be ignored.
* <li> DGNCF_COPY_SEED_FILE_COLOR_TABLE: Should the first color table occuring
* in the seed file also be copied?
* <li> DGNCF_COPY_WHOLE_SEED_FILE: By default only the first three elements
* (TCB, Digitizer Setup and Level Symbology) are copied from the seed file.
* If this flag is provided the entire seed file is copied verbatim (with the
* TCB origin and units possibly updated).
* </ul>
*
* @param pszNewFilename the filename to create. If it already exists
* it will be overwritten.
* @param pszSeedFile the seed file to copy header from.
* @param nCreationFlags An ORing of DGNCF_* flags that are to take effect.
* @param dfOriginX the X origin for the file.
* @param dfOriginY the Y origin for the file.
* @param dfOriginZ the Z origin for the file.
* @param nSubUnitPerMasterUnit the number of subunits in one master unit.
* @param nUORPerSubUnit the number of UOR (units of resolution) per subunit.
* @param pszMasterUnits the name of the master units (2 characters).
* @param pszSubUnits the name of the subunits (2 characters).
*/
DGNHandle
DGNCreate( const char *pszNewFilename, const char *pszSeedFile,
int nCreationFlags,
double dfOriginX, double dfOriginY, double dfOriginZ,
int nSubUnitsPerMasterUnit, int nUORPerSubUnit,
const char *pszMasterUnits, const char *pszSubUnits )
{
DGNInfo *psSeed, *psDGN;
FILE *fpNew;
DGNElemCore *psSrcTCB;
/* -------------------------------------------------------------------- */
/* Open seed file, and read TCB element. */
/* -------------------------------------------------------------------- */
psSeed = (DGNInfo *) DGNOpen( pszSeedFile, FALSE );
if( psSeed == NULL )
return NULL;
DGNSetOptions( psSeed, DGNO_CAPTURE_RAW_DATA );
psSrcTCB = DGNReadElement( psSeed );
CPLAssert( psSrcTCB->raw_bytes >= 1536 );
/* -------------------------------------------------------------------- */
/* Open output file. */
/* -------------------------------------------------------------------- */
fpNew = VSIFOpen( pszNewFilename, "wb" );
if( fpNew == NULL )
{
CPLError( CE_Failure, CPLE_OpenFailed,
"Failed to open output file: %s", pszNewFilename );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Modify TCB appropriately for the output file. */
/* -------------------------------------------------------------------- */
GByte *pabyRawTCB = (GByte *) CPLMalloc(psSrcTCB->raw_bytes);
memcpy( pabyRawTCB, psSrcTCB->raw_data, psSrcTCB->raw_bytes );
if( !(nCreationFlags & DGNCF_USE_SEED_UNITS) )
{
memcpy( pabyRawTCB+1120, pszMasterUnits, 2 );
memcpy( pabyRawTCB+1122, pszSubUnits, 2 );
DGN_WRITE_INT32( nUORPerSubUnit, pabyRawTCB+1116 );
DGN_WRITE_INT32( nSubUnitsPerMasterUnit,pabyRawTCB+1112);
}
else
{
nUORPerSubUnit = DGN_INT32( pabyRawTCB+1116 );
nSubUnitsPerMasterUnit = DGN_INT32( pabyRawTCB+1112 );
}
if( !(nCreationFlags & DGNCF_USE_SEED_ORIGIN) )
{
dfOriginX *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);
dfOriginY *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);
dfOriginZ *= (nUORPerSubUnit * nSubUnitsPerMasterUnit);
memcpy( pabyRawTCB+1240, &dfOriginX, 8 );
memcpy( pabyRawTCB+1248, &dfOriginY, 8 );
memcpy( pabyRawTCB+1256, &dfOriginZ, 8 );
IEEE2DGNDouble( pabyRawTCB+1240 );
IEEE2DGNDouble( pabyRawTCB+1248 );
IEEE2DGNDouble( pabyRawTCB+1256 );
}
/* -------------------------------------------------------------------- */
/* Write TCB and EOF to new file. */
/* -------------------------------------------------------------------- */
unsigned char abyEOF[2];
VSIFWrite( pabyRawTCB, psSrcTCB->raw_bytes, 1, fpNew );
CPLFree( pabyRawTCB );
abyEOF[0] = 0xff;
abyEOF[1] = 0xff;
VSIFWrite( abyEOF, 2, 1, fpNew );
DGNFreeElement( psSeed, psSrcTCB );
/* -------------------------------------------------------------------- */
/* Close and re-open using DGN API. */
/* -------------------------------------------------------------------- */
VSIFClose( fpNew );
psDGN = (DGNInfo *) DGNOpen( pszNewFilename, TRUE );
/* -------------------------------------------------------------------- */
/* Now copy over elements according to options in effect. */
/* -------------------------------------------------------------------- */
DGNElemCore *psSrcElement, *psDstElement;
while( (psSrcElement = DGNReadElement( psSeed )) != NULL )
{
if( (nCreationFlags & DGNCF_COPY_WHOLE_SEED_FILE)
|| (psSrcElement->stype == DGNST_COLORTABLE
&& nCreationFlags & DGNCF_COPY_SEED_FILE_COLOR_TABLE)
|| psSrcElement->element_id <= 2 )
{
psDstElement = DGNCloneElement( psSeed, psDGN, psSrcElement );
DGNWriteElement( psDGN, psDstElement );
DGNFreeElement( psDGN, psDstElement );
}
DGNFreeElement( psSeed, psSrcElement );
}
DGNClose( psSeed );
return psDGN;
}
/************************************************************************/
/* DGNCloneElement() */
/************************************************************************/
/**
* Clone a retargetted element.
*
* Creates a copy of an element in a suitable form to write to a
* different file than that it was read from.
*
* NOTE: At this time the clone operation will fail if the source
* and destination file have a different origin or master/sub units.
*
* @param hDGNSrc the source file (from which psSrcElement was read).
* @param hDGNDst the destination file (to which the returned element may be
* written).
* @param psSrcElement the element to be cloned (from hDGNSrc).
*
* @return NULL on failure, or an appropriately modified copy of
* the source element suitable to write to hDGNDst.
*/
DGNElemCore *DGNCloneElement( DGNHandle hDGNSrc, DGNHandle hDGNDst,
DGNElemCore *psSrcElement )
{
DGNElemCore *psClone = NULL;
DGNLoadTCB( hDGNDst );
/* -------------------------------------------------------------------- */
/* Per structure specific copying. The core is fixed up later. */
/* -------------------------------------------------------------------- */
if( psSrcElement->stype == DGNST_CORE )
{
psClone = (DGNElemCore *) CPLMalloc(sizeof(DGNElemCore));
memcpy( psClone, psSrcElement, sizeof(DGNElemCore) );
}
else if( psSrcElement->stype == DGNST_MULTIPOINT )
{
DGNElemMultiPoint *psMP, *psSrcMP;
int nSize;
psSrcMP = (DGNElemMultiPoint *) psSrcElement;
nSize = sizeof(DGNElemMultiPoint)
+ sizeof(DGNPoint) * (psSrcMP->num_vertices-2);
psMP = (DGNElemMultiPoint *) CPLMalloc( nSize );
memcpy( psMP, psSrcElement, nSize );
psClone = (DGNElemCore *) psMP;
}
else if( psSrcElement->stype == DGNST_ARC )
{
DGNElemArc *psArc;
psArc = (DGNElemArc *) CPLMalloc(sizeof(DGNElemArc));
memcpy( psArc, psSrcElement, sizeof(DGNElemArc) );
psClone = (DGNElemCore *) psArc;
}
else if( psSrcElement->stype == DGNST_TEXT )
{
DGNElemText *psText, *psSrcText;
int nSize;
psSrcText = (DGNElemText *) psSrcElement;
nSize = sizeof(DGNElemText) + strlen(psSrcText->string);
psText = (DGNElemText *) CPLMalloc( nSize );
memcpy( psText, psSrcElement, nSize );
psClone = (DGNElemCore *) psText;
}
else if( psSrcElement->stype == DGNST_TEXT_NODE )
{
DGNElemTextNode *psNode;
psNode = (DGNElemTextNode *)
CPLMalloc(sizeof(DGNElemTextNode));
memcpy( psNode, psSrcElement, sizeof(DGNElemTextNode) );
psClone = (DGNElemCore *) psNode;
}
else if( psSrcElement->stype == DGNST_COMPLEX_HEADER )
{
DGNElemComplexHeader *psCH;
psCH = (DGNElemComplexHeader *)
CPLMalloc(sizeof(DGNElemComplexHeader));
memcpy( psCH, psSrcElement, sizeof(DGNElemComplexHeader) );
psClone = (DGNElemCore *) psCH;
}
else if( psSrcElement->stype == DGNST_COLORTABLE )
{
DGNElemColorTable *psCT;
psCT = (DGNElemColorTable *) CPLMalloc(sizeof(DGNElemColorTable));
memcpy( psCT, psSrcElement, sizeof(DGNElemColorTable) );
psClone = (DGNElemCore *) psCT;
}
else if( psSrcElement->stype == DGNST_TCB )
{
DGNElemTCB *psTCB;
psTCB = (DGNElemTCB *) CPLMalloc(sizeof(DGNElemTCB));
memcpy( psTCB, psSrcElement, sizeof(DGNElemTCB) );
psClone = (DGNElemCore *) psTCB;
}
else if( psSrcElement->stype == DGNST_CELL_HEADER )
{
DGNElemCellHeader *psCH;
psCH = (DGNElemCellHeader *) CPLMalloc(sizeof(DGNElemCellHeader));
memcpy( psCH, psSrcElement, sizeof(DGNElemCellHeader) );
psClone = (DGNElemCore *) psCH;
}
else if( psSrcElement->stype == DGNST_CELL_LIBRARY )
{
DGNElemCellLibrary *psCL;
psCL = (DGNElemCellLibrary *) CPLMalloc(sizeof(DGNElemCellLibrary));
memcpy( psCL, psSrcElement, sizeof(DGNElemCellLibrary) );
psClone = (DGNElemCore *) psCL;
}
else if( psSrcElement->stype == DGNST_TAG_VALUE )
{
DGNElemTagValue *psTV;
psTV = (DGNElemTagValue *) CPLMalloc(sizeof(DGNElemTagValue));
memcpy( psTV, psSrcElement, sizeof(DGNElemTagValue) );
if( psTV->tagType == 1 )
psTV->tagValue.string = CPLStrdup( psTV->tagValue.string );
psClone = (DGNElemCore *) psTV;
}
else if( psSrcElement->stype == DGNST_TAG_SET )
{
DGNElemTagSet *psTS;
int iTag;
DGNTagDef *pasTagList;
psTS = (DGNElemTagSet *) CPLMalloc(sizeof(DGNElemTagSet));
memcpy( psTS, psSrcElement, sizeof(DGNElemTagSet) );
psTS->tagSetName = CPLStrdup( psTS->tagSetName );
pasTagList = (DGNTagDef *)
CPLMalloc( sizeof(DGNTagDef) * psTS->tagCount );
memcpy( pasTagList, psTS->tagList,
sizeof(DGNTagDef) * psTS->tagCount );
for( iTag = 0; iTag < psTS->tagCount; iTag++ )
{
pasTagList[iTag].name = CPLStrdup( pasTagList[iTag].name );
pasTagList[iTag].prompt = CPLStrdup( pasTagList[iTag].prompt );
if( pasTagList[iTag].type == 1 )
pasTagList[iTag].defaultValue.string =
CPLStrdup( pasTagList[iTag].defaultValue.string);
}
psTS->tagList = pasTagList;
psClone = (DGNElemCore *) psTS;
}
else if( psSrcElement->stype == DGNST_CONE )
{
DGNElemCone *psCone;
psCone = (DGNElemCone *) CPLMalloc(sizeof(DGNElemCone));
memcpy( psCone, psSrcElement, sizeof(DGNElemCone) );
psClone = (DGNElemCore *) psCone;
}
else if( psSrcElement->stype == DGNST_BSPLINE_SURFACE_HEADER )
{
DGNElemBSplineSurfaceHeader *psSurface;
psSurface = (DGNElemBSplineSurfaceHeader *)
CPLMalloc(sizeof(DGNElemBSplineSurfaceHeader));
memcpy( psSurface, psSrcElement, sizeof(DGNElemBSplineSurfaceHeader) );
psClone = (DGNElemCore *) psSurface;
}
else if( psSrcElement->stype == DGNST_BSPLINE_CURVE_HEADER )
{
DGNElemBSplineCurveHeader *psCurve;
psCurve = (DGNElemBSplineCurveHeader *)
CPLMalloc(sizeof(DGNElemBSplineCurveHeader));
memcpy( psCurve, psSrcElement, sizeof(DGNElemBSplineCurveHeader) );
psClone = (DGNElemCore *) psCurve;
}
else if( psSrcElement->stype == DGNST_BSPLINE_SURFACE_BOUNDARY )
{
DGNElemBSplineSurfaceBoundary *psBSB, *psSrcBSB;
int nSize;
psSrcBSB = (DGNElemBSplineSurfaceBoundary *) psSrcElement;
nSize = sizeof(DGNElemBSplineSurfaceBoundary)
+ sizeof(DGNPoint) * (psSrcBSB->numverts-1);
psBSB = (DGNElemBSplineSurfaceBoundary *) CPLMalloc( nSize );
memcpy( psBSB, psSrcElement, nSize );
psClone = (DGNElemCore *) psBSB;
}
else if( psSrcElement->stype == DGNST_KNOT_WEIGHT )
{
DGNElemKnotWeight *psArray, *psSrcArray;
int nSize, numelems;
// FIXME: Is it OK to assume that the # of elements corresponds
// directly to the element size? kintel 20051218.
numelems = (psSrcElement->size - 36 - psSrcElement->attr_bytes)/4;
psSrcArray = (DGNElemKnotWeight *) psSrcElement;
nSize = sizeof(DGNElemKnotWeight) + sizeof(long) * (numelems-1);
psArray = (DGNElemKnotWeight *) CPLMalloc( nSize );
memcpy( psArray, psSrcElement, nSize );
psClone = (DGNElemCore *) psArray;
}
else if( psSrcElement->stype == DGNST_SHARED_CELL_DEFN )
{
DGNElemSharedCellDefn *psCH;
psCH = (DGNElemSharedCellDefn *)CPLMalloc(sizeof(DGNElemSharedCellDefn));
memcpy( psCH, psSrcElement, sizeof(DGNElemSharedCellDefn) );
psClone = (DGNElemCore *) psCH;
}
else
{
CPLAssert( FALSE );
return NULL;
}
/* -------------------------------------------------------------------- */
/* Copy core raw data, and attributes. */
/* -------------------------------------------------------------------- */
if( psClone->raw_bytes != 0 )
{
psClone->raw_data = (unsigned char *) CPLMalloc(psClone->raw_bytes);
memcpy( psClone->raw_data, psSrcElement->raw_data,
psClone->raw_bytes );
}
if( psClone->attr_bytes != 0 )
{
psClone->attr_data = (unsigned char *) CPLMalloc(psClone->attr_bytes);
memcpy( psClone->attr_data, psSrcElement->attr_data,
psClone->attr_bytes );
}
/* -------------------------------------------------------------------- */
/* Clear location and id information. */
/* -------------------------------------------------------------------- */
psClone->offset = -1;
psClone->element_id = -1;
return psClone;
}
/************************************************************************/
/* DGNUpdateElemCore() */
/************************************************************************/
/**
* Change element core values.
*
* The indicated values in the element are updated in the structure, as well
* as in the raw data. The updated element is not written to disk. That
* must be done with DGNWriteElement(). The element must have raw_data
* loaded.
*
* @param hDGN the file on which the element belongs.
* @param psElement the element to modify.
* @param nLevel the new level value.
* @param nGraphicGroup the new graphic group value.
* @param nColor the new color index.
* @param nWeight the new element weight.
* @param nStyle the new style value for the element.
*
* @return Returns TRUE on success or FALSE on failure.
*/
int DGNUpdateElemCore( DGNHandle hDGN, DGNElemCore *psElement,
int nLevel, int nGraphicGroup, int nColor,
int nWeight, int nStyle )
{
psElement->level = nLevel;
psElement->graphic_group = nGraphicGroup;
psElement->color = nColor;
psElement->weight = nWeight;
psElement->style = nStyle;
return DGNUpdateElemCoreExtended( hDGN, psElement );
}
/************************************************************************/
/* DGNUpdateElemCoreExtended() */
/************************************************************************/
/**
* Update internal raw data representation.
*
* The raw_data representation of the passed element is updated to reflect
* the various core fields. The DGNElemCore level, type, complex, deleted,
* graphic_group, properties, color, weight and style values are all
* applied to the raw_data representation. Spatial bounds, element type
* specific information and attributes are not updated in the raw data.
*
* @param hDGN the file to which the element belongs.
* @param psElement the element to be updated.
*
* @return TRUE on success, or FALSE on failure.
*/
int DGNUpdateElemCoreExtended( DGNHandle hDGN, DGNElemCore *psElement )
{
GByte *rd = psElement->raw_data;
int nWords = (psElement->raw_bytes / 2) - 2;
if( psElement->raw_data == NULL
|| psElement->raw_bytes < 36 )
{
CPLAssert( FALSE );
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Setup first four bytes. */
/* -------------------------------------------------------------------- */
rd[0] = (GByte) psElement->level;
if( psElement->complex )
rd[0] |= 0x80;
rd[1] = (GByte) psElement->type;
if( psElement->deleted )
rd[1] |= 0x80;
rd[2] = (GByte) (nWords % 256);
rd[3] = (GByte) (nWords / 256);
/* -------------------------------------------------------------------- */
/* If the attribute offset hasn't been set, set it now under */
/* the assumption it should point to the end of the element. */
/* -------------------------------------------------------------------- */
if( psElement->raw_data[30] == 0 && psElement->raw_data[31] == 0 )
{
int nAttIndex = (psElement->raw_bytes - 32) / 2;
psElement->raw_data[30] = (GByte) (nAttIndex % 256);
psElement->raw_data[31] = (GByte) (nAttIndex / 256);
}
/* -------------------------------------------------------------------- */
/* Handle the graphic properties. */
/* -------------------------------------------------------------------- */
if( psElement->raw_bytes > 36 && DGNElemTypeHasDispHdr( psElement->type ) )
{
rd[28] = (GByte) (psElement->graphic_group % 256);
rd[29] = (GByte) (psElement->graphic_group / 256);
rd[32] = (GByte) (psElement->properties % 256);
rd[33] = (GByte) (psElement->properties / 256);
rd[34] = (GByte) (psElement->style | (psElement->weight << 3));
rd[35] = (GByte) psElement->color;
}
return TRUE;
}
/************************************************************************/
/* DGNInitializeElemCore() */
/************************************************************************/
static void DGNInitializeElemCore( DGNHandle hDGN, DGNElemCore *psElement )
{
memset( psElement, 0, sizeof(DGNElemCore) );
psElement->offset = -1;
psElement->element_id = -1;
}
/************************************************************************/
/* DGNWriteBounds() */
/* */
/* Write bounds to element raw data. */
/************************************************************************/
static void DGNWriteBounds( DGNInfo *psInfo, DGNElemCore *psElement,
DGNPoint *psMin, DGNPoint *psMax )
{
CPLAssert( psElement->raw_bytes >= 28 );
DGNInverseTransformPointToInt( psInfo, psMin, psElement->raw_data + 4 );
DGNInverseTransformPointToInt( psInfo, psMax, psElement->raw_data + 16 );
/* convert from twos complement to "binary offset" format. */
psElement->raw_data[5] ^= 0x80;
psElement->raw_data[9] ^= 0x80;
psElement->raw_data[13] ^= 0x80;
psElement->raw_data[17] ^= 0x80;
psElement->raw_data[21] ^= 0x80;
psElement->raw_data[25] ^= 0x80;
}
/************************************************************************/
/* DGNCreateMultiPointElem() */
/************************************************************************/
/**
* Create new multi-point element.
*
* The newly created element will still need to be written to file using
* DGNWriteElement(). Also the level and other core values will be defaulted.
* Use DGNUpdateElemCore() on the element before writing to set these values.
*
* NOTE: There are restrictions on the nPointCount for some elements. For
* instance, DGNT_LINE can only have 2 points. Maximum element size
* precludes very large numbers of points.
*
* @param hDGN the file on which the element will eventually be written.
* @param nType the type of the element to be created. It must be one of
* DGNT_LINE, DGNT_LINE_STRING, DGNT_SHAPE, DGNT_CURVE or DGNT_BSPLINE_POLE.
* @param nPointCount the number of points in the pasVertices list.
* @param pasVertices the list of points to be written.
*
* @return the new element (a DGNElemMultiPoint structure) or NULL on failure.
*/
DGNElemCore *DGNCreateMultiPointElem( DGNHandle hDGN, int nType,
int nPointCount, DGNPoint *pasVertices )
{
DGNElemMultiPoint *psMP;
DGNElemCore *psCore;
DGNInfo *psDGN = (DGNInfo *) hDGN;
int i;
DGNPoint sMin, sMax;
CPLAssert( nType == DGNT_LINE
|| nType == DGNT_LINE_STRING
|| nType == DGNT_SHAPE
|| nType == DGNT_CURVE
|| nType == DGNT_BSPLINE_POLE );
DGNLoadTCB( hDGN );
/* -------------------------------------------------------------------- */
/* Is this too many vertices to write to a single element? */
/* -------------------------------------------------------------------- */
if( nPointCount > 101 )
{
CPLError( CE_Failure, CPLE_ElementTooBig,
"Attempt to create %s element with %d points failed.\n"
"Element would be too large.",
DGNTypeToName( nType ), nPointCount );
return NULL;
}