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
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
// File:        OCCShapeAttributeSet.cxx
// Created:     Thur Jul 10  2008
// Author:      Jane Hu

#include <Standard_Stream.hxx>
#include <BRepTools.hxx>
#include "OCCShapeAttributeSet.hpp"
#include "CubitSimpleAttrib.hpp"
#include "OCCAttribSet.hpp"
#include "OCCQueryEngine.hpp"
//#include <Poly.hxx>
#include <TopoDS.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_HArray1OfInteger.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <BRep_GCurve.hxx>
#include <Handle_BRep_CurveOnClosedSurface.hxx>
#include <Handle_BRep_CurveOnSurface.hxx>
#include <TopAbs_ShapeEnum.hxx>
//#include <BRep_TFace.hxx>
//#include <BRep_TEdge.hxx>
//#include <BRep_TVertex.hxx>
#include <Handle_BRep_GCurve.hxx>
#include <BRep_Tool.hxx>
#include <TDF_ChildIterator.hxx>
#include <TopTools_LocationSet.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopoDS_Iterator.hxx>
//#include <BRep_CurveRepresentation.hxx>
#include <Poly_Polygon3D.hxx>
//#include <BRep_Polygon3D.hxx>
//#include <BRep_PolygonOnSurface.hxx>
//#include <BRep_PolygonOnClosedSurface.hxx>
//#include <BRep_PolygonOnTriangulation.hxx>
//#include <BRep_PolygonOnClosedTriangulation.hxx>
#include <BRep_CurveOnSurface.hxx>
#include <BRep_CurveOnClosedSurface.hxx>
//#include <BRep_ListOfCurveRepresentation.hxx>
//#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_PointOnCurve.hxx>
#include <BRep_PointOnCurveOnSurface.hxx>
#include <BRep_PointOnSurface.hxx>
//#include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
#include <TDF_Label.hxx>
#include <TopTools_DataMapOfShapeInteger.hxx>

#ifndef OCC_VERSION_MINOR
#include "Standard_Version.hxx"
#endif
#if OCC_VERSION_MINOR < 5
  #include <TDataStd_Shape.hxx>
  typedef TDataStd_Shape TDataXtd_Shape;
  typedef Handle_TDataStd_Shape Handle_TDataXtd_Shape;
#else
  #include <TDataXtd_Shape.hxx>
#endif

#include <Handle_TDataStd_Name.hxx>
#include <TDataStd_Name.hxx>
#include <Handle_TDataStd_ExtStringArray.hxx>
#include <TDataStd_ExtStringArray.hxx>
#include <TCollection_ExtendedString.hxx>
#include <Handle_TDataStd_IntegerArray.hxx>
#include <TDataStd_IntegerArray.hxx>
#include <Handle_TDataStd_RealArray.hxx>
#include <TDataStd_RealArray.hxx>
#include <Handle_BRep_TVertex.hxx>
#include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
#include <BRep_TVertex.hxx>
#include <BRep_PointRepresentation.hxx>
#include <Handle_BRep_TEdge.hxx>
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <Handle_BRep_TFace.hxx>
#include <BRep_TFace.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_CurveRepresentation.hxx>

#include <TopoDS_Vertex.hxx>

//#include <TColgp_HArray1OfPnt.hxx>
//#include <TColgp_HArray1OfPnt2d.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <Poly_Triangulation.hxx>
#include <Poly_PolygonOnTriangulation.hxx>


#ifdef MacOS
#define strcasecmp(p,q) strcmp(p,q)
#elseif WNT
#define strcasecmp strcmp
#elseif AIX
#include <string.h>
#endif

const char* dVersion  = "CASCADE Topology V1, (c) Matra-Datavision";
const char* dVersion2 = "CASCADE Topology V2, (c) Matra-Datavision";

//=======================================================================
//function : PrintShapeEnum
//purpose  :
//=======================================================================

static void PrintShapeEnum(const TopAbs_ShapeEnum T,
                           Standard_OStream& S,
                           Standard_Boolean C)
{
  switch(T) {

  case TopAbs_VERTEX :
    if (C) S << "Ve"; else S << "VERTEX   ";
    break;

  case TopAbs_EDGE :
    if (C) S << "Ed"; else S << "EDGE     ";
    break;

  case TopAbs_WIRE :
    if (C) S << "Wi"; else S << "WIRE     ";
    break;

  case TopAbs_FACE :
    if (C) S << "Fa"; else S << "FACE     ";
    break;

  case TopAbs_SHELL :
    if (C) S << "Sh"; else S << "SHELL    ";
    break;

  case TopAbs_SOLID :
    if (C) S << "So"; else S << "SOLID    ";
    break;

  case TopAbs_COMPSOLID :
    if (C) S << "CS"; else S << "COMPSOLID";
    break;

  case TopAbs_COMPOUND :
    if (C) S << "Co"; else S << "COMPOUND ";
    break;

  case TopAbs_SHAPE :
    if (C) S << "Sp"; else S << "SHAPE";
    break;
  }
}

//=======================================================================
//function : PrintRegularity
//purpose  :
//=======================================================================

static void PrintRegularity(const GeomAbs_Shape C,
                            Standard_OStream& OS)
{
  switch (C) {

  case GeomAbs_C0 :
    OS << "C0";
    break;

  case GeomAbs_G1 :
    OS << "G1";
    break;

  case GeomAbs_C1 :
    OS << "C1";
    break;

  case GeomAbs_G2 :
    OS << "G2";
    break;

  case GeomAbs_C2 :
    OS << "C2";
    break;

  case GeomAbs_C3 :
    OS << "C3";
    break;

  case GeomAbs_CN :
    OS << "CN";
    break;

  }
}

//=======================================================================
//function : PrintOrientation
//purpose  :
//=======================================================================

static void PrintOrientation(const TopAbs_Orientation O,
                             Standard_OStream& S,
                             Standard_Boolean C)
{
  switch(O) {

  case TopAbs_FORWARD :
    if (C) S << "+"; else S << "FORWARD";
    break;

  case TopAbs_REVERSED :
    if (C) S << "-"; else S << "REVERSED";
    break;

  case TopAbs_INTERNAL :
    if (C) S << "i"; else S << "INTERNAL";
    break;

  case TopAbs_EXTERNAL :
    if (C) S << "e"; else S << "EXTERNAL";
    break;
  }
}

