# # Copyright 2004 (c) Pointwise, Inc. # All rights reserved. # # This sample Gridgen script is not supported by Pointwise, Inc. # It is provided freely for demonstration purposes only. # SEE THE WARRANTY DISCLAIMER AT THE BOTTOM OF THIS FILE. # # Globals # Storage for the seg data file path set seg_file "" # Global storage for the genpts button widget # This needs to exist since it is referenced during # a validate command. As the validator runs outside of # the scope of the MakeWindow proc, the genpts variable needs # to be preserved across calls set btn_genpts "" # option variables from the gui set db_opt_ptsavg 0 # Utility functions ###################################################################### ## PROC: GetFile ## Open File Popup box ## Displays an open file dialog and then sets the global seg_file ## to the name and path of the file ###################################################################### proc GetFile {} { global seg_file fileentry # Create a set of file types for the default types # within the open file dialog. set types { { {Seg Files} {.dat .grd} } { {All Files} * } } # Render an open file dialog as a top level window. # Set the default extension to .dat set file [tk_getOpenFile -defaultextension "dat" -filetypes $types \ -title "Load Seg File" -parent .] if {$file == ""} { #-- cancel return } set seg_file $file #-- Since seg_file changed, we have to reset the entry validation if {[info exists $fileentry] && [winfo exists $fileentry]} { $fileentry config -validate key } return } ###################################################################### ## PROC: ErrorMsg ## Error popup message ###################################################################### proc ErrorMsg {msg {title "Error"}} { global dialog_win tk_messageBox -message "$msg" -type ok -icon error -title $title if { [info exists dialog_win] } { after 20 {RaiseWin $dialog_win} } } gg::tkLoad catch { set scriptDir [file dirname [info script]] source [file join $scriptDir pwiLogo.glf] } ###################################################################### ## PROC: makeWindow ## Generate the GUI window ###################################################################### proc makeWindow { } { global gui global seg_file global db_opt_ptsavg global btn_genpts fileentry # Define and render window title label .title -text "Import Points From Segment File" set font [.title cget -font] .title configure -font [font create \ -family [font actual $font -family] -weight bold] pack .title -expand 1 -side top # define window regions # define the Generate Points / Cancel frame here # It is ok to render the frame here, as we are binding # it to the bottom of the window. # This is done since the state of button # 'btn_genpts' depends upon a value within the file input region. # Hence, the button frame is predefined so that # the file input region can access the button. set button_frame [frame .buttons] pack $button_frame -fill x -padx 2 -pady 2 -side bottom pack [button $button_frame.cancel -text "Quit" -command { exit }] \ -side right -padx 2 set btn_genpts [button $button_frame.genpts -text "Generate Points" \ -command {segToPoints $seg_file} -state disabled] pack $btn_genpts \ -side right -padx 2 # file input region # setup window box set input_frame [frame .inputs] label $input_frame.fileprompt -text "Segment file:" -anchor e -width 13 pack $input_frame.fileprompt -side left -fill both entry $input_frame.fileentry -textvariable seg_file -width 64 \ -validate key \ -validatecommand { if { [file exists %P ] } { $btn_genpts configure -state active } else { $btn_genpts configure -state disabled } return 1 } # Render the entry box to the parent frame pack $input_frame.fileentry -side left -fill both -expand 1 set fileentry $input_frame.fileentry # Create the browse button and render it to the frame button $input_frame.browse -text "Browse" -command GetFile -width 6 pack $input_frame.browse -side left # render input frame with all of the contents from above # packed into it pack $input_frame -fill both -padx 2 -pady 2 # define option buttons, pack them into option_frame set option_frame [frame .options] # Keep label label $option_frame.keep -text "Keep:" -anchor e -width 13 pack $option_frame.keep -side left # Radio buttons for the Points / Average selection radiobutton $option_frame.pointonly -text "Points Only" \ -variable db_opt_ptsavg -value 0 pack $option_frame.pointonly -side left radiobutton $option_frame.pointandavg -text "Points and Average" \ -variable db_opt_ptsavg -value 1 pack $option_frame.pointandavg -side left radiobutton $option_frame.avgonly -text "Average Only" \ -variable db_opt_ptsavg -value 2 pack $option_frame.avgonly -side left # render the previously defined option frame to window pack $option_frame -fill both -padx 2 -pady 2 -expand 1 # render a horizontal rulebar to the main window pack [frame .hr1 -relief sunken -height 2 -bd 1] -side top -padx 1 \ -fill both -pady 1 # Create the Gridgen logo and put it into the parent window if {![catch {pwiLogoCreate .buttons.logo 1} b]} { $b configure -bd 0 -relief flat pack $b -side left -padx 5 -fill y } set f [frame .top] pack $f -side top -padx 2 -pady 1 -fill both -expand 1 } ###################################################################### ## PROC: segToPoints ## Get the points from a file of segments and inport into DB ###################################################################### proc segToPoints { filename } { global db_opt_ptsavg if { [ catch { set fd [open $filename "r"] } msg ] } { ErrorMsg "Failed to open file.\n$msg" return } #--------------------------------------------------------------------------# # Create DB Points for a single segment or multi-segment file. #--------------------------------------------------------------------------# # Get points out of file, and load them into a list # This loop checks for obvious errors, such as: # NumPts a non-integer # EOF while reading file # X,Y,Z values not of type double # If all the points load correctly from the file, # the list is then fed into to DB # within the foreach loop below. list points # Loop until the end of file. This should allow # the user to chain multiple segment files together in # one file as long as the expected format is maintained. while {[eof $fd]==0} { gets $fd numPts # Verify that the number of points is sane if { ![string is integer $numPts] } { ErrorMsg "Unexpected data: first element in file, '$numPts' is \ not an integer. Expected integer number of points" close $fd return } # Loop through the file reading in one line per point # and adding the point to a list. set i 0 while {$i < $numPts} { # First, make sure that we can grab the line from the file if { [ gets $fd line ] != -1 } { if { [llength $line] != 3 } { ErrorMsg "Point [ expr $i + 1 ] data does not contain an\ x,y, and z component. Check file." close $fd return } elseif { ![ string is double [ lindex $line 0 ] ] || \ ![ string is double [ lindex $line 1 ] ] || \ ![ string is double [ lindex $line 2 ] ] } { ErrorMsg "Point [ expr $i + 1 ] contains a non-double component. \ Check file" close $fd return } # The read-in point has passed all of the sanity checks. # Add the point to the point list. lappend points $line # Point has now been successfully added to the list # Insert any code needed for manipulating a single point here } else { # Point failed to load, gets failed # Display an error, and bail out of proc ErrorMsg "File failed to load properly. Failed at point \ [ expr $i + 1 ] out of $numPts" close $fd return } # Point processed, move to next point incr i } # end while $i < $numPts } # end while not eof # list of points was created OK, now enter the points in the db # File has been read close it now close $fd # Setup the DB to allow points to be added. # Use $db_opt_ptsavg to select average and # keep points as selected in the GUI. if { [ catch { if { $db_opt_ptsavg == 0 } { gg::dbPtsBegin } elseif { $db_opt_ptsavg == 1 } { gg::dbPtsBegin -keep 1 -average } elseif { $db_opt_ptsavg == 2 } { gg::dbPtsBegin -keep 0 -average } } msg ] } { # gg::dbPtsBegin failed ErrorMsg "gg::dbPtsBegin failed.\n$msg" return } # Iterate through list, and try to add the point to the DB # If the addition fails, give an error message and bail out foreach {currPt} $points { if { [ catch { gg::dbPtsAddPt $currPt } msg ] } { ErrorMsg "Failed while adding points into the database.\n$msg" gg::dbPtsEnd return } } # End the DB point creation # Close the DB point mode if { [ catch { set db_pts [gg::dbPtsEnd] } msg ] } { ErrorMsg "gg::dbPtsEnd failed.\n$msg" } # rename all of the points to match their filename # get the filename set pointNamePrefix [file rootname [file tail $filename]] # deck the points by one to handle the avgpt set numNewPts [llength $db_pts] if { $numNewPts == 0 } { # No points were generated. Probably this is since they are # all dupes. For now, do not handle this as an error. return } # Set the point names for each rendered point # to reference the base file name if { [ \ catch { set i 0 while { $i < [ expr $numNewPts - 1 ] } { gg::dbName [ lindex $db_pts $i ] \ "${pointNamePrefix}_[expr $i + 1]" incr i } if { $db_opt_ptsavg == 0 } { gg::dbName [ lindex $db_pts $i ] \ "${pointNamePrefix}_[expr $i + 1]" } else { gg::dbName [ lindex $db_pts $i ] \ "${pointNamePrefix}_avgpt" } } msg ] } \ { ErrorMsg "Setting the point names failed in the DB.\n\ $msg" } } # End proc segToFile makeWindow ::tk::PlaceWindow . widget wm protocol . WM_DELETE_WINDOW "destroy ." gg::tkLoop # # DISCLAIMER: # TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, POINTWISE DISCLAIMS # ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED # TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE, WITH REGARD TO THIS SCRIPT. TO THE MAXIMUM EXTENT PERMITTED # BY APPLICABLE LAW, IN NO EVENT SHALL POINTWISE BE LIABLE TO ANY PARTY # FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES # WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF # BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE # USE OF OR INABILITY TO USE THIS SCRIPT EVEN IF POINTWISE HAS BEEN # ADVISED OF THE POSSIBILITY OF SUCH DAMAGES AND REGARDLESS OF THE # FAULT OR NEGLIGENCE OF POINTWISE. #