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 | ###############################################################################
# Copyright (c) 2020-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform. As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose. Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
# @plugin
# Hierarchical Netlist Generation
# @namespace
# HierGen
# @section
# Miscellaneous Userware Examples
# @description
# Read a Spice netlist with X/Y annotations for each device and a side
# file that contains polygon definitions then this Plugin can be used
# to re-create a hierarchical database.
# @files
# cust32/hierGen.tcl
# @tag
# gui zdb spice
###############################################################################
# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc HierGen:Init {argc argv} {
global HierGen
##
# Load required 'geometry' package from the TclLib.
#
package require math::geometry
##
# Name of the created tab.
#
set HierGen(tabName) "HierGen"
##
# Enable overlap check.
#
set HierGen(checkForOverlap) false
##
# Disable schematic generation for the flat netlist.
#
set HierGen(persist:bigModuleLimit) [::Settings::Get "bigModuleLimit"]
::Settings::Set "bigModuleLimit" -1
gui settings changed
##
# Define a set of used colors.
#
set HierGen(color:background) "#FFFFFF"
set HierGen(color:polygon) "#068000"
set HierGen(color:selected) "#FF0000"
set HierGen(color:stroke) "#C1004F"
set HierGen(color:instance) "#000000"
set HierGen(color:minimap_background) "#FFFFFF"
set HierGen(color:minimap_devices) "#7F7F7F"
set HierGen(color:PMOS) "#E23D28"
set HierGen(color:NMOS) "#0000FF"
set HierGen(color:RES) "#D2691E"
set HierGen(color:CAP) "#DEB887"
set HierGen(color:DIODE) "#BC8F8F"
set HierGen(color:UNKNOWNDEV) "#808080"
##
# Configure the minimap.
#
set HierGen(minimap:size) 200
##
# Add the menu entries.
#
gui menu mainMenu "Hier_gen"
gui menu command {"Hiergen" "_Start"} {HierGen:Start}
gui menu command {"Hiergen" "_Recognize Gates"} {HierGen:RecognizeGates}
gui menu command {"Hiergen" "_Check for Overlaps"} \
{HierGen:CheckForOverlaps}
gui menu command {"Hiergen" "_Hide HierGen Window"} {HierGen:_hideWindow}
gui menu customizeEntry {"Hiergen" "Hide HierGen Window"} \
{HierGen:_customizeHideMenu}
##
# Extend the Popup menu.
#
gui popup append \
-menuname "HierGen" \
"Show in Physical View" \
{HierGen:_showInPhysicalView}
##
# Set options from the command line.
#
set HierGen(blocksFile) [lindex $argv 0]
set HierGen(netlistFile) [lindex $argv 1]
##
# Run the Start procedure after reading the netlist.
#
gui database runAndRegisterChangedCallback "HierGen:Start"
}
# =============================================================================
# Finit - Finalize the plugin.
# This procedure is automatically called when deactivating the plugin.
# =============================================================================
#
proc HierGen:Finit {} {
##
# Undo modifications of the GUI.
#
gui menu removeEntry {"Hiergen" "Start"}
gui menu removeEntry {"Hiergen" "Recognize Gates"}
gui menu removeEntry {"Hiergen" "Check for Overlaps"}
gui menu removeEntry {"Hiergen" "Hide HierGen Window"}
gui popup remove -menuname "HierGen" "Show in Physical View"
gui database removeChangedCallback "HierGen:Start"
}
# =============================================================================
# Start -
# =============================================================================
#
proc HierGen:Start {{db {}}} {
global HierGen
##
# Clear the HierGen widget if the database changed to an empty database.
#
set db [gui database get]
if {$db == {}} {
if {[gui window exists $HierGen(tabName)]} {
set w [gui window path $HierGen(tabName)]
$w.list delete [$w.list children {}]
$w.c delete all
$w.c.minimap delete all
}
return
}
##
# Get a block definitions file.
#
if {$HierGen(blocksFile) eq ""} {
HierGen:_browseBlocks
}
if {$HierGen(blocksFile) eq ""} {
return
}
##
# Create a custom widget in the bottom tab.
#
if {![gui window exists $HierGen(tabName)]} {
HierGen:_create
}
##
# Get the widget path of the HierGen tab.
#
set w [gui window path $HierGen(tabName)]
##
# Start the progress bar.
#
zprogress begin
##
# Create an empty dictionary to store all data.
#
set HierGen(subcktDef) [dict create]
set HierGen(devices) {}
set HierGen(bbox) {}
set HierGen(motion) 0
set percent 0.0
##
# Read the sub-circuit definition file and store the polygon points for
# each named block in the subcktDef dictionary.
#
set percent [expr {$percent + 0.01}]
zprogress push "Read Subcircuit Definition" $percent
HierGen:_readSubcktDefinitionFile $HierGen(blocksFile) HierGen(subcktDef)
zprogress pop
##
# Check if a device is located in overlapping polygons.
#
if {$HierGen(checkForOverlap)} {
set percent [expr {$percent + 0.08}]
zprogress push "Check Overlaps" $percent
HierGen:_doCheckOverlaps
zprogress pop
}
##
# Normalize the coordinates in the subcktDef dictionary and
# in the database attributes.
# Also find the closest device distance.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.02}]
zprogress push "Normalize Coordinates" $percent
set minDistance [HierGen:_normalizeCoordinates $db HierGen(subcktDef)]
zprogress pop
}
##
# Calculate the hierarchy.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.75}]
zprogress push "" $percent
HierGen:_fillTree $w $db $HierGen(subcktDef)
zprogress pop
}
##
# Draw a physical view.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.02}]
zprogress push "Draw Devices" $percent
HierGen:_drawDevices $w $db $HierGen(subcktDef) $minDistance
zprogress pop
}
##
# Draw the blocks (polygons from the sub-circuit definition file) into
# the physical view.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.03}]
zprogress push "Draw Polygons" $percent
HierGen:_drawPolygons $w $db $HierGen(subcktDef)
zprogress pop
}
##
# Create the minimap.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.01}]
zprogress push "Create Minimap" $percent
HierGen:_minimapCreate $w $db
zprogress pop
}
##
# Set the scroll region of the canvas
# and display the physical view in fullfit mode.
#
if {![zprogress isinterrupted]} {
HierGen:_fullfit $w
}
##
# Re-Create a hierarchical database.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.01}]
zprogress push "Create Hierarchy" $percent
HierGen:_createHierarchy $w $db
zprogress pop
}
##
# Regenerate the schematic view.
#
if {![zprogress isinterrupted]} {
set percent [expr {$percent + 0.01}]
zprogress push "Regenerate Schematic" $percent
gui database modified
zprogress pop
}
##
# Write the hierarchical netlist.
#
if {(![zprogress isinterrupted]) && ($HierGen(netlistFile) ne "")} {
zprogress push "Write Netlist" 1.0
$db write spice -comments -1 -noEnd $HierGen(netlistFile)
zprogress pop
}
##
# End the progress bar.
#
zprogress end
##
# Restore the original value for the big module limit.
#
::Settings::Set bigModuleLimit $HierGen(persist:bigModuleLimit)
gui settings changed
}
# =============================================================================
# CheckForOverlaps -
# =============================================================================
#
proc HierGen:CheckForOverlaps {} {
zprogress begin
zprogress push "Check Overlaps" 1.0
HierGen:_doCheckOverlaps
zprogress pop
zprogress end
}
# =============================================================================
# RecognizeGates - Try to recognize logic gates.
# =============================================================================
#
proc HierGen:RecognizeGates {} {
set db [gui database get]
if {$db == {}} {
return
}
zprogress begin
zprogress push "Recognize Gates" 1.0
$db oper gate -noflat -createSupplyPorts
zprogress pop
zprogress end
gui database changed $db
}
# -----------------------------------------------------------------------------
# _customizeHideMenu -
# -----------------------------------------------------------------------------
#
proc HierGen:_customizeHideMenu {menuId} {
global HierGen
if {[gui window exists $HierGen(tabName)]} {
set state normal
} else {
set state disabled
}
$menuId entryconfigure end -state $state
}
# -----------------------------------------------------------------------------
# _hideWindow -
# -----------------------------------------------------------------------------
#
proc HierGen:_hideWindow {} {
global HierGen
gui window removeCustomWidget $HierGen(tabName)
}
# -----------------------------------------------------------------------------
# _browseBlocks -
# -----------------------------------------------------------------------------
#
proc HierGen:_browseBlocks {} {
global HierGen
set fname [gui window fileDialog openFile "Open Subcircuit Definition" {}]
if {$fname ne ""} {
set HierGen(blocksFile) $fname
}
}
# -----------------------------------------------------------------------------
# _create -
# -----------------------------------------------------------------------------
#
proc HierGen:_create {} {
global HierGen
##
# Insert a custom widget into the bottom tab.
#
set w [gui window insertCustomWidget \
-pluginNamespace "HierGen" \
$HierGen(tabName)]
if {[winfo exists $w.pane]} {
return
}
##
# Create a toolbar.
#
ttk::frame $w.toolbar
ttk::label $w.toolbar.l -text "Block Definitions:"
ttk::entry $w.toolbar.e \
-textvariable HierGen(blocksFile)
ttk::button $w.toolbar.browse \
-text "Browse" \
-command [list HierGen:_browseBlocks]
grid $w.toolbar.l -row 0 -column 0 -sticky w -padx 2 -pady 2
grid $w.toolbar.e -row 0 -column 1 -sticky we -padx 2 -pady 2
grid $w.toolbar.browse -row 0 -column 2 -sticky w -padx 2 -pady 2
grid columnconfigure $w.toolbar 1 -weight 1
##
# Split the widget horizontally into a left and right side.
#
panedwindow $w.pane -orient horizontal
##
# Add a tree view showing the re-created hierarchy to the left side.
#
ttk::frame $w.left
ttk::treeview $w.list \
-selectmode browse \
-show tree \
-yscrollcommand [list $w.left.y set]
ttk::scrollbar $w.left.y -orient vertical -command [list $w.list yview]
grid $w.list -row 0 -column 0 -sticky news -in $w.left
grid $w.left.y -row 0 -column 1 -sticky ns
grid rowconfigure $w.left 0 -weight 1
grid columnconfigure $w.left 0 -weight 1
$w.pane add $w.left
##
# Add bindings to interact with the tree widget.
#
bind $w.list <<TreeviewSelect>> [list HierGen:_treeNodeSelected $w]
##
# Create a canvas for drawing the physical view.
#
ttk::frame $w.right
canvas $w.c \
-background $HierGen(color:background)
grid $w.c -row 0 -column 0 -sticky news -in $w.right
grid rowconfigure $w.right 0 -weight 1
grid columnconfigure $w.right 0 -weight 1
$w.pane add $w.right
##
# Add bindings to interact with the physical view.
#
if {[::Settings::Get "dndbutton"] eq "left"} {
set strokeButton 3
} else {
set strokeButton 1
}
bind $w.c <Button> [list HierGen:_press $w %x %y]
bind $w.c <B${strokeButton}-Motion> [list HierGen:_motion $w %x %y]
bind $w.c <ButtonRelease> [list HierGen:_release $w %x %y]
bind $w.c <Button-2> {%W scan mark %x %y}
bind $w.c <B2-Motion> [list HierGen:_dragTo $w %x %y]
bind $w.c <Configure> [list HierGen:_minimapUpdate $w]
grid $w.toolbar -row 0 -column 0 -sticky we
grid $w.pane -row 1 -column 0 -sticky news
grid rowconfigure $w 1 -weight 1
grid columnconfigure $w 0 -weight 1
update
return $w
}
# -----------------------------------------------------------------------------
# _removeVisualFeedback -
# -----------------------------------------------------------------------------
#
proc HierGen:_removeVisualFeedback {w} {
global HierGen
$w.c itemconfigure POLYGON \
-fill $HierGen(color:polygon) \
-width 1
$w.c itemconfigure DEVICE \
-outline $HierGen(color:instance) \
-width 1
}
# -----------------------------------------------------------------------------
# _selectItem -
# -----------------------------------------------------------------------------
#
proc HierGen:_selectItem {w item interactive} {
global HierGen
if {$HierGen(motion)} {
return
}
set HierGen(interactive) $interactive
$w.list see $item
$w.list selection set [list $item]
$w.list focus $item
}
# -----------------------------------------------------------------------------
# _createOidList -
# -----------------------------------------------------------------------------
#
proc HierGen:_createOidList {w name} {
if {[$w.list parent $name] == {}} {
return {}
}
set oid [list "inst"]
set start $name
while {$start != {}} {
set oid [linsert $oid 1 $start]
set start [$w.list parent $start]
}
return [list $oid]
}
# -----------------------------------------------------------------------------
# _treeNodeSelected -
# -----------------------------------------------------------------------------
#
proc HierGen:_treeNodeSelected {w} {
global HierGen
set item [$w.list focus]
HierGen:_removeVisualFeedback $w
if {[$w.list tag has DEVICE $item]} {
$w.c itemconfigure $item \
-outline $HierGen(color:selected) \
-width 2
if {!$HierGen(interactive)} {
HierGen:_goto $w $item 0.05
}
}
if {[$w.list tag has POLYGON $item]} {
$w.c itemconfigure $item \
-fill $HierGen(color:selected) \
-width 2
if {!$HierGen(interactive)} {
HierGen:_goto $w $item 0.5
}
}
set oidList [HierGen:_createOidList $w $item]
gui goto -class Schem $oidList
gui tree setCurrentModule [lindex $oidList 0]
set HierGen(interactive) false
}
# -----------------------------------------------------------------------------
# _readSubcktDefinitionFile - Read a sub-circuit definition file and store all
# values in a dictionary.
# -----------------------------------------------------------------------------
#
proc HierGen:_readSubcktDefinitionFile {fileName subcktDefName} {
upvar 1 $subcktDefName subcktDef
set fileSize [file size $fileName]
set bytesRead 0
set in [open $fileName "r"]
while {![eof $in]} {
set bytes [gets $in line]
incr bytesRead $bytes
if {[zprogress update "" $bytesRead $fileSize]} {
break
}
set line [string trim $line]
if {($line eq "") || ([string match "#*" $line])} {
continue
}
if {[llength $line] != 2} {
zmessage print ERR "Bad line: $line"
continue
}
set name [lindex $line 0]
set polygon [lindex $line 1]
if {[dict exists $subcktDef $name]} {
zmessage print ERR "Polygon $name: already exists"
continue
}
if {[llength $polygon] < 8} {
zmessage print ERR "Polygon $name: too few coordinates"
continue
}
if {([llength $polygon] % 2) != 0} {
zmessage print ERR "Polygon $name: odd number of coordinates"
continue
}
if {([lindex $polygon 0] != [lindex $polygon end-1]) &&
([lindex $polygon 1] != [lindex $polygon end])} \
{
lappend polygon [lindex $polygon 0] [lindex $polygon 1]
}
set nonRectangular 0
set lastX {}
set lastY {}
foreach {x y} $polygon {
if {($lastX != {}) && ($x != $lastX) && ($y != $lastY)} {
set nonRectangular 1
break
}
set lastX $x
set lastY $y
}
if {$nonRectangular} {
zmessage print ERR "Polygon $name: non-rectangular"
continue
}
set sanitized {}
foreach c $polygon {
lappend sanitized [HierGen:_sanitizeFloat $c]
}
dict set subcktDef $name POLYGON $sanitized
dict set subcktDef $name RECT [expr {[llength $sanitized] == 10}]
dict set subcktDef $name AREA {}
dict set subcktDef $name BBOX {}
}
close $in
}
# -----------------------------------------------------------------------------
# _sanitizeFloat -
# -----------------------------------------------------------------------------
#
proc HierGen:_sanitizeFloat {value} {
if {[regexp {^([^.]*\.[^.]*)\.([^.]*)$} $value -> base exp]} {
set value [expr {$base * (10 ** $exp)}]
}
return [string trimright $value "0"]
}
# -----------------------------------------------------------------------------
# _getFractions -
# -----------------------------------------------------------------------------
#
proc HierGen:_getFractions {value} {
set s [split $value .]
set len [llength $s]
if {$len == 1} {
return 0
} elseif {$len == 2} {
return [string length [string trimright [lindex $s 1] 0]]
}
error "Bad coordinate: $value"
}
# -----------------------------------------------------------------------------
# _updateBBOX -
# -----------------------------------------------------------------------------
#
proc HierGen:_updateBBOX {x y x0n x1n y0n y1n} {
upvar 1 $x0n x0
upvar 1 $x1n x1
upvar 1 $y0n y0
upvar 1 $y1n y1
if {$x0 == {}} {
set x0 $x
set x1 $x
set y0 $y
set y1 $y
} else {
set x0 [expr {min($x, $x0)}]
set x1 [expr {max($x, $x1)}]
set y0 [expr {min($y, $y0)}]
set y1 [expr {max($y, $y1)}]
}
}
# -----------------------------------------------------------------------------
# _normalizeCoordinates -
# -----------------------------------------------------------------------------
#
proc HierGen:_normalizeCoordinates {db subcktDefName} {
global HierGen
upvar 1 $subcktDefName subcktDef
lassign {} bboxX0 bboxX1 bboxY0 bboxY1
##
# Determine the overall bbox, compute bbox/area for all polygons as a
# side-effect.
#
set names [dict keys $subcktDef]
set progress_max [llength $names]
set progress 0
zprogress push "" 0.2
foreach name $names {
if {[zprogress update "" [incr progress] $progress_max]} {
break
}
set polygon {}
lassign {} pbboxX0 pbboxX1 pbboxY0 pbboxY1
foreach {x y} [dict get $subcktDef $name POLYGON] {
##
# Flip y-coordinates.
#
set y [expr {-1 * $y}]
lappend polygon $x $y
HierGen:_updateBBOX $x $y pbboxX0 pbboxX1 pbboxY0 pbboxY1
}
HierGen:_updateBBOX $pbboxX0 $pbboxY0 bboxX0 bboxX1 bboxY0 bboxY1
HierGen:_updateBBOX $pbboxX1 $pbboxY1 bboxX0 bboxX1 bboxY0 bboxY1
dict set subcktDef $name POLYGON $polygon
dict set subcktDef $name AREA [::math::geometry::areaPolygon $polygon]
dict set subcktDef $name BBOX [list $pbboxX0 $pbboxX1 $pbboxY0 $pbboxY1]
}
if {[zprogress pop]} {
return
}
set devices {}
set top [$db get_top_design]
set progress_max [$db count inst $top]
set progress 0
zprogress push "" 1.0
set points {}
$db foreach inst $top inst {
if {[zprogress update "" [incr progress] $progress_max]} {
break
}
set x [HierGen:_getCoord $db $inst X]
set y [HierGen:_getCoord $db $inst Y]
if {($x eq "") || ($y eq "")} {
continue
}
##
# Flip y-coordinates.
#
set y [expr {-1 * $y}]
HierGen:_updateBBOX $x $y bboxX0 bboxX1 bboxY0 bboxY1
lappend devices \
$x $y [$db oid oname $inst] [HierGen:_getInstFillColor $db $inst]
lappend points $x $y
}
set HierGen(devices) $devices
set HierGen(bbox) [list $bboxX0 $bboxX1 $bboxY0 $bboxY1]
if {[zprogress pop]} {
return
}
#checker exclude badSwitch
#checker exclude numArgs
set points [lsort -real -index 0 -stride 2 $points]
return [HierGen:_minDistance $points]
}
# -----------------------------------------------------------------------------
# _qtreeCreate -
# -----------------------------------------------------------------------------
#
proc HierGen:_qtreeCreate {} {
set t [dict create]
set node /
dict set t $node {}
return $t
}
# -----------------------------------------------------------------------------
# _qtreeInsert -
# -----------------------------------------------------------------------------
#
proc HierGen:_qtreeInsert {tName tbbox bbox name} {
upvar 1 $tName t
lassign $tbbox X0 X1 Y0 Y1
lassign $bbox x0 x1 y0 y1
if {($x0 < $X0) || ($x1 > $X1) || ($y0 < $Y0) || ($y1 > $Y1)} {
error "Cannot insert $name (bbox=$bbox); outside qtree (bbox=$tbbox)."
}
set node "/"
while {1} {
set X [expr {($X0 + $X1) * 0.5}]
set Y [expr {($Y0 + $Y1) * 0.5}]
if {(($x0 <= $X) && ($X < $x1)) || (($y0 <= $Y) && ($Y < $y1))} {
dict lappend t $node $name $x0 $y0 $x1 $y1
return
}
if {![dict exists $t ${node}0]} {
dict set t ${node}0 {}
dict set t ${node}1 {}
dict set t ${node}2 {}
dict set t ${node}3 {}
}
if {$y1 <= $Y} {
set Y1 $Y
if {$x1 <= $X} {
set X1 $X
append node "0"
} else {
set X0 $X
append node "1"
}
} elseif {$x1 <= $X} {
set Y0 $Y
set X1 $X
append node "2"
} else {
set Y0 $Y
set X0 $X
append node "3"
}
}
}
# -----------------------------------------------------------------------------
# _qtreeFind -
# -----------------------------------------------------------------------------
#
proc HierGen:_qtreeFind {t tbbox x y} {
lassign $tbbox X0 X1 Y0 Y1
if {($x < $X0) || ($x > $X1) || ($y < $Y0) || ($y > $Y1)} {
return {}
}
set result {}
set node "/"
while {1} {
foreach {name x0 y0 x1 y1} [dict get $t $node] {
if {($x0 <= $x) && ($x <= $x1) && ($y0 <= $y) && ($y <= $y1)} {
lappend result $name
}
}
if {![dict exists $t ${node}0]} {
break
}
set X [expr {($X0 + $X1) * 0.5}]
set Y [expr {($Y0 + $Y1) * 0.5}]
if {$y <= $Y} {
set Y1 $Y
if {$x <= $X} {
set X1 $X
append node "0"
} else {
set X0 $X
append node "1"
}
} elseif {$x <= $X} {
set Y0 $Y
set X1 $X
append node "2"
} else {
set Y0 $Y
set X0 $X
append node "3"
}
}
return $result
}
# -----------------------------------------------------------------------------
# _pointInPoly -
# -----------------------------------------------------------------------------
#
proc HierGen:_pointInPoly {x y polygon} {
##
# on poly border => in poly
#
lassign [lrange $polygon 0 1] x0 y0
foreach {x1 y1} [lrange $polygon 2 end] {
if {$x0 == $x1} {
if {($x == $x0) && ($y >= min($y0, $y1)) && ($y <= max($y0, $y1))} {
return 1
}
} else {
if {($y == $y0) && ($x >= min($x0, $x1)) && ($x <= max($x0, $x1))} {
return 1
}
}
set x0 $x1
set y0 $y1
}
return [::math::geometry::pointInsidePolygon [list $x $y] $polygon]
}
# -----------------------------------------------------------------------------
# _createTreeNodes -
# -----------------------------------------------------------------------------
#
proc HierGen:_createTreeNodes {w topoName id children} {
upvar 1 $topoName topo
foreach child $children {
$w.list insert $id end -id $child -text $child -tags POLYGON
HierGen:_createTreeNodes $w topo $child [dict get $topo $child]
}
}
# -----------------------------------------------------------------------------
# _fillTree -
# -----------------------------------------------------------------------------
#
proc HierGen:_fillTree {w db subcktDef} {
global HierGen
##
# build quadtree
#
set tbbox $HierGen(bbox)
set qt [HierGen:_qtreeCreate]
zprogress push "Reconstruct Subcircuit Hierarchy" 0.01
set progressMax [dict size $subcktDef]
set progress 0
dict for {name subckt} $subcktDef {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
HierGen:_qtreeInsert qt $tbbox [dict get $subckt BBOX] $name
}
if {[zprogress pop]} {
return
}
set progressMax [dict size $subcktDef]
set progress 0
zprogress push "Reconstruct Subcircuit Hierarchy" 0.02
set topo [dict create]
dict set topo {} {}
dict for {name0 subckt0} $subcktDef {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
dict set topo $name0 {}
}
if {[zprogress pop]} {
return
}
set progressMax [dict size $subcktDef]
set progress 0
zprogress push "Reconstruct Subcircuit Hierarchy" 0.06
dict for {name0 subckt0} $subcktDef {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
set area0 [dict get $subckt0 AREA]
set polygon0 [dict get $subckt0 POLYGON]
set rect0 [dict get $subckt0 RECT]
set point0 [lrange $polygon0 0 1]
lassign $point0 X Y
lassign [dict get $subckt0 BBOX] X0 X1 Y0 Y1
set name1_area1 {}
foreach name1 [HierGen:_qtreeFind $qt $tbbox $X $Y] {
set subckt1 [dict get $subcktDef $name1]
set area1 [dict get $subckt1 AREA]
if {$area1 <= $area0} {
continue
}
lappend name1_area1 $name1 $area1
}
set parent {}
##
# Work-around for TclChecker who doesn't known -stride (excludes
# don't work here).
#
set lsort lsort
foreach {name1 area1} [$lsort -real -index 1 -stride 2 $name1_area1] {
set subckt1 [dict get $subcktDef $name1]
set bbox1 [dict get $subckt1 BBOX]
lassign $bbox1 x0 x1 y0 y1
if {($x0 > $X0) || ($x1 < $X1) || ($y0 > $Y0) || ($y1 < $Y1)} {
continue
}
set parent $name1
set rect1 [dict get $subckt1 RECT]
if {$rect0 && $rect1} {
##
# Both polygons are rectangles
# => the above bbox check is enough
# => subckt0 is a child of subckt1!
#
break
}
if {$rect1} {
##
# Polygon1 is a rectangle, bbox checks are enough.
#
foreach {x y} $polygon0 {
if {($x < $x0) || ($x > $x1) || ($y < $y0) || ($y > $y1)} {
set parent {}
break
}
}
} else {
set polygon1 [dict get $subckt1 POLYGON]
foreach {x y} $polygon0 {
if {![HierGen:_pointInPoly $x $y $polygon1]} {
set parent {}
break
}
}
}
if {$parent != {}} {
break
}
}
dict set topo $parent [list $name0 {*}[dict get $topo $parent]]
}
if {[zprogress pop]} {
return
}
##
# Add the design name as the tree root node.
#
set topName [$db get_top_design -name]
$w.list insert {} end -id $topName -text $topName
HierGen:_createTreeNodes $w topo $topName [dict get $topo {}]
zprogress push "Determine Parents of Devices" 1.0
set progressMax [expr {[llength $HierGen(devices)] / 4}]
set progress 0
foreach {X Y name color} $HierGen(devices) {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
set name1_area1 {}
foreach name1 [HierGen:_qtreeFind $qt $tbbox $X $Y] {
set subckt1 [dict get $subcktDef $name1]
set area1 [dict get $subckt1 AREA]
lappend name1_area1 $name1 $area1
}
set parent {}
##
# Work-around for TclChecker who doesn't known -stride (excludes
# don't work here).
#
set lsort lsort
foreach {name1 area1} [$lsort -real -index 1 -stride 2 $name1_area1] {
set subckt1 [dict get $subcktDef $name1]
##
# The polygon is a rectangle.
# => the bbox check in _qtreeFind is enough
# => X/Y is inside subckt1
#
if {[dict get $subckt1 RECT]} {
set parent $name1
break
}
if {[HierGen:_pointInPoly $X $Y [dict get $subckt1 POLYGON]]} {
set parent $name1
break
}
}
if {$parent == {}} {
set parent $topName
}
$w.list insert $parent end -id $name -text $name -tags DEVICE
}
zprogress pop
}
# -----------------------------------------------------------------------------
# _minDistance - Find minimum pairwise distance among $points. $points must be
# sorted by x-coordinate.
# -----------------------------------------------------------------------------
#
proc HierGen:_minDistance {points} {
set nPoints [expr {[llength $points] / 2}]
##
# Brute force min distance calculation if there's <= 3 points.
#
if {$nPoints <= 1} {
return Inf
} elseif {$nPoints == 2} {
lassign $points a0 a1 b0 b1
return [expr {hypot(($a0 - $b0), ($a1 - $b1))}]
} elseif {$nPoints == 3} {
lassign $points a0 a1 b0 b1 c0 c1
set ab [expr {hypot(($a0 - $b0), ($a1 - $b1))}]
set ac [expr {hypot(($a0 - $c0), ($a1 - $c1))}]
set bc [expr {hypot(($b0 - $c0), ($b1 - $c1))}]
return [expr {min($ab, min($ac, $bc))}]
}
##
# Split points in half, find min distances for left and right sets.
#
set mid [expr {2 * int(ceil($nPoints / 2.0))}]
set left [lrange $points 0 [expr {$mid - 1}]]
set right [lrange $points $mid end]
set leftDist [HierGen:_minDistance $left]
set rightDist [HierGen:_minDistance $right]
set minDist [expr {min($leftDist, $rightDist)}]
##
# Now compute the 2*$minDist wide border strip between left and right.
#
set midPx [lindex $left end-1]
set border {}
foreach {p0 p1} $points {
if {abs($midPx - $p0) < $minDist} {
lappend border [list $p0 $p1]
}
}
set nBorder [llength $border]
##
# Find point pairs (pi, pk) in the border, that have a smaller distance
# than $minDist.
#
set ySorted [lsort -real -index 1 $border]
for {set i 0} {$i < ($nBorder - 1)} {incr i} {
lassign [lindex $ySorted $i] pix piy
for {set k [expr {$i + 1}]} {$k < $nBorder} {incr k} {
lassign [lindex $ySorted $k] pkx pky
set dy [expr {$pky - $piy}]
if {$dy >= $minDist} {
break
}
set dist [expr {hypot(($pkx - $pix), $dy)}]
if {$dist < $minDist} {
set minDist $dist
}
}
}
return $minDist
}
# -----------------------------------------------------------------------------
# _drawDevices -
# -----------------------------------------------------------------------------
#
proc HierGen:_drawDevices {w db subcktDef dist} {
global HierGen
set top [$db get_top_design]
set d [expr {($dist / sqrt(2)) / 2}]
set progressMax [expr {[llength $HierGen(devices)] / 4}]
set progress 0
foreach {x y name color} $HierGen(devices) {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
$w.c create rectangle \
[expr {$x - $d}] [expr {$y - $d}] \
[expr {$x + $d}] [expr {$y + $d}] \
-outline $HierGen(color:instance) \
-fill $color \
-tags [list DEVICE $name]
$w.c bind $name <ButtonRelease-1> \
[list HierGen:_selectItem $w $name true]
}
##
# If interrupt was pressed, then remove all created items.
#
if {[zprogress isinterrupted]} {
$w.c delete all
}
}
# -----------------------------------------------------------------------------
# _getInstFillColor -
# -----------------------------------------------------------------------------
#
proc HierGen:_getInstFillColor {db inst} {
global HierGen
##
# Get the functions for the given device.
#
set primFunc [$db primFuncOf $inst]
##
# Check for device functions without color definition.
#
if {![info exists HierGen(color:$primFunc)]} {
set refCount [$db refCount [$db down $inst]]
set msg "Device type ($primFunc) without color definition "
append msg "($refCount devices).\n"
append msg "\tUsing the default color \"$HierGen(color:UNKNOWNDEV)\".\n"
append msg "\tTo use a custom color: "
append msg "'set HierGen(color:$primFunc) \"#RGB\"'"
zmessage print ERR $msg
set HierGen(color:$primFunc) $HierGen(color:UNKNOWNDEV)
}
return $HierGen(color:$primFunc)
}
# -----------------------------------------------------------------------------
# _showInPhysicalView -
# -----------------------------------------------------------------------------
#
proc HierGen:_showInPhysicalView {oidList} {
global HierGen
if {![gui window exists $HierGen(tabName)]} {
return
}
set w [gui window path $HierGen(tabName)]
HierGen:_removeVisualFeedback $w
set db [gui database get]
foreach oid $oidList {
HierGen:_selectItem $w [$db oid oname $oid] false
break
}
}
# -----------------------------------------------------------------------------
# _dragTo -
# -----------------------------------------------------------------------------
#
proc HierGen:_dragTo {w x y} {
$w.c scan dragto $x $y 1
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _fullfit - Scale the plot to fit the visible area.
# -----------------------------------------------------------------------------
#
proc HierGen:_fullfit {w} {
lassign [$w.c bbox all] x0 y0 x1 y1
if {$x0 == {}} {
return
}
set width [winfo width $w.c]
set height [winfo height $w.c]
if {($width == 0) || ($height == 0)} {
return
}
set dx [expr {double($x1 - $x0)}]
set dy [expr {double($y1 - $y0)}]
if {($dx == 0) || ($dy == 0)} {
return
}
set factor [expr {min(($width / $dx), ($height / $dy))}]
$w.c scale all 0 0 $factor $factor
##
# Move the canvas' bbox center to the canvas' widget center.
#
lassign [$w.c bbox all] x0 y0 x1 y1
set mx [expr {($x0 + $x1) / 2.0}]
set my [expr {($y0 + $y1) / 2.0}]
$w.c scan mark {*}[HierGen:_toPixel $w $mx $my]
$w.c scan dragto \
[expr {round($width / 2.0)}] [expr {round($height / 2.0)}] 1
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _zoomOut -
# -----------------------------------------------------------------------------
#
proc HierGen:_zoomOut {w stroke} {
lassign $stroke x0 y0 x1 y1
set dx [expr {abs($x1 - $x0)}]
set dy [expr {abs($y1 - $y0)}]
if {$dx == 0} {
if {$dy == 0} {
return
}
set strokeLen $dy
set height [winfo height $w.c]
set hh [$w.c canvasy $height]
set maxLen [expr {abs($hh - $y0)}]
set ratio [expr {min(1.0, ($strokeLen / $maxLen))}]
} elseif {$dy == 0} {
set strokeLen $dx
set width [winfo width $w.c]
set ww [$w.c canvasx $width]
set maxLen [expr {abs($ww - $x0)}]
} else {
set strokeLen [expr {hypot($dx, $dy)}]
set width [winfo width $w.c]
set height [winfo height $w.c]
set ww [$w.c canvasx $width]
set hh [$w.c canvasy $height]
set cdx [expr {abs($ww - $x0)}]
set cdy [expr {abs($hh - $y0)}]
if {($dx / $dy) >= ($cdx / $cdy)} {
set maxLen [expr {hypot($cdx, ($cdx * ($dy / $dx)))}]
} else {
set maxLen [expr {hypot(($cdy * ($dx / $dy)), $cdy)}]
}
}
set ratio [expr {min(1.0, ($strokeLen / $maxLen))}]
##
# Map ratio to "zoom factor":
# 0 -> 1.0 (no zoom if stroke length is 0),
# 1 -> 0.25 (4x zoom, if stroke length is maximal)
#
set factor [expr {1.0 - (0.75 * $ratio)}]
##
# Scale canvas, keep $x0/$$y0 fixed.
#
$w.c scale all $x0 $y0 $factor $factor
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _zoomRectangle -
# -----------------------------------------------------------------------------
#
proc HierGen:_zoomRectangle {w rect} {
if {[llength $rect] != 4} {
return
}
lassign $rect x0 y0 x1 y1
if {($x0 == $x1) || ($y0 == $y1)} {
return
}
##
# Determine scale factor.
#
set dx [expr {abs($x0 - $x1)}]
set dy [expr {abs($y0 - $y1)}]
if {($dx == 0) || ($dy == 0)} {
return
}
set width [winfo width $w.c]
set height [winfo height $w.c]
set factor [expr {min(($width / $dx), ($height / $dy))}]
set mx [expr {$factor * (($x0 + $x1) / 2.0)}]
set my [expr {$factor * (($y0 + $y1) / 2.0)}]
$w.c scale all 0 0 $factor $factor
##
# Move the zoom rectangle's center to the canvas center.
#
$w.c scan mark {*}[HierGen:_toPixel $w $mx $my]
$w.c scan dragto \
[expr {round($width / 2.0)}] [expr {round($height / 2.0)}] 1
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _press -
# -----------------------------------------------------------------------------
#
proc HierGen:_press {w x y} {
global HierGen
set HierGen(startX) [$w.c canvasx $x]
set HierGen(startY) [$w.c canvasy $y]
set HierGen(motion) 0
}
# -----------------------------------------------------------------------------
# _motion -
# -----------------------------------------------------------------------------
#
proc HierGen:_motion {w x y} {
global HierGen
set x0 $HierGen(startX)
set y0 $HierGen(startY)
set x [$w.c canvasx $x]
set y [$w.c canvasy $y]
##
# do nothing in a range of +/- 5 pixel to avoid unwanted
# actions to be triggered
#
if {(abs($x - $x0) <= 5) && (abs($y - $y0) <= 5)} {
set HierGen(strokeOper) ""
return
}
set HierGen(motion) 1
$w.c delete rubberband
if {($x < $x0) && ($y > $y0)} {
set HierGen(strokeOper) "Zoom Fit"
$w.c create line $x0 $y0 $x $y \
-tags {rubberband rubberband_stroke} \
-fill $HierGen(color:stroke) \
-width 2
} elseif {($x > $x0) && ($y < $y0)} {
set HierGen(strokeOper) "Zoom Out"
$w.c create line $x0 $y0 $x $y \
-tags {rubberband rubberband_stroke} \
-fill $HierGen(color:stroke) \
-width 2
} else {
set HierGen(strokeOper) "Zoom In"
$w.c create rectangle $x0 $y0 $x $y \
-width 2 \
-fill {} \
-tags {rubberband rubberband_stroke} \
-outline $HierGen(color:stroke)
}
if {$x < $x0} {
set anchor se
set xr [expr {$x - 5}]
} else {
set anchor sw
set xr [expr {$x + 5}]
}
$w.c create text $xr $y \
-text $HierGen(strokeOper) \
-tags {rubberband rubberband_txt} \
-fill $HierGen(color:stroke) \
-anchor $anchor
$w.c create rectangle [$w.c bbox rubberband_txt] \
-outline $HierGen(color:background) \
-fill $HierGen(color:background) \
-tags {rubberband}
$w.c raise rubberband_txt
}
# -----------------------------------------------------------------------------
# _toPixel - Convert canvas coordinates to widget pixel coordinates.
# -----------------------------------------------------------------------------
#
proc HierGen:_toPixel {w x y} {
set width [winfo width $w.c]
set height [winfo height $w.c]
set cx0 [$w.c canvasx 0]
set cxw [$w.c canvasx $width]
set cy0 [$w.c canvasy 0]
set cyh [$w.c canvasy $height]
set px [expr {round((($x - $cx0) * $width) / ($cxw - $cx0))}]
set py [expr {round((($y - $cy0) * $height) / ($cyh - $cy0))}]
return [list $px $py]
}
# -----------------------------------------------------------------------------
# _release -
# -----------------------------------------------------------------------------
#
proc HierGen:_release {w x y} {
global HierGen
set coords [$w.c coords rubberband_stroke]
$w.c delete rubberband
if {$HierGen(motion) == 0} {
HierGen:_removeVisualFeedback $w
}
if {$coords == {}} {
return
}
switch -- $HierGen(strokeOper) {
"Zoom Fit" {
HierGen:_fullfit $w
}
"Zoom Out" {
HierGen:_zoomOut $w $coords
}
"Zoom In" {
HierGen:_zoomRectangle $w $coords
}
default {
}
}
}
# -----------------------------------------------------------------------------
# _drawPolygons -
# -----------------------------------------------------------------------------
#
proc HierGen:_drawPolygons {w db subcktDef} {
global HierGen
set size [dict size $subcktDef]
set i 0
dict for {name subckt} $subcktDef {
if {[zprogress update "" [incr i] $size]} {
break
}
$w.c create line [dict get $subckt POLYGON] \
-fill $HierGen(color:polygon) \
-tags [list POLYGON $name]
$w.c bind $name <ButtonRelease-1> \
[list HierGen:_selectItem $w $name true]
}
##
# If interrupt was pressed, then remove all created items.
#
if {[zprogress isinterrupted]} {
$w.c delete all
}
}
# -----------------------------------------------------------------------------
# _getCoord -
# -----------------------------------------------------------------------------
#
proc HierGen:_getCoord {db oid v} {
foreach attr [list @$v $v \$$v] {
set value [$db attr $oid getValue $attr]
if {$value ne ""} {
return $value
}
}
return ""
}
# -----------------------------------------------------------------------------
# _goto - Center and zoom to $tag.
# -----------------------------------------------------------------------------
#
proc HierGen:_goto {w tag ratio} {
set canvas $w.c
lassign [$canvas bbox $tag] x0 y0 x1 y1
if {$x0 == {}} {
return
}
##
# Move bbox center to widget center.
#
set mx [expr {($x0 + $x1) / 2.0}]
set my [expr {($y0 + $y1) / 2.0}]
$canvas scan mark {*}[HierGen:_toPixel $w $mx $my]
set width [winfo width $w.c]
set height [winfo height $w.c]
$canvas scan dragto \
[expr {round($width / 2.0)}] [expr {round($height / 2.0)}] 1
##
# Zoom in such that the bbox fills $ratio of canvas.
# First, zoom in such that the bbox fills the whole canvas, then zoom out
# again to $ratio. This avoids zoom loops when repeatedly calling _goto for
# the same tag.
#
foreach r [list 1.0 $ratio] {
lassign [$canvas bbox $tag] x0 y0 x1 y1
set dx [expr {abs($x1 - $x0)}]
set dy [expr {abs($y1 - $y0)}]
set ww [expr {abs([$canvas canvasx $width] - [$canvas canvasx 0])}]
set hh [expr {abs([$canvas canvasy $height] - [$canvas canvasy 0])}]
set factor [expr {$r * min(($ww / $dx), ($hh / $dy))}]
set mx [expr {($x0 + $x1) / 2.0}]
set my [expr {($y0 + $y1) / 2.0}]
$canvas scale all $mx $my $factor $factor
}
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _createHierarchy -
# -----------------------------------------------------------------------------
#
proc HierGen:_createHierarchy {w db} {
global HierGen
set top [lindex [$w.list children {}] 0]
set topOid [$db search top $top]
set count [llength [$w.list children $top]]
set i 0
zprogress push "" 0.1
set subList {}
set total 0
foreach sub [$w.list children $top] {
if {[zprogress update "" [incr i] $count]} {
break
}
if {[$w.list tag has DEVICE $sub]} {
continue
}
set HierGen($sub:deviceList) {}
lappend subList $sub
HierGen:_collectDevices $w $db $sub $sub
foreach deviceList $HierGen($sub:deviceList) {
incr total [llength [lrange $deviceList 1 end]]
}
}
if {[zprogress pop]} {
return
}
set i 0
zprogress push "" 1.0
$db oper hierStart $topOid
foreach sub $subList {
foreach deviceList $HierGen($sub:deviceList) {
set oidList {}
foreach inst [lrange $deviceList 1 end] {
if {[zprogress update "" [incr i] $total]} {
break
}
set oid [$db search inst $topOid $inst]
if {[$db oid isnull $oid]} {
zmessage print ERR "Cannot find inst '$inst' in $top."
continue
}
lappend oidList $oid
}
if {[zprogress isinterrupted]} {
break
}
##
# Create a new hierarchy.
#
set instName [lindex $deviceList 0]
zprogress push "" [expr {$i / double($total)}]
$db oper hierAdd $instName $instName \
{} \
{} \
{} \
[lrange $deviceList 1 end] \
-createSupplyPorts
if {[zprogress pop]} {
break
}
}
}
zprogress pop
$db oper hierFinish
}
# -----------------------------------------------------------------------------
# _collectDevices -
# -----------------------------------------------------------------------------
#
proc HierGen:_collectDevices {w db root node} {
global HierGen
set subList [list $node]
foreach sub [$w.list children $node] {
if {[$w.list tag has POLYGON $sub]} {
HierGen:_collectDevices $w $db $root $sub
}
lappend subList $sub
}
lappend HierGen($root:deviceList) $subList
}
# -----------------------------------------------------------------------------
# _minimapCreate -
# -----------------------------------------------------------------------------
#
proc HierGen:_minimapCreate {w db} {
global HierGen
set minimap $w.c.minimap
if {[winfo exists $minimap]} {
destroy $minimap
}
##
# Determine minimap size/scale.
#
lassign $HierGen(bbox) X0 X1 Y0 Y1
set dx [expr {double($X1 - $X0)}]
set dy [expr {double($Y1 - $Y0)}]
if {$dx >= $dy} {
set scale [expr {$HierGen(minimap:size) / $dx}]
set width $HierGen(minimap:size)
set height [expr {int($scale * $dy)}]
} else {
set scale [expr {$HierGen(minimap:size) / $dy}]
set width [expr {int($scale * $dx)}]
set height $HierGen(minimap:size)
}
##
# Create minimap image.
#
set photo [image create photo -width $width -height $height]
set top [$db get_top_design]
$photo put $HierGen(color:minimap_background) \
-to 0 0 [expr {$width - 1}] [expr {$height - 1}]
foreach {X Y name color} $HierGen(devices) {
set x [expr {int($scale * ($X - $X0))}]
set y [expr {int($scale * ($Y - $Y0))}]
$photo put $HierGen(color:minimap_devices) -to $x $y
}
set HierGen(minimap:scale) $scale
set HierGen(minimap:width) $width
set HierGen(minimap:height) $height
canvas $minimap -width $width -height $height
gui window internal $w.c $minimap "Minimap" $width $height
bind $minimap <ButtonPress-1> [list HierGen:_minimapPress $w %x %y]
$minimap create image 1 1 -image $photo -anchor nw -tags map
##
# Create viewport rectangle and crosshairs.
#
$minimap create rectangle 0 0 0 0 \
-tags viewport \
-width 2 \
-fill {} \
-outline $HierGen(color:stroke)
foreach tag {crossx0 crossx1 crossy0 crossy1} {
$minimap create line 0 0 0 0 \
-tags $tag \
-width 1 \
-fill $HierGen(color:stroke)
}
}
# -----------------------------------------------------------------------------
# _minimapUpdate -
# -----------------------------------------------------------------------------
#
proc HierGen:_minimapUpdate {w} {
global HierGen
if {![winfo exists $w.c.minimap]} {
return
}
lassign [$w.c bbox all] x0 y0 x1 y1
if {$x0 == {}} {
foreach tag {viewport crossx0 crossx1 crossy0 crossy1} {
$w.c.minimap coords $tag 0 0 0 0
}
return
}
set width [winfo width $w.c]
set height [winfo height $w.c]
if {($width == 0) || ($height == 0)} {
foreach tag {viewport crossx0 crossx1 crossy0 crossy1} {
$w.c.minimap coords $tag 0 0 0 0
}
return
}
set dx [expr {double($x1 - $x0)}]
set dy [expr {double($y1 - $y0)}]
if {($dx == 0) || ($dy == 0)} {
foreach tag {viewport crossx0 crossx1 crossy0 crossy1} {
$w.c.minimap coords $tag 0 0 0 0
}
return
}
set cx0 [$w.c canvasx 0]
set cx1 [$w.c canvasx $width]
set cy0 [$w.c canvasy 0]
set cy1 [$w.c canvasy $height]
set mw $HierGen(minimap:width)
set mh $HierGen(minimap:height)
set mx0 [expr {($mw / $dx) * ($cx0 - $x0)}]
set mx1 [expr {($mw / $dx) * ($cx1 - $x0)}]
set my0 [expr {($mh / $dy) * ($cy0 - $y0)}]
set my1 [expr {($mh / $dy) * ($cy1 - $y0)}]
set mx [expr {($mx0 + $mx1) / 2.0}]
set my [expr {($my0 + $my1) / 2.0}]
$w.c.minimap coords viewport $mx0 $my0 $mx1 $my1
$w.c.minimap coords crossx0 0 $my $mx0 $my
$w.c.minimap coords crossx1 $mx1 $my $mw $my
$w.c.minimap coords crossy0 $mx 0 $mx $my0
$w.c.minimap coords crossy1 $mx $my1 $mx $mh
}
# -----------------------------------------------------------------------------
# _minimapPress -
# -----------------------------------------------------------------------------
#
proc HierGen:_minimapPress {w x y} {
global HierGen
lassign [$w.c bbox all] x0 y0 x1 y1
if {$x0 == {}} {
return
}
set width [winfo width $w.c]
set height [winfo height $w.c]
if {($width == 0) || ($height == 0)} {
return
}
set dx [expr {double($x1 - $x0)}]
set dy [expr {double($y1 - $y0)}]
if {($dx == 0) || ($dy == 0)} {
return
}
set mw $HierGen(minimap:width)
set mh $HierGen(minimap:height)
set xx [expr {(($dx / $mw) * $x) + $x0}]
set yy [expr {(($dy / $mh) * $y) + $y0}]
lassign [HierGen:_toPixel $w $xx $yy] cx cy
$w.c scan mark $cx $cy
$w.c scan dragto [expr {int($width / 2)}] [expr {int($height / 2)}] 1
HierGen:_minimapUpdate $w
}
# -----------------------------------------------------------------------------
# _doCheckOverlaps -
# -----------------------------------------------------------------------------
#
proc HierGen:_doCheckOverlaps {} {
global HierGen
if {[gui database get] == {}} {
return
}
set w [gui window path $HierGen(tabName)]
if {($HierGen(subcktDef) == {}) || ([dict size $HierGen(subcktDef)] == 0)} {
zmessage print ERR \
"Cannot check for overlaps: no block definitions found"
return
}
if {[llength $HierGen(devices)] == 0} {
zmessage print ERR \
"Cannot check for overlaps: no device information found"
return
}
if {[llength $HierGen(bbox)] == 0} {
zmessage print ERR \
"Cannot check for overlaps: no bbox information found"
return
}
set tbbox $HierGen(bbox)
set qt [HierGen:_qtreeCreate]
set ancestors [dict create]
zprogress push "Build Quadtree" 0.1
set progressMax [dict size $HierGen(subcktDef)]
set progress 0
dict for {name subckt} $HierGen(subcktDef) {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
HierGen:_qtreeInsert qt $tbbox [dict get $subckt BBOX] $name
dict set ancestors $name {}
}
zprogress pop
if {[zprogress isinterrupted]} {
gui console printError "Aborted..."
zprogress end
return
}
zprogress push "Compute Hierarchy" 0.3
set progressMax [dict size $HierGen(subcktDef)]
set progress 0
dict for {name0 subckt0} $HierGen(subcktDef) {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
set area0 [dict get $subckt0 AREA]
set polygon0 [dict get $subckt0 POLYGON]
set rect0 [dict get $subckt0 RECT]
set point0 [lrange $polygon0 0 1]
lassign $point0 X Y
lassign [dict get $subckt0 BBOX] X0 X1 Y0 Y1
set parents {}
foreach name1 [HierGen:_qtreeFind $qt $tbbox $X $Y] {
set subckt1 [dict get $HierGen(subcktDef) $name1]
set area1 [dict get $subckt1 AREA]
if {$area1 <= $area0} {
continue
}
set bbox1 [dict get $subckt1 BBOX]
lassign $bbox1 x0 x1 y0 y1
if {($x0 > $X0) || ($x1 < $X1) || ($y0 > $Y0) || ($y1 < $Y1)} {
continue
}
set parent $name1
set rect1 [dict get $subckt1 RECT]
if {$rect0 && $rect1} {
##
# Both polygons are rectangles
# => the above bbox check is enough
# => subckt0 is a child of subckt1!
#
} elseif {$rect1} {
##
# Polygon1 is a rectangle, bbox checks are enough.
#
foreach {x y} $polygon0 {
if {($x < $x0) || ($x > $x1) || ($y < $y0) || ($y > $y1)} {
set parent {}
break
}
}
} else {
set polygon1 [dict get $subckt1 POLYGON]
foreach {x y} $polygon0 {
if {![HierGen:_pointInPoly $x $y $polygon1]} {
set parent {}
break
}
}
}
if {$parent != {}} {
lappend parents $parent
}
}
dict set ancestors $name0 [lsort $parents]
}
zprogress pop
if {[zprogress isinterrupted]} {
gui console printError "Aborted..."
zprogress end
return
}
zprogress push "Check Devices" 1.0
set progressMax [expr {[llength $HierGen(devices)] / 4}]
set progress 0
set count 0
foreach {X Y name color} $HierGen(devices) {
if {[zprogress update "" [incr progress] $progressMax]} {
break
}
set parents {}
foreach name1 [HierGen:_qtreeFind $qt $tbbox $X $Y] {
set subckt1 [dict get $HierGen(subcktDef) $name1]
if {[dict get $subckt1 RECT]} {
lappend parents $name1 [dict get $subckt1 AREA]
} elseif {[HierGen:_pointInPoly $X $Y \
[dict get $subckt1 POLYGON]]} {
lappend parents $name1 [dict get $subckt1 AREA]
}
}
if {[llength $parents] == 0} {
continue
}
#checker exclude badSwitch
#checker exclude numArgs
set parents [lsort -real -index 1 -stride 2 $parents]
set parent [lindex $parents 0]
set all {}
foreach {name1 area1} [lrange $parents 2 end] {
lappend all $name1
}
set all [lsort $all]
set anc [dict get $ancestors $parent]
if {$all != $anc} {
gui console printWithCallback \
"Overlap @ $name \[click to display\]" \
e \
[list HierGen:_selectItem $w $name 0]
foreach {name1 area1} $parents {
gui console printWithCallback \
"- Polygon $name1 \[click to display\]" \
e \
[list HierGen:_selectItem $w $name1 0]
}
incr count
}
}
zprogress pop
if {[zprogress isinterrupted]} {
gui console printError "Aborted..."
gui console printError "$count overlaps, so far"
} else {
gui console printError "$count total overlaps"
}
}
# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
HierGen:Init $argc $argv
|