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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
/*******************************************************************************<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
    COPYRIGHT 2002 CATERPILLAR INC.  ALL RIGHTS RESERVED

    This program is the property of Caterpillar Inc., includes Caterpillar's
    confidential and trade secret information, and is maintained as an
    unpublished copyrighted work.  It is not to be copied or used by others
    except under license from Caterpillar.  This program is also protected as an
    unpublished work in accordance with the copyright act of 1976.  In the event
    of either inadvertent or deliberate publication, Caterpillar Inc. intends to
    maintain copyright protection for this work under the relevant copyright
    laws pertaining to published works.  The inclusion of a copyright notice
    hereon is precautionary only, and does not imply publication or disclosure.

 
 Filename      : CCubitFile.cpp

 Purpose       : Implements reading and writing data in the Cubit file format.
           
 Special Notes :

 Creator       : Will A. Helden

 Creation Date : 02/15/02

 Owner         : Will A. Helden

*******************************************************************************/

#include "CCubitFile.hpp"
#include "CubitDefines.h"
#include "CubitFileIOWrapper.hpp"
#include "CubitFileMetaData.hpp"
#include "CubitFileFEModel.hpp"
#include "CubitFileSimModel.hpp"
#include <cstring>
#include "CubitFileUtil.hpp"

using namespace NCubitFile;

///////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////////////////////////////////

/* Note: some platforms define both BIG_ENDIAN and LITTLE_ENDIAN
   so don't do if defined(LITTLE_ENDIAN) here.  Compare to BYTE_ORDER instead. */
#if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || defined(CUBIT_LINUX) || (defined(LITTLE_ENDIAN) && (BYTE_ORDER==LITTLE_ENDIAN)) /* should be little endian platforms */
    const UnsignedInt32 CCubitFile::mintNativeEndian = 0;
#else // big endian platforms
    const UnsignedInt32 CCubitFile::mintNativeEndian = 0xFFFFFFFF;
#endif
    const UnsignedInt32 CCubitFile::mintSizeOfContents =
        sizeof(SCubitFileContentsHeader) / sizeof(UnsignedInt32);
    const UnsignedInt32 CCubitFile::mintSizeOfModel =
        sizeof(SCubitFileModelEntry) / sizeof(UnsignedInt32);

CCubitFile::CCubitFile() <--- Member variable 'CCubitFile::mintWriteTempFile' is not initialized in the constructor.<--- Member variable 'CCubitFile::mintWriteBuffNumModels' is not initialized in the constructor.<--- Member variable 'CCubitFile::mReadContents' is not initialized in the constructor.<--- Member variable 'CCubitFile::mWriteContents' is not initialized in the constructor.<--- Member variable 'CCubitFile::mintFEModelIndex' is not initialized in the constructor.<--- Member variable 'CCubitFile::mintSimModelIndex' is not initialized in the constructor.
{
    meErrorState = eSuccess;
    mintNextModelID = 1;

    mpaReadModels = mpaWriteModels = NULL;
    mpaReadModelStat = NULL;
    mpMetaData = NULL;
    mstrReadFileName = mstrWriteFileName = mstrBackupFileName = NULL;
    mpReadFEModel = mpWriteFEModel = NULL;
    mpReadSimModel = mpWriteSimModel = NULL;

    mModelBuff.mintNumModels = 0;
    mModelBuff.mpaModelData = NULL;
}

CCubitFile::~CCubitFile()
{
    // auto-save?
    // delete incomplete temp files
    FreeAll();

    // Assume if any FE models are still around now (i.e. EndRead/Write hasn't
    // been called) there was an error, so free their memory.
    if(mpReadFEModel)
        delete mpReadFEModel;
    if(mpWriteFEModel)
        delete mpWriteFEModel;
    if(mpReadSimModel)
        delete mpReadSimModel;
    if(mpWriteSimModel)
        delete mpWriteSimModel;

    if(mModelBuff.mpaModelData)
        delete [] mModelBuff.mpaModelData;
}

void CCubitFile::FreeAll()
// Frees all allocated memory, closes open files, and resets class members to
// their constructed state.
{
    // Free all allocated memory.
    if(mstrReadFileName)
        delete [] mstrReadFileName;
    if(mstrWriteFileName)
        delete [] mstrWriteFileName;
    if(mstrBackupFileName)
        delete [] mstrBackupFileName;
    mstrReadFileName = mstrWriteFileName = mstrBackupFileName = NULL;
    if(mpaReadModels)
        delete [] mpaReadModels;
    if(mpaWriteModels)
        delete [] mpaWriteModels;
    mpaReadModels = mpaWriteModels = NULL;
    if(mpaReadModelStat)
        delete [] mpaReadModelStat;
    mpaReadModelStat = NULL;
    if(mpMetaData)
        delete mpMetaData;
    mpMetaData = NULL;

    // Close any open files.
    try {
        if(mpReadFile)
            mpReadFile.close();
        if(mpWriteFile)
            mpWriteFile.close();
    }
    catch(...)  { }
}

CCubitFile::EErrorCode CCubitFile::Open(const char* xstrReadFileName,
                                        const char* xstrWriteFileName,
                                        const char* xstrBackupFileName)