//=======================================================================
//function : ReadShapeEnum
//purpose  :
//=======================================================================

static TopAbs_ShapeEnum ReadShapeEnum(Standard_IStream& IS)
{
  std::string buffer;
  IS >> buffer;

  switch (buffer[0]) {

  case 'V' :
    return TopAbs_VERTEX;

  case 'E' :
    return TopAbs_EDGE;

  case 'W' :
    return TopAbs_WIRE;

  case 'F' :
    return TopAbs_FACE;

  case 'S' :
    if (buffer[1] == 'h')
      return TopAbs_SHELL;
    else
      return TopAbs_SOLID;

  case 'C' :
    if (buffer[1] == 'S')
      return TopAbs_COMPSOLID;
    else
      return TopAbs_COMPOUND;

  }
  return TopAbs_COMPOUND;
}

//=======================================================================
//function : ReadRegularity
//purpose  :
//=======================================================================

static GeomAbs_Shape ReadRegularity(Standard_IStream& IS)
{
  std::string buffer;
  IS >> buffer;
  switch (buffer[0]) {

  case 'C' :
    switch (buffer[1]) {

    case '0' :
      return GeomAbs_C0;

    case '1' :
      return GeomAbs_C1;

    case '2' :
      return GeomAbs_C2;

    case '3' :
      return GeomAbs_C3;

    case 'N' :
      return GeomAbs_CN;
    }
    break;

  case 'G' :
    switch (buffer[1]) {

    case '1' :
      return GeomAbs_G1;

    case '2' :
      return GeomAbs_G2;

    }
    break;
  }
  return GeomAbs_C0;
}

//=======================================================================
//function : OCCShapeAttributeSet
//purpose  :
//=======================================================================

OCCShapeAttributeSet::OCCShapeAttributeSet()
  :myFormatNb(1)
{
}

//=======================================================================
//function : OCCShapeAttributeSet
//purpose  :
//=======================================================================

OCCShapeAttributeSet::OCCShapeAttributeSet (const BRep_Builder& B)
  :myBuilder(B)
{
}
//=======================================================================
//function : Add
//purpose  :
//=======================================================================

Standard_Integer  OCCShapeAttributeSet::Add(const TopoDS_Shape& S)
{
  if (S.IsNull()) return 0;
  myLocations.Add(S.Location());
  TopoDS_Shape S2 = S;
  S2.Location(TopLoc_Location());
  Standard_Integer index = myShapes.FindIndex(S2);
  if (index == 0) {
    AddGeometry(S2);

    for (TopoDS_Iterator its(S2,Standard_False,Standard_False);
         its.More(); its.Next())
      Add(its.Value());
    index = myShapes.Add(S2);
  }
 
  return index;
}

//=======================================================================
//function : AddGeometry
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::AddGeometry(const TopoDS_Shape& S)
{
  // Add the geometry

  if (S.ShapeType() == TopAbs_VERTEX) {

    Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
    BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());

    while (itrp.More()) {
      const Handle(BRep_PointRepresentation)& PR = itrp.Value();

      if (PR->IsPointOnCurve()) {
        myCurves.Add(PR->Curve());
      }

      else if (PR->IsPointOnCurveOnSurface()) {
        myCurves2d.Add(PR->PCurve());
        mySurfaces.Add(PR->Surface());
      }

      else if (PR->IsPointOnSurface()) {
        mySurfaces.Add(PR->Surface());
      }

      myLocations.Add(PR->Location());
      itrp.Next();
    }

  }
  else if (S.ShapeType() == TopAbs_EDGE) {

    // Add the curve geometry
    Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
    BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves());

    while (itrc.More()) {
      const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
      if (CR->IsCurve3D()) {
        if (!CR->Curve3D().IsNull()) {
          myCurves.Add(CR->Curve3D());
          myLocations.Add(CR->Location());
        }
      }
      else if (CR->IsCurveOnSurface()) {
        mySurfaces.Add(CR->Surface());
        myCurves2d.Add(CR->PCurve());
        myLocations.Add(CR->Location());
        if (CR->IsCurveOnClosedSurface())
          myCurves2d.Add(CR->PCurve2());
      }
      else if (CR->IsRegularity()) {
        mySurfaces.Add(CR->Surface());
        myLocations.Add(CR->Location());
        mySurfaces.Add(CR->Surface2());
        myLocations.Add(CR->Location2());
      }
      itrc.Next();
    }
  }

  else if (S.ShapeType() == TopAbs_FACE) {

    // Add the surface geometry
    Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
    if (!TF->Surface().IsNull())  mySurfaces.Add(TF->Surface());

    myLocations.Add(TF->Location());
  }
}

//=======================================================================
//function : ReadAttribute
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::ReadAttribute(TopoDS_Shape& S,
                                          Standard_IStream&   IS,
                                          TDF_Label& l_attr)
{
  std::string buffer, type, stringdata;
  std::vector<CubitString> strings;
  std::vector<double> doubles;
  std::vector<int> ints;
  int c_num, length;
  char s;
  do {
    IS >> c_num; //length of name string
    IS.get(); //' '
    type.clear();
    for(int j = 0; j < c_num; j ++)
    {
      IS.get(s);
      type.push_back(s);
    }

    strings.clear();
    strings.push_back(type.c_str());

    IS >> length; //number of strings
    for(int i =0; i < length; i++)
    { 
      IS >> c_num ; //length of each string
      IS.get(); //' '
      stringdata.clear();
      for(int j = 0; j < c_num; j ++)
      {
        IS.get(s);
        stringdata.push_back(s);
      }
      strings.push_back(stringdata.c_str());
    }

    int tmp_int;
    double  tmp_dbl; 
    IS >> length; //number of ints
    ints.clear();
    for (int i = 0; i < length ; i++)
    {
      IS >> tmp_int;
      ints.push_back( tmp_int );
    }

    IS >> length; //number of doubles
    doubles.clear();
    for (int i = 0; i < length ; i++)
    {
      IS >> tmp_dbl;
      doubles.push_back( tmp_dbl );
    }

    CubitSimpleAttrib tmp_attrib(&strings, &doubles, &ints);

    OCCAttribSet::append_attribute(tmp_attrib, S);
  
    IS >> buffer;
  }while(buffer[0] != '*');
}

