
# 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_hexa {} {
    global grid_type grid_qual
    
    set grid_type hexa
    set grid_qual det_aspect
    return 0
}

proc edit_grid_hexa {d} {
    global unit_default pipe_mesh_params
    
    form_label $d.unl "Units for meshing values" {0 0}
    futil_unitmenu $d.un "unit_get_choices length wavlen" {0 1} meshing_units \
	"" "set_planecut_units 0; list"
        
    form_checkbutton $d.on "Meshing parameters:" pipe_mesh_params(on) {1 0}    
    set act {{pipe_mesh_params(on) 1}}
    
    form_label $d.mll "    Max length" {2 0} $act
    form_label $d.ihl "    Init height" {3 0} $act
    form_label $d.hrl "    Height ratio" {4 0} $act
    form_label $d.ogl "    O-grid height" {5 0} $act
    form_entry $d.ml pipe_mesh_params(max_length) float {2 1}  $act
    form_entry $d.ih pipe_mesh_params(init_height) float {3 1}  $act
    form_entry $d.hr pipe_mesh_params(height_ratio) float {4 1}  $act
    form_entry $d.og pipe_mesh_params(ogrid_height) float {5 1} $act
    
    form_checkbutton $d.hinter "Run hexa interactively" \
	    grid_hexa_interactive {6 0}
}

proc grid_runonce_hexa {gfile mfile efile pfile mapfile do_part suff inc_others} {
    global advanced_mesher_ok grid_current_prefix hexa_path no_run_mesher
    global grid_hexa_interactive grid_mapping file_name env
    
    if !$advanced_mesher_ok {
	mess "Warning: you do not have a license for the Hexa mesher. \
		Please contact technical support.\n" red
    }
    
    set grid_current_prefix ""
    
    if ![file executable $hexa_path] {
	mess "Error: the hexa binary could not be found!\n" red
	mess "Path = $hexa_path\n"
	return 0
    }
    
    mess "Creating pipe information ...\n"
    pipe_model_create $mfile
    set tetfile [directory tetin]/tetin_pipe
    set blockfile [directory tetin]/blocking_pipe
    set replayfile [directory tetin]/replay_pipe
    mess "Done.\n"
    set grid_mapping() ""
    grid_write_mapping $mapfile ""
    
    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
    }
    
    if [info exists no_run_mesher] {
	mess "Not running mesher\n" red
	return 0
    }
    
    mess "Running mesher...\n"
    set no_exit_job mesher
    
    cmd_rm $mfile
    if $grid_hexa_interactive {
	set file_name(project_dir) [directory]
	catch {unset env(ICEM_ONE_WINDOW)}
	catch {unset env(ICEM_USE_MENUBAR)}
	run_application_guibase icemcfd/hex_gui icemcfd/hexa HEXA_BIN \
	    lib/hexa/hexa.tcl 0 [list -block __no_blocking__ -tetin $tetfile -replay $replayfile -domain_dir [directory] -mesh_dir [directory]]
    } else {
	set cmd1 [list $hexa_path $blockfile -tetin $tetfile -i $replayfile]
	
	run_background $cmd1 "" 1
	if ![file exists $mfile] {
	    unset no_exit_job
	    return 0
	}
    }
    cmd_rm [glob_fixed logfile.*]
    
    tetin_free_cutouts_for_solid
    
    unset no_exit_job
    return 1
}

# ------------------------------------------------------------------

proc grid_any_hexa_params_used {sh} {
    foreach i {height} {
	foreach v [$sh get -grid_${i}] {
	    if $v { return USED }
	}
    }
    return ""
}

proc grid_set_all_hexa_enabled {on} {
    global edit_params_active
    foreach i [array names edit_params_active] {
	set edit_params_active($i) $on
    }
}