// Open a file for reading or writing.
// === NOTES ===
// *  The read file is a file to be read only.  The file must exist and be
//    accessable or this function will fail.  
// *  The write file is an output file.  The file must be creatable or this
//    function will fail.  If the file already exists, it will be overwritten.
//    If the write file name is the same as the read file name, a temporary
//    write file name will be generated to be written to until the completion
//    of the write operation at which time the read file will be deleted and
//    the write file renamed to take its place.
// *  The backup file name is used for preventing the loss of old read files.
//    If the read file was to be deleted automatically by a write operation,
//    specification of a backup name, will cause the read file to be renamed
//    instead.
// *  Any of these three filenames may be NULL, but at least one name, read or
//    write, must be specified.
{
    try {
        if(!xstrReadFileName && !xstrWriteFileName)
            throw ePassedNullPointer;

        mpMetaData = new CMetaData;

        // If a file has been designated for reading, copy its name and try to
        // open it.
        if(xstrReadFileName ) {
            UnsignedInt32 lintFileName = std::strlen(xstrReadFileName);
            mstrReadFileName = new char[lintFileName + 1];
            std::strcpy(mstrReadFileName, xstrReadFileName);     

            if( !xstrWriteFileName )
            {
              mpReadFile.open(mstrReadFileName, "rb");
              if(!mpReadFile)
                  throw eFileReadError;

              // Test to see if the file is a cubit file... it should begin with
              // the string 'CUBE'.
              unsigned char lachrMagic[4];
              if(fread(lachrMagic, 1, 4, mpReadFile.file()) != 4)
                  throw eFileUnrecognizedFormat;
              if((lachrMagic[0] != 'C') || (lachrMagic[1] != 'U') ||
                  (lachrMagic[2] != 'B') || (lachrMagic[3] != 'E'))
                  throw eFileUnrecognizedFormat;
              
              // Load the file's "table of contents".
              CIOWrapper lIO(mpReadFile.file(), 4, 0);
              lIO.BeginReadBlock(4);
              lIO.Read((UnsignedInt32*)&mReadContents, mintSizeOfContents);
              lIO.EndReadBlock();
              
              if(mReadContents.mintHeaderSchema > 0)
                  throw eFileUnrecognizedFormat;
              
              if(mReadContents.mintNumModels) {
                  mpaReadModels =
                      new SCubitFileModelEntry[mReadContents.mintNumModels];
                  mpaReadModelStat = new EModelStat[mReadContents.mintNumModels];
                  if(!mpaReadModels || !mpaReadModelStat)  throw eMemoryError;

                  lIO.BeginReadBlock(mReadContents.mintModelTableOffset);
                  lIO.Read((UnsignedInt32*)mpaReadModels,
                      mReadContents.mintNumModels * mintSizeOfModel);
                  lIO.EndReadBlock();

                  UnsignedInt32 lintMod;
                  for(lintMod = 0; lintMod < mReadContents.mintNumModels; lintMod++) {
                      mpaReadModelStat[lintMod] = eStatNotWritten;
                      if(mpaReadModels[lintMod].mintModelHandle >= mintNextModelID)
                          mintNextModelID = mpaReadModels[lintMod].mintModelHandle + 1;
                  }
              }
              
              // Read the file-scoped (model) meta-data.
              mpMetaData->ReadMetaData(mpReadFile.file(),
                  mReadContents.mintModelMetaDataOffset, 0,
                  mReadContents.mintHeaderSourceEndian);
            }
            else
            {
               mReadContents.mintHeaderSourceEndian = mintNativeEndian;
               mReadContents.mintHeaderSchema = 0;
               mReadContents.mintNumModels = 0;
               mReadContents.mintModelTableOffset = 0;
               mReadContents.mintModelMetaDataOffset = 0;
               mReadContents.mintActiveFEModel = 0;

              // Copy the backup file name, if specified.
              if(xstrBackupFileName) {
                  UnsignedInt32 lintFileName = std::strlen(xstrBackupFileName);
                  mstrBackupFileName = new char[lintFileName + 1];
                  std::strcpy(mstrBackupFileName, xstrBackupFileName);
              }
            }
          }
          else {
              // If there wasn't a file to read, initialize the read contents to
              // nothing.
              mReadContents.mintHeaderSourceEndian = mintNativeEndian;
              mReadContents.mintHeaderSchema = 0;
              mReadContents.mintNumModels = 0;
              mReadContents.mintModelTableOffset = 0;
              mReadContents.mintModelMetaDataOffset = 0;
              mReadContents.mintActiveFEModel = 0;
        }
        
        // If a file has been designated for writing
        if(xstrWriteFileName) {
            UnsignedInt32 lintFileName = std::strlen(xstrWriteFileName);
            mstrWriteFileName = new char[lintFileName + 10];

            // If the read and write file names don't match, then the write
            // file can be opened by name.
            if(!mstrReadFileName || std::strcmp(mstrReadFileName, xstrWriteFileName)) {
                mintWriteTempFile = 0;
                std::strcpy(mstrWriteFileName, xstrWriteFileName);       
                mpWriteFile.open(mstrWriteFileName, "wb");
            }
            // Otherwise, generate a temporary file name to write to so that
            // the read file's contents will not be destroyed until it is 
            // verifiable that any new writes are successful.
            else {
                mintWriteTempFile = 1;
                UnsignedInt32 lintTempFile = 0;
                while(!mpWriteFile && (lintTempFile < 0xFF)) {
                    sprintf(mstrWriteFileName, "%s~%.2x.tmp",
                        mstrReadFileName, lintTempFile);
                    mpWriteFile.open(mstrWriteFileName, "wb");
                    lintTempFile++;
                }
            }
            if(!mpWriteFile)
                throw eFileWriteError;
            
            // Initialize the write file contents - copy the model inventory
            // (but not actual contents yet) from the read file.
            mWriteContents.mintHeaderSourceEndian = mintNativeEndian;
            mWriteContents.mintHeaderSchema = 0;
            mWriteContents.mintNumModels = mReadContents.mintNumModels;
            mWriteContents.mintModelTableOffset = 0;
            mWriteContents.mintModelMetaDataOffset = 0;
            mWriteContents.mintActiveFEModel = mReadContents.mintActiveFEModel;
            mintWriteBuffNumModels = 0;

            if(mWriteContents.mintNumModels) {
                mpaWriteModels =
                    new SCubitFileModelEntry[mWriteContents.mintNumModels];
                if(!mpaWriteModels)  throw eMemoryError;

                UnsignedInt32 lintMod;
                for(lintMod = 0; lintMod < mWriteContents.mintNumModels; lintMod++) {
                    mpaWriteModels[lintMod].mintModelHandle =
                        mpaReadModels[lintMod].mintModelHandle;
                    mpaWriteModels[lintMod].mintModelOffset = 0;
                    mpaWriteModels[lintMod].mintModelLength = 0;
                    mpaWriteModels[lintMod].mintModelType =
                        mpaReadModels[lintMod].mintModelType;
                    mpaWriteModels[lintMod].mintModelOwner =
                        mpaReadModels[lintMod].mintModelOwner;
                }
            }

            // Initialize the write file by writing its identity and an initial
            // table of contents.
            CIOWrapper lIO(mpWriteFile.file());
            lIO.BeginWriteBlock(0);
            lIO.Write("CUBE", 4);
            lIO.Write((UnsignedInt32*)&mWriteContents, mintSizeOfContents);
            lIO.EndWriteBlock();

            WriteModelTable();
        }
        else {
            // If there wasn't a file to written, initialize the write contents
            // to nothing.
            mWriteContents.mintHeaderSourceEndian = mintNativeEndian;
            mWriteContents.mintHeaderSchema = 0;
            mWriteContents.mintNumModels = 0;
            mWriteContents.mintModelTableOffset = 0;
            mWriteContents.mintModelMetaDataOffset = 0;
            mWriteContents.mintActiveFEModel = 0;
        }
        
        return eSuccess;
    }

    // Handle all open errors by reseting the file object.
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        FreeAll();
        return xeErrorCode;
    }
    catch(...)  {
        FreeAll();
        return eUnknownError;
    }
}

CCubitFile::EErrorCode CCubitFile::Close()
// Close any open files, completing any pending write operations, and free any
// allocated resources.  See Open() comments for more info.
{
    try {

        if(mpWriteFile) {
            // Write file scoped (model) metadata and update the file's table
            // of contents one last time.
            UnsignedInt32 lintMetaDataLength;
            mpMetaData->WriteMetaData(mpWriteFile.file(),
                mWriteContents.mintModelMetaDataOffset, lintMetaDataLength);
            WriteModelTable();

// throw away erroneously written files?!?

            // If the written file was a temporary file, delete the original
            // file and replace it with the temporary file.
            if(mintWriteTempFile) {             
                // Close any open files.
                mpReadFile.close();
                mpWriteFile.close();

                // If there was a backup name specified for the old read file
                // rename it instead of deleting it.
                if(mstrBackupFileName)
                    CubitFileUtil::rename_file(mstrReadFileName, mstrBackupFileName);
                else
                    CubitFileUtil::remove_file(mstrReadFileName);
                if(CUBIT_SUCCESS != CubitFileUtil::rename_file(mstrWriteFileName, mstrReadFileName))
                    throw eUnknownError;
            }
        }

        // Close any open files and clean up allocated memory.
        FreeAll();
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        FreeAll();
        return xeErrorCode;
    }
    catch(...)  {
        FreeAll();
        return eUnknownError;
    }
}