//=======================================================================
//function : WriteAttribute
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::WriteAttribute(const TopoDS_Shape& S,
                                           Standard_OStream&   OS,
                                           TDF_Label& l_attr)
{
  if(l_attr.IsNull())
    return;

  Standard_Boolean found = Standard_False;
  TDF_Label myLabel;
  for (TDF_ChildIterator it1(l_attr,Standard_False); it1.More(); it1.Next())
  {
    //find the same shape attribute first
    myLabel = it1.Value();

    Handle_TDataXtd_Shape attr_shape;

    TopoDS_Shape exsiting_shape;

    if(TDataXtd_Shape::Find(myLabel, attr_shape))
      exsiting_shape = attr_shape->Get(myLabel);

    if(!exsiting_shape.IsNull())
    {
      if(exsiting_shape.IsPartner(S))
      {
        //It's possible that the myLabel found in this way wasn't the bounded
        //lable with the shape. Find the bounded one now.
        if(!OCCQueryEngine::instance()->OCCMap->IsBound(exsiting_shape))
          continue;
  
        int k = OCCQueryEngine::instance()->OCCMap->Find(exsiting_shape);
        std::map<int, TDF_Label>::iterator it =
           OCCQueryEngine::instance()->Shape_Label_Map->find(k);
        if(it != OCCQueryEngine::instance()->Shape_Label_Map->end())
        {
          found = Standard_True;
          myLabel = (*it).second;
          break;
        }
      }
    }
  }
  if(!found)
  {
    OS << "\n*";
    return;
  }

  if(!myLabel.HasChild())
  {
    OS << "\n*";
    return;
  }

  for (TDF_ChildIterator it2(myLabel,Standard_False); it2.More(); it2.Next())
  {
    TDF_Label child = it2.Value();
    //Write out all attributes
    Handle_TDataStd_Name attr_name;
    TCollection_ExtendedString name_string;
    if(child.FindAttribute(TDataStd_Name::GetID(), attr_name))
    {
      OS << "\n";
      OS << "CGM_ATTRIB ";
      name_string = attr_name->Get(); 
      int length = name_string.Length();
      OS << length << ' ';
      name_string.Print(OS);
      OS << ' ' ;
    }
    else
      continue;

    Handle_TDataStd_ExtStringArray attr_strings;
    if(child.FindAttribute(TDataStd_ExtStringArray::GetID(), attr_strings))
    {
      Standard_Integer i = attr_strings->Lower();
      TCollection_ExtendedString string;
      int size = attr_strings->Upper();
      OS << size -i + 1 << " ";
      for(; i <= size; i++)
      {
         string = attr_strings->Value(i);     
         int length = string.Length();
         OS << length << ' ';
         string.Print(OS);
         OS << ' ';
      }
    }
    else
      OS << "0 " ;

    Handle_TDataStd_IntegerArray attr_ints;
    
    if(child.FindAttribute(TDataStd_IntegerArray::GetID(), attr_ints))
    {
      Standard_Integer i = attr_ints->Lower();
      int size = attr_ints->Upper();
      OS << size -i + 1 << " ";
      for(; i <= size; i++)
        OS << attr_ints->Value(i) << ' ';
    }
    else
      OS << "0 " ;    

    Handle_TDataStd_RealArray attr_doubles;
    if(child.FindAttribute(TDataStd_RealArray::GetID(), attr_doubles))
    {
      Standard_Integer i = attr_doubles->Lower();
      Standard_Integer size = attr_doubles->Upper();
      OS << size -i + 1 << " ";
      for(;i <= size; i++)
        OS << attr_doubles->Value(i) << ' ';
    }
    else
      OS << "0 " ;
  }
  OS << "\n*";
}

//=======================================================================
//function : Write
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::Write(Standard_OStream& OS)
{
  //on sauvegarde l'ancien LC_NUMERIC
  
  char *oldnum,*plocal ;
  plocal =   setlocale(LC_NUMERIC, NULL) ;
  oldnum = new char[strlen(plocal)+1] ;
  strcpy(oldnum,plocal);

  // on positionne LC_NUMERIC a "C" (point decimal)
  setlocale(LC_NUMERIC, "C") ;

  int  prec = OS.precision(15);

  // write the copyright
  if (myFormatNb == 2)
    OS << "\n" << dVersion2 << endl;
  else
    OS << "\n" << dVersion << endl;

  //-----------------------------------------
  // write the locations
  //-----------------------------------------
  myLocations.Write(OS);

  //-----------------------------------------
  // write the geometry
  //-----------------------------------------

  WriteGeometry(OS);

  //-----------------------------------------
  // write the shapes
  //-----------------------------------------
  Standard_Integer i, nbShapes = myShapes.Extent();

  OS << "\nTShapes " << nbShapes << "\n";


  // subshapes are written first
  for (i = 1; i <= nbShapes; i++) {

    const TopoDS_Shape& S = myShapes(i);

    // Type
    PrintShapeEnum(S.ShapeType(),OS,Standard_True);
    OS << "\n";

    // Geometry
    WriteGeometry(S,OS);

    // Flags
    OS << "\n";
    OS << (S.Free()       ? 1 : 0);
    OS << (S.Modified()   ? 1 : 0);
    OS << (S.Checked()    ? 1 : 0);
    OS << (S.Orientable() ? 1 : 0);
    OS << (S.Closed()     ? 1 : 0);
    OS << (S.Infinite()   ? 1 : 0);
    OS << (S.Convex()     ? 1 : 0);
    OS << "\n";

    // sub-shapes

    Standard_Integer l = 0;
    TopoDS_Iterator its(S,Standard_False,Standard_False);
    while (its.More()) {
      Write(its.Value(),OS);
      l++;
      if (l == 10) {
        OS << "\n";
        l = 0;
      }
      its.Next();
    }
    Write(TopoDS_Shape(),OS); // Null shape to end the list
    OS << "\n";
  }

  OS << endl;
  OS.precision(prec);

  // on remet le LC_NUMERIC a la precedente valeur
  setlocale(LC_NUMERIC, oldnum) ;
  delete[] oldnum;
}

