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
###############################################################################
# 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
#       BrowseCdsLib
#   @namespace
#       BrowseCdsLib
#   @section
#       Cds
#   @description
#       Display the contents of a Cadence cds.lib file.
#   @files
#       cust42/browseCdsLib.tcl
#       cust42/img/cell-16.png
#       cust42/img/cell-24.png
#       cust42/img/cell-32.png
#       cust42/img/cell-48.png
#       cust42/img/cell-64.png
#       cust42/img/layoutView-16.png
#       cust42/img/layoutView-24.png
#       cust42/img/layoutView-32.png
#       cust42/img/layoutView-48.png
#       cust42/img/layoutView-64.png
#       cust42/img/library-16.png
#       cust42/img/library-24.png
#       cust42/img/library-32.png
#       cust42/img/library-48.png
#       cust42/img/library-64.png
#       cust42/img/schematicView-16.png
#       cust42/img/schematicView-24.png
#       cust42/img/schematicView-32.png
#       cust42/img/schematicView-48.png
#       cust42/img/schematicView-64.png
#       cust42/img/symbolView-16.png
#       cust42/img/symbolView-24.png
#       cust42/img/symbolView-32.png
#       cust42/img/symbolView-48.png
#       cust42/img/symbolView-64.png
#   @tag
#       gui
###############################################################################


namespace eval BrowseCdsLib {
    variable _W                 ""
    variable _FileName          ""
}


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc ::BrowseCdsLib::Init {
    argc
    argv
} {
    ##
    # Add menu entries.
    #
    gui menu command {"BrowseCdsLib" "Show Tab"} \
        [list ::BrowseCdsLib::_showWidget]
    gui menu customizeEntry {"BrowseCdsLib" "Show Tab"} \
        [list ::BrowseCdsLib::_customizeShowMenu]

    gui menu command {"BrowseCdsLib" "Close Tab"} \
        [list ::BrowseCdsLib::_closeWidget]
    gui menu customizeEntry {"BrowseCdsLib" "Close Tab"} \
        [list ::BrowseCdsLib::_customizeCloseMenu]
}


# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc ::BrowseCdsLib::Finit {} {
    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"BrowseCdsLib"}

    ##
    # Close tab.
    #
    _closeWidget
}


# -----------------------------------------------------------------------------
# _showWidget - Show the BrowseCdsLib tab.
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_showWidget {} {
    variable _W

    if {[gui window exists "BrowseCdsLib"]} {
        return
    }

    set _W [gui window insertCustomWidget \
        -destroyCallback ::BrowseCdsLib::_closeWidget \
        -pluginNamespace "BrowseCdsLib" \
        "BrowseCdsLib"]

    set top $_W.top
    ttk::frame $top
    grid $top            -row 0 -column 0 -sticky news -padx 5
    grid rowconfigure    $_W 0 -weight 1
    grid columnconfigure $_W 0 -weight 1

    ttk::label  $top.fileNameLabel \
        -text "Filename:"
    ttk::entry  $top.fileNameEntry \
        -textvariable ::BrowseCdsLib::_FileName
    bind $top.fileNameEntry <Key-Return>   [list ::BrowseCdsLib::_readFile]
    bind $top.fileNameEntry <Key-KP_Enter> [list ::BrowseCdsLib::_readFile]

    ttk::button $top.fileNameBrowse \
        -text "Browse" \
        -command ::BrowseCdsLib::_browse
    grid $top.fileNameLabel  -row 0 -column 0 -sticky nw  -padx 5 -pady 2
    grid $top.fileNameEntry  -row 0 -column 1 -sticky nw  -padx 5 -pady 2
    grid $top.fileNameBrowse -row 0 -column 2 -sticky nw  -padx 5 -pady 2
    grid rowconfigure    $top 1 -weight 1
    grid columnconfigure $top 2 -weight 1

    ttk::frame $top.frame
    grid $top.frame -row 1 -column 0 -sticky news -padx 5 -pady 2 \
                           -columnspan 3
    grid rowconfigure    $top.frame 0 -weight 1
    grid columnconfigure $top.frame 0 -weight 1

    tablelist::tablelist $top.frame.table \
        -expandcommand ::BrowseCdsLib::_populate \
        -stripebackground #f0f0f0 \
        -stretch all \
        -xscrollcommand [list $top.frame.x set] \
        -yscrollcommand [list $top.frame.y set] \
        -columns { \
            0 "Library" left \
            0 "Path"    left \
        }
    gui window autoscroll $top.frame $top.frame.table
    bind $top.frame.table <<TablelistSelect>> [list ::BrowseCdsLib::_select]

    bind [$top.frame.table bodytag] <Button-3> {
        ::BrowseCdsLib::_showPopupMenu %W %x %y %X %Y
    }

    set base [file join [file dirname [info script]] "img"]
    gui imageset load SchematicView [file join $base "schematicView-SIZE.png"]
    gui imageset load LayoutView    [file join $base "layoutView-SIZE.png"]
    gui imageset load SymbolView    [file join $base "symbolView-SIZE.png"]
    gui imageset load Library       [file join $base "library-SIZE.png"]
    gui imageset load Cell          [file join $base "cell-SIZE.png"]

    _readFile
}