/*UnsignedInt32 CCubitFile::GetModelList(UnsignedInt32& xintNumModels,
                                       const SModelData*& xpaModels)
// Returns a list of all models contained in the file.
{
    SCubitFileModelEntry* lpaModels = NULL;
    if(mpaWriteModels) {
        lpaModels = mpaWriteModels;
        xintNumModels = mWriteContents.mintNumModels;
    }
    else if(mpaReadModels) {
        lpaModels = mpaReadModels;
        xintNumModels = mReadContents.mintNumModels;
    }

    if(!lpaModels || !xintNumModels) {
        xpaModels = NULL;
        return eNotFound;
    }

    if(mModelBuff.mintNumModels < xintNumModels) {
        mModelBuff.mintNumModels = xintNumModels;
        if(mModelBuff.mpaModelData)
            delete [] mModelBuff.mpaModelData;
        mModelBuff.mpaModelData = new SModelData[xintNumModels];
    }
    xpaModels = mModelBuff.mpaModelData;
    
    for(UnsignedInt32 lintModel = 0; lintModel < xintNumModels; lintModel++) {
        mModelBuff.mpaModelData[lintModel].mintModelHandle =
            lpaModels[lintModel].mintModelHandle;
        mModelBuff.mpaModelData[lintModel].mintModelType =
            lpaModels[lintModel].mintModelType;
        mModelBuff.mpaModelData[lintModel].mintModelOwner =
            lpaModels[lintModel].mintModelOwner;
    }
    return eSuccess;
}*/

UnsignedInt32 CCubitFile::IsModel(HModel xintModel)
{
    UnsignedInt32 lintIndex;
    if(mWriteContents.mintNumModels)
        return FindModel(xintModel, mpaWriteModels, mWriteContents.mintNumModels, lintIndex);
    else if(mReadContents.mintNumModels)
        return FindModel(xintModel, mpaReadModels, mReadContents.mintNumModels, lintIndex);
    return 0;  // failure
}

UnsignedInt32 CCubitFile::GetReadModelLength(HModel xintModel,
                                             UnsignedInt32& xintLength)
// Looks up the length of the data block in the cubit filr for the requested
// model.  Returns success if the model is found, otherwise an error is returned.
{
    UnsignedInt32 lintIndex;
    if(FindModel(xintModel, mpaReadModels, mReadContents.mintNumModels, lintIndex)) {
        xintLength = mpaReadModels[lintIndex].mintModelLength;
        return eSuccess;
    }
    else {
        xintLength = 0;
        return eNotFound;
    }
}

UnsignedInt32 CCubitFile::CreateModel(EModelType xeModelType, HModel& xintModel)
// Define a new model in the temp file.
{
    if(!mpWriteFile)  return eFileWriteError;

    // Allocate a new bigger model table, copy the existing table (if any)
    // into the new one and then replace the old one.
    SCubitFileModelEntry* lpaModels =
        new SCubitFileModelEntry[mWriteContents.mintNumModels + 1];
    if(!lpaModels)  return eMemoryError;
    if(mpaWriteModels) {
        memcpy(lpaModels, mpaWriteModels,
            sizeof(SCubitFileModelEntry) * mWriteContents.mintNumModels);
        delete [] mpaWriteModels;
    }
    mpaWriteModels = lpaModels;
    
    // Initialize the new model's table entry.
    mpaWriteModels[mWriteContents.mintNumModels].mintModelHandle =
        xintModel;// = mintNextModelID++;
    mpaWriteModels[mWriteContents.mintNumModels].mintModelOffset = 0;
    mpaWriteModels[mWriteContents.mintNumModels].mintModelLength = 0;
    mpaWriteModels[mWriteContents.mintNumModels].mintModelType = xeModelType;
    mpaWriteModels[mWriteContents.mintNumModels].mintModelOwner = 0;
    mpaWriteModels[mWriteContents.mintNumModels].mintModel64bitOSPad = 0;
    mWriteContents.mintNumModels++;
    
    return eSuccess;
}

UnsignedInt32 CCubitFile::DeleteModel(HModel xintModel)
// Flag an model that exists in the read file not to be copied into the write file.
{
    if(!mpWriteFile)  return eFileWriteError;

    // Flag the model in the old file as marked for deletion.
    UnsignedInt32 lintIndex;
    if(!FindModel(xintModel, mpaReadModels, mReadContents.mintNumModels, lintIndex))
        return eNotFound;
    if(mpaReadModelStat[lintIndex] == eStatWritten)
        return eOrderError;
    mpaReadModelStat[lintIndex] = eStatDelete;

    // Remove the model from the write file's table and close up the position in
    // the model table (if applicable).
    if(FindModel(xintModel, mpaWriteModels, mWriteContents.mintNumModels, lintIndex)) {
        mWriteContents.mintNumModels--;
        if(mWriteContents.mintNumModels &&
            (lintIndex < mWriteContents.mintNumModels)) {
            for(UnsignedInt32 lintCopyTo = lintIndex; lintCopyTo <
                mWriteContents.mintNumModels; lintCopyTo++) {
                memcpy(&mpaWriteModels[lintCopyTo],
                    &mpaWriteModels[lintCopyTo + 1],
                    sizeof(SCubitFileModelEntry));
            }
        }
    }

    // Find any models that have the deleted model as their owner and reset
    // the owner to no owner (0).
    if(mWriteContents.mintNumModels) {
        for(UnsignedInt32 lintModel = 0; lintModel <
            mWriteContents.mintNumModels; lintModel++) {
            if(mpaWriteModels[lintModel].mintModelOwner == xintModel)
                mpaWriteModels[lintModel].mintModelOwner = 0;
        }
    }

    // Delete any meta-data that belonged with the deleted model.
    mpMetaData->ClearMetaDataForID(xintModel);

    return eSuccess;
}

UnsignedInt32 CCubitFile::GetModelOwner(HModel xintModel, HModel& xintOwner)
// Return the owning model of the passed model.
{
    // Flag the model in the old file as marked for deletion.
    UnsignedInt32 lintIndex;
    if(!FindModel(xintModel, mpaWriteModels, mWriteContents.mintNumModels, lintIndex))
        return eNotFound;
    xintOwner = mpaWriteModels[lintIndex].mintModelOwner;
    return eSuccess;
}

UnsignedInt32 CCubitFile::SetModelOwner(HModel xintModel, HModel xintOwner)
// Set the owning model for the passed model.
{
    // Flag the model in the old file as marked for deletion.
    UnsignedInt32 lintIndex;
    if(xintOwner &&  // don't allow the model to have a non-existant owner.
        !FindModel(xintOwner, mpaWriteModels, mWriteContents.mintNumModels, lintIndex))
        return eNotFound;
    if(!FindModel(xintModel, mpaWriteModels, mWriteContents.mintNumModels, lintIndex))
        return eNotFound;
    mpaWriteModels[lintIndex].mintModelOwner = xintOwner;
    return eSuccess;
}

UnsignedInt32 CCubitFile::FindModel(HModel xintModel,
                                    SCubitFileModelEntry* xpaModels,
                                    UnsignedInt32 xintNumModels,
                                    UnsignedInt32& xintIndex)
// Determine the index of the passed model handle in the passed model table.
// Private function, return true/false, no exceptions thrown.
{
    if(xintNumModels) {
        for(UnsignedInt32 lintMod = 0; lintMod < xintNumModels; lintMod++) {
            if(xpaModels[lintMod].mintModelHandle == xintModel) {
                xintIndex = lintMod;
                return 1; // success
            }
        }
    }
    return 0; // failure
}

void CCubitFile::WriteModelTable()
// Write (or rewrite) the model table to the write file.
{
    if(!mpWriteFile)  throw eFileWriteError;

    if(!mWriteContents.mintNumModels)  return;  // no models... nothing to do!

    CIOWrapper lIO(mpWriteFile.file());

    // Write the model table, if the number of models has increased, write the
    // table to a new location in the file, otherwise reuse the previous
    // table's file space.
    if(mWriteContents.mintNumModels > mintWriteBuffNumModels) {
        mintWriteBuffNumModels = mWriteContents.mintNumModels;
        mWriteContents.mintModelTableOffset = lIO.BeginWriteBlock();
    }
    else
        lIO.BeginRewriteBlock(mWriteContents.mintModelTableOffset, 0);
    lIO.Write((UnsignedInt32*)mpaWriteModels,
        mWriteContents.mintNumModels * mintSizeOfModel);
    lIO.EndWriteBlock();

    // Rewrite the contents header to reflect any model table changes.
    lIO.BeginRewriteBlock(4, 0);
    lIO.Write((UnsignedInt32*)&mWriteContents, mintSizeOfContents);
    lIO.EndWriteBlock();
}

