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
###############################################################################
# Copyright (c) 2015-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.
# =============================================================================
#   @script
#       Transfer Design
#
#       description
#           Script to copy the design files into a zdb binfile and to copy the
#           design files out of the zdb to a new location.
#           Update the binfile to point to the new source file position.
###############################################################################


# -----------------------------------------------------------------------------
# _removeTransAttrs - cleanup database
# -----------------------------------------------------------------------------
#
proc _removeTransAttrs {db} {
    ##
    # remove all source files currently stored in db
    #
    $db attr -db foreach attr {
        set name [lindex [split $attr =] 0]
        if {![string match "TRANS*" $name]} {
            continue
        }
        $db attr -db delete $name
    }
}


# -----------------------------------------------------------------------------
# _copyFilesIntoDB - copy files into DB
# -----------------------------------------------------------------------------
#
proc _copyFilesIntoDB {db} {
    ##
    # remove all source files currently stored in db
    #
    _removeTransAttrs $db

    ##
    # store all source files in db
    #
    set fCnt 0
    $db spos foreachfile fname modtime {
        incr fCnt
    }
    set thisFile 1
    $db spos foreachfile fname modtime {
        zprogress update "File $thisFile" $thisFile $fCnt

        ##
        # open source file
        #
        set fp [open $fname r]
        fconfigure $fp -translation binary

        ##
        # get contents in chunks which fit in a db attribute
        #
        set maxSize 0x7FFF0000
        set thisChunk 0
        while {![eof $fp]} {
            ##
            # get chunk
            #
            set data [read $fp $maxSize]

            ##
            # create unique attribute name
            #
            set name [format "%s_%d_%d" TRANS $thisFile [incr thisChunk]]

            ##
            # store this chunk in db root
            #
            $db attr -db add $name=$data
        }

        ##
        # close source file
        #
        close $fp

        ##
        # Increment file counter
        #
        incr thisFile
    }
}


# =============================================================================
# CopyFilesIntoDB - show usage of _copyFilesIntoDB procedure
# =============================================================================
#
proc CopyFilesIntoDB {} {
    ##
    # If the given file name is empty then show an open file dialog to browse
    # for an binfile.
    #
    set ftype {{"Binfile" ".zdb"}}
    set binFname [gui window fileDialog openFile "Binfile" $ftype]
    if {$binFname == ""} {
        return
    }

    ##
    # get binfile
    #
    set db [zdb open $binFname]

    ##
    # copy source files into db
    #
    zprogress begin
    zprogress push "Copy Files" 0.9
    _copyFilesIntoDB $db
    zprogress pop

    ##
    # save db (including source files) to binfile
    #
    zprogress push "Save Database" 1.0
    $db save $binFname
    zprogress pop
    zprogress end

    ##
    # close db
    #
    $db close
}


# -----------------------------------------------------------------------------
# _copyFilesOutOfDB - restore source files from db
# -----------------------------------------------------------------------------
#
proc _copyFilesOutOfDB {db destDir} {
    ##
    # restore all source files in db
    #
    set thisFile 1
    $db spos foreachfile fname modtime {
        ##
        # get absolute path
        #
        set absFname [zos convertFilename $fname absolute]

        ##
        # remove first "/"
        #
        set relPath [eval file join [lrange [file split $absFname] 1 end]]

        ##
        # prepend destination dir
        #
        set destFname [file join $destDir $relPath]
        set destFname [zos convertFilename $destFname absolute]

        ##
        # create needed directory
        #
        file mkdir [file dirname $destFname]

        ##
        # open source file
        #
        set fp [open $destFname w]
        fconfigure $fp -translation binary

        set thisChunk 0
        while {1} {
            ##
            # create unique attribute name
            #
            set name [format "%s_%d_%d" TRANS $thisFile [incr thisChunk]]

            ##
            # get this chunk from db root
            #
            set data [$db attr -db getValue $name]

            ##
            # check for end of contents
            #
            if {$data eq ""} {
                break
            }

            ##
            # write contents to file
            #
            puts -nonewline $fp $data
        }

        ##
        # close source file
        #
        close $fp

        ##
        # update db spos to point to newly created file
        #
        $db spos setfname $fname $destFname

        ##
        # update timestamp of newly created file
        #
        file mtime $destFname $modtime

        ##
        # Increment file counter
        #
        incr thisFile
    }
}


# =============================================================================
# CopyFilesOutOfDB - copy design files out of DB
# =============================================================================
#
proc CopyFilesOutOfDB {} {
    ##
    # Show an open file dialog to browse for a binfile.
    #
    set ftype {{"Binfile" ".zdb"}}
    set binFname [gui window fileDialog openFile "Binfile" $ftype]
    if {$binFname == ""} {
        return
    }

    ##
    # Show an open dir dialog to browse for a directory.
    #
    set destDir [gui window fileDialog chooseDir "Destination Directory" {}]
    if {$destDir == ""} {
        return
    }

    ##
    # get binfile
    #
    set db [zdb open $binFname]

    ##
    # copy source files into db
    #
    _copyFilesOutOfDB $db $destDir

    ##
    # remove all source files currently stored in binfile
    #
    _removeTransAttrs $db

    ##
    # save db (including updated file names) to binfile
    #
    $db save $binFname

    ##
    # close db
    #
    $db close
}


# =============================================================================
# SaveBinfile - Save a binfile including the design files.
# =============================================================================
#
proc SaveBinfile {} {
    ##
    # Get the current database.
    #
    set db [gui database get]
    if {$db eq ""} {
        return
    }

    ##
    # Show a save file dialog to browse for a binfile.
    #
    set ftype {{"Binfile" ".zdb"}}
    set binFname [gui window fileDialog saveFile "Binfile" $ftype]
    if {$binFname eq ""} {
        return
    }

    ##
    # copy source files into db
    #
    zprogress begin
    zprogress push "Copy Files" 0.9
    _copyFilesIntoDB $db
    zprogress pop

    ##
    # save db (including source files) to binfile
    #
    zprogress push "Save Database" 1.0
    $db save $binFname
    zprogress pop
    zprogress end
}


# =============================================================================
# Add a new main menu entry 'Transfer' to access the procedure of this script.
# =============================================================================
#
gui menu command {"Transfer" "Copy Design Files into Binfile"} \
        {CopyFilesIntoDB}
gui menu command {"Transfer" "Copy Design Files out of Binfile"} \
        {CopyFilesOutOfDB}
gui menu command {"Transfer" "Save Binfile with Design Files"} \
        {SaveBinfile}