proc grid_edit_hexa_perobject {} {
    global edit_params_active edit_params_used
    global object_type_list object_type_title form_done_flag botform_geo
    global grid_perobject_buttons
    
    set buts [list done cancel \
		  [list "Enable all" "grid_set_all_enabled 1"] \
		  [list "Disable all" "grid_set_all_enabled 0"]]
    
    catch {unset edit_params_active}
    catch {unset edit_params_used}
    catch {unset grid_perobject_buttons}
    
    form_kill .grid_hexa_perobject
    set d [form_init .grid_hexa_perobject "Per-object hexa sizes" \
	       "mesh sub" $buts]
    if {$d == ""} return
    
    set fr [form_scroller $d.scr 400 {0 0}]
    
    set row 1
    foreach type $object_type_list {
	switch $type { material - part continue }
	
	set objects ""
	foreach obj [db_list_objects $type] {
	    if [$obj si_value active] { lappend objects $obj }
	}
	if {$objects == ""} continue
	
	form_label $fr.l$row "$object_type_title($type):" \
	    "$row 0 -cols -"
	incr row
	
	foreach obj $objects {
	    if {$type == "domain"} {
		set obname ""
	    } else {
		set obname [$obj getval name]
	    }
	    set sects [$obj getval section_list]
	    switch [llength $sects] {
		0 continue
		1 {
		    set sh [$obj getval ${sects}_shape]
		    if {[$sh get -shtype] == "none"} continue
		    set edit_params_active($sh) [$sh get -grid_enabled]
		    set edit_params_used($sh) [grid_any_params_used $sh]
		    form_checkbutton $fr.a$row $obname \
			edit_params_active($sh) "$row 3"
		    set grid_perobject_buttons($sh) $fr.a$row
		    form_button $fr.b$row Edit "grid_hexa_perobject_one $obj $sh" \
			"$row 4" edit_params_active($sh)
		    form_label $fr.u$row "" "$row 5" edit_params_active($sh)
		    $fr.u$row configure -textvariable edit_params_used($sh)
		    incr row
		}
		default {
		    form_label $fr.l$row $obname "$row 1 -cols 2"
		    incr row
		    foreach ss $sects {
			set sh [$obj getval ${ss}_shape]
			if {[$sh get -shtype] == "none"} continue
			set edit_params_active($sh) [$sh get -grid_enabled]
			set edit_params_used($sh) [grid_any_params_used $sh]
			form_checkbutton $fr.a$row $ss \
			    edit_params_active($sh) "$row 3"
			set grid_perobject_buttons($sh) $fr.a$row
			form_button $fr.b$row Edit "grid_hexa_perobject_one $obj $sh" \
			    "$row 4" edit_params_active($sh)
			form_label $fr.u$row "" "$row 5" \
			    edit_params_active($sh)
			$fr.u$row configure -textvariable edit_params_used($sh)
			incr row
		    }
		}
	    }
	}
    }
    form_colwidth $fr 0 2 M
    form_colwidth $fr 1 2 M
    
    grid_hexa_sizes_out
    
    form_finish bot
    $d.scr configure -height [expr {[winfo reqheight $d.scr] + 1}]
    
    if ![form_wait .grid_hexa_perobject] return
    
    grid_hexa_sizes_in
    
    foreach sh [array names edit_params_active] {
	$sh configure -grid_enabled $edit_params_active($sh)
    }
}

proc grid_hexa_perobject_type {sh} {
    global domain edit_params_for use_grid_grading
    
    set obj [$sh get -object]
    
    set tt [lindex [split [$sh get -type] _] 1]
    set tp $tt
    switch $tt {
	quad    { append tt _[$sh getval plane] }
	incline {
	    append tt _[$sh getval axis]
	    if {[$sh si_value thickness] != 0} {
		append tt _thick
	    }
	}
	polygon {
	    set n [$sh getval nverts]
	    append tt _$n
	    if ![info exists edit_params_for(polygon_$n)] {
		set ll ""; set hh ""; set rr ""
		for {set i 0} {$i < $n} {incr i}  {
		    set i2 [expr {$i + 2}]
		    lappend ll [list "Side [expr $i + 1] count" length $i]
		    lappend hh [list "Side [expr $i + 1] height" height $i2]
		    lappend rr [list "Side [expr $i + 1] ratio" ratio $i2]
		}
		set edit_params_for(polygon_$n) \
		    [concat $ll [list [list "Height count" length $n]] {{}} \
			 [list [list "Bottom height" height 0] \
			      [list "Top height" height 1]] $hh {{}} \
			 [list [list "Bottom ratio" ratio 0] \
			      [list "Top ratio" ratio 1]] $rr]
		
		if $use_grid_grading {
		    lappend edit_params_for(polygon_$n) {} \
			[list "Grading height" height [expr $n + 2]] \
			[list "Grading ratio"  ratio  [expr $n + 2]]
		}
	    }
	}
    }
    if {$obj == $domain} { append tt _domain }
    if {[$obj getval obtype] == "wall"} {
	append tt _oneside
    }
    return $tt
}