//=======================================================================
//function : Read
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::Read(Standard_IStream& IS, bool print_results)
{
 // on sauvegarde l'ancien LC_NUMERIC
  char *oldnum,*plocal ;
  plocal =   setlocale(LC_NUMERIC, NULL) ;
  oldnum = new char[strlen(plocal)+1] ;
  strcpy(oldnum,plocal);

  Clear();

  // Check the version
  char vers[101];
  do {
    IS.getline(vers,100,'\n');
    // BUC60769 PTV 18.10.2000: remove possible '\r' at the end of the line
    //Standard_Integer lv = strlen(vers);
    //char *pm;
    //if(pm = strchr(vers,'\r'))
    //  *pm ='\0';

    for (Standard_Integer lv = (strlen(vers)- 1); lv > 1 && (vers[lv] == '\r' || vers[lv] == '\n') ;lv--)
      vers[lv] = '\0';

  } while ( ! IS.fail() && strcmp(vers,dVersion) && strcmp(vers,dVersion2) );
  if (IS.fail()) {
    if (print_results)
      cout << "File was not written with this version of the topology"<<endl;
    setlocale(LC_NUMERIC, oldnum) ;
    delete[] oldnum;
    return;
  }
  if (strcmp(vers,dVersion2) == 0) myFormatNb = 2;
  else myFormatNb = 1;

  //-----------------------------------------
  // read the locations
  //-----------------------------------------

  myLocations.Read(IS);

  //-----------------------------------------
  // read the geometry
  //-----------------------------------------

  ReadGeometry(IS);

  //-----------------------------------------
  // read the shapes
  //-----------------------------------------

  std::string buffer;
  IS >> buffer;
  if (buffer != "TShapes") {
    if (print_results)
      cout << "Not a TShape table"<<endl;
    setlocale(LC_NUMERIC, oldnum) ;
    delete[] oldnum;
    return;
  }

  Standard_Integer i, nbShapes;
  IS >> nbShapes;

  for (i = 1; i <= nbShapes; i++) {

    TopoDS_Shape S;

    //Read type and create empty shape.
    TopAbs_ShapeEnum T = ReadShapeEnum(IS);
    ReadGeometry(T,IS,S);

    // Set the flags
    IS >> buffer;

    // sub-shapes
    TopoDS_Shape SS;
    do {
      Read(SS,IS,nbShapes);
      if (!SS.IsNull())
        myBuilder.Add(S,SS);
    } while(!SS.IsNull());

    S.Free      (buffer[0] == '1');
    S.Modified  (buffer[1] == '1');

    if (myFormatNb == 2)
      S.Checked   (buffer[2] == '1');
    else
      S.Checked   (Standard_False);     // force check at reading..

    S.Orientable(buffer[3] == '1');
    S.Closed    (buffer[4] == '1');
    S.Infinite  (buffer[5] == '1');
    S.Convex    (buffer[6] == '1');

    // check

    if (myFormatNb == 1)
      Check(T,S);

    myShapes.Add(S);
  }

  setlocale(LC_NUMERIC, oldnum) ;
  delete[] oldnum;
}

//=======================================================================
//function : WriteGeometry
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::WriteGeometry(Standard_OStream& OS) 
{
  myCurves2d.Write(OS);
  myCurves.Write(OS);
  WritePolygon3D(OS);
  WritePolygonOnTriangulation(OS);
  mySurfaces.Write(OS);
  WriteTriangulation(OS);
}

//=======================================================================
//function : ReadGeometry
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::ReadGeometry(Standard_IStream& IS)
{
  myCurves2d.Read(IS);
  myCurves.Read(IS);
  ReadPolygon3D(IS);
  ReadPolygonOnTriangulation(IS);
  mySurfaces.Read(IS);
  ReadTriangulation(IS);
}

//=======================================================================
//function : WritePolygon3D
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::WritePolygon3D(Standard_OStream&      OS,
                                        const Standard_Boolean Compact)
{
  Standard_Integer i, j, nbpol = myPolygons3D.Extent();
  if (Compact)
    OS << "Polygon3D " << nbpol << endl;
  else {
    OS << " -------\n";
    OS <<"Dump of " << nbpol << " Polygon3Ds\n";
    OS << " -------\n";
  }

  Handle(Poly_Polygon3D) P;
  for (i = 1; i <= nbpol; i++) {
    P = Handle(Poly_Polygon3D)::DownCast(myPolygons3D(i));
    if (Compact) {
      OS << P->NbNodes() << " ";
      OS << ((P->HasParameters()) ? "1" : "0") << "\n";
    }
    else {
      OS << "  "<< i << " : Polygon3D with " << P->NbNodes() << " Nodes\n";
      OS << ((P->HasParameters()) ? "with" : "without") << " parameters\n";
    }


    // write the deflection
    if (!Compact) OS << "Deflection : ";
    OS << P->Deflection() << "\n";

    // write the nodes
    if (!Compact) OS << "\nNodes :\n";

    Standard_Integer i1, nbNodes = P->NbNodes();
    const TColgp_Array1OfPnt& Nodes = P->Nodes();
    for (j = 1; j <= nbNodes; j++) {
      if (!Compact) OS << setw(10) << j << " : ";<--- First condition
      if (!Compact) OS << setw(17);<--- Second condition
      OS << Nodes(j).X() << " ";
      if (!Compact) OS << setw(17);
      OS << Nodes(j).Y() << " ";
      if (!Compact) OS << setw(17);
      OS << Nodes(j).Z();
      if (!Compact) OS << "\n";
      else OS << " ";
    }
    OS <<"\n";

    if (P->HasParameters()) {
      if (!Compact) OS << "\nParameters :\n";
      const TColStd_Array1OfReal& Param = P->Parameters();
      for ( i1 = 1; i1 <= nbNodes; i1++ ) {
        OS << Param(i1) << " ";
      }
      OS <<"\n";
    }
  }
}

//=======================================================================
//function : WritePolygonOnTriangulation
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::WritePolygonOnTriangulation(
                                          Standard_OStream&      OS,
                                          const Standard_Boolean Compact)
{
  Standard_Integer i, j, nbpOntri = myNodes.Extent();
  if (Compact)
    OS << "PolygonOnTriangulations " << nbpOntri << endl;
  else {
    OS << " -------\n";
    OS <<"Dump of " << nbpOntri << " PolygonOnTriangulations\n";
    OS << " -------\n";
  }

  Handle(Poly_PolygonOnTriangulation) Poly;
  Handle(TColStd_HArray1OfReal) Param;

  for (i=1; i<=nbpOntri; i++) {
    Poly = Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(i));
    const TColStd_Array1OfInteger& Nodes = Poly->Nodes();
    if (!Compact) {
      OS << "  "<< i << " : PolygonOnTriangulation with " << Nodes.Length() << " Nodes\n";
    }
    else OS << Nodes.Length()<<" ";
    if (!Compact) OS <<"  ";
    for (j=1; j <= Nodes.Length(); j++) OS << Nodes.Value(j) << " ";
    OS << "\n";

    // writing parameters:
    Param = Poly->Parameters();
    if (Compact) OS <<"p ";

    // write the deflection
    if (!Compact) OS << "  Deflection : ";
    OS <<Poly->Deflection() << " ";
    if (!Compact) OS << "\n";

    if (!Param.IsNull()) {
      if (!Compact) {
        OS << "  "<< "Parameters :";
      }
      else OS << "1 " ;
      if (!Compact) OS <<"  ";
      for (j=1; j <= Param->Length(); j++) OS << Param->Value(j) << " ";
      OS << "\n";
    }
    else OS <<"0 \n";
  }

}

