
# This code creates the user interface for tetra.
#
# Copyright (c) 1993 - 1995 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

set tool_name "ICEM TETRA"
set whoami "$tool_name $version"

set tool_id tetra

set tool_dir $env(ICEM_ACN)/lib/tetra
if [info exists env(TETRA_DIR)] { set tool_dir $env(TETRA_DIR) }

set tetra_bin $env(ICEM_ACN)/icemcfd/tetra
if [info exists env(TETRA_BIN)] { set tetra_bin $env(TETRA_BIN) }

set cutter_bin $env(ICEM_ACN)/icemcfd/cutter
if [info exists env(CUTTER_BIN)] { set cutter_bin $env(CUTTER_BIN) }

set tetra_leo_bin $env(ICEM_ACN)/bin/leo3
if [info exists env(TETRA_LEO_BIN)] { set tetra_leo_bin $env(TETRA_LEO_BIN) }

set coarse_bin $env(ICEM_ACN)/icemcfd/coarse
if [info exists env(COARSE_BIN)] { set coarse_bin $env(COARSE_BIN) }

set offset_bin $env(ICEM_ACN)/icemcfd/toffset
if [info exists env(OFFSET_BIN)] { set offset_bin $env(OFFSET_BIN) }

set improver_bin $env(ICEM_ACN)/icemcfd/smooth
if [info exists env(IMPROVER_BIN)] { set improver_bin $env(IMPROVER_BIN) }

set tri_improver_bin $env(ICEM_ACN)/icemcfd/tsmooth
if [info exists env(TRI_IMPROVER_BIN)] { set tri_improver_bin $env(TRI_IMPROVER_BIN) }

set help_base tetra/tetra

set cart_file ""
set tetin_file tetin1
set cut_domain_in_import 0
set single_buffer [info exists env(TETRA_SINGLE_BUFFER)]
set no_modify 1
# [info exists env(TETRA_NO_MODIFY_MENU)]

set n_processors 1
set max_binary 0

set tool_options {
    { -single single_buffer }
    { -tetin  tetin_file 1 }
    { -nproc n_processors 1 }
    { -max_binary max_binary 0 }
}

set tool_files {
    disp_tetra
    mesher_tetra
    modify_tetra
    improver
    coarsener
    offset
    leo_edit
    tri_edit
}

set guibase_files_extra {
    bocofile
}

set tool_prompt tetra%

proc tool_command {} {
    global display_axes
    set display_axes 0
    return ""
}

proc tool_setup {} {
    global config_dir tool_arguments tetin_file leo_tetra_dir is_tetra is_win
    
    if {[llength $tool_arguments] < 1} {
	error "Usage: tetra config_dir"
    }
    set temp [lindex $tool_arguments 0]
    if { [ file exists $temp/mesh ] || [ file exists $temp/domains ] ||
		[ file exists $temp/import ] } {
	set config_dir(mesh) $temp/mesh
	set config_dir(domains) $temp/domains
	set config_dir(import) $temp/import
    } else {
	set config_dir(mesh) $temp
	set config_dir(domains) $temp
	set config_dir(import) $temp
    }
	

    if {[lindex $tool_arguments 1] != ""} {
	set tetin_file [lindex $tool_arguments 1]
    }
    
    if {[info commands struct_mesh] != ""} {
	set is_tetra 1
	uplevel \#0 source $leo_tetra_dir/leo_internal.tcl
    } else {
	if $is_win {
	    bind all <Key> {check_binding %W %X %Y %x %y %K}
	}
    }
    if { $tetin_file != "" } {
        set tetin_file [ file tail $tetin_file ]

	if {[scan $tetin_file "tetin%d" number] == 1} {
        	set uncut_domain uncut_domain.$number
	} else {
		set uncut_domain ""
	}
        if { $uncut_domain == "" } {
	    mesher_tetra
	} else {
            set tf $config_dir(mesh)/$tetin_file
            set uf $config_dir(domains)/$uncut_domain
            if {[file exists $tf] && [file exists $uf] && \
                ([file mtime $uf] > [file mtime $tf])} {
    	        mesher_cutter 1
	    } else {
		mesher_tetra
	    }
	}
    }
}

