This file is indexed.

/usr/share/cgnstools/cgnsplot.tcl is in cgns-convert 3.3.0-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
#!/bin/sh
# the next line restarts using wish \
exec plotwish -f "$0" "$@"

proc error_exit {msg} {
  wm withdraw .
  tk_dialog .error Error $msg error 0 Exit
  exit 1
}

if {[catch {package require Tk 8.0} msg]} {
  error_exit $msg
}

#---------- get platform

set platform $tcl_platform(platform)
if {$platform == "windows" && [info exists env(TERM)] &&
    ($env(TERM) == "cygwin" || $env(TERM) == "xterm")} {
  set platform cygwin
}

#----- get startup directory and name

set cmd_name [file tail $argv0]
set cmd_dir  [file dirname $argv0]
if {![file exists $argv0] || [file isdirectory $argv0]} {
  if {$platform == "windows"} {
    set sep ";"
  } else {
    set sep ":"
  }
  foreach i [split $env(PATH) $sep] {
    if {$sep == ";"} {
      set i [join [split $i \\] /]
    }
    if {[file exists $i/$cmd_name] && ![file isdirectory $i/$cmd_name]} {
      set cmd_dir $i
      break;
    }
  }
}
set curdir [pwd]
if ![catch {cd $cmd_dir}] {
  set cmd_dir [pwd]
  cd $curdir
}
if {$tcl_platform(platform) == "windows"} {
  set cmd_dir [file attributes $cmd_dir -shortname]
}

#----- set path to tcl scripts

set auto_path "$cmd_dir $cmd_dir/../common $auto_path"
if {[info exists env(TCL_PROC_DIR)]} {
  set auto_path "$env(TCL_PROC_DIR) $auto_path"
}

#----- initialize

if {$platform == "windows"} {
  set vers [join [split $tcl_version .] {}]
  if {[info commands CGNSopen] == {}} {
    if {[catch {load cgnstcl$vers} msg]} {
      error_exit $msg
    }
  }
  catch {load tclreg$vers registry}
} else {
  if {[info commands CGNSopen] == {}} {
    error_exit "need to run script with plotwish"
  }
}

if {[catch {package require Tkogl} msg]} {
  error_exit $msg
}

if [catch {config_defaults 1} msg] {error_exit $msg}

array set ProgData {
  winwidth 700
  winheight 500
  seppos 0.25
  sepwd 7
  cgnsfile ""
  displaylist ""
  axislist ""
  planelist ""
  cutlist ""
  background {0.0 0.0 0.0}
  autocolor 1
  showcolors 1
  showlines 1
  twosided 0
  culling disable
  revnorm 0
  axis 0
  meshvis 1
  meshmode 1
  regvis 0
  regmode 2
  edgeangle 60
  fov 30
  np 0.025
  fp 2.0
  bases {}
  nzones 0
  zones {}
  curbase ""
  curnode ""
  curdim 0
  curclr ""
  curmode 0
  cutplane ""
  cutcolor {0.7 0.7 0.4 0.5}
  cutmode 1
  usecutclr 0
  ignorevis 0
  drawID ""
  dotrace 0
  reg,file ".cgnstools"
  reg,base "HKEY_CURRENT_USER/Software/CGNS"
  reg,key  "CGNSplot"
  reg,vals {cgnsfile background autocolor twosided culling \
            axis edgeangle meshvis meshmode regvis regmode fov \
            winwidth winheight seppos cutcolor usecutclr ignorevis \
            showcolors showlines}
}

#----- read registry

if {[tclreg_init -base $ProgData(reg,base) -fname $ProgData(reg,file)]} {
  foreach i $ProgData(reg,vals) {
    if {![catch {tclreg_get $ProgData(reg,key) $i} val] && $val != ""} {
      set ProgData($i) $val
    }
  }
}

#---------- main window

wm title . "CGNSplot"
wm protocol . WM_DELETE_WINDOW do_quit

proc do_quit {} {
  global ProgData
  catch CGNSclose
  set ProgData(winwidth) [winfo width .main]
  set ProgData(winheight) [winfo height .main]
  foreach i $ProgData(reg,vals) {
    catch {tclreg_set $ProgData(reg,key) $i $ProgData($i)}
  }
  catch tclreg_close
  catch {WinHtml close}
  destroy .
  exit 0
}

#----- menu

menubar_create {File Options View Display Help}

#----- file menu

set m [menubar_get File]
$m add command -label "Load CGNS..." -command load_cgns
$m add separator
$m add command -label "Quit" -command do_quit

#----- options menu

set m [menubar_get Options]
$m add command -label "Set Defaults..." -command set_defaults
$m add command -label "Set Perspective..." -command set_perspective

#----- view menu

set m [menubar_get View]
$m add command -label "Open Level" -command tree_open
$m add command -label "Close Level" -command tree_close
$m add separator
$m add command -label "Reset View" -command {reset_view; $OGLwin redraw}
$m add command -label "Center View" -command Center

#----- display menu

set m [menubar_get Display]
$m add checkbutton -label "Two Sided" -command set_twosided \
  -variable ProgData(twosided) -onvalue 1 -offvalue 0
$m add checkbutton -label "Backface Culling" -command set_culling \
  -variable ProgData(culling) -onvalue enable -offvalue disable
#$m add checkbutton -label "Reverse Normals" -command set_normals \
#  -variable ProgData(revnorm) -onvalue 1 -offvalue 0
$m add checkbutton -label "Display Axis" -command set_axis \
  -variable ProgData(axis) -onvalue 1 -offvalue 0

if {[info commands OGLcutplane] != ""} {
  $m add command -label "Cutting Plane..." -command cutplane_control
}

proc set_twosided {} {
  global ProgData OGLwin
  if {$ProgData(twosided)} {
    .toolbar.but.b3.twosided configure -relief sunken
    set_balloon .toolbar.but.b3.twosided "Disable Two-Sided Lighting"
  } else {
    .toolbar.but.b3.twosided configure -relief raised
    set_balloon .toolbar.but.b3.twosided "Enable Two-Sided Lighting"
  }
  $OGLwin eval -lightmodel lightmodeltwoside $ProgData(twosided)
  $OGLwin redraw
}

proc set_culling {} {
  global ProgData OGLwin
  if {$ProgData(culling) == "enable"} {
    .toolbar.but.b3.culling configure -relief sunken
    set_balloon .toolbar.but.b3.culling "Disable Backface Culling"
  } else {
    .toolbar.but.b3.culling configure -relief raised
    set_balloon .toolbar.but.b3.culling "Enable Backface Culling"
  }
  $OGLwin eval -$ProgData(culling) cullface
  $OGLwin redraw
}

proc set_normals {} {
  global ProgData OGLwin
  if {$ProgData(revnorm)} {
    .toolbar.but.b3.revnorm configure -relief sunken
    set_balloon .toolbar.but.b3.revnorm "Default Face Normals"
  } else {
    .toolbar.but.b3.revnorm configure -relief raised
    set_balloon .toolbar.but.b3.revnorm "Reverse Face Normals"
  }
}

proc set_axis {} {
  global ProgData OGLwin
  if {$ProgData(axis)} {
    .toolbar.but.b3.axis configure -relief sunken
    set_balloon .toolbar.but.b3.axis "Disable Axis Display"
  } else {
    .toolbar.but.b3.axis configure -relief raised
    set_balloon .toolbar.but.b3.axis "Enable Axis Display"
  }
  OGLaxis $ProgData(axis)
  $OGLwin redraw
}

#----- help menu

set m [menubar_get Help]
$m add command -label "CGNSplot Help..." \
  -command {help_show cgnstools/cgnsplot/index.html}
$m add command -label "CGNS Help..." -command {help_show}
$m add separator
$m add command -label "Configure Help..." -command help_setup
$m add separator
$m add command -label "About..." -underline 0 -command do_about

image create photo img_about -data {\
R0lGODlhIAAgALMAAAAA/wD//4QAAMbGxv8AAP//////////////////////////////////\
/////////ywAAAAAIAAgAAAEv3AMIGelEttbN8+fp3mkGGpj2plgawluiZ6XAJMooO98v9sC\
H881cwkINtyqyBocgaoOs3hE2qI0oqQKvMWas6o1eRKaxd2gT0sUj8my7MSdvo3AFfebfJ/m\
CYCAdTA5Zj+BgYM9AQEAjI6MjYx6e1CRko+RkJSVNpiYkJmTiIJ1mp+hnwE2iHWphmeJXUKi\
p5eSXEC2l6mqorO3r7e+tQK1tb3ExMmOsM7Ow8rSydHU09HD1tfYx9nYvc/hQhEAADs=}

proc do_about {} {
  global ProgData
  dialog .about -1 -1 "About CGNSplot" \
"CGNSplot Version 3.2

Bruce Wedan
leavingdust@gmail.com" img_about 0 Close
}

#---------- toolbar

frame .toolbar
pack .toolbar -side top -pady 2 -fill x

set f [frame .toolbar.but]
pack $f -side left

#---