//=======================================================================
//function : WriteTriangulation
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::WriteTriangulation(Standard_OStream&      OS,
                                        const Standard_Boolean Compact)
{
  Standard_Integer i, j, nbNodes, nbtri = myTriangulations.Extent();
  Standard_Integer nbTriangles = 0, n1, n2, n3;<--- Variable 'nbTriangles' is assigned a value that is never used.
  if (Compact)
    OS << "Triangulations " << nbtri << endl;
  else {
    OS << " -------\n";
    OS <<"Dump of " << nbtri << " Triangulations\n";
    OS << " -------\n";
  }

  Handle(Poly_Triangulation) T;
  for (i = 1; i <= nbtri; i++) {
    T = Handle(Poly_Triangulation)::DownCast(myTriangulations(i));
    if (Compact) {
      OS << T->NbNodes() << " " << T->NbTriangles() << " ";
      OS << ((T->HasUVNodes()) ? "1" : "0") << " ";
    }
    else {
      OS << "  "<< i << " : Triangulation with " << T->NbNodes() << " Nodes and "
         << T->NbTriangles() <<" Triangles\n";
      OS << "      "<<((T->HasUVNodes()) ? "with" : "without") << " UV nodes\n";
    }

    // write the deflection

    if (!Compact) OS << "  Deflection : ";
    OS <<T->Deflection() << "\n";

    // write the 3d nodes

    if (!Compact) OS << "\n3D Nodes :\n";

    nbNodes = T->NbNodes();
    const TColgp_Array1OfPnt& Nodes = T->Nodes();
    for (j = 1; j <= nbNodes; j++) {
      if (!Compact) OS << setw(10) << j << " : ";<--- First condition
      if (!Compact) OS << setw(17);<--- Second condition
      OS << Nodes(j).X() << " ";
      if (!Compact) OS << setw(17);
      OS << Nodes(j).Y() << " ";
      if (!Compact) OS << setw(17);
      OS << Nodes(j).Z();
      if (!Compact) OS << "\n";
      else OS << " ";
    }

    if (T->HasUVNodes()) {
      if (!Compact) OS << "\nUV Nodes :\n";
      const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
      for (j = 1; j <= nbNodes; j++) {
        if (!Compact) OS << setw(10) << j << " : ";<--- First condition
        if (!Compact) OS << setw(17);<--- Second condition
        OS << UVNodes(j).X() << " ";
        if (!Compact) OS << setw(17);
        OS << UVNodes(j).Y();
        if (!Compact) OS << "\n";
        else OS << " ";
      }
    }

    if (!Compact) OS << "\nTriangles :\n";
    nbTriangles = T->NbTriangles();
    const Poly_Array1OfTriangle& Triangles = T->Triangles();
    for (j = 1; j <= nbTriangles; j++) {
      if (!Compact) OS << setw(10) << j << " : ";
      Triangles(j).Get(n1, n2, n3);
      if (!Compact) OS << setw(10);
      OS << n1 << " ";
      if (!Compact) OS << setw(10);
      OS << n2 << " ";
      if (!Compact) OS << setw(10);
      OS << n3;
      if (!Compact) OS << "\n";
      else OS << " ";
    }
    OS << "\n";
  }

}
//=======================================================================
//function : WriteGeometry
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::WriteGeometry(const TopoDS_Shape& S,
                                        Standard_OStream&   OS)
{
  // Write the geometry

  if (S.ShapeType() == TopAbs_VERTEX) {

    // Write the point geometry
    TopoDS_Vertex V = TopoDS::Vertex(S);
    OS << BRep_Tool::Tolerance(V) << "\n";
    gp_Pnt p = BRep_Tool::Pnt(V);
    OS<<p.X()<<" "<<p.Y()<<" "<<p.Z()<<"\n";

    Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
    BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points());

    while (itrp.More()) {
      const Handle(BRep_PointRepresentation)& PR = itrp.Value();

      OS << PR->Parameter();
      if (PR->IsPointOnCurve()) {
        OS << " 1 " << myCurves.Index(PR->Curve());
      }

      else if (PR->IsPointOnCurveOnSurface()) {
        OS << " 2 " <<  myCurves2d.Index(PR->PCurve());
        OS << " " << mySurfaces.Index(PR->Surface());
      }

      else if (PR->IsPointOnSurface()) {
        OS << " 3 " << PR->Parameter2() << " ";
        OS << mySurfaces.Index(PR->Surface());
      }

      OS << " " << myLocations.Index(PR->Location());
      OS << "\n";

      itrp.Next();
    }

    OS << "0 0\n"; // end representations

  }

  else if (S.ShapeType() == TopAbs_EDGE) {

    // Write the curve geometry

    Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());

    OS << " " << TE->Tolerance() << " ";
    OS << ((TE->SameParameter()) ? 1 : 0) << " ";
    OS << ((TE->SameRange())     ? 1 : 0) << " ";
    OS << ((TE->Degenerated())   ? 1 : 0) << "\n";

    Standard_Real first, last;
    BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
    while (itrc.More()) {
      const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
      if (CR->IsCurve3D()) {
        if (!CR->Curve3D().IsNull()) {
          Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
          GC->Range(first, last);
          OS << "1 ";                               // -1- Curve 3D
          OS << " "<<myCurves.Index(CR->Curve3D());
          OS << " "<<myLocations.Index(CR->Location());
          OS << " "<<first<<" "<<last;
          OS << "\n";
        }
      }
      else if (CR->IsCurveOnSurface()) {
        Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
        GC->Range(first, last);
        if (!CR->IsCurveOnClosedSurface())
          OS << "2 ";                             // -2- Curve on surf
        else
          OS << "3 ";                             // -3- Curve on closed surf
        OS <<" "<<myCurves2d.Index(CR->PCurve());
        if (CR->IsCurveOnClosedSurface()) {
          OS <<" " << myCurves2d.Index(CR->PCurve2());
          PrintRegularity(CR->Continuity(),OS);
        }
        OS << " " << mySurfaces.Index(CR->Surface());
        OS << " " << myLocations.Index(CR->Location());
        OS << " "<<first<<" "<<last;
        OS << "\n";

        // Write UV Points // for XML Persistence higher performance
        if (myFormatNb == 2)
        {
          gp_Pnt2d Pf,Pl;
          if (CR->IsCurveOnClosedSurface()) {
            Handle(BRep_CurveOnClosedSurface) COCS =
              Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
            COCS->UVPoints2(Pf,Pl);
          }
          else {
            Handle(BRep_CurveOnSurface) COS =
              Handle(BRep_CurveOnSurface)::DownCast(CR);
            COS->UVPoints(Pf,Pl);
          }
          OS << Pf.X() << " " << Pf.Y() << " " << Pl.X() << " " << Pl.Y() << "\n";
        }
      }
      else if (CR->IsRegularity()) {
        OS << "4 ";                              // -4- Regularity
        PrintRegularity(CR->Continuity(),OS);
        OS << " "<<mySurfaces.Index(CR->Surface());
        OS << " "<<myLocations.Index(CR->Location());
        OS << " "<<mySurfaces.Index(CR->Surface2());
        OS << " "<<myLocations.Index(CR->Location2());
        OS << "\n";
      }
      itrc.Next();
    }
    OS << "0\n"; // end of the list of representations
  }
   else if (S.ShapeType() == TopAbs_FACE) {

    Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
    const TopoDS_Face& F = TopoDS::Face(S);

    if (!(TF->Surface()).IsNull()) {
      OS << ((BRep_Tool::NaturalRestriction(F)) ? 1 : 0);
      OS << " ";
      // Write the surface geometry
      OS << " " <<TF->Tolerance();
      OS << " " <<mySurfaces.Index(TF->Surface());
      OS << " " <<myLocations.Index(TF->Location());
      OS << "\n";
    }
  }

}