proc check_binding {win x y wx wy c} {
    set viewx1 [winfo rootx .v.view]
    set viewy1 [winfo rooty .v.view]
    set viewx2 [expr $viewx1 + [winfo width .v.view]]
    set viewy2 [expr $viewy1 + [winfo height .v.view]]
    set cc [winfo containing $wx $wy]
    if {($wx < $viewx1) || ($wx > $viewx2) || \
	($wy < $viewy1) || ($wy > $viewy2) || ($cc != "")} return

    if {$win == ".v"} {
        if {$c == "h"} {
	    dl_home
	} elseif {$c == "F9"} {
	    send_event k O $wx $wy 0
	}
    }
}

proc make_tool_windows {} {
    make_display_window
}

#set tool_logo "$tool_dir/tetra-logo.xbm black"
#set tool_logo $tool_dir/tetra-logo.ppm
set tool_logotext "ICEM CFD 10.0\nTetra Mesher"
set tool_logo_relief flat
set tool_logo "$env(ICEM_ACN)/lib/med_gui/med-logo-bits.xbm \#0000ff \
        $env(ICEM_ACN)/lib/med_gui/med-logo-mask.xbm \#547eff"
set tool_logo_small "$env(ICEM_ACN)/lib/med_gui/med-smlogo-bits.xbm black \
        $env(ICEM_ACN)/lib/med_gui/med-smlogo-mask.xbm blue"


proc file_dirty {} { global mesh_dirty; set mesh_dirty 1 }

proc orient_home_command {} {
    global current_phase
    
    if {$current_phase == "mesher"} {
	command "orient +z" 0
    } else {
	command "view_home"
    }
}

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

if $no_modify {
    set tool_phases {{Mesher mesher}}
} elseif [file exists /etc/passwd] {
    set tool_phases {
	{Mesher mesher}
	{}
	{Modify modify}
	{Triangles tri_edit}
	{Quads quad_edit}
    }
} elseif {[info commands struct_mesh] != ""} {
    set tool_phases {
	{}
	{Modify modify}
	{Triangles tri_edit}
	{Quads quad_edit}
    }
} else {
    set tool_phases {{Mesher mesher}}
}
# {unput      button "Unput"            0,4 file_unput}

set tool_phase_info(mesher) {
    {tetra      button "Run tetra"        0,1 mesher_tetra}
    {cutter     button "Run cutter"       0,2 "mesher_cutter 1"}
    {monitor    button "Progress monitor" 0,3 mesher_monitor}
    {auto_orfn  button "Create orphans"   0,4 "mesher_auto_orphan"}
    {quit       button "Quit"             0,5 file_quit}
    {fix_spikes button "Fix spikes"	  0,6 mesher_fix_spikes}
    
    {degenerate button "Degen boundary"   1,1 mesher_degenerate}
    {closehole  button "Close holes"      1,2 mesher_close_holes}
    {floodfill  button "Flood fill"       1,3 mesher_flood_fill}
    {save_vol   button "Save volume"      1,4 "mesher_save volume"}
    {save_surf  button "Save surface"     1,5 "mesher_save surface"}
}

set tool_phase_info(modify) {
    {load_mesh     button "Load mesh"       0,1 modify_load_mesh}
    {save_mesh     button "Save mesh"       0,2 modify_save_mesh}
    {quit          button "Quit"            0,3 file_quit}
    {smooth_tetra  button "Tetra smoother"  1,1 "modify_smooth 1"}
    {smooth_tri    button "Tri smoother"    1,2 "modify_smooth 0"}
    {coarsen       button "Tri coarsener"   1,3 modify_coarsen}
}

#    {offset        button "Offset surface"  1,4 modify_offset}
    
set tool_phase_info(tri_edit) {
    {selection     button "Edit selection"  0,1 te_selection}
    {move          button "Move nodes"      0,2 te_movenodes}
    {merge         button "Merge nodes"     0,3 te_merge_nodes}
    {delete_nodes  button "Delete nodes"    0,4 te_delete_nodes}
    {diagnostics   button "Diagnostics"     0,5 te_diagnostics}
    
    {create_elems  button "Create elements" 1,1 te_create_screen}
    {delete_elems  button "Delete elements" 1,2 te_del_screen}
    {split_quads   button "Split elements"  1,3 te_split}
    {reorient      button "Reorient"        1,4 te_reorient}
    {join          button "Join edges"      1,5 te_join}

    {change_fam    button "Change family"   2,1 te_change_family}
    {smooth        button "Smooth elements" 2,2 te_smooth_disp}
    {swap          button "Swap edges"      2,3 te_swap_edges}
    {project       button "Project nodes"   2,4 te_project_nodes}
    
    {addbar        button "Add bars"        0,6 te_add_bars}
    {delbar        button "Delete bars"     1,6 te_del_bars}    
}