void CCubitFile::CopyModel(UnsignedInt32 xintReadOffset,
                           UnsignedInt32& xintWriteOffset,
                           UnsignedInt32 xintLength,
                           FILE* xpReadFile, FILE* xpWriteFile)
// Copy a model from one file to another.  The copied model is byte identical
// to the original.
{
    // Create a 16 kilobyte memory buffer to try to improve copy IO efficiency
    // over a byte-per-byte copy.
    char* lpachrCopyBuffer = new char[0x4000]; //16KB
    if(!lpachrCopyBuffer)
        throw eMemoryError;
    
    // Position the files pointers for the copy.
    CIOWrapper lReadIO(xpReadFile);
    CIOWrapper lWriteIO(xpWriteFile);
    lReadIO.BeginReadBlock(xintReadOffset);
    xintWriteOffset = lWriteIO.BeginWriteBlock();
    
    // Copy the models 16K at a time.
    UnsignedInt32 lintRemaining = xintLength;
    UnsignedInt32 lintCopyBytes = 0x4000;
    while(lintRemaining) {
        if(lintRemaining > 0x4000)  // More than 16K left, copy 16K.
            lintRemaining -= 0x4000;
        else {  // 16K or less left, copy all of what's left.
            lintCopyBytes = lintRemaining;
            lintRemaining = 0;
        }
        lReadIO.Read(lpachrCopyBuffer, lintCopyBytes);
        lWriteIO.Write(lpachrCopyBuffer, lintCopyBytes);
    }

    // Make sure the copy was complete and free the buffer.
    lReadIO.EndReadBlock();
    if(lWriteIO.EndWriteBlock() != xintLength)
        throw eCorruptBlock;
    delete [] lpachrCopyBuffer;
}


///////////////////////////////////////////////////////////////////////////////
// Geometry Model Read/Write
///////////////////////////////////////////////////////////////////////////////

UnsignedInt32 CCubitFile::BeginWriteGeomModel(HModel xintGeomModel,
                                              const char* xstrGeomFile)
// Copy an external geometry file into the cubit file as a geometry model.
{
    if(!mpWriteFile)  return eFileWriteError;

    // Try to open the geometry model file for reading.
    CubitFile lpGeomFile(xstrGeomFile, "rb");
    if(!lpGeomFile)
        return eFileReadError;

    // Determine the size of the geometry file and then copy it into the
    // cubit file.
    EErrorCode leReturn = eSuccess;
    try {
        UnsignedInt32 lintReadIndex, lintWriteIndex;
        // Locate the model's index in the write contents and make sure it is
        // not an FEA model.
        if(!FindModel(xintGeomModel, mpaWriteModels,
            mWriteContents.mintNumModels, lintWriteIndex))
            throw eNotFound;
        if(mpaWriteModels[lintWriteIndex].mintModelType == eFEModel)
            throw eNotFound;
        // Locate the model's index in the read contents, if possible, and
        // make sure the model has not already been written or deleted, and
        // then mark it written.
        if(FindModel(xintGeomModel, mpaReadModels,
            mReadContents.mintNumModels, lintReadIndex)) {
            if(mpaReadModelStat[lintReadIndex] != eStatNotWritten)
                throw eOrderError;
            mpaReadModelStat[lintReadIndex] = eStatWritten;
        }

        // Measure the length of the geometry model file and then copy it into
        // to cubit file.
        if(NCubitFile::SetLocation(lpGeomFile.file(), 0, SEEK_END))
            throw CCubitFile::eFileSeekError;
        UnsignedInt32 lintGeomLength = GetLocation(lpGeomFile.file());
        CopyModel(0, mpaWriteModels[lintWriteIndex].mintModelOffset,
            lintGeomLength, lpGeomFile.file(), mpWriteFile.file());
        mpaWriteModels[lintWriteIndex].mintModelLength = lintGeomLength;
    }
    catch(EErrorCode xeErrorCode)  {  leReturn = xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  leReturn = eUnknownError;  }

    return leReturn;
}

UnsignedInt32 CCubitFile::EndWriteGeomModel()<--- The function 'EndWriteGeomModel' is never used.
// The formal end to a geometry model write.
{
    return eSuccess;
}

UnsignedInt32 CCubitFile::BeginReadGeomModel(HModel xintGeomModel,
                                             FILE* xpGeomFile)
// Copy an geometry model out of the cubit file into an external geometry file.
{
    if(!mpReadFile)  return eFileReadError;
    if(!xpGeomFile)  return eFileWriteError;

	UnsignedInt32 lintStartFilePos = GetLocation(xpGeomFile);

    // Determine the size of the geometry file and then copy it into the
    // cubit file.
    EErrorCode leReturn = eSuccess;
    try {
        UnsignedInt32 lintReadIndex, lintWriteOffset;
        // Locate the model's index in the read contents and make sure it is
        // not an FEA model.
        if(!FindModel(xintGeomModel, mpaReadModels,
            mReadContents.mintNumModels, lintReadIndex))
            throw eNotFound;
        if(mpaReadModels[lintReadIndex].mintModelType == eFEModel)
            throw eNotFound;

        // Copy the geometry model file out of the cubit file.
        CopyModel(mpaReadModels[lintReadIndex].mintModelOffset,
            lintWriteOffset, mpaReadModels[lintReadIndex].mintModelLength,
            mpReadFile.file(), xpGeomFile);
    }
    catch(EErrorCode xeErrorCode)  {  leReturn = xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  leReturn = eUnknownError;  }

    NCubitFile::SetLocation(xpGeomFile, lintStartFilePos, SEEK_SET);
    return leReturn;
}

UnsignedInt32 CCubitFile::EndReadGeomModel()
// The formal end to a geometry model read.
{
    return eSuccess;
}


UnsignedInt32 CCubitFile::BeginWriteModel(HModel xintModelId, EModelType type,
                                                  FILE*& writeable_file)
{
    // Let's not return a FILE* until we know it's going to be OK to write to.
  writeable_file = NULL;
  
  if(!mpWriteFile)
    return eFileWriteError;
  
  EErrorCode leReturn = eSuccess;
  try
  {
    UnsignedInt32 lintReadIndex, lintWriteIndex;
      // Locate the model's index in the write contents and make sure it is
      // the right kind of model.
    if(!FindModel(xintModelId,
                  mpaWriteModels,
                  mWriteContents.mintNumModels,
                  lintWriteIndex))
      throw eNotFound;
    
    if(mpaWriteModels[lintWriteIndex].mintModelType != type)
      throw eNotFound;
    
      // Locate the model's index in the read contents, if possible, and
      // make sure the model has not already been written or deleted, and
      // then mark it as being written.
    if(FindModel(xintModelId, mpaReadModels,
                 mReadContents.mintNumModels, lintReadIndex))
    {
      if(mpaReadModelStat[lintReadIndex] != eStatNotWritten)
        throw eOrderError;
      mpaReadModelStat[lintReadIndex] = eStatWriting;
    }
    
      // Move the FILE* to the correct write position so the FILE*
      // can be written to directly.  From CopyFile, it looks like the
      // correct location is the end of the file.
    if (NCubitFile::SetLocation(mpWriteFile.file(), 0, SEEK_END))
      throw eFileSeekError;
    
      // Save our current FILE* position so we can detect the size of
      // data written to the model.
    mpaWriteModels[lintWriteIndex].mintModelOffset = GetLocation(mpWriteFile.file());
    
      // set the FILE*
    writeable_file = mpWriteFile.file();
  }
  catch(EErrorCode xeErrorCode)<--- Exception should be caught by reference.
  {  leReturn = xeErrorCode;  }
  catch(...)
  {  leReturn = eUnknownError;  }
  
  return leReturn;
}