//=======================================================================
//function : Write
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::Write(const TopoDS_Shape& S,
                               Standard_OStream& OS,
                               TDF_Label* l_attr)
{
  if (S.IsNull()) OS << "*";
  else {
    PrintOrientation(S.Orientation(),OS,Standard_True);
    OS << myShapes.Extent() - myShapes.FindIndex(S.Located(TopLoc_Location())) + 1;
    OS << " " << myLocations.Index(S.Location()) << " ";
  }
  //Write Attributes
  Standard_Integer i, nbShapes = myShapes.Extent();
  if(l_attr != NULL)
  {
    for ( i = 1; i <= nbShapes; i++)
    {
      const TopoDS_Shape& Sh = myShapes(i);
      WriteAttribute(Sh, OS, *l_attr);
    }
  }
}


//=======================================================================
//function : ReadGeometry
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::ReadGeometry(const TopAbs_ShapeEnum T,
                                         Standard_IStream&      IS,
                                         TopoDS_Shape&          S)
{
  // Read the geometry

  Standard_Integer val,c,pc,pc2,s,s2,l,l2,t, pt, pt2;
  Standard_Real tol,X,Y,Z,first,last,p1,p2;
  Standard_Real PfX,PfY,PlX,PlY;
  gp_Pnt2d aPf, aPl;
  Standard_Boolean closed;
#ifndef DEB
  GeomAbs_Shape reg = GeomAbs_C0;
#else
  GeomAbs_Shape reg;
#endif
  switch (T) {


    //---------
    // vertex
    //---------

  case TopAbs_VERTEX :
    {
      TopoDS_Vertex& V = TopoDS::Vertex(S);

      // Read the point geometry
      IS >> tol;
      IS >> X >> Y >> Z;
      myBuilder.MakeVertex(V,gp_Pnt(X,Y,Z),tol);
      Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(V.TShape());

      BRep_ListOfPointRepresentation& lpr = TV->ChangePoints();
      TopLoc_Location L;

      do {
        IS >> p1 >> val;

        Handle(BRep_PointRepresentation) PR;
        switch (val) {

        case 1 :
          {
            IS >> c;

            if (myCurves.Curve(c).IsNull())
              break;

            Handle(BRep_PointOnCurve) POC =
              new BRep_PointOnCurve(p1,
                                    myCurves.Curve(c),
                                    L);
            PR = POC;
          }
          break;

        case 2 :
          {
            IS >> pc >> s;

            if (myCurves2d.Curve2d(pc).IsNull() ||
                mySurfaces.Surface(s).IsNull())
              break;

            Handle(BRep_PointOnCurveOnSurface) POC =
              new BRep_PointOnCurveOnSurface(p1,
                                             myCurves2d.Curve2d(pc),
                                             mySurfaces.Surface(s),
                                             L);
            PR = POC;
          }
          break;

        case 3 :
          {
            IS >> p2 >> s;

            if (mySurfaces.Surface(s).IsNull())
              break;

            Handle(BRep_PointOnSurface) POC =
              new BRep_PointOnSurface(p1,p2,
                                      mySurfaces.Surface(s),
                                      L);
            PR = POC;
          }
          break;
        }

        if (val > 0) {
          IS >> l;
          if (!PR.IsNull()) {
            PR->Location(myLocations.Location(l));
            lpr.Append(PR);
          }
        }
      } while (val > 0);
    }
    break;


    //---------
    // edge
    //---------


    case TopAbs_EDGE :

      // Create an edge
      {
        TopoDS_Edge& E = TopoDS::Edge(S);

        myBuilder.MakeEdge(E);

        // Read the curve geometry
        IS >> tol;
        IS >> val;
        myBuilder.SameParameter(E,(val == 1));
        IS >> val;
        myBuilder.SameRange(E,(val == 1));
        IS >> val;
        myBuilder.Degenerated(E,(val == 1));

        do {
          IS >> val;
          switch (val) {

          case 1 :                               // -1- Curve 3D
            IS >> c >> l;
            if (!myCurves.Curve(c).IsNull()) {
              myBuilder.UpdateEdge(E,myCurves.Curve(c),
                                   myLocations.Location(l),tol);
            }
            IS >> first >> last;
            if (!myCurves.Curve(c).IsNull()) {
              Standard_Boolean Only3d = Standard_True;
              myBuilder.Range(E,first,last,Only3d);
            }
            break;


          case 2 :                               // -2- Curve on surf
          case 3 :                               // -3- Curve on closed surf
            closed = (val == 3);
            IS >> pc;
            if (closed) {
              IS >> pc2;
              reg = ReadRegularity(IS);
            }

            // surface, location
            IS >> s >> l;

            // range
            IS >> first >> last;

            // read UV Points // for XML Persistence higher performance
            if (myFormatNb == 2)
            {
              IS >> PfX >> PfY >> PlX >> PlY;
              aPf = gp_Pnt2d(PfX,PfY);
              aPl = gp_Pnt2d(PlX,PlY);
            }

            if (myCurves2d.Curve2d(pc).IsNull() ||
                (closed && myCurves2d.Curve2d(pc2).IsNull()) ||
                mySurfaces.Surface(s).IsNull())
              break;

            if (closed) {
              if (myFormatNb == 2)
                myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
                                     myCurves2d.Curve2d(pc2),
                                     mySurfaces.Surface(s),
                                     myLocations.Location(l),tol,
                                     aPf, aPl);
              else
                myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
                                     myCurves2d.Curve2d(pc2),
                                     mySurfaces.Surface(s),
                                     myLocations.Location(l),tol);

              myBuilder.Continuity(E,
                                   mySurfaces.Surface(s),
                                   mySurfaces.Surface(s),
                                   myLocations.Location(l),
                                   myLocations.Location(l),
                                   reg);
            }
            else
            {
              if (myFormatNb == 2)
                myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
                                     mySurfaces.Surface(s),
                                     myLocations.Location(l),tol,
                                     aPf, aPl);
              else
                myBuilder.UpdateEdge(E,myCurves2d.Curve2d(pc),
                                     mySurfaces.Surface(s),
                                     myLocations.Location(l),tol);
            }
            myBuilder.Range(E,
                            mySurfaces.Surface(s),
                            myLocations.Location(l),
                            first,last);
            break;

          case 4 :                               // -4- Regularity
            reg = ReadRegularity(IS);
            IS >> s >> l >> s2 >> l2;
            if (mySurfaces.Surface(s).IsNull() ||
                mySurfaces.Surface(s2).IsNull())
              break;
            myBuilder.Continuity(E,
                                 mySurfaces.Surface(s),
                                 mySurfaces.Surface(s2),
                                 myLocations.Location(l),
                                 myLocations.Location(l2),
                                 reg);
            break;

          case 5 :   // -5- Polygon3D
            IS >> c >> l;
            myBuilder.UpdateEdge(E,Handle(Poly_Polygon3D)::DownCast(myPolygons3D(c)), myLocations.Location(l));
            break;

          case 6 :
          case 7 :
            closed = (val == 7);
            IS >> pt;
            if (closed) {
              IS >> pt2;
            }
            IS >> t >> l;
            if (closed) {
              myBuilder.UpdateEdge
                (E, Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
                 Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt2)),
                 Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
                 myLocations.Location(l));
            }
            else {
              myBuilder.UpdateEdge
                (E,Handle(Poly_PolygonOnTriangulation)::DownCast(myNodes(pt)),
                 Handle(Poly_Triangulation)::DownCast(myTriangulations(t)),
                 myLocations.Location(l));
            }
            // range

            break;

          }
        } while (val > 0);
      }
    break;


    //---------
    // wire
    //---------

  case TopAbs_WIRE :
    myBuilder.MakeWire(TopoDS::Wire(S));
    break;


    //---------
    // face
    //---------
  case TopAbs_FACE :
    {
    // create a face :
    TopoDS_Face& F = TopoDS::Face(S);
    myBuilder.MakeFace(F);

    IS >> val; // natural restriction
    if (val == 0 || val == 1) {
      IS >> tol >> s >> l;
      if (!mySurfaces.Surface(s).IsNull()) {
        myBuilder.UpdateFace(TopoDS::Face(S),
                             mySurfaces.Surface(s),
                             myLocations.Location(l),tol);
        myBuilder.NaturalRestriction(TopoDS::Face(S),(val == 1));
      }
    }

    // BUC60769
    std::string line;
    std::getline( IS, line );
    std::getline( IS, line );
    std::istringstream str( line );

    if (str.get() == '2') {
      // cas triangulation
      str >> s;
      myBuilder.UpdateFace(TopoDS::Face(S),
                           Handle(Poly_Triangulation)::DownCast(myTriangulations(s)));
    }
//    else IS.seekg(pos);
    }
    break;


    //---------
    // shell
    //---------

  case TopAbs_SHELL :
    myBuilder.MakeShell(TopoDS::Shell(S));
    break;


    //---------
    // solid
    //---------

  case TopAbs_SOLID :
    myBuilder.MakeSolid(TopoDS::Solid(S));
    break;


    //---------
    // compsolid
    //---------

  case TopAbs_COMPSOLID :
    myBuilder.MakeCompSolid(TopoDS::CompSolid(S));
    break;


    //---------
    // compound
    //---------

  case TopAbs_COMPOUND :
    myBuilder.MakeCompound(TopoDS::Compound(S));
    break;

  default:
    break;
  }

}

//=======================================================================
//function : ReadPolygonOnTriangulation
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::ReadPolygonOnTriangulation(Standard_IStream& IS)
{
    std::string buffer;
    IS >> buffer;
    if (buffer.find("PolygonOnTriangulations") == std::string::npos)
      return;

    Standard_Integer i, j, val, nbpol = 0, nbnodes =0;
    Standard_Integer hasparameters;
    Standard_Real par;
    Handle(TColStd_HArray1OfReal) Param;
    Handle(Poly_PolygonOnTriangulation) Poly;
    IS >> nbpol;
    for (i=1; i<=nbpol; i++) {
      IS >> nbnodes;
      TColStd_Array1OfInteger Nodes(1, nbnodes);
      for (j = 1; j <= nbnodes; j++) {
        IS >> val;
        Nodes(j) = val;
      }
      IS >> buffer;
      Standard_Real def;
      IS >> def;
      IS >> hasparameters;
      if (hasparameters) {
        TColStd_Array1OfReal Param1(1, nbnodes);
        for (j = 1; j <= nbnodes; j++) {
          IS >> par;
          Param1(j) = par;
        }
        Poly = new Poly_PolygonOnTriangulation(Nodes, Param1);
      }
      else Poly = new Poly_PolygonOnTriangulation(Nodes);
      Poly->Deflection(def);
      myNodes.Add(Poly);
    }
}

//=======================================================================
//function : ReadPolygon3D
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::ReadPolygon3D(Standard_IStream& IS)
{
    std::string buffer;
    //  Standard_Integer i, j, p, val, nbpol, nbnodes, hasparameters;
    Standard_Integer i, j, p, nbpol=0, nbnodes =0, hasparameters = Standard_False;  Standard_Real d, x, y, z;

    IS >> buffer;
    if (buffer.find("Polygon3D") == std::string::npos)
      return;
  
    Handle(Poly_Polygon3D) P;
    IS >> nbpol;
    for (i=1; i<=nbpol; i++) {
      IS >> nbnodes;
      IS >> hasparameters;
      TColgp_Array1OfPnt Nodes(1, nbnodes);
      IS >> d;
      for (j = 1; j <= nbnodes; j++) {
        IS >> x >> y >> z;
        Nodes(j).SetCoord(x,y,z);
      }
      if (hasparameters) {
        TColStd_Array1OfReal Param(1,nbnodes);
        for (p = 1; p <= nbnodes; p++) {
          IS >> Param(p);
        }
        P = new Poly_Polygon3D(Nodes, Param);
      }
      else P = new Poly_Polygon3D(Nodes);
      P->Deflection(d);
      myPolygons3D.Add(P);
    }
}

//=======================================================================
//function : ReadTriangulation
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::ReadTriangulation(Standard_IStream& IS)
{
  std::string buffer;
  //  Standard_Integer i, j, val, nbtri;
  Standard_Integer i, j, nbtri =0;
  Standard_Real d, x, y, z;
  Standard_Integer nbNodes =0, nbTriangles=0;
  Standard_Boolean hasUV= Standard_False;

  Handle(Poly_Triangulation) T;

  IS >> buffer;
  if (buffer.find("Triangulations") != std::string::npos) {
    IS >> nbtri;
    for (i=1; i<=nbtri; i++) {
      IS >> nbNodes >> nbTriangles >> hasUV;
      IS >> d;

      TColgp_Array1OfPnt Nodes(1, nbNodes);
      TColgp_Array1OfPnt2d UVNodes(1, nbNodes);

      for (j = 1; j <= nbNodes; j++) {
        IS >> x >> y >> z;
        Nodes(j).SetCoord(x,y,z);
      }

      if (hasUV) {
        for (j = 1; j <= nbNodes; j++) {
          IS >> x >> y;
          UVNodes(j).SetCoord(x,y);
        }
      }


      // read the triangles
      Standard_Integer n1,n2,n3;
      Poly_Array1OfTriangle Triangles(1, nbTriangles);
      for (j = 1; j <= nbTriangles; j++) {
        IS >> n1 >> n2 >> n3;
        Triangles(j).Set(n1,n2,n3);
      }

      if (hasUV) T =  new Poly_Triangulation(Nodes,UVNodes,Triangles);
      else T = new Poly_Triangulation(Nodes,Triangles);

      T->Deflection(d);

      myTriangulations.Add(T);
    }
  }
}

//=======================================================================
//function : Clear
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::Clear()
{
  mySurfaces.Clear();
  myCurves.Clear();
  myCurves2d.Clear();
  myPolygons3D.Clear();
  myPolygons2D.Clear();
  myNodes.Clear();
  myTriangulations.Clear();
  myShapes.Clear();
  my_ShapeNum_Location.clear();
  myLocations.Clear();
}

//=======================================================================
//function : Read
//purpose  :
//=======================================================================

void  OCCShapeAttributeSet::Read(TopoDS_Shape& S,
                                 Standard_IStream& IS,
                                 const int nbshapes,
                                 TDF_Label* label )
{
  std::string buffer, buffer_attr;
  IS >> buffer;
  std::map<int,int>::iterator it;
  if (buffer[0] == '*')
    S = TopoDS_Shape();
  else {
    char type;
    int num;
    std::istringstream buffstr(buffer);
    buffstr >> type >> num;
    S = myShapes(nbshapes - num + 1);
    switch (type) {

    case '+' :
      S.Orientation(TopAbs_FORWARD);
      break;

    case '-' :
      S.Orientation(TopAbs_REVERSED);
      break;

    case 'i' :
      S.Orientation(TopAbs_INTERNAL);
      break;

    case 'e' :
      S.Orientation(TopAbs_EXTERNAL);
      break;
    }

    Standard_Integer l;
    IS >> l;
    S.Location(myLocations.Location(l));

    int shape_num = nbshapes - num + 1;
    typedef std::pair <int,int> sh_loc_pair; 
    it = my_ShapeNum_Location.find(shape_num);
    if(it == my_ShapeNum_Location.end())
      my_ShapeNum_Location.insert(sh_loc_pair(shape_num, l));
  }
  if(label != NULL)
  {
    Standard_Integer i, nbShapes = myShapes.Extent();
    for ( i = 1; i <= nbShapes; i++)
    {
      TopoDS_Shape Sh = myShapes(i);
      IS >> buffer_attr;
      if(buffer_attr[0] != '*' && buffer_attr[0] != 'C')
        break;
      if(buffer_attr[0] == '*') //empty attributes for this shape
        continue;
      it = my_ShapeNum_Location.find(i);
      if(it != my_ShapeNum_Location.end())
      {
        int loc_num = it->second;
        if (loc_num > 0)
          Sh.Location(myLocations.Location(loc_num));
      }
      ReadAttribute(Sh, IS,*label);
    }
  }
}

//=======================================================================
//function : Check
//purpose  :
//=======================================================================

void OCCShapeAttributeSet::Check(const TopAbs_ShapeEnum T,
                                 TopoDS_Shape&          S)
{
  if (T == TopAbs_FACE) {
    const TopoDS_Face& F = TopoDS::Face(S);
    BRepTools::Update(F);
  }
}

//=======================================================================
//function : NbShapes
//purpose  :
//=======================================================================

int  OCCShapeAttributeSet::NbShapes() 
{
  return myShapes.Extent();
}