 
# Stuff to run the mesher.  This does not include any of the code that
# handles the mesh after it has been loaded, or the mesher form.
#
# Copyright (c) 1993, 1994 Pacific Marketing and Consulting
# Author:  Wayne A. Christopher, wayne@pmac.com
#          Pacific Marketing and Consulting, Berkeley, California
#

proc grid_set_type_iceboard {} {
    global grid_type grid_qual model_container type_name

    set grid_type iceboard

    grid_set_default_sizes
    return 0
}

proc edit_grid_iceboard {d} {
    form_checkbutton $d.usex "Max X size" grid_usesize_x {3 0}
    form_checkbutton $d.usey "Max Y size" grid_usesize_y {4 0}
    form_checkbutton $d.usez "Max Z size" grid_usesize_z {5 0}
    form_entry $d.x grid_size_x float {3 1} grid_usesize_x
    form_entry $d.y grid_size_y float {4 1} grid_usesize_y
    form_entry $d.z grid_size_z float {5 1} grid_usesize_z
}

proc grid_runonce_iceboard {gfile mfile efile pfile mapfile do_part suff inc_others} {
    global grid_current_prefix iceboard_meshing_path no_exit_job
    global mesher_was_terminated 
    
    set grid_current_prefix ""
    
    set board [db_get_name board]
    if {$board == ""} { set board [db_get_name BOARD_OUTLINE.1] }
    if {$board == ""} {
	mess "There is no object called \"board\" or \"BOARD_OUTLINE.1\"- you must have one in order to run the board mesher\n" red
	return 0
    }
    set bsh [$board getval body_shape]
    $bsh configure -mesh_as_board 1
    
    mess "Writing grid input to file \"$gfile\" ...\n"
    busy 1
    set r [grid_write_input $gfile $do_part $suff $inc_others 0]
    busy 0
    $bsh configure -mesh_as_board 0
    
    if !$r { return 0 }
    
    mess "Writing [directory]/tetin_auto ...\n"
    write_tetin_file [directory]/tetin_auto 0
    mess "Done.\n"
    
    grid_write_mapping $mapfile $suff
    
    cmd_rm $mfile
    if [file exists $mfile] {
	user_error "Can't remove the file $mfile before running mesher --\
               please check the permissions on the directory [directory mesh]"
	return
    }
    cmd_rm $efile
    
    mess "Running mesher...\n"
    set no_exit_job mesher
    set cmd [list $iceboard_meshing_path -tetin [directory]/tetin_auto -grid_input $gfile -cut_domain $mfile]
    
    set ignore_messages [list "*splitting * into * and*" \
			     "*degenerated surface * in loop*"]
    
    set retval [run_background $cmd "" 1 "" $ignore_messages]
    
    unset no_exit_job
    
    if [info exists mesher_was_terminated] { return 0 }
    
    if ![file exists $mfile] {
	mess "Couldn't run mesher -- please check to make sure you have\
		enough disk space and that the file \"$iceboard_meshing_path\"\
		exists.\n" red
	return 0
    }
    
    return $retval
}