image create photo img_open -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQ4EMlJKwJvZcC7BxdnSV04nCgKjtR6vZgmZ49L\
bmlus7xV9j4QQPYRtWbI3RCXU10WgKaTVPQAexEAOw==}

button $f.open -image img_open -takefocus 0 \
  -command {load_cgns ""}
pack $f.open -side left -padx 5
set_balloon $f.open "Open a CGNS File..."

#---

image create photo img_colors -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwRFEMlJ63TYsc2ab1QiJku5KKhCPewDvMAhH+Fo\
nimVadwHTi0XbEazGI/IyohkSqkmy5tTl+F0PlSM1bdqwWKzLutLTB4jADs=}

image create photo img_viewing -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQqEMlJqwT2uo0z6JkFht44fdSnVuipmiXpyUhL\
wiwMbA5onzzcj3bB0ToRADs=}

set b [frame $f.b1]
pack $b -side left -padx 5

button $b.colors -image img_colors -takefocus 0 \
  -command set_defaults
set_balloon $b.colors "Set Defaults..."

button $b.viewing -image img_viewing -takefocus 0 \
  -command set_perspective
set_balloon $b.viewing "Set Perspective..."

pack $b.colors $b.viewing -side left -padx 1

#---

image create photo img_expand -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAAAQvEMlJq712DJzX5pP2VQBIWuWVUitBTCsMlMTz\
IrHl3jnYUy7J7Cep8UA7k3IpiQAAOw==}

image create photo img_collapse -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAAAQkEMlJq7046wrwGBQgXsMCTh35hRvbSqm1urJJ\
V98Jvzvv/5UIADs=}

image create photo img_reset -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAAAQiEMlJKwXYWqA17xXGjQgZguA3qWhbni58vaQY\
vzHb2XdvRQA7}

image create photo img_center -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQmEMmJAL22Ypmp/dolduJYcqiXqiB5umcFwG9s\
YzOd07fZy7yfJAIAOw==}

set b [frame $f.b2]
pack $b -side left -padx 5

button $b.expand -image img_expand -takefocus 0 -command tree_open
set_balloon $b.expand "Open One Level"

button $b.collapse -image img_collapse -takefocus 0 -command tree_close
set_balloon $b.collapse "Close One Level"

button $b.reset -image img_reset -takefocus 0 \
  -command {reset_view; $OGLwin redraw}
set_balloon $b.reset "Reset View"

button $b.center -image img_center -takefocus 0 \
  -command Center
set_balloon $b.center "Center View"

pack $b.expand $b.collapse $b.reset $b.center -side left

#---

image create photo img_twosided -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwRGEMlJKwLnUMZoBtSyPNwjWpJIMuaCbh2KTQvn\
Sgd4aak95S/OyyIc0nyyG6KIWOgwB9GoJP1ApavW4iMBKJnOV42hNDIpEQA7}

image create photo img_culling -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAAAQqEMlJq5UgX6oxuF0VhuCHfKR3YlM3spzZwpxI\
z/Vq56nc+zcRKlXaGC0RADs=}

image create photo img_axis -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQwEEkJUJ04o5V6p9o2hJlAnteJLaOKKIqJujNt\
Z+ydY1UqwYKAMGAp/mIqgNKm9J0iADs=}

image create photo img_revnorm -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAAAQ5EMlJq6XsIsAn+1rnhdsEMMCVStzpiCwLODRD\
v2s634595zIeDVYK4iw600q1RGSYTc3l85SOrNgIADs=}

image create photo img_cutting -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQwEMlJq5UEkFsBQB6HbGNpXp+UimDIErBJWjFL\
rSuH22DP+7wNbFYhCTEXI4b4m0QAADs=}

set b [frame $f.b3]
pack $b -side left -padx 5

button $b.twosided -image img_twosided -takefocus 0 \
  -command toggle_twosided
if {$ProgData(twosided)} {
  $b.twosided configure -relief sunken
  set_balloon $b.twosided "Disable Two-Sided Lighting"
} else {
  set_balloon $b.twosided "Enable Two-Sided Lighting"
}

button $b.culling -image img_culling -takefocus 0 \
  -command toggle_culling
if {$ProgData(culling) == "enable"} {
  $b.culling configure -relief sunken
  set_balloon $b.culling "Disable Backface Culling"
} else {
  set_balloon $b.culling "Enable Backface Culling"
}

button $b.revnorm -image img_revnorm -takefocus 0 \
  -command toggle_normals
if {$ProgData(revnorm)} {
  $b.revnorm configure -relief sunken
  set_balloon $b.revnorm "Default Face Normals"
} else {
  set_balloon $b.revnorm "Reverse Face Normals"
}

button $b.axis -image img_axis -takefocus 0 \
  -command toggle_axis
if {$ProgData(axis)} {
  $b.axis configure -relief sunken
  set_balloon $b.axis "Disable Axis Display"
} else {
  set_balloon $b.axis "Enable Axis Display"
}

#pack $b.twosided $b.culling $b.revnorm $b.axis -side left -padx 1
pack $b.twosided $b.culling $b.axis -side left -padx 1

if {[info commands OGLcutplane] != ""} {
  button $b.cutting -image img_cutting -takefocus 0 \
    -command cutplane_control
  set_balloon $b.cutting "Cutting Plane..."
  pack $b.cutting -side left -padx 1
}

proc toggle_twosided {} {
  global ProgData
  if {$ProgData(twosided)} {
    set ProgData(twosided) 0
  } else {
    set ProgData(twosided) 1
  }
  set_twosided
}

proc toggle_culling {} {
  global ProgData
  if {$ProgData(culling) == "enable"} {
    set ProgData(culling) disable
  } else {
    set ProgData(culling) enable
  }
  set_culling
}

proc toggle_normals {} {
  global ProgData
  if {$ProgData(revnorm)} {
    set ProgData(revnorm) 0
  } else {
    set ProgData(revnorm) 1
  }
  set_normals
}

proc toggle_axis {} {
  global ProgData
  if {$ProgData(axis)} {
    set ProgData(axis) 0
  } else {
    set ProgData(axis) 1
  }
  set_axis
}

#---

image create photo img_help -data {\
R0lGODlhEAAQALMAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A\
/wD//////yH5BAEAAAgALAAAAAAQABAAQwQiEMlJq50kX5kJ1hvShd+4mSJ4qmTrXl28ehw7\
t+j75joVAQA7}

button $f.help -image img_help -takefocus 0 \
  -command {help_show cgnstools/cgnsplot/index.html}
set_balloon $f.help "CGNSplot Help..."

pack $f.help -side left -padx 5

frame .toolsep -bd 1 -height 2 -relief sunken
pack .toolsep -side top -fill x

#----- status bar

set ProgData(status) ""
label .status -textvariable ProgData(status) -relief sunken -anchor w
pack .status -side bottom -fill x -padx 5 -pady 3

proc display_message {msg} {
  global ProgData
  set ProgData(status) $msg
  update idletasks
}

#---------- main window

frame .main -width $ProgData(winwidth) -height $ProgData(winheight)
pack .main -side top -fill both -expand 1 -padx 5 -pady 2

#--- window seperator

frame .main.sep -width 4 -bd 2 -relief raised -cursor sb_h_double_arrow

bind .main.sep <ButtonPress-1> sep_begin_move

proc sep_begin_move {} {
  set width [winfo width .main]
  set x [winfo rootx .main.sep]
  set y [winfo rooty .main.sep]
  set h [winfo height .main.sep]
  set xmin [expr [winfo rootx .main] + 100]
  set xmax [expr [winfo rootx .main] + $width - 100]

  set top [toplevel .main.move -borderwidth 1 \
    -relief raised -cursor sb_h_double_arrow]
  wm overrideredirect $top 1
  wm geom $top "4x$h+$x+$y"

  update idletasks
  grab set $top
  bind $top <ButtonRelease-1> "sep_end_move $top $xmin $xmax %X"
  bind $top <Motion> "sep_move $top $xmin $xmax %X $y"
}

proc sep_move {top xmin xmax x y} {
  if {$x < $xmin} {
    set x $xmin
  } elseif {$x > $xmax} {
    set x $xmax
  }
  wm geom $top "+$x+$y"
}

proc sep_end_move {top xmin xmax x} {
  global ProgData
  destroy $top
  if {$x < $xmin} {
    set x $xmin
  } elseif {$x > $xmax} {
    set x $xmax
  }
  set s [expr double($x - [winfo rootx .main]) / \
    double([winfo width .main])]
  sep_locate $s
}

proc sep_locate {s} {
  global ProgData
  if [expr $s < 0.5] {
    place .main.sel -relx 0 -x 0 \
      -relwidth $s -width -$ProgData(sepwd) \
      -rely 0 -relheight 1
    place .main.plot -relx $s -x $ProgData(sepwd) \
      -relwidth [expr 1.0 - $s] -width -$ProgData(sepwd) \
      -rely 0 -relheight 1
  } else {
    place .main.plot -relx 0 -x 0 \
      -relwidth $s -width -$ProgData(sepwd) \
      -rely 0 -relheight 1
    place .main.sel -relx $s -x $ProgData(sepwd) \
      -relwidth [expr 1.0 - $s] -width -$ProgData(sepwd) \
      -rely 0 -relheight 1
  }
  place .main.sep -relx $s -x -2 -rely 0 -relheight 1
  set ProgData(seppos) $s
}