proc grid_hexa_perobject_one {obj sh} {
    global edit_params_used edit_params_for form_done_flag botform_geo
    global edit_param_val edit_param_actual edit_param_on domain
    global grid_copyto_mode use_grid_grading
    
    set tt [grid_hexa_perobject_type $sh]
    
    set tw .grid_hexa_params_$tt
    
    set buts [list done cancel [list "Copy to" "grid_perobject_copyto $tt"]]
    set d [form_init $tw "" "grid sub sub" $buts]
    top_title $tw [$obj getval name]
    
    if {$d != ""} {
	form_label $d.top1 Requested {0 1}
	# form_label $d.top2 Actual {0 2}
	set sc [form_scroller $d.fr 500 {1 0 -cols -}]
    }
    set r 1
    foreach p $edit_params_for($tt) {
	if {$p == ""} {
	    incr r
	    continue
	}
	if {[lindex $p 1] == "height"} {
	    if {$d != ""} {
		form_checkbutton $sc.l$r [lindex $p 0] edit_param_on($tt,$r) "$r 0"
		form_entry $sc.e$r edit_param_val($tt,$r) float "$r 1" \
		    edit_param_on($tt,$r)
		#form_entry $d.a$r edit_param_actual($tt,$r) float "$r 2"
		#$d.a$r configure -state disabled
	    }
	}
	set n [lindex $p 2]
	set rval [lindex [$sh get -grid_[lindex $p 1]] $n]
	set aval [lindex [$sh get -grid_[lindex $p 1]_used] $n]
	if {($rval == "") || ($rval == 0)} {
	    set edit_param_on($tt,$r) 0
	    switch [lindex $p 1] {
		height { set edit_param_val($tt,$r) 0 }
		length { set edit_param_val($tt,$r) 0 }
		ratio  { set edit_param_val($tt,$r) 2 }
	    }
	} else {
	    set edit_param_on($tt,$r) 1
	    set edit_param_val($tt,$r) $rval
	}
	set edit_param_actual($tt,$r) $aval
	
	incr r
    }
    if {$d != ""} {
	form_finish bot
	$d.fr configure -height [expr {[winfo reqheight $d.fr] + 1}]
    }
    
    tkwait variable form_done_flag($tw)
    if !$form_done_flag($tw) return
    
    grid_perobject_toshape $sh $tt 0
    if [info exists grid_copyto_mode] {
	grid_perobject_copyto $tt
    }
}

proc grid_hexa_params {sh} {
    global edit_params_for
    
    set tt [grid_perobject_type $sh]
    set stuff ""
    foreach p $edit_params_for($tt) {
	if {([lindex $p 1] != "height") || ([lindex $p 3] != "")} continue
	lappend stuff [list [lindex $p 0] [lindex $p 2]]
    }
    return $stuff
}

proc grid_hexa_sizes_out {} {
    global edit_params_tetsize
    
    foreach shi [array names edit_params_tetsize] {
	set val [lindex [[lindex $shi 0] get -grid_height] [lindex $shi 1]]
	if {($val == "") && ($val == 0)} {
	    set edit_params_tetsize($shi) ""
	} else {
	    set edit_params_tetsize($shi) $val
	}
    }
}

proc grid_hexa_sizes_in {} {
    global edit_params_tetsize
    
    foreach shi [array names edit_params_tetsize] {
	set sh [lindex $shi 0]; set n [lindex $shi 1]
	set nv $edit_params_tetsize($shi)
	if {$nv == ""} { set nv 0 }
	set val [$sh get -grid_height]
	set vl [llength $val]
	while {$vl <= $n} { lappend val 0; incr vl }
	set val [lreplace $val $n $n $nv]
	$sh configure -grid_height $val
    }
}