UnsignedInt32 CCubitFile::EndWriteModel(HModel xintModelId)
{
  if (!mpWriteFile)
    return eFileWriteError;
  
    // Find the model's write record
  UnsignedInt32 lintWriteIndex;
  if(!FindModel(xintModelId,
                mpaWriteModels,
                mWriteContents.mintNumModels,
                lintWriteIndex))
    return eNotFound;

    // Make sure we saved the start position
  if (mpaWriteModels[lintWriteIndex].mintModelOffset == 0)
    return eOrderError;
  
    // Get the end of the FILE.
  if (NCubitFile::SetLocation(mpWriteFile.file(), 0, SEEK_END))
    return eFileSeekError;
  UnsignedInt32 cur_pos = GetLocation(mpWriteFile.file());
  
    // See how many bytes that is past our saved position.
  UnsignedInt32 size_in_bytes = cur_pos - mpaWriteModels[lintWriteIndex].mintModelOffset;
  
    // Locate the model's index in the read contents, if possible, and
    // make sure the model has not already been written or deleted, and
    // then mark it as written.
  UnsignedInt32 lintReadIndex;
  if(FindModel(xintModelId, mpaReadModels,
               mReadContents.mintNumModels, lintReadIndex))
  {
    if(mpaReadModelStat[lintReadIndex] != eStatWriting)
      throw eOrderError;
    mpaReadModelStat[lintReadIndex] = eStatWritten;
  }
  
    // Save the size to the model header.
  mpaWriteModels[lintWriteIndex].mintModelLength = size_in_bytes;
  
  return eSuccess;
}

// Returns a FILE* that can be read from.  The FILE* will NOT return EOF
// at the end of the data...the reader should not go GetReadModelLength()
// bytes beyond the current FILE* position.  You should copy the contents
// to another FILE if this is an issue for the reader.
UnsignedInt32 CCubitFile::BeginReadModel(HModel xintModelId, EModelType type,
                                                 FILE*& f)
{
  f = NULL;
  
  if(!mpReadFile)
    return eFileReadError;
  
    // Determine the size of the geometry file and then copy it into the
    // cubit file.
  EErrorCode leReturn = eSuccess;
  try
  {
    UnsignedInt32 lintReadIndex;
      // Locate the model's index in the read contents and make sure it is
      // an assembly model.
    if(!FindModel(xintModelId, mpaReadModels,
                  mReadContents.mintNumModels, lintReadIndex))
      throw eNotFound;
    if(mpaReadModels[lintReadIndex].mintModelType != type)
      throw eNotFound;
    
      // Set the read pointer to the correct location
    NCubitFile::SetLocation(mpReadFile.file(), mpaReadModels[lintReadIndex].mintModelOffset, SEEK_SET);
    
      // Set the FILE*
    f = mpReadFile.file();
  }
  catch(EErrorCode xeErrorCode)<--- Exception should be caught by reference.
  { leReturn = xeErrorCode; }
  catch(...)
  { leReturn = eUnknownError; }
  
  return leReturn;
}

UnsignedInt32 CCubitFile::EndReadModel()<--- The function 'EndReadModel' is never used.
{
  return eSuccess;
}


///////////////////////////////////////////////////////////////////////////////
// FEA Model Read/Write
///////////////////////////////////////////////////////////////////////////////

UnsignedInt32 CCubitFile::BeginWriteFEModel(HModel xintFEModel,
                                            UnsignedInt32 xintGeomCount,
                                            UnsignedInt32 xintGroupCount,
                                            UnsignedInt32 xintBlockCount,
                                            UnsignedInt32 xintNodeSetCount,
                                            UnsignedInt32 xintSideSetCount)
// Prepares the file for the writing of a FEA model.  After this call there
// should be no other write oprations on the file not associated with the 
// writing of the FEA model until a matching call is made to EndWriteFEModel.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFile)  throw eFileWriteError;
        if(mpWriteFEModel)  throw eOrderError;

        // Write the model table first to try to guarentee that it will be
        // at the beginning of the file.
        WriteModelTable();

        // Lookup the requested model in the model table and make sure it is
        // an FEA model.
        if(!FindModel(xintFEModel, mpaWriteModels, mWriteContents.mintNumModels,
            mintFEModelIndex))
            throw eNotFound;
        if(mpaWriteModels[mintFEModelIndex].mintModelType != eFEModel)
            throw eNotFound;

        // Create the object that manages FEA model file transactions and have
        // it begin its model write.
        mpWriteFEModel = new CFEModel();
        if(!mpWriteFEModel)
            throw eMemoryError;
        
        mpaWriteModels[mintFEModelIndex].mintModelOffset =
            mpWriteFEModel->InitWrite(mpWriteFile.file(), xintGeomCount, xintGroupCount,
            xintBlockCount, xintNodeSetCount, xintSideSetCount);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

// Try to write the nodes for the passed geometry index/ID to the writable file
UnsignedInt32 CCubitFile::WriteNodes(UnsignedInt32 xintIndex,
                                     UnsignedInt32 xintGeomID,
                                     UnsignedInt32 xintNodeCount,
                                     UnsignedInt32 *xpaintNodeIDs,
                                     double *xpadblX,
                                     double *xpadblY,
                                     double *xpadblZ)
{
  try
  {
    if(!mpWriteFEModel)
      throw eOrderError;
    mpWriteFEModel->WriteNodes(xintIndex, xintGeomID,
                               xintNodeCount, xpaintNodeIDs,
                               xpadblX, xpadblY, xpadblZ);
  }
  catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
  catch(...)  {  return eUnknownError;  }
  return eSuccess;
}

UnsignedInt32 CCubitFile::WriteElems(UnsignedInt32 xintIndex,
                                     UnsignedInt32 xintNumTypes,
                                     SElemData* xpaElemData)
// Try to write the element connectivity for the passed geometry index/ID
// to the writable file.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpWriteFEModel->WriteElems(xintIndex, xintNumTypes, xpaElemData);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteGroup(UnsignedInt32 xintIndex,
                                     UnsignedInt32 xintGroupID,
                                     UnsignedInt32 xintGroupType,
                                     ConstCharPtr xpachrGroupName,
                                     UnsignedInt32 xintNumTypes,
                                     SGroupData* xpaGroupData)
// Try to write the membership of the passed group index to the writable file.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpWriteFEModel->WriteGroup(xintIndex, xintGroupID,
            xintGroupType, xpachrGroupName, xintNumTypes, xpaGroupData);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteBlock(UnsignedInt32 xintIndex,
                                     UnsignedInt32 xintBlockID,
                                     int unique_id,
                                     UnsignedInt32 xintBlockType,
                                     UnsignedInt32 xintBlockColor,
                                     UnsignedInt32 xintMixedElemType,
                                     UnsignedInt32 xintDefPyramidType,
                                     UnsignedInt32 xintMaterialID,
                                     UnsignedInt32 xintBlockDimension,
                                     UnsignedInt32 xintNumTypes,
                                     SBlockData* xpaBlockData,
                                     UnsignedInt32 xintAttributeOrder,
                                     double* xpadblAttributes)