frame .main.plot
frame .main.sel

sep_locate $ProgData(seppos)

#----- OpenGL window

set OGLwin .main.plot.gl
if {[catch {OGLwin $OGLwin} msg]} {
  error_exit $msg
}
pack $OGLwin -side left -fill both -expand 1

#---------- selections

# base selection

set f [frame .main.sel.base]
pack $f -side top -fill x -pady 5
label $f.lab -text Base
pack $f.lab -side left
set ProgData(baselist) [ComboboxCreate $f.cb -width 10 \
  -edit 0 -state disabled -command set_base]
pack $f.cb -side left -fill x -expand 1

# object tree

image create photo box_empty -data {
R0lGODlhDAAMALMAAAAAAMbGxv//////////////////////////////////////////////\
/////////yH5BAEAAAEALAAAAAAMAAwAAAQaEMhJZ7g4y8zD7tgHesB4iSDaqRyrlWYlSxEA\
Ow==}

image create photo box_checked -data {
R0lGODlhDAAMALMAAAAAAMbGxv//////////////////////////////////////////////\
/////////yH5BAEAAAEALAAAAAAMAAwAAAQjEMhJabj4CrmznkGHddJnBuUnokC2siOwpqJY\
vqTcelvlSxEAOw==}

image create photo box_question -data {
R0lGODlhDAAMALMAAIQAAMbGxv8AAP//////////////////////////////////////////\
/////////yH5BAEAAAEALAAAAAAMAAwAAAQjMMggap1TgL0FrsAEXJIWSqNXbiKJoePbqnIn\
i/Gtv7beSxEAOw==}

set f [frame .main.sel.objs]
pack $f -side top -fill both -expand 1

set ProgData(tree) $f.tree

scrollbar $f.ys -orient vertical -command "$ProgData(tree) yview" \
  -takefocus 0 -highlightthickness 0
pack $f.ys -side right -fill y

scrollbar $f.xs -orient horizontal -command "$ProgData(tree) xview" \
  -takefocus 0 -highlightthickness 0
pack $f.xs -side bottom -fill x

TreeCreate $ProgData(tree) -width 150 -height 300 -relief sunken \
  -bd 2 -highlightthickness 1 -yscrollcommand "$f.ys set" -takefocus 1 \
  -xscrollcommand "$f.xs set" -font $Font(normal) -padx 4 \
  -lines $ProgData(showlines)
pack $ProgData(tree) -side left -fill both -expand 1

bind $ProgData(tree) <1> {tree_show %W %x %y}
bind $ProgData(tree) <ButtonRelease-2> {tree_info %W %x %y}
bind $ProgData(tree) <3> {tree_menu %W %x %y}
bind $ProgData(tree) <Double-1> {tree_at %W %x %y Toggle}
bind $ProgData(tree) <Shift-1> {tree_at %W %x %y Expand}
bind $ProgData(tree) <Control-1> {tree_at %W %x %y Collapse}

set ProgData(menu) [menu .nodemenu -tearoff 0]
$ProgData(menu) add radiobutton -label "Outline" \
  -variable ProgData(curmode) -value 1 -command update_mode
$ProgData(menu) add radiobutton -label "Mesh" \
  -variable ProgData(curmode) -value 2 -command update_mode
$ProgData(menu) add radiobutton -label "Shaded" \
  -variable ProgData(curmode) -value 3 -command update_mode
$ProgData(menu) add separator
$ProgData(menu) add command -label "Color..." \
  -command "update_color $ProgData(tree)"
$ProgData(menu) add command -label "AutoColor" \
  -command "auto_color $ProgData(tree)"
$ProgData(menu) add command -label "Info..."

proc tree_show {w x y} {
  set value [TreeTypeAt $w $x $y]
  if {$value == ""} return
  set type [lindex $value 0]
  set node [lindex $value 1]
  if {$type == "image"} {
    toggle_visibility $node
  } else {
    if {$node != [TreeSelectionGet $w]} {
      set_node $node
    }
  }
}

proc tree_info {w x y} {
  set value [TreeTypeAt $w $x $y]
  if {$value == ""} return
  if {[lindex $value 0] == "text"} {
    show_info $w $x $y [lindex $value 1]
  }
}

proc tree_menu {w x y} {
  global ProgData
  set value [TreeTypeAt $w $x $y]
  if {$value == ""} return
  if {[lindex $value 0] == "image"} return
  set node [lindex $value 1]
  if {$node != [TreeSelectionGet $w]} {
    set_node $node
  }
  if {$ProgData(curdim) == 0} {
    set state disabled
  } else {
    set state normal
  }
  foreach n {0 1 2 4 5} {
    $ProgData(menu) entryconfigure $n -state $state
  }
  set cnt [llength [TreeGet $w $node -tag]]
  if {$cnt < 1 || $cnt > 2} {
    $ProgData(menu) entryconfigure 6 -state disabled
  } else {
    $ProgData(menu) entryconfigure 6 -state normal \
      -command "show_info $w $x $y {$node}"
  }
  $ProgData(menu) post [expr [winfo rootx $ProgData(tree)] + $x] \
    [expr [winfo rooty $ProgData(tree)] + $y]
}

proc tree_at {w x y mode} {
  set value [TreeTypeAt $w $x $y]
  if {$value == ""} return
  set type [lindex $value 0]
  set node [lindex $value 1]
  if {$type == "image"} {
    if {$mode == "Expand"} {
      set_visibility $node 1 1
      if {$node != [TreeSelectionGet $w]} {
        set_node $node
      }
    } elseif {$mode == "Collapse"} {
      set_visibility $node 0 1
    } else {
      return
    }
    update_node $node
  } else {
    if {$node != [TreeSelectionGet $w]} {
      set_node $node
    }
    Tree$mode $w $node
  }
}

proc tree_open {} {
  global ProgData
  catch {TreeOpenLevel $ProgData(tree) /}
}

proc tree_close {} {
  global ProgData
  catch {TreeCloseLevel $ProgData(tree) /}
}

# options

frame .main.sel.opts
pack .main.sel.opts -side top -fill x

set f [frame .main.sel.opts.top]
pack $f -side top -fill x

radiobutton $f.o -text "Outline" -variable ProgData(curmode) -value 1 \
   -command update_mode -state disabled
pack $f.o -side left
radiobutton $f.m -text "Mesh" -variable ProgData(curmode) -value 2 \
   -command update_mode -state disabled
pack $f.m -side right

set f [frame .main.sel.opts.bot]
pack $f -side top -fill x

radiobutton $f.s -text "Shaded" -variable ProgData(curmode) -value 3 \
   -command update_mode -state disabled
pack $f.s -side left

set ProgData(clrbut) [button $f.c -relief solid -text Color... \
  -borderwidth 1 -command "update_color $f.c" -state disabled]
pack $f.c -side right -padx 5

proc options_state {dim} {
  if {$dim} {
    set state normal
  } else {
    set state disabled
  }
  foreach i {top.o top.m bot.s} {
    .main.sel.opts.$i configure -state $state
  }
}

#----- help

proc help_menu {} {
  if [help_valid] {
    menubar_state Help normal 0
    menubar_state Help normal 1
    .toolbar.but.help configure -state normal
  } else {
    menubar_state Help disabled 0
    menubar_state Help disabled 1
    .toolbar.but.help configure -state disabled
  }
}

help_init

#----- set default colors and zone/region viewing