set tool_phase_info(quad_edit) {
    {selection     button "Edit selection"  0,1 te_selection}
    {move          button "Move nodes"      0,2 te_movenodes}
    {delete_nodes  button "Delete nodes"    0,3 te_delete_nodes}
    {reorient      button "Reorient"        0,4 te_reorient}
    {create_elems  button "Create elements" 1,1 te_create_screen}
    {delete_elems  button "Delete elements" 1,2 te_del_screen}
    {change_fam    button "Change family"   1,3 te_change_family}
    {project       button "Project nodes"   1,4 te_project_nodes}
    
    {addbar        button "Add bars"        0,6 te_add_bars}
    {delbar        button "Delete bars"     1,6 te_del_bars}    
}

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

set tool_phase_command(mesher) set_phase_mesher
set tool_phase_command(modify) set_phase_modify
set tool_phase_command(tri_edit) set_phase_tri_edit
set tool_phase_command(quad_edit) set_phase_tri_edit

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

set tool_extra(2) {button Print file_hardcopy}
set tool_extra(6) {button Shell file_shell}	

set orient_buttons {
	usual
}
if !$no_modify {
    set tool_extra(3) {button Undo undo_action}
    set tool_extra(4) {button Redo redo_action}
    
    if !$is_win {
	lappend orient_buttons { command -label {By element} -command parallel_to_elem }
    }
    
    set view_buttons {
	{ checkbutton -label {Pos marker} -variable marker_on \
	    -command te_marker_update }
	{ checkbutton -label {Solid} -variable te_shading \
	      -command te_update_shading }
	{ checkbutton -label {Show icons} -variable show_icons \
	      -command showstuff_update }
	{ checkbutton -label {Show nodes} -variable show_nodes \
	      -command showstuff_update }
	{ checkbutton -label {Node numbers} -variable show_nnums \
	      -command showstuff_update }
	{ checkbutton -label {Elem numbers} -variable show_enums \
	      -command showstuff_update }
	{ command -label {Set shrink} -command set_shrink }
    }
    if !$is_win {
lappend view_buttons { command -label Hardcopy -command file_hardcopy }
    }
}

set show_icons 0
set show_nodes 0
set show_nnums 0
set show_enums 0
set shrink_pct 0

proc showstuff_update {} {
    global show_icons show_nodes show_nnums show_enums current_phase
    if {($current_phase == "mesher") || ($current_phase == "modify")} {
	mess "Please switch to the triangles menu to use this function\n"
	return
    }
    command "lte_set_visible $show_icons $show_nodes $show_nnums $show_enums"
}

proc set_shrink {} {
    global shrink_pct current_phase
    if {($current_phase == "mesher") || ($current_phase == "modify")} {
	mess "Please switch to the triangles menu to use this function\n"
	return
    }
    
    set pct [question_box .shrink "Shrink pct" " Enter the shrink pct as\n\
	    a number between 0 and 100\n\
	    (0 = none, 100 = zero size)." $shrink_pct]
    if {$pct != ""} {
	set shrink_pct $pct
	command "lte_set_shrink $pct"
    }
}

proc te_marker_update {} {
    global marker_on marker_name last_line current_phase
    if {$current_phase == "mesher"} {
	mess "Please switch to the triangles menu to use this function\n"
	return
    }
    
    if $marker_on {
	set pos [question_box .posn "Marker pos" " Enter the X Y Z position\n\
of the marker, separated\n\
by spaces, or a node number.\n" ""]
	if {$pos == ""} { set marker_on 0; return }
	if {[llength $pos] == 1} {
	    command "lte_node_location $pos"
	    set pos $last_line
	}
	command "add_point_image $pos"
    } else {
	command "delete_point_image"
    }
}


# Initialize some parameters.

set remote_host ""
set remote_acn $env(ICEM_ACN)
set after_run_cutter 1
set after_run_smoother 0
set after_cutter_which inter
set after_smoother_iters 20
set after_smoother_aspect 0.5
set spt_tradeoff space

# Should go somewhere else -----------------------

set i 0
foreach c {
    #ff4040
    #40ff40
    #4040ff
    #40ffff
    #ff40ff
    #ffff40
    #bb22ff
    #2244ff
    #22ffbb
    #44ff22
    #ffbb22
    #8844ff
    #4488ff
    #44ff88
    #88ff44
    #ff8844
    #4422ff
    #22bbff
    #22ff44
    #bbff22
    #ff4422
} {
    set default_misc_colors($i) $c
    incr i
}

set num_default_misc_colors $i