// Try to write the membership of the passed group index to the writable file.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpWriteFEModel->WriteBlock(xintIndex, xintBlockID, unique_id, xintBlockType,
            xintBlockColor, xintMixedElemType, xintDefPyramidType,
            xintMaterialID, xintBlockDimension, xintNumTypes, xpaBlockData,
            xintAttributeOrder, xpadblAttributes);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteNodeSet(UnsignedInt32 xintIndex,
                                       UnsignedInt32 xintNodeSetID,
                                       int unique_id,
                                       UnsignedInt32 xintColor,
                                       UnsignedInt32 xintPointSymbol,
                                       UnsignedInt32 xintNumTypes,
                                       SNodeSetData* xpaNodeSetData,
                                       const std::vector<char>& bcdata)
// Try to write the membership of the passed group index to the writable file.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpWriteFEModel->WriteNodeSet(xintIndex, xintNodeSetID, unique_id, xintColor,
            xintPointSymbol, xintNumTypes, xpaNodeSetData, bcdata);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteSideSet_11(UnsignedInt32 xintIndex,
                                       UnsignedInt32 xintSideSetID,
                                       int unique_id,
                                       UnsignedInt32 xintColor,
                                       UnsignedInt32 xintUseShells,
                                       UnsignedInt32 xintNumTypes,
                                       SSideSetData_11* xpaSideSetData,
                                       UnsignedInt32 xintNumDistFact,
                                       double* xpadblDistribution,
                                       const std::vector<char>& bcdata)
// Try to write the membership of the passed group index to the writable file.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpWriteFEModel->WriteSideSet_11(xintIndex, xintSideSetID, unique_id, xintColor,
            xintUseShells, xintNumTypes, xpaSideSetData, xintNumDistFact,
            xpadblDistribution, bcdata);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::EndWriteFEModel()<--- The function 'EndWriteFEModel' is never used.
// Designate the end of writing a FEA model and free any memory allocated for
// the write operations and update the file contents header.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteFEModel)
            throw eOrderError;
        mpaWriteModels[mintFEModelIndex].mintModelLength =
            mpWriteFEModel->EndWrite();
        delete mpWriteFEModel;
        mpWriteFEModel = NULL;

        UnsignedInt32 lintReadIndex;
        if(FindModel(mpaWriteModels[mintFEModelIndex].mintModelHandle,
            mpaReadModels, mReadContents.mintNumModels, lintReadIndex))
            mpaReadModelStat[lintReadIndex] = eStatWritten;

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

//-----------------------------------------------------------------------------

UnsignedInt32 CCubitFile::BeginReadFEModel(HModel xintFEModel,
                                           UnsignedInt32& xintGeomCount,
                                           UnsignedInt32& xintGroupCount,
                                           UnsignedInt32& xintBlockCount,
                                           UnsignedInt32& xintNodeSetCount,
                                           UnsignedInt32& xintSideSetCount)
// Prepares the file for the reading of a FEA model.  After this call there
// should be no other read oprations on the file not associated with the 
// reading of the FEA model until a matching call is made to EndReadFEModel.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpReadFile)  throw eFileReadError;
        if(mpReadFEModel)  throw eOrderError;

        UnsignedInt32 lintIndex;
        if(!FindModel(xintFEModel, mpaReadModels, mReadContents.mintNumModels,
            lintIndex))
            throw eNotFound;
        if(mpaReadModels[lintIndex].mintModelType != eFEModel)
            throw eNotFound;

        mpReadFEModel = new CFEModel();
        if(!mpReadFEModel)  throw eMemoryError;
        
        mpReadFEModel->InitRead(mpReadFile.file(), mpaReadModels[lintIndex].mintModelOffset,
            xintGeomCount, xintGroupCount,
            xintBlockCount, xintNodeSetCount, xintSideSetCount);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::ReadNodes(UnsignedInt32 xintIndex,
									UnsignedInt32& xintGeomID,
                                    UnsignedInt32& xintNodeCount,
                                    UnsignedInt32*& xpaintNodeIDs,
                                    double*& xpadblX,
                                    double*& xpadblY,
                                    double*& xpadblZ)
// Try to read the nodes for the passed geometry index/ID from the
// read-only file.
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadNodes(xintIndex, xintGeomID,
            xintNodeCount, xpaintNodeIDs, xpadblX, xpadblY, xpadblZ);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintNodeCount = 0;
    xpaintNodeIDs = NULL;
    xpadblX = xpadblY = xpadblZ = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadElems(HModel meshModelId,
                                    UnsignedInt32 xintIndex,
                                    UnsignedInt32& xintGeomID,
                                    UnsignedInt32& xintNumTypes,
                                    SElemData*& xpaElemData)
// Try to read the element connectivity for the passed geometry index/ID
// from the read-only file.
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {

      CMetaData *mmd = NULL;
      this->GetReadMetaData(CCubitFile::eModelMetaData, mmd);
      if( !mmd )
      {
        throw eFileReadError;
      }

      // get the data version from the file.
      // Version 1.0 has an old element type enum
      double data_version = 0.0;
      mmd->GetValue(meshModelId, "DataVersion", data_version);
      
      if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadElems(data_version, xintIndex, xintGeomID,
			xintNumTypes, xpaElemData);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintNumTypes = 0;
    xpaElemData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadGroupIdentity(UnsignedInt32 xintIndex,
                                            UnsignedInt32& xintGroupID,
                                            UnsignedInt32& xintGroupType,
                                            ConstCharPtr& xpachrGroupName)
// Try to read the identity (ID, name, type) of the passed group index from the
// read-only file.
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadGroupIdentity(xintIndex,
            xintGroupID, xintGroupType, xpachrGroupName);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintGroupID = xintGroupType = 0;
    xpachrGroupName = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadGroupMembers(UnsignedInt32 xintIndex,
                                           UnsignedInt32& xintNumTypes,
                                           SGroupData*& xpaGroupData)
// Try to read the membership of the passed group index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadGroupMembers(xintIndex, xintNumTypes, xpaGroupData);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintNumTypes = 0;
    xpaGroupData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadBlock(UnsignedInt32 xintIndex,
                                    UnsignedInt32& xintBlockID,
                                    int& unique_id,
                                    UnsignedInt32& xintBlockType,
                                    UnsignedInt32& xintBlockColor,
                                    UnsignedInt32& xintMixedElemType,
                                    UnsignedInt32& xintDefPyramidType,
                                    UnsignedInt32& xintMaterialID,
                                    UnsignedInt32& xintBlockDimension,
                                    UnsignedInt32& xintNumTypes,
                                    SBlockData*& xpaBlockData,
                                    UnsignedInt32& xintAttributeOrder,
                                    double*& xpadblAttributes)
// Try to read the membership of the passed block index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadBlock(xintIndex, xintBlockID, unique_id,
            xintBlockType, xintBlockColor, xintMixedElemType,
            xintDefPyramidType, xintMaterialID, xintBlockDimension,
            xintNumTypes, xpaBlockData, xintAttributeOrder, xpadblAttributes);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintBlockID = xintBlockType = xintBlockColor = xintMixedElemType =
        xintDefPyramidType = xintMaterialID = xintNumTypes =
        xintAttributeOrder = 0;
    xpaBlockData = NULL;
    xpadblAttributes = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadNodeSet(UnsignedInt32 xintIndex,
                                      UnsignedInt32& xintNodeSetID,
                                      int& unique_id,
                                      UnsignedInt32& xintColor,
                                      UnsignedInt32& xintPointSymbol,
                                      UnsignedInt32& xintNumTypes,
                                      SNodeSetData*& xpaNodeSetData,
                                      std::vector<char>& bcdata)