proc set_defaults {{loc .}} {
  global ProgData Defaults OGLwin fgColor Font
  foreach i {showcolors showlines meshvis meshmode regvis regmode \
             background autocolor} {
    set Defaults($i) $ProgData($i)
  }

  set w .defaults
  toplevel $w
  wm title $w "Defaults"
  wm transient $w .
  wm protocol $w WM_DELETE_WINDOW {set ProgData(done) 0}

  FrameCreate $w.tree -text "Display Tree" -font $Font(bold) -padx 0 -pady 0
  pack $w.tree -side top -padx 5 -pady 2 -fill x -expand 1
  set tree [FrameGet $w.tree]

  checkbutton $tree.colors -text "Show Colors" -onvalue 1 -offvalue 0 \
    -variable Defaults(showcolors) -highlightthickness 0
  checkbutton $tree.lines -text "Show Lines" -onvalue 1 -offvalue 0 \
    -variable Defaults(showlines) -highlightthickness 0
  pack $tree.colors $tree.lines -side left -expand 1

  FrameCreate $w.zone -text "Volume Mesh" -font $Font(bold) -padx 0 -pady 0
  pack $w.zone -side top -padx 5 -pady 2 -fill x -expand 1
  set zone [FrameGet $w.zone]

  checkbutton $zone.vis -text "Visible as" -onvalue 1 -offvalue 0 \
    -variable Defaults(meshvis) -highlightthickness 0
  pack $zone.vis -side top -anchor w
  set f [frame $zone.mode]
  pack $f -side top -fill x -expand 1
  radiobutton $f.o -text "Outline" -variable Defaults(meshmode) -value 1 \
     -highlightthickness 0
  radiobutton $f.m -text "Mesh" -variable Defaults(meshmode) -value 2 \
     -highlightthickness 0
  radiobutton $f.s -text "Shaded" -variable Defaults(meshmode) -value 3 \
     -highlightthickness 0
  pack $f.o $f.m $f.s -side left -expand 1

  FrameCreate $w.reg -text Regions -font $Font(bold) -padx 0 -pady 0
  pack $w.reg -side top -padx 5 -pady 2 -fill x -expand 1
  set reg [FrameGet $w.reg]

  checkbutton $reg.vis -text "Visible as" -onvalue 1 -offvalue 0 \
    -variable Defaults(regvis) -highlightthickness 0
  pack $reg.vis -side top -anchor w
  set f [frame $reg.mode]
  pack $f -side top -fill x -expand 1
  radiobutton $f.o -text "Outline" -variable Defaults(regmode) -value 1 \
     -highlightthickness 0
  radiobutton $f.m -text "Mesh" -variable Defaults(regmode) -value 2 \
     -highlightthickness 0
  radiobutton $f.s -text "Shaded" -variable Defaults(regmode) -value 3 \
     -highlightthickness 0
  pack $f.o $f.m $f.s -side left -expand 1

  FrameCreate $w.colors -text Colors -font $Font(bold)
  pack $w.colors -side top -padx 5 -pady 2 -fill x -expand 1
  set colors [FrameGet $w.colors]

  set f [frame $colors.bg]
  pack $f -side top -fill x -expand 1
  label $f.lab -text "Background Color"
  pack $f.lab -side left
  button $f.but -text Select -relief solid -bd 1 \
    -command "background_color $f.but"
  color_button $f.but $Defaults(background)
  pack $f.but -side right

  set auto [frame $colors.auto]
  pack $auto -side top -fill x -expand 1
  label $auto.lab -text "Assign Color Based On"
  pack $auto.lab -side top -anchor w
  set f [frame $auto.mode]
  pack $f -side top -fill x -expand 1
  radiobutton $f.z -text "Zone" -variable Defaults(autocolor) -value 0 \
     -highlightthickness 0
  radiobutton $f.r -text "Region" -variable Defaults(autocolor) -value 1 \
     -highlightthickness 0
  pack $f.z $f.r -side left -expand 1

  set f [frame $w.but]
  pack $f -side top -padx 5 -pady 2 -fill x -expand 1
  button $f.default -text Defaults -width 8 -underline 0 \
    -command "default_values $w"
  button $f.accept -text Accept -width 8 -underline 0 \
    -command {set ProgData(done) 1}
  button $f.cancel -text Cancel -width 8 -underline 0 \
    -command {set ProgData(done) 0}
  pack $f.default $f.accept $f.cancel -side left -pady 5

  bind $w <Alt-d> "$f.default flash; default_values $w"
  bind $w <Alt-a> "$f.accept flash; set ProgData(done) 1"
  bind $w <Alt-c> "$f.cancel flash; set ProgData(done) 0"

  center_window $w $loc
  set oldFocus [focus]
  set oldGrab [grab current $w]
  if {$oldGrab != ""} {
    set grabStatus [grab status $oldGrab]
  }
  catch {grab $w}
  tkwait visibility $w
  focus $w
  tkwait variable ProgData(done)
  catch {focus $oldFocus}
  destroy $w
  if {$oldGrab != ""} {
    if {$grabStatus == "global"} {
      grab -global $oldGrab
    } else {
      grab $oldGrab
    }
  }

  if {$ProgData(done)} {
    foreach i {meshvis meshmode regvis regmode background autocolor} {
      set ProgData($i) $Defaults($i)
    }
    eval $OGLwin eval -clearcolor $ProgData(background)
    init_display
    Center
    if {$Defaults(showlines) != $ProgData(showlines)} {
      set ProgData(showlines) $Defaults(showlines)
      TreeConfig $ProgData(tree) -lines $ProgData(showlines)
    }
    if {$Defaults(showcolors) != $ProgData(showcolors)} {
      set ProgData(showcolors) $Defaults(showcolors)
      build_tree
    }
  }
}

proc default_values {w} {
  global Defaults
  array set Defaults {
    showcolors 1
    showlines 1
    meshvis 1
    meshmode 1
    regvis 0
    regmode 2
    autocolor 0
    background {0.0 0.0 0.0}
  }

  set cf [FrameGet $w.colors]
  color_button $cf.bg.but $Defaults(background)
}

proc background_color {but} {
  global Defaults
  set clr [select_color $Defaults(background) "Background Color" $but]
  if {$clr != ""} {
    set Defaults(background) $clr
    color_button $but $clr
  }
}

proc color_button {but clr} {
  global fgColor bgColor
  if {$clr == ""} {
    $but configure -fg $fgColor(normal) -activeforeground $fgColor(active) \
      -bg $bgColor(normal) -activebackground $bgColor(active) \
      -state disabled
  } else {
    if {[color_gray $clr] > 0.5} {
      set fg black
    } else {
      set fg white
    }
    $but configure -fg $fg -activeforeground $fg \
      -bg [color_value $clr] \
      -activebackground [color_value [color_lighten $clr]] \
      -state normal
  }
}

proc select_color {oldclr title {loc .}} {
  return [color_select .color $title $oldclr $loc]
}

#----- set perspective -----

set Perspective(fov) $ProgData(fov)
set Perspective(fd) [expr 0.5 / tan(0.00872665 * $Perspective(fov))]
set Perspective(np) [expr $Perspective(fd) * $ProgData(np)]
set Perspective(fp) [expr $Perspective(fd) * $ProgData(fp)]
set Perspective(ar) 1.0

set Viewpoint(x) 0
set Viewpoint(y) 0
set Viewpoint(z) $Perspective(fd)

proc apply_perspective {what val} {
  global ProgData Perspective OGLwin Viewpoint
  set Perspective(fov) $ProgData(fov)
  set fd [expr 0.5 / tan(0.00872665 * $Perspective(fov))]
  set Viewpoint(z) [expr $fd * $Viewpoint(z) / $Perspective(fd)]
  set Perspective(fd) $fd
  if {[expr $ProgData(fp) <= $ProgData(np)]} {
    if {$what == "np"} {
      set ProgData(fp) [expr 0.01 * (100.0 * $ProgData(np) + 1.0)]
    } else {
      set ProgData(np) [expr $ProgData(fp) - 0.001]
    }
  }
  set Perspective(np) [expr $fd * $ProgData(np)]
  set Perspective(fp) [expr $fd * $ProgData(fp)]
  compute_viewport $OGLwin
  set_view $OGLwin
}

proc set_perspective {{loc .}} {
  global ProgData fgColor Font

  set w .perspective
  if [winfo exists $w] {
      wm deiconify $w
      return
  }

  toplevel $w
  wm title $w "Perspective"

  FrameCreate $w.opts -font $Font(bold)
  pack $w.opts -side top -padx 5 -pady 5
  set opts [FrameGet $w.opts]

  set f [frame $opts.fov]
  pack $f -side top
  label $f.lab -width 18 -anchor w -text "Field of View"
  scale $f.scl -length 200 -from 5 -to 85 -variable ProgData(fov) \
    -orient horizontal -command {apply_perspective fov}
  pack $f.lab $f.scl -side left

  set f [frame $opts.np]
  pack $f -side top
  label $f.lab -width 18 -anchor w -text "Near Clipping Plane"
  scale $f.scl -length 200 -from 0.001 -to 1 -resolution .001 \
    -variable ProgData(np) -orient horizontal -command {apply_perspective np}
  pack $f.lab $f.scl -side left

  set f [frame $opts.fp]
  pack $f -side top
  label $f.lab -width 18 -anchor w -text "Far Clipping Plane"
  scale $f.scl -length 200 -from .01 -to 5 -resolution .01 \
    -variable ProgData(fp) -orient horizontal -command {apply_perspective fp}
  pack $f.lab $f.scl -side left

  set f [frame $w.but]
  pack $f -side top -fill x -expand 1
  button $f.default -text Defaults -width 8 -command perspective_defaults
  button $f.close -text Close -width 8 -command "destroy $w"
  pack $f.default $f.close -side left -pady 2 -expand 1

  center_window $w $loc
}

proc perspective_defaults {} {
  global ProgData

  array set ProgData {
    fov 30
    np 0.025
    fp 2
  }
  apply_perspective all 0
}

#----- OGL window controls

set ProgData(axislist) [OGLaxis $ProgData(axis)]
$OGLwin main -clear colorbuffer depthbuffer -call $ProgData(axislist)

if {[info commands OGLcutplane] != ""} {
  OGLcutconfig $ProgData(cutcolor) $ProgData(usecutclr) $ProgData(ignorevis)
  set ProgData(cutlist) [OGLcutplane]
  set ProgData(planelist) [OGLdrawplane]
}