# -----------------------------------------------------------------------------
# _parseFile -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_parseFile {fname} {
    if {$fname eq ""} {
        return
    }

    set fname [zos expandenv $fname]

    if {(!([file exists $fname] && [file readable $fname])) ||
        [file isdirectory $fname]} {
        zmessage print WAR "Could not open file '$fname'"
        return
    }

    set f [open $fname]
    set s [read $f]
    close $f

    set lines [split $s \n]
    foreach line $lines {
        set line [string trim $line]
        if {$line eq ""} {
            continue
        }
        if {[string match "#*" $line]} {
            continue
        }
        set line [regsub -all -- {\s+} $line { }]
        set sep1 [string first " " $line]
        if {$sep1 < 0} {
            continue
        }
        set keyword [string range $line 0 $sep1-1]
        switch -- $keyword {
            "INCLUDE" -
            "SOFTINCLUDE" {
                set fname [string range $line $sep1+1 end]
                _parseFile $fname
            }
            "DEFINE" {
                set sep2 [string first " " $line $sep1+1]
                if {$sep2 < 0} {
                    set name [string range $line $sep1+1 end]
                    set path ""
                } else {
                    set name [string range $line $sep1+1 $sep2-1]
                    set path [string range $line $sep2+1 end]
                }
                _insertLibrary $name [zos expandenv $path]
            }
            default {
                zmessage print DBG "unknown keyword '$keyword'"
            }
        }
    }
}


# -----------------------------------------------------------------------------
# _readFile -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_readFile {} {
    variable _W
    variable _FileName

    $_W.top.frame.table delete 0 end
    _parseFile $_FileName
}


# -----------------------------------------------------------------------------
# _insertLibrary -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_insertLibrary {name path} {
    variable _W

    set item [$_W.top.frame.table insertchildren root end [list $name $path]]
    $_W.top.frame.table collapse $item

    set image [::BrowseCdsLib::_getImage library]
    set cellindex [$_W.top.frame.table cellindex end,0]
    $_W.top.frame.table cellconfigure $cellindex -image $image
}


# -----------------------------------------------------------------------------
# _populate -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_populate {tree row} {
    if {[$tree childcount $row] != 0} {
        return
    }

    set name [$tree cellcget $row,0 -text]
    set path [$tree cellcget $row,1 -text]

    if {(![file isdirectory $path]) || (![file readable $path])} {
        zmessage print WAR "Cannot open path to Library '$name' ($path)"
        return
    }

    set i $row
    set cellList [glob -directory $path -nocomplain -tails -types d *]
    foreach cell [lsort -dictionary $cellList] {
        set cellItem  [$tree insertchildren $row end $cell]
        incr i
        set image     [::BrowseCdsLib::_getImage cell]
        set cellindex [$tree cellindex $i,0]
        $tree cellconfigure $cellindex -image $image

        set cellPath [file join $path $cell]
        set viewList [glob -directory $cellPath -nocomplain -tails -types d *]
        foreach view [lsort -dictionary $viewList] {
            $tree insertchildren $cellItem end $view
            incr i

            set cellindex [$tree cellindex $i,0]
            set image [::BrowseCdsLib::_getImage $view]
            $tree cellconfigure $cellindex -image $image
        }
        $tree collapse $cellItem
    }
}


# -----------------------------------------------------------------------------
# _showPopupMenu -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_showPopupMenu {w x y X Y} {
    foreach {tbl x y} [tablelist::convEventFields $w $x $y] {
    }
    set cellIndex [$tbl containingcell $x $y]

    set menu .browseCdsLibPopup
    destroy $menu
    menu $menu

    $menu add command \
        -label       "Clicked on cell: $cellIndex" \
        -command     [list ::BrowseCdsLib::_popupAction $tbl $cellIndex]

    tk_popup $menu $X $Y
}


# -----------------------------------------------------------------------------
# _popupAction -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_popupAction {tbl cellIndex} {
    ##
    # Implement action for clicked cell $cellIndex.
    #
}


# -----------------------------------------------------------------------------
# _getImage -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_getImage {view} {
    set size 16
    set name "UserImage:"
    switch -nocase -- $view {
        "schematic" -
        "layout"    -
        "symbol"    {
            append name [string totitle $view] View
        }
        "cell"    {
            append name "Cell"
        }
        "library"   -
        default     {
            append name "Library"
        }
    }
    append name "-" $size

    return $name
}


# -----------------------------------------------------------------------------
# _select -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_select {} {
    variable _W

    foreach selection [$_W.top.frame.table curselection] {
        set lib  [$_W.top.frame.table cellcget $selection,0 -text]
        set path [$_W.top.frame.table cellcget $selection,1 -text]
        zmessage print INF "Selected: $lib - $path"
    }
}


# -----------------------------------------------------------------------------
# _customizeShowMenu - Disable the 'Show Tab' menu entry if the tab
#                      already exists.
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_customizeShowMenu {menuId} {
    variable _W

    set state "disabled"
    if {$_W eq ""} {
        set state "normal"
    }
    $menuId entryconfigure end -state $state
}


# -----------------------------------------------------------------------------
# _customizeCloseMenu - Disable the 'Close Tab' menu entry if the tab is
#                       already closed.
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_customizeCloseMenu {menuId} {
    variable _W

    set state "disabled"
    if {$_W ne ""} {
        set state "normal"
    }
    $menuId entryconfigure end -state $state
}


# -----------------------------------------------------------------------------
# _closeWidget - Destroy the BrowseCdsLib tab.
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_closeWidget {} {
    variable _W

    if {$_W ne ""} {
        gui window removeCustomWidget "BrowseCdsLib"
        destroy $_W
        set _W ""
    }
}


# -----------------------------------------------------------------------------
# _browse -
# -----------------------------------------------------------------------------
#
proc ::BrowseCdsLib::_browse {} {
    variable _FileName

    set fname [gui window fileDialog openFile \
        "Cds File" {{"Lib file" {.lib}}}]
    if {$fname != {}} {
        set _FileName $fname
        _readFile
    }
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
::BrowseCdsLib::Init $argc $argv
::BrowseCdsLib::_showWidget