// Try to read the membership of the passed node set index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadNodeSet(xintIndex, xintNodeSetID, unique_id,
            xintColor, xintPointSymbol, xintNumTypes, xpaNodeSetData, bcdata);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintNodeSetID = xintColor = xintPointSymbol = xintNumTypes = 0;
    xpaNodeSetData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadSideSet_10(UnsignedInt32 xintIndex,
                                      UnsignedInt32& xintSideSetID,
                                      UnsignedInt32& xintColor,
                                      UnsignedInt32& xintUseShells,
                                      UnsignedInt32& xintNumTypes,
                                      SSideSetData_10*& xpaSideSetData,
                                      UnsignedInt32& xintNumDistFact,
                                      double*& xpadblDistribution)
// Try to read the membership of the passed side set index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadSideSet_10(xintIndex, xintSideSetID,
            xintColor, xintUseShells, xintNumTypes, xpaSideSetData,
            xintNumDistFact, xpadblDistribution);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintSideSetID = xintColor = xintNumTypes = xintNumDistFact = 0;
    xpaSideSetData = NULL;
    xpadblDistribution = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadSideSet_11(UnsignedInt32 xintIndex,
                                      UnsignedInt32& xintSideSetID,
                                      int& unique_id,
                                      UnsignedInt32& xintColor,
                                      UnsignedInt32& xintUseShells,
                                      UnsignedInt32& xintNumTypes,
                                      SSideSetData_11*& xpaSideSetData,
                                      UnsignedInt32& xintNumDistFact,
                                      double*& xpadblDistribution,
                                      std::vector<char>& bcdata)
// Try to read the membership of the passed side set index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadFEModel)
            throw eOrderError;
        mpReadFEModel->ReadSideSet_11(xintIndex, xintSideSetID, unique_id,
            xintColor, xintUseShells, xintNumTypes, xpaSideSetData,
            xintNumDistFact, xpadblDistribution, bcdata);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintSideSetID = xintColor = xintNumTypes = xintNumDistFact = 0;
    xpaSideSetData = NULL;
    xpadblDistribution = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::EndReadFEModel()<--- The function 'EndReadFEModel' is never used.