eval $OGLwin eval \
  -clearcolor $ProgData(background) \
  -$ProgData(culling) cullface \
  -matrixmode projection \
  -loadidentity \
  -perspective $Perspective(fov) $Perspective(ar) \
    $Perspective(np) $Perspective(fp) \
  -matrixmode modelview \
  -loadidentity \
  -light light0 position 0 1 5 0 \
  -light light0 diffuse .8 .8 .8 \
  -light light0 specular 1 1 1 \
  -light light0 ambient .2 .2 .2 \
  -lightmodel lightmodeltwoside $ProgData(twosided) \
  -enable lighting -enable light0 \
  -enable depthtest -enable normalize -shademodel flat

set ViewMatrix [$OGLwin get modelviewmatrix]

bind $OGLwin <Configure> {compute_viewport %W}
bind $OGLwin <Any-ButtonPress> {set xlast %x ; set ylast %y}
bind $OGLwin <B1-Motion> {Rotate %W %x %y}
bind $OGLwin <Shift-B1-Motion> {Pan %W %x %y}
bind $OGLwin <Control-B1-Motion> {Zoom %W %x %y}
bind $OGLwin <B2-Motion> {Zoom %W %x %y}
bind $OGLwin <B3-Motion> {Pan %W %x %y}

catch {bind . <MouseWheel> {WheelZoom %D}}
bind . <KeyPress-c> {Center}
bind . <KeyPress-r> {reset_view ; $OGLwin redraw}
bind . <KeyPress-t> toggle_twosided
bind . <KeyPress-b> toggle_culling
bind . <KeyPress-a> toggle_axis
if {[info commands OGLcutplane] != ""} {
  bind . <KeyPress-p> cutplane_control
  bind . <Return> {if [winfo exists .cutplane] draw_cut}
}

proc compute_viewport {w} {
  global ProgData Perspective
  set ProgData(width) [winfo width $w]
  set ProgData(height) [winfo height $w]
  set ProgData(scale) [expr 1.0 / double($ProgData(height))]
  set Perspective(ar) [expr double($ProgData(width)) / \
      double($ProgData(height))]
  $w eval \
    -matrixmode projection \
    -loadidentity \
    -perspective $Perspective(fov) $Perspective(ar) \
      $Perspective(np) $Perspective(fp) \
    -matrixmode modelview
}

proc set_view {w} {
  global Viewpoint ViewMatrix
  eval $w eval \
    -loadidentity \
    -lookat $Viewpoint(x) $Viewpoint(y) $Viewpoint(z) \
      $Viewpoint(x) $Viewpoint(y) 0 0 1 0 \
    -multmatrix $ViewMatrix
  $w redraw
}

proc Rotate {w x y} {
  global xlast ylast ViewMatrix Viewpoint Perspective
  set scl [expr $Viewpoint(z) / $Perspective(fd)]
  if {[expr $scl > 1.0]} {
    set dx [expr $x - $xlast]
    set dy [expr $y - $ylast]
  } else {
    if {[expr $scl < 0.1]} {set scl 0.1}
    set dx [expr $scl * ($x - $xlast)]
    set dy [expr $scl * ($y - $ylast)]
  }
  set xlast $x
  set ylast $y
  eval $w eval \
    -loadidentity \
    -rotate $dy 1 0 0 \
    -rotate $dx 0 1 0 \
    -multmatrix $ViewMatrix
  set ViewMatrix [$w get modelviewmatrix]
  set_view $w
}

proc Pan {w x y} {
  global xlast ylast Viewpoint Perspective ProgData
  set scl [expr $Viewpoint(z) / $Perspective(fd)]
  if {[expr $scl > 1.0]} {
    set scl $ProgData(scale)
  } else {
    if {[expr $scl < 0.1]} {set scl 0.1}
    set scl [expr $scl * $ProgData(scale)]
  }
  set Viewpoint(x) [expr $scl * ($xlast - $x) + $Viewpoint(x)]
  set Viewpoint(y) [expr $scl * ($y - $ylast) + $Viewpoint(y)]
  set xlast $x
  set ylast $y
  set_view $w
}

proc Zoom {w x y} {
  global xlast ylast Viewpoint
  set scl [expr 0.01 * $Viewpoint(z)]
  if {[expr $scl < 0.001]} {set scl 0.001}
  set Viewpoint(z) [expr $scl * ($ylast - $y) + $Viewpoint(z)]
  if {[expr $Viewpoint(z) < 0.001]} {
    set Viewpoint(z) 0.001
  }
  set xlast $x
  set ylast $y
  set_view $w
}

proc WheelZoom {d} {
  global Viewpoint OGLwin
  set scl [expr 0.001 * $Viewpoint(z)]
  if {[expr $scl < 0.0001]} {set scl 0.0001}
  set Viewpoint(z) [expr $scl * $d + $Viewpoint(z)]
  if {[expr $Viewpoint(z) < 0.001]} {
    set Viewpoint(z) 0.001
  }
  set_view $OGLwin
}

proc reset_clipping {} {
  global ProgData Perspective OGLwin
  array set ProgData {
    np 0.025
    fp 2.0
  }
  set Perspective(np) [expr $Perspective(fd) * $ProgData(np)]
  set Perspective(fp) [expr $Perspective(fd) * $ProgData(fp)]
  compute_viewport $OGLwin
}

proc Center {} {
  global ProgData Viewpoint ViewMatrix Perspective OGLwin
  set bounds [CGNSbounds 0 $ViewMatrix]
  set b [lindex $bounds 0]
  set xt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set xd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set b [lindex $bounds 1]
  set yt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set yd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set b [lindex $bounds 2]
  set zt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set zd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set dd [expr $xd * $xd + $yd * $yd + $zd * $zd]
  if {[expr $dd < 1.0e-15]} {
    set scl 1.0
  } else {
    set scl [expr 1.0 / sqrt($dd)]
  }

#  reset_clipping
  eval $OGLwin eval \
    -matrixmode modelview \
    -loadidentity \
    -scale $scl $scl $scl \
    -translate $xt $yt $zt \
    -multmatrix $ViewMatrix
  set ViewMatrix [$OGLwin get modelviewmatrix]

  set Viewpoint(x) 0
  set Viewpoint(y) 0
  set Viewpoint(z) $Perspective(fd)
  set_view $OGLwin
}

proc reset_view {} {
  global ProgData Viewpoint ViewMatrix Perspective OGLwin
  set bounds [CGNSbounds 0]
  set b [lindex $bounds 0]
  set xt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set xd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set b [lindex $bounds 1]
  set yt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set yd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set b [lindex $bounds 2]
  set zt [expr -0.5 * ([lindex $b 1] + [lindex $b 0])]
  set zd [expr double([lindex $b 1]) - double([lindex $b 0])]
  set dd [expr $xd * $xd + $yd * $yd + $zd * $zd]
  if {[expr $dd < 1.0e-15]} {
    set scl 1.0
  } else {
    set scl [expr 1.0 / sqrt($dd)]
  }

  set Viewpoint(x) 0
  set Viewpoint(y) 0
  set Viewpoint(z) $Perspective(fd)

  reset_clipping
  $OGLwin eval \
    -matrixmode modelview \
    -loadidentity \
    -scale $scl $scl $scl \
    -translate $xt $yt $zt
  set ViewMatrix [$OGLwin get modelviewmatrix]
  eval $OGLwin eval \
    -loadidentity \
    -lookat $Viewpoint(x) $Viewpoint(y) $Viewpoint(z) \
      $Viewpoint(x) $Viewpoint(y) 0 0 1 0 \
    -multmatrix $ViewMatrix
}

proc init_data {} {
  global ProgData
  set ProgData(/,dim) 0
  set ProgData(/,mode) 0
  set ProgData(/,vis) 1
  set ProgData(/,clr) ""
  set ProgData(/,msg) [CGNSsummary]
  set nz 0
  foreach z $ProgData(zones) {
    set clr [OGLcolor $nz]
    set zone /$z
    set ProgData($zone,dim) 0
    set ProgData($zone,vis) 1
    set ProgData($zone,mode) 0
    set ProgData($zone,clr) $clr
    set ProgData($zone,msg) [CGNSsummary $nz]
    set subdirs {}
    set nr 0
    foreach r $ProgData($nz,regions) {
      set reg $zone/$r
      if [catch {CGNSregiondim $nz $nr} dim] {
        set msg $dim
        set dim 0
      } else {
        set msg ""
      }
      if {$dim < 1} {
        set ProgData($reg,dim) 0
        set ProgData($reg,vis) 0
        set ProgData($reg,mode) 0
        set ProgData($reg,clr) ""
        if {$msg == ""} {
          set ProgData($reg,msg) "couldn't process for exterior faces"
        } else {
          set ProgData($reg,msg) $msg
        }
      } else {
        set ProgData($reg,dim) $dim
        if {$nr == 0 || $dim == 3} {
          set ProgData($reg,vis) $ProgData(meshvis)
          set ProgData($reg,mode) $ProgData(meshmode)
        } else {
          set ProgData($reg,vis) $ProgData(regvis)
          set ProgData($reg,mode) $ProgData(regmode)
        }
if {$ProgData(autocolor)} {
set clr [OGLcolor [expr $nz + $nr]]
}
        set ProgData($reg,clr) $clr
        set ProgData($reg,msg) [CGNSsummary $nz $nr]
      }
      set dir [file dirname $r]
      if {$dir != "" && $dir != "."} {
        if {[lsearch $subdirs $dir] < 0} {
          lappend subdirs $dir
          set ProgData($zone/$dir,dim) $ProgData($reg,dim)
          set ProgData($zone/$dir,vis) 1
          set ProgData($zone/$dir,mode) $ProgData($reg,mode)
          set ProgData($zone/$dir,clr) $clr
          set ProgData($zone/$dir,msg) ""
        } else {
          if {$ProgData($zone/$dir,dim) < $ProgData($reg,dim)} {
            set ProgData($zone/$dir,dim) $ProgData($reg,dim)
          }
          if {$ProgData($zone/$dir,mode) != $ProgData($reg,mode)} {
            set ProgData($zone/$dir,mode) 0
          }
        }
      }
      if {$nr == 0} {
          set ProgData($zone,dim) $ProgData($reg,dim)
          set ProgData($zone,mode) $ProgData($reg,mode)
      } else {
        if {$ProgData($zone,dim) < $ProgData($reg,dim)} {
          set ProgData($zone,dim) $ProgData($reg,dim)
        }
        if {$ProgData($zone,mode) != $ProgData($reg,mode)} {
          set ProgData($zone,mode) 0
        }
      }
      incr nr
    }
    if {$nz == 0} {
      set ProgData(/,dim) $ProgData($zone,dim)
      set ProgData(/,mode) $ProgData($zone,mode)
    } else {
      if {$ProgData(/,dim) < $ProgData($zone,dim)} {
        set ProgData(/,dim) $ProgData($zone,dim)
      }
      if {$ProgData(/,mode) != $ProgData($zone,mode)} {
        set ProgData(/,mode) 0
      }
    }
    incr nz
  }
}

proc init_display {} {
  global ProgData OGLwin
  if {$ProgData(displaylist) != ""} {
    $OGLwin deletelist $ProgData(displaylist)
  }
  if {$ProgData(nzones) < 1} {
    set ProgData(displaylist) ""
    $OGLwin main -clear colorbuffer depthbuffer -call $ProgData(axislist)
  } else {
    set calllist {}
    set nz 0
    foreach z $ProgData(zones) {
      set nr 0
      foreach r $ProgData($nz,regions) {
        if {$ProgData(/$z/$r,dim)} {
          set clr $ProgData(/$z/$r,clr)
          if {$ProgData(/$z/$r,vis)} {
            set dl [OGLregion $nz $nr $ProgData(/$z/$r,mode) $clr]
          } else {
            set dl [OGLregion $nz $nr 0 $clr]
          }
          lappend calllist -call $dl
        }
        incr nr
      }
      incr nz
    }
    if {$ProgData(cutlist) != ""} {
      lappend calllist -call $ProgData(cutlist)
    }
    if {$ProgData(planelist) != ""} {
      lappend calllist -call $ProgData(planelist)
    }
    set ProgData(displaylist) [eval $OGLwin newlist $calllist]
    $OGLwin main -clear colorbuffer depthbuffer \
      -call $ProgData(axislist) -call $ProgData(displaylist)
  }
  OGLaxis $ProgData(axis)
}

proc cleanup_display {} {
  global ProgData OGLwin
  display_message ""
  if {$ProgData(cgnsfile) == ""} return
  catch CGNSclose
  close_cutplane
  if {$ProgData(displaylist) != ""} {
    $OGLwin deletelist $ProgData(displaylist)
    set ProgData(displaylist) ""
  }
  $OGLwin main -clear colorbuffer depthbuffer -call $ProgData(axislist)
  TreeDelete $ProgData(tree) /
  array set ProgData {
    bases {}
    nzones 0
    zones {}
    curnode ""
    curdim 0
    curclr ""
    curmode 0
  }
  OGLaxis $ProgData(axis)
  $OGLwin redraw
}

#---------- set base

proc build_tree {} {
  global ProgData
  set_node ""
  TreeDelete $ProgData(tree) /
  TreeInsert $ProgData(tree) / -text $ProgData(curbase)
  set nz 0
  foreach z $ProgData(zones) {
    set zone /$z
    if {$ProgData($zone,dim) == 0} {
      set icon box_question
      set clr ""
    } else {
      if {$ProgData($zone,vis)} {
        set icon box_checked
      } else {
        set icon box_empty
      }
      if {$ProgData(showcolors)} {
        set clr [color_value $ProgData($zone,clr)]
      } else {
        set clr ""
      }
    }
    TreeInsert $ProgData(tree) $zone -tag $nz -icon $icon -iconbg $clr
    set nr 0
    foreach r $ProgData($nz,regions) {
      set dir [file dirname $r]
      if {$dir != "" && $dir != "." &&
        ![TreeExists $ProgData(tree) $zone/$dir]} {
        if {$ProgData($zone/$dir,dim) == 0} {
          set icon box_question
          set clr ""
        } else {
          if {$ProgData($zone/$dir,vis)} {
            set icon box_checked
          } else {
            set icon box_empty
          }
          if {$ProgData(showcolors)} {
            set clr [color_value $ProgData($zone/$dir,clr)]
          } else {
            set clr ""
          }
        }
        TreeInsert $ProgData(tree) $zone/$dir -icon $icon -iconbg $clr
      }
      set reg $zone/$r
      if {$ProgData($reg,dim) == 0} {
        set icon box_question
        set clr ""
      } else {
        if {$ProgData($reg,vis)} {
          set icon box_checked
        } else {
          set icon box_empty
        }
        if {$ProgData(showcolors)} {
          set clr [color_value $ProgData($reg,clr)]
        } else {
          set clr ""
        }
      }
      TreeInsert $ProgData(tree) $reg -tag "$nz $nr" -icon $icon -iconbg $clr
      incr nr
    }
    incr nz
  }
}

proc set_base {w nb} {
  global ProgData
  display_message "reading zone information..."
  if {[catch {CGNSbase $nb} zones]} {
    display_message $zones
    errormsg $zones
    return ""
  }
  set ProgData(zones) $zones
  set ProgData(nzones) [llength $zones]
  . configure -cursor watch
  update

  for {set nz 0} {$nz < $ProgData(nzones)} {incr nz} {
    display_message "reading zone [expr $nz + 1]..."
    if {[catch {CGNSzone $nz} regs]} {
      errormsg $regs
      set regs ""
    }
    set ProgData($nz,regions) $regs
  }

  set ProgData(curbase) [lindex $ProgData(bases) $nb]
  init_data
  display_message "building display lists..."
  init_display
  reset_view
  build_tree
  if [winfo exists .cutplane] {
    OGLcutplane 0
    center_cutplane
  }

  . configure -cursor {}
  display_message ""
  return ""
}

proc show_info {w x y node} {
  global ProgData Font
  if {$node == ""} return
  if {$node == "/"} {
    catch CGNSgetbase msg
  } else {
    set tag [TreeGet $w $node -tag]
    if {[llength $tag] == 1} {
      catch {CGNSgetzone $tag} msg
    } elseif {[llength $tag] == 2} {
      catch {CGNSgetregion [lindex $tag 0] [lindex $tag 1]} msg
    } else {
      set msg ""
    }
  }
  if {$msg != ""} {
    incr x [winfo rootx $w]
    incr y [winfo rooty $w]
    popup_message $msg -parent $w -position "$x $y" \
      -font $Font(fixed) -wrap 0
  }
}

proc set_node {node} {
  global ProgData
  TreeSelectionSet $ProgData(tree) $node
  set ProgData(curnode) $node
  if {$node == "" || ![info exists ProgData($node,dim)]} {
    set ProgData(curdim) 0
    set ProgData(curmode) 0
    set ProgData(curclr) ""
    set msg ""
  } else {
    set ProgData(curdim) $ProgData($node,dim)
    set ProgData(curmode) $ProgData($node,mode)
    set ProgData(curclr) $ProgData($node,clr)
    set msg $ProgData($node,msg)
  }
  color_button $ProgData(clrbut) $ProgData(curclr)
  options_state $ProgData(curdim)
  display_message $msg
}

proc set_visibility {node vis all} {
  global ProgData
  if {$ProgData($node,dim) == 0} return
  if {$vis} {
    set ProgData($node,vis) 1
    TreeSet $ProgData(tree) $node -icon box_checked
  } else {
    set ProgData($node,vis) 0
    TreeSet $ProgData(tree) $node -icon box_empty
  }
  if {$all} {
    foreach c [TreeGet $ProgData(tree) $node -children] {
      set_visibility $node/$c $vis 1
    }
  }
}