// Designate the end of reading a FEA model and free any memory allocated for
// the read operations.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpReadFEModel)
            return eOrderError;
        mpReadFEModel->EndRead();
        delete mpReadFEModel;
        mpReadFEModel = NULL;
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::BeginWriteSimModel(HModel xintSimModel,
                                             UnsignedInt32 xintBCCount,
                                             UnsignedInt32 xintICCount,
                                             UnsignedInt32 xintBCSetCount,
                                             UnsignedInt32 xintMaterialCount,
                                             UnsignedInt32 xintAmplitudeCount,
                                             UnsignedInt32 xintConstraintCount)
{
    try {
        if(!mpWriteFile)  throw eFileWriteError;
        if(mpWriteSimModel)  throw eOrderError;

        // Write the model table first to try to guarentee that it will be
        // at the beginning of the file.
        WriteModelTable();

        // Lookup the requested model in the model table and make sure it is
        // an FEA model.
        if(!FindModel(xintSimModel, mpaWriteModels, mWriteContents.mintNumModels,
            mintSimModelIndex))
            throw eNotFound;
        if(mpaWriteModels[mintSimModelIndex].mintModelType != eSimModel)
            throw eNotFound;

        // Create the object that manages FEA model file transactions and have
        // it begin its model write.
        mpWriteSimModel = new CSimModel();
        if(!mpWriteSimModel)
            throw eMemoryError;

        mpaWriteModels[mintSimModelIndex].mintModelOffset =
            mpWriteSimModel->InitWrite(mpWriteFile.file(), xintBCCount,
                                       xintICCount, xintBCSetCount,
                                       xintMaterialCount, xintAmplitudeCount,
                                       xintConstraintCount);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteBCSet(UnsignedInt32 xintIndex,
                                     UnsignedInt32 xintBCSetID,
                                     UnsignedInt32 xintBCSetUniqueID,
                                     UnsignedInt32 xintBCSetAnalysisType,
                                     UnsignedInt32 xintRestraintTypesCount,
                                     UnsignedInt32 xintLoadTypesCount,
                                     UnsignedInt32 xintContactPairTypesCount,
                                     SBCSetData* xpaBCSetRestraintData,
                                     SBCSetData* xpaBCSetLoadData,
                                     SBCSetData* xpaBCSetContactPairData)
// Try to write the membership of the passed group index to the writable file.
// RETURNS: 0 on success, other values indicate error code.
{
    try {
        if(!mpWriteSimModel)
            throw eOrderError;

        mpWriteSimModel->WriteBCSet(xintIndex,xintBCSetID,xintBCSetUniqueID,
            xintBCSetAnalysisType,xintRestraintTypesCount,xintLoadTypesCount,
            xintContactPairTypesCount,xpaBCSetRestraintData,
            xpaBCSetLoadData,xpaBCSetContactPairData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteMaterial(UnsignedInt32 xintIndex,
                                        UnsignedInt32 xintMaterialID,
                                        UnsignedInt32 xintMaterialUniqueID,
                                        UnsignedInt32 xintPropertiesCount,
                                        SMaterialData* xpaMaterialData
                                        )
{
    try {
        if(!mpWriteSimModel)
            throw eOrderError;

        mpWriteSimModel->WriteMaterial(xintIndex,xintMaterialID,xintMaterialUniqueID,
            xintPropertiesCount,xpaMaterialData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::WriteConstraint(UnsignedInt32 xintIndex,
                                UnsignedInt32 xintConstraintID,
                                UnsignedInt32 xintConstraintUniqueID,
                                UnsignedInt32 xintConstraintType,
                                UnsignedInt32 xintIndependentTypeCount,
                                SConstraintData* xpaIndependentData,
                                UnsignedInt32 xintDependentTypeCount,
                                SConstraintData* xpaDependentData
                                )
{
    try {
        if(!mpWriteSimModel)
            throw eOrderError;

        mpWriteSimModel->WriteConstraint(xintIndex,xintConstraintID,xintConstraintUniqueID,
            xintConstraintType,xintIndependentTypeCount,xpaIndependentData,
            xintDependentTypeCount,xpaDependentData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::EndWriteSimModel()<--- The function 'EndWriteSimModel' is never used.
{
    try {
        if(!mpWriteSimModel)
            throw eOrderError;
        mpaWriteModels[mintSimModelIndex].mintModelLength =
            mpWriteSimModel->EndWrite();
        delete mpWriteSimModel;
        mpWriteSimModel = NULL;

        UnsignedInt32 lintReadIndex;
        if(FindModel(mpaWriteModels[mintSimModelIndex].mintModelHandle,
            mpaReadModels, mReadContents.mintNumModels, lintReadIndex))
            mpaReadModelStat[lintReadIndex] = eStatWritten;

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::BeginReadSimModel(HModel xintSimModel,
                                            UnsignedInt32& xintBCCount,
                                            UnsignedInt32& xintICCount,
                                            UnsignedInt32& xintBCSetCount,
                                            UnsignedInt32& xintMaterialCount,
                                            UnsignedInt32& xintAmplitudeCount,
                                            UnsignedInt32& xintConstraintCount)
{
    try {
        if(!mpReadFile)  throw eFileReadError;
        if(mpReadSimModel)  throw eOrderError;

        UnsignedInt32 lintIndex;
        if(!FindModel(xintSimModel, mpaReadModels, mReadContents.mintNumModels,
            lintIndex))
            throw eNotFound;
        if(mpaReadModels[lintIndex].mintModelType != eSimModel)
            throw eNotFound;

        mpReadSimModel = new CSimModel();
        if(!mpReadSimModel)  throw eMemoryError;

        mpReadSimModel->InitRead(mpReadFile.file(), mpaReadModels[lintIndex].mintModelOffset,
                                 xintBCCount, xintICCount, xintBCSetCount,
                                 xintMaterialCount, xintAmplitudeCount,
                                 xintConstraintCount);
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

UnsignedInt32 CCubitFile::ReadBCSet(UnsignedInt32 xintIndex,
                                    UnsignedInt32& xintBCSetID,
                                    UnsignedInt32& xintBCSetUniqueID,
                                    UnsignedInt32& xintBCSetAnalysisType,
                                    UnsignedInt32& xintRestraintTypesCount,
                                    UnsignedInt32& xintLoadTypesCount,
                                    UnsignedInt32& xintContactPairTypesCount,
                                    SBCSetData*& xpaBCSetRestraintData,
                                    SBCSetData*& xpaBCSetLoadData,
                                    SBCSetData*& xpaBCSetContactPairData)
// Try to read the membership of the passed node set index from the read-only file.
// Note: The 
// RETURNS: 0 on success, other values indicate error code.
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadSimModel)
            throw eOrderError;

        mpReadSimModel->ReadBCSet(xintIndex,xintBCSetID,xintBCSetUniqueID,
            xintBCSetAnalysisType,xintRestraintTypesCount,
            xintLoadTypesCount,xintContactPairTypesCount,xpaBCSetRestraintData,
            xpaBCSetLoadData,xpaBCSetContactPairData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintBCSetID = xintBCSetUniqueID = xintBCSetAnalysisType = 0;
    xintRestraintTypesCount = xintLoadTypesCount = xintContactPairTypesCount = 0;
    xpaBCSetRestraintData = NULL;
    xpaBCSetLoadData = NULL;
    xpaBCSetContactPairData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadMaterial(UnsignedInt32 xintIndex,
                                       UnsignedInt32& xintMaterialID,
                                       UnsignedInt32& xintMaterialUniqueID,
                                       UnsignedInt32& xintPropertiesCount,
                                       SMaterialData*& xpaMaterialData
                                       )
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadSimModel)
            throw eOrderError;

        mpReadSimModel->ReadMaterial(xintIndex,xintMaterialID,xintMaterialUniqueID,
            xintPropertiesCount,xpaMaterialData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintMaterialID = xintMaterialUniqueID = xintPropertiesCount = 0;
    xpaMaterialData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::ReadConstraint(UnsignedInt32 xintIndex,
                               UnsignedInt32& xintConstraintID,
                               UnsignedInt32& xintConstraintUniqueID,
                               UnsignedInt32& xintConstraintType,
                               UnsignedInt32& xintIndependentTypeCount,
                               SConstraintData*& xpaIndependentData,
                               UnsignedInt32& xintDependentTypeCount,
                               SConstraintData*& xpaDependentData
                               )
{
    UnsignedInt32 lintErrorCode;
    try {
        if(!mpReadSimModel)
            throw eOrderError;

        mpReadSimModel->ReadConstraint(xintIndex,xintConstraintID,xintConstraintUniqueID,
            xintConstraintType,xintIndependentTypeCount,xpaIndependentData,
            xintDependentTypeCount,xpaDependentData);

        return eSuccess;
    }
    catch(EErrorCode xeErrorCode) {<--- Exception should be caught by reference.
        lintErrorCode = xeErrorCode;
    }
    catch(...) {
        lintErrorCode = eUnknownError;
    }
    xintConstraintID = xintConstraintUniqueID = xintConstraintType = 0;
    xpaIndependentData = xpaDependentData = NULL;
    return lintErrorCode;
}

UnsignedInt32 CCubitFile::EndReadSimModel()<--- The function 'EndReadSimModel' is never used.
{
    try {
        if(!mpReadSimModel)
            return eOrderError;
        mpReadSimModel->EndRead();
        delete mpReadSimModel;
        mpReadSimModel = NULL;
        return eSuccess;
    }
    catch(EErrorCode xeErrorCode)  {  return xeErrorCode;  }<--- Exception should be caught by reference.
    catch(...)  {  return eUnknownError;  }
}

///////////////////////////////////////////////////////////////////////////////
// Meta-data accessors
///////////////////////////////////////////////////////////////////////////////

UnsignedInt32 CCubitFile::GetReadMetaData(EMetaDataOwnerType xeType,
                                          CMetaData*& xpMetaData)
// Return the requested meta-data object for the read file if possible.
{
    xpMetaData = NULL;
    if(xeType == eModelMetaData)
        xpMetaData = mpMetaData;
    else if(mpReadFEModel) {
        switch(xeType) {
        case eGeomMetaData:
            xpMetaData = &mpReadFEModel->GetGeomMetaData();
            break;
        case eNodeMetaData:
            xpMetaData = &mpReadFEModel->GetNodeMetaData();
            break;
        case eElemMetaData:
            xpMetaData = &mpReadFEModel->GetElemMetaData();
            break;
        case eGroupMetaData:
            xpMetaData = &mpReadFEModel->GetGroupMetaData();
            break;
        case eBlockMetaData:
            xpMetaData = &mpReadFEModel->GetBlockMetaData();
            break;
        case eNodeSetMetaData:
            xpMetaData = &mpReadFEModel->GetNodeSetMetaData();
            break;
        case eSideSetMetaData:
            xpMetaData = &mpReadFEModel->GetSideSetMetaData();
            break;
          default:
            break;
        }
    }
    else if (mpReadSimModel) {
        switch (xeType) {
            case eBCSetMetaData:
                xpMetaData = &mpReadSimModel->GetBCSetMetaData();
                break;
            case eMaterialMetaData:
                xpMetaData = &mpReadSimModel->GetMaterialMetaData();
                break;
            case eConstraintMetaData:
                xpMetaData = &mpReadSimModel->GetConstraintMetaData();
                break;
            default:
                break;
        }
    }
    else
        return eOrderError;
    return xpMetaData ? eSuccess : eNotFound;
}

UnsignedInt32 CCubitFile::GetWriteMetaData(EMetaDataOwnerType xeType,
                                           CMetaData*& xpMetaData)
// Return the requested meta-data object for the write file if possible.
{
    xpMetaData = NULL;
    if(xeType == eModelMetaData)
        xpMetaData = mpMetaData;
    else if(mpWriteFEModel) {
        switch(xeType) {
        case eGeomMetaData:
            xpMetaData = &mpWriteFEModel->GetGeomMetaData();
            break;
        case eNodeMetaData:
            xpMetaData = &mpWriteFEModel->GetNodeMetaData();
            break;
        case eElemMetaData:
            xpMetaData = &mpWriteFEModel->GetElemMetaData();
            break;
        case eGroupMetaData:
            xpMetaData = &mpWriteFEModel->GetGroupMetaData();
            break;
        case eBlockMetaData:
            xpMetaData = &mpWriteFEModel->GetBlockMetaData();
            break;
        case eNodeSetMetaData:
            xpMetaData = &mpWriteFEModel->GetNodeSetMetaData();
            break;
        case eSideSetMetaData:
            xpMetaData = &mpWriteFEModel->GetSideSetMetaData();
            break;
          default:
              break;
        }
    }
    else if (mpWriteSimModel) {
        switch (xeType) {
            case eBCSetMetaData:
                xpMetaData = &mpWriteSimModel->GetBCSetMetaData();
                break;
            case eMaterialMetaData:
                xpMetaData = &mpWriteSimModel->GetMaterialMetaData();
                break;
            case eConstraintMetaData:
                xpMetaData = &mpWriteSimModel->GetConstraintMetaData();
                break;
            default:
                break;
        }
    }
    else
        return eOrderError;
    return xpMetaData ? eSuccess : eNotFound;
}