proc toggle_visibility {node {all 0}} {
  global ProgData
  if {$node == "" || ![info exists ProgData($node,dim)] ||
      $ProgData($node,dim) == 0} return
  if {$ProgData($node,vis)} {
    set vis 0
  } else {
    set vis 1
  }
  set_visibility $node $vis $all
  update_node $node
  if {$vis && $node != [TreeSelectionGet $ProgData(tree)]} {
    set_node $node
  }
}

proc node_visible {node} {
  global ProgData
  if {$ProgData($node,vis) == 0} {return 0}
  set node [file dirname $node]
  while {$node != "" && $node != "." && $node != "/"} {
    if {$ProgData($node,vis) == 0} {return 0}
    set node [file dirname $node]
  }
  return 1
}

proc update_children {node mode clr} {
  global ProgData
  if {$mode != ""} {
    set ProgData($node,mode) $mode
  }
  if {$clr != ""} {
    if {[llength $clr] == 1} {
      set ProgData($node,clr) [OGLcolor $clr]
    } else {
      set ProgData($node,clr) $clr
    }
    if {$ProgData(showcolors)} {
      TreeSet $ProgData(tree) $node -iconbg [color_value $ProgData($node,clr)]
    }
  }
  set children [TreeGet $ProgData(tree) $node -children]
  if {[llength $children] > 0} {
    if {$node == "/"} {set node ""}
    foreach c $children {
      update_children $node/$c $mode $clr
      if {[llength $clr] == 1} {incr clr}
    }
  } else {
    set tag [TreeGet $ProgData(tree) $node -tag]
    if {[llength $tag] == 2} {
      if [node_visible $node] {
        set mode $ProgData($node,mode)
      } else {
        set mode 0
      }
      OGLregion [lindex $tag 0] [lindex $tag 1] $mode $ProgData($node,clr)
    }
  }
}

proc update_node {node {mode ""} {clr ""}} {
  global ProgData OGLwin
  if {$node == "" || $ProgData($node,dim) == 0} return
  update_children $node $mode $clr
  OGLaxis $ProgData(axis)
  $OGLwin redraw
}

proc parent_mode {node} {
  global ProgData
  if {$node == "" || $node == "."} return
  set mode ""
  set par $node
  if {$par == "/"} {set par ""}
  foreach c [TreeGet $ProgData(tree) $node -children] {
    if {$mode == ""} {
      set mode $ProgData($par/$c,mode)
    } else {
      if {$mode != $ProgData($par/$c,mode)} {
        set mode 0
        break
      }
    }
  }
  if {$mode == ""} return
  set ProgData($node,mode) $mode
  if {$node != "/"} {
    parent_mode [file dirname $node]
  }
}

proc update_mode {} {
  global ProgData
  set node $ProgData(curnode)
  if {$node == "" || $ProgData($node,dim) == 0} return
  update_node $node $ProgData(curmode)
  if {$node != "/"} {
    parent_mode [file dirname $node]
  }
}

proc update_color {w} {
  global ProgData
  set node $ProgData(curnode)
  if {$node == "" || $ProgData($node,dim) == 0} return
  set newclr [select_color $ProgData(curclr) "Set Color" $w]
  if {$newclr != ""} {
    set ProgData(curclr) $newclr
    color_button $ProgData(clrbut) $newclr
    update_node $node "" $newclr
  }
}

proc auto_color {w} {
  global ProgData
  set node $ProgData(curnode)
  if {$node == "" || $ProgData($node,dim) == 0} return
  set clr [expr [llength [split $node /]] - 2]
  set newclr [OGLcolor $clr]
  set ProgData(curclr) $newclr
  color_button $ProgData(clrbut) $newclr
  update_node $node "" $clr
}

#---------- cutting plane

proc normalize_cutplane {} {
  global ProgData
  set r 0.0
  foreach i {x y z} {
    set r [expr $r + $ProgData(cut$i) * $ProgData(cut$i)]
  }
  if [expr $r == 0.0] {
    set r 1.0
  } else {
    set r [expr sqrt($r)]
  }
  foreach i {x y z} {
    set ProgData(cut$i) [expr $ProgData(cut$i) / $r]
  }
}

proc reset_cutplane {} {
  global ProgData
  set bounds [CGNSbounds 0]
  array set ProgData {
    rotx 0
    roty 0
    rotz 0
    lastx 0
    lasty 0
    lastz 0
  }
  set n 0
  set dist 0.0
  set diag 0.0
  foreach i {x y z} {
    set b [lindex $bounds $n]
    set r [expr [lindex $b 1] - [lindex $b 0]]
    set diag [expr $diag + $r * $r]
    set r [expr 0.5 * ([lindex $b 0] + [lindex $b 1])]
    set dist [expr $dist + $r * $ProgData(cut$i)]
    incr n
  }
  set ProgData(translate) [expr ($ProgData(cutd) - $dist) / sqrt($diag)]
}

proc draw_cut {{recompute 1}} {
  global ProgData OGLwin
  set ProgData(cutplane) \
    [list $ProgData(cutx) $ProgData(cuty) $ProgData(cutz) $ProgData(cutd)]
  if {$recompute} {
    OGLcutplane $ProgData(cutmode) $ProgData(cutplane)
  } else {
    OGLcutplane $ProgData(cutmode)
  }
  if {$ProgData(cutmode)} OGLdrawplane
  $OGLwin redraw
  reset_cutplane
}

proc draw_cutplane {} {
  global ProgData OGLwin
  if {$ProgData(drawID) == ""} {
    set ProgData(drawID) [after idle draw_cutplane]
    return
  }
  set ProgData(drawID) ""
  set plane {}
  foreach i {x y z d} {
    if [catch {expr double($ProgData(cut$i))} val] return
    lappend plane $val
  }
  if [catch {OGLdrawplane $plane} msg] {
    OGLdrawplane
  } else {
    set msg ""
  }
  $OGLwin redraw
  display_message $msg
}

proc update_cutplane {data var op} {
  global ProgData
  if {$ProgData(dotrace)} draw_cutplane
}

proc center_cutplane {} {
  global ProgData OGLwin
  set ProgData(dotrace) 0
  normalize_cutplane
  set bounds [CGNSbounds 0]
  set d 0.0
  set n 0
  foreach i {x y z} {
    set b [lindex $bounds $n]
    set r [expr 0.5 * ([lindex $b 0] + [lindex $b 1])]
    set d [expr $d + $r * $ProgData(cut$i)]
    incr n
  }
  set ProgData(cutd) [format "%g" $d]
  set ProgData(translate) 0
  draw_cutplane
  set ProgData(dotrace) 1
}

proc set_cutplane {dir} {
  global ProgData OGLwin
  set ProgData(dotrace) 0
  set bounds [CGNSbounds 0]
  if {$dir == "x"} {
    array set ProgData {
      cutx 1
      cuty 0
      cutz 0
    }
    set b [lindex $bounds 0]
  } elseif {$dir == "y"} {
    array set ProgData {
      cutx 0
      cuty 1
      cutz 0
    }
    set b [lindex $bounds 1]
  } else {
    array set ProgData {
      cutx 0
      cuty 0
      cutz 1
    }
    set b [lindex $bounds 2]
  }
  set ProgData(cutd) [format "%g" \
    [expr 0.5 * ([lindex $b 0] + [lindex $b 1])]]
  draw_cutplane
  reset_cutplane
  set ProgData(dotrace) 1
}

proc rotate_cutplane {axis amt} {
  global ProgData
  set ProgData(dotrace) 0
  normalize_cutplane
  set bounds [CGNSbounds 0]
  set n 0
  set d $ProgData(cutd)
  foreach i {x y z} {
    set b [lindex $bounds $n]
    set p$i [expr 0.5 * ([lindex $b 0] + [lindex $b 1])]
    set d [expr $d - [set p$i] * $ProgData(cut$i)]
    incr n
  }
  foreach i {x y z} {
    set r$i [expr [set p$i] + $d * $ProgData(cut$i)]
  }
  set ca [expr cos(0.0174533 * ($amt - $ProgData(last$axis)))]
  set sa [expr sin(0.0174533 * ($amt - $ProgData(last$axis)))]
  set ProgData(last$axis) $amt
  set x $ProgData(cutx)
  set y $ProgData(cuty)
  set z $ProgData(cutz)
  if {$axis == "x"} {
    set ProgData(cuty) [format "%g" [expr $ca * $y - $sa * $z]]
    set ProgData(cutz) [format "%g" [expr $ca * $z + $sa * $y]]
  } elseif {$axis == "y"} {
    set ProgData(cutx) [format "%g" [expr $ca * $x + $sa * $z]]
    set ProgData(cutz) [format "%g" [expr $ca * $z - $sa * $x]]
  } else {
    set ProgData(cutx) [format "%g" [expr $ca * $x - $sa * $y]]
    set ProgData(cuty) [format "%g" [expr $ca * $y + $sa * $x]]
  }
  set d 0.0
  foreach i {x y z} {
    set d [expr $d + $ProgData(cut$i) * [set r$i]]
  }
  set ProgData(cutd) [format "%g" $d]
  draw_cutplane
  set ProgData(dotrace) 1
}

proc translate_cutplane {amt} {
  global ProgData
  set ProgData(dotrace) 0
  normalize_cutplane
  set bounds [CGNSbounds 0]
  set n 0
  set dist 0.0
  set diag 0.0
  foreach i {x y z} {
    set b [lindex $bounds $n]
    set r [expr [lindex $b 1] - [lindex $b 0]]
    set diag [expr $diag + $r * $r]
    set r [expr 0.5 * ([lindex $b 0] + [lindex $b 1])]
    set dist [expr $dist + $r * $ProgData(cut$i)]
    incr n
  }
  set ProgData(cutd) [format "%g" [expr $dist + $amt * sqrt($diag)]]
  draw_cutplane
  set ProgData(dotrace) 1
}

proc cutplane_color {but {trans ""}} {
  global ProgData
  if {$trans == ""} {
    set clr [select_color [lrange $ProgData(cutcolor) 0 2] "Select Color" $but]
    if {$clr == ""} return
    set trans [lindex $ProgData(cutcolor) 3]
    set ProgData(cutcolor) $clr
    lappend ProgData(cutcolor) $trans
    color_button $but $clr
  } else {
    set ProgData(cutcolor) [lreplace $ProgData(cutcolor) 3 3 $trans]
  }
  OGLcutconfig $ProgData(cutcolor)
  draw_cutplane
}

proc cutplane_config {recompute} {
  global ProgData
  OGLcutconfig $ProgData(cutcolor) $ProgData(usecutclr) $ProgData(ignorevis)
  draw_cut $recompute
}

proc cutplane_defaults {but} {
  global ProgData
  set ProgData(cutcolor) {0.7 0.7 0.4 0.5}
  set ProgData(transparency) 0.5
  color_button $but $ProgData(cutcolor)
  set ProgData(cutmode) 1
  set ProgData(usecutclr) 0
  set ProgData(ignorevis) 0
  OGLcutconfig $ProgData(cutcolor) 0 0
  set_cutplane x
}

proc cutplane_control {} {
  global ProgData Font
  set w .cutplane
  if [winfo exists $w] {
    wm deiconify $w
    raise $w
    focus $w
    return
  }
  if {$ProgData(cutplane) == ""} {
    array set ProgData {
      cutx 1
      cuty 0
      cutz 0
    }
  } else {
    set ProgData(cutx) [lindex $ProgData(cutplane) 0]
    set ProgData(cuty) [lindex $ProgData(cutplane) 1]
    set ProgData(cutz) [lindex $ProgData(cutplane) 2]
  }

  set ProgData(transparency) [lindex $ProgData(cutcolor) 3]

  toplevel $w
  wm title $w "Cutting Plane"
  wm protocol $w WM_DELETE_WINDOW close_cutplane

  FrameCreate $w.clr -text "Color" -font $Font(bold) -padx 0 -pady 0
  pack $w.clr -side top -padx 5 -pady 2 -fill x -expand 1
  set clr [FrameGet $w.clr]

  label $clr.lab -text Transparency
  pack $clr.lab -side left
  scale $clr.scl -length 50 -from 0 -to 1 -resolution 0.01 \
    -orient horizontal -label "" -showvalue 0 -width 12 \
    -highlightthickness 0 -variable ProgData(transparency) \
    -command "cutplane_color $clr.but"
  pack $clr.scl -side left -fill x -expand 1
  button $clr.but -text Select -relief solid -bd 1 \
    -command "cutplane_color $clr.but"
  pack $clr.but -side right
  color_button $clr.but $ProgData(cutcolor)

  FrameCreate $w.pos -text "Cut Plane" -font $Font(bold) -padx 0 -pady 0
  pack $w.pos -side top -padx 5 -pady 2 -fill x -expand 1
  set pos [FrameGet $w.pos]

  foreach r {X Y Z} {
    set i [string tolower $r]
    set f [frame $pos.d$i]
    pack $f -side top -fill x
    label $f.lab -text "$r Direction" -width 10 -anchor w
    pack $f.lab -side left
    entry $f.ent -width 10 -textvariable ProgData(cut$i)
    pack $f.ent -side left -fill x -expand 1 -padx 2
    button $f.but -text "$r Cut" -width 6 -command "set_cutplane $i"
    pack $f.but -side right
    set f [frame $pos.r$i]
    pack $f -side top -fill x
    label $f.lab -width 10 -text "$r Rotate" -anchor e
    pack $f.lab -side left
    scale $f.scl -length 50 -from -90 -to 90 -resolution 1 \
      -orient horizontal -label "" -showvalue 0 -width 12 \
      -highlightthickness 0 -variable ProgData(rot$i) \
      -command "rotate_cutplane $i"
    pack $f.scl -side left -fill x -expand 1
  }

  set f [frame $pos.dd]
  pack $f -side top -fill x
  label $f.lab -text "Offset" -width 10 -anchor w
  pack $f.lab -side left
  entry $f.ent -width 10 -textvariable ProgData(cutd)
  pack $f.ent -side left -fill x -expand 1 -padx 2
  button $f.but -text Center -width 6 -command center_cutplane
  pack $f.but -side right
  set f [frame $pos.rd]
  pack $f -side top -fill x
  label $f.lab -width 10 -text "Translate" -anchor e
  pack $f.lab -side left
  scale $f.scl -length 50 -from -.5 -to 0.5 -resolution 0.001 \
    -orient horizontal -label "" -showvalue 0 -width 12 \
    -highlightthickness 0 -variable ProgData(translate) \
    -command translate_cutplane
  pack $f.scl -side left -fill x -expand 1

  FrameCreate $w.mode -text "Display" -font $Font(bold) -padx 0 -pady 0
  pack $w.mode -side top -padx 5 -pady 2 -fill x -expand 1
  set mode [FrameGet $w.mode]
  set f [frame $mode.d]
  pack $f -side top -fill x
  set n 0
  foreach i {Off Planar Elements Shaded} {
    radiobutton $f.f$n -text $i -variable ProgData(cutmode) \
      -value $n -command {draw_cut 0}
    pack $f.f$n -side left -expand 1
    incr n
  }
  set f [frame $mode.b]
  pack $f -side top -fill x
  checkbutton $f.cc -text "Use Cutplane Color" \
    -variable ProgData(usecutclr) -onvalue 1 -offvalue 0 \
    -command {cutplane_config 0}
  checkbutton $f.iv -text "Ignore Region Visibility" \
    -variable ProgData(ignorevis) -onvalue 1 -offvalue 0 \
    -command {cutplane_config 1}
  pack $f.cc $f.iv -side left -expand 1

  frame $w.but
  pack $w.but -side top -fill x -padx 5 -expand 1
  button $w.but.r -text Compute -width 8 -default active \
    -command draw_cut
  button $w.but.d -text Defaults -width 8 \
    -command "cutplane_defaults $clr.but"
  button $w.but.c -text Close -width 8 -command close_cutplane
  pack $w.but.r $w.but.d $w.but.c -side left -expand 1

  bind $w <Return> draw_cut

  center_cutplane
  reset_cutplane
  foreach i {x y z d} {
    trace variable ProgData(cut$i) w update_cutplane
  }
  set ProgData(dotrace) 1
}

proc close_cutplane {} {
  global ProgData OGLwin
  if {[winfo exists .cutplane]} {
    set ProgData(dotrace) 1
    foreach i {x y z d} {
      trace vdelete ProgData(cut$i) w update_cutplane
    }
    OGLcutplane 0
    OGLdrawplane
    $OGLwin redraw
    destroy .cutplane
  }
}

#---------- set variable for plotting

proc set_variable {w n} {
  global ProgData
  return ""
}

#---------- read cgns file

set FileList {
  {{CGNS Files} {.cgns .cga .cgh .cgx}}
  {{All Files} {*}}
}

proc load_cgns {{filename ""}} {
  global ProgData FileList ModelData
  global Translate Scale

  if {$filename == ""} {
    set filename [FileOpen "Open CGNS File" \
      $ProgData(cgnsfile) . $FileList]
  }
  if {$filename == ""} return
  cleanup_display
  display_message "reading base information..."
  . configure -cursor watch
  update
  set msg ""
  if {[catch {CGNSopen $filename} bases]} {
    set msg $bases
  } else {
    set msg ""
  }
  . configure -cursor {}
  if {$msg != ""} {
    display_message $msg
    errormsg $msg
    return
  }
  set ProgData(bases) $bases
  ComboboxConfig $ProgData(baselist) -values $bases -index 0 -state normal
  if {[llength $bases] < 2} {
    ComboboxConfig $ProgData(baselist) -state disabled
  }
  set_base $ProgData(baselist) 0

  wm title . "CGNSplot : [file tail $filename]"
  set ProgData(cgnsfile) $filename
}

catch {
  config_icon . [list cgnsplot cgns] \
    [list $cmd_dir $cmd_dir/images $cmd_dir/../common]
}
wm deiconify .

if {$argc} {
  set file [lindex $argv [expr $argc - 1]]
  if {[string index $file 0] != "-" && [file exists $file]} {
    load_cgns $file
  }
}