


# This code creates the user interface for the hexa mesher.
#
# 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 HEXA"
set whoami "$tool_name $version"
set no_blt 1

set tool_id hexa
set tool_id2 unput

if [info exists env(IBLOCK_DIR)] {
    set tool_dir $env(IBLOCK_DIR)
} elseif [info exists env(HEXA_DIR)] {
    set tool_dir $env(HEXA_DIR)
} else {
    set tool_dir $env(ICEM_ACN)/lib/hexa
}

if [info exists env(UNPUT_DIR)] {
    set tool_dir2 $env(UNPUT_DIR)
} else {
    set tool_dir2 $env(ICEM_ACN)/lib/unput
}

if [info exists env(HEXA_BIN)] {
    set tool_bin $env(HEXA_BIN)
} else {
    set tool_bin $env(ICEM_ACN)/icemcfd/hexa
}

if [info exists env(UNPUT_BIN)] {
    set tool_bin2 $env(UNPUT_BIN)
} else {
    set tool_bin2 $env(ICEM_ACN)/icemcfd/unput.3.1.2.5
}

set help_base hexa/hexa.html

global helpset_global
if [info exist env(ICEM_AI_ENVIRONMENT)] {
  set helpset_global "hexa"
}

array set mouse_buttons { rot 1 trans 2 scale 3 scale_dir 1 }

set tetin_file ""
set boco_file ""
set domain_dir ""
set domain_name ""
set mesh_dir ""
set blocking_file ""
set tagging_file ""
set initial_replay_file ""
set replay_handle_undo 0
set replay_always_update 1
set use_file_suffixes 0

set tool_options {
    { -tetin             tetin_file    1 }
    { -boco              boco_file     1 }
    { -domain_dir        domain_dir    1 }
    { -domain_name       domain_name   1 }
    { -mesh_dir          mesh_dir      1 }
    { -block             blocking_file 1 }
    { -tagging           tagging_file  1 }
    { -replay            initial_replay_file 1 }
    { -use_file_suffixes use_file_suffixes }
}

set tool_files {
    disp_hexa
    file_hexa
    block_hexa
    edge_hexa
    proj_hexa
    mesh_hexa
    meshc_hexa
    undo_hexa
    select_hexa
    replay_hexa
    topo_hexa
    glsm_hexa
}

set tool_files2 {
}

set guibase_files_extra {
    bocofile
    coloredit
    boco_solver
    replay
    family_boco
}

set tool_prompt hex%
set tool_prompt2 unput%

proc tool_command {} {
    global tool_bin blocking_file tool_arguments config_dir
    global tetin_file boco_file domain_dir mesh_dir black_and_white
    global display_axes domain_name use_file_suffixes
    
    if {[llength $tool_arguments] != 1} {
	tk_dialog .err "Error" "Usage: hexa config-dir" "" 0 Ok
    }
    set config_dir [lindex $tool_arguments 0]
    if {$mesh_dir == ""}   { set mesh_dir   $config_dir/mesh }

    if {$blocking_file == "__no_blocking__"} {
	set blocking_file ""
    } elseif {$blocking_file == ""} {
	file_load 1
    }
    set display_axes 0

    
    if {$tetin_file == ""} {
	set files ""
	foreach f [glob -nocomplain $mesh_dir/tetin*] {
	    lappend files [file tail $f]
	}
	foreach f [glob -nocomplain $mesh_dir/*.tin] {
	    lappend files [file tail $f]
	}
	if {$files == ""} { 
		tk_dialog .err "Error" "No tetin files found!"  "" 0 Ok
	}
	
	set mess "Please select a tetin file."
	set tetin_file [choose_one $mess $files]
	if {$tetin_file == ""} { exit 0 }
    }
	
    set tetin_file [ file tail $tetin_file ]
    if {$blocking_file == "" } {
# if there is no blocking file make sure the cart file is in order
	if {  ![file exists $mesh_dir/cart_file]} {
		set fp [open $mesh_dir/cart_file w]
		puts $fp "set_grid_type cartesian"
		puts $fp "define_grid i 2"
		puts $fp "0.0"
		puts $fp "1.0"
		puts $fp "define_grid j 2"
		puts $fp "0.0"
		puts $fp "1.0"
		puts $fp "define_grid k 2"
		puts $fp "0.0"
		puts $fp "1.0"
		puts $fp "include $mesh_dir/$tetin_file"
		puts $fp "return"
		close $fp
    	} else {
# replace the chosen tetin file for one that exists in the cart_file
		set cart_file [open $mesh_dir/cart_file r]
		set new_cart_file [ open $mesh_dir/new_cart_file w]
		while { ![eof $cart_file] } {
			set line  [ gets $cart_file ]
			if { [lindex $line 0] == "include" } {
				puts $new_cart_file \
				"include $mesh_dir/$tetin_file"
			} elseif { $line != "" } {
				puts $new_cart_file "$line"
			}
		}
		close $new_cart_file
		close $cart_file
		cmd_mv $mesh_dir/cart_file $mesh_dir/old_cart_file
		cmd_mv $mesh_dir/new_cart_file $mesh_dir/cart_file
	}
    }
    
    set tetin ""
    set domainname ""
    if {$tetin_file != ""} { set tetin "-tetin \"$tetin_file\"" }
    if {$boco_file == ""}  { set boco_file  $mesh_dir/boco }
    if {$domain_dir == ""} { set domain_dir $config_dir/domains }
    if {$domain_name != ""} { set domainname "-domains_name \"$domain_name\"" }
    
    if $black_and_white { set bwopt "-bw" } else { set bwopt "" }
    if { $blocking_file != "" } {
	set bf \"$blocking_file\"
    } else {
	set bf ""
    }
    
    set cmd "\"$tool_bin\" \"$config_dir\" $bf $tetin $bwopt \
	-domains_dir \"$domain_dir\" $domainname -mesh_dir \"$mesh_dir\""
    if { $use_file_suffixes } {
	append cmd " -use_file_suffixes"
    }
    return $cmd
}

proc tool_setup {} {
    global display_flag all_lines initial_replay_file replay_dir replay_file
    global config_dir is_win
    
    add_families
    get_indices 1
    set_scan_plane_state
    
    # Set the default viewing parameters.
    
    set display_flag(block) 0
    set display_flag(block_super) 1
    set display_flag(block_struct) 1
    set display_flag(block_unstruct) 1
    set display_flag(block_swept) 1
    
    set display_flag(edge) 1
    set display_flag(edge_vol) 1
    set display_flag(attached_faces) 0
    update_display
    command "orient +z"
    toggle_hexa_meshtype

    if $is_win {
	bind all <Key> {check_binding %W %X %Y %x %y %K}
    }
    
    set first [ lindex [ file split $config_dir ] 0]
    if { $first == "mulcad" } {
	set replay_dir $config_dir/hexa
    } else {
	set replay_dir $config_dir
    }
    set replay_file replay.hxa
    
    if {$initial_replay_file != ""} {
	file_replay_batch $initial_replay_file
    }
}

proc make_tool_windows {} {
    make_display_window
}

set view_buttons {
    { command -label {Set linewidth} -command set_linewidth }
}

set line_width 0

proc set_linewidth {} {
    global line_width
    
    set ww [question_box .linew "Line width" " Enter line width." $line_width]
    if {$ww != ""} {
	set line_width $ww
	command "set_linewidth $ww"
	update_display
    }
}

set hexa_version 10.0
if [file exists $tool_dir/version.tcl] {
    source $tool_dir/version.tcl
    puts $hexa_version
}

#set tool_logo $tool_dir/hexa-logo.ppm
#set tool_logo_small $tool_dir/hexa-logo-small.ppm
#set tool_logo "$tool_dir/hexa-logo.xbm black"

set tool_logotext "ANSYS ICEM CFD $hexa_version\nHexa 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"

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

set tool_phases {
    {File file}
    {Blocking blocking}
    {Edges edges}
    {Projection projection}
    {Meshing meshing}
    {Topology topology}
}
#    {Import import}

set hexa_modals(menu_mode) expert
set hexa_modals(mesh_unstruct_active) 1
set hexa_modals(mesh_struct_active) 1
if { [info exists env(ICEM_AI_ENVIRONMENT)] &&
		$env(ICEM_AI_ENVIRONMENT) == 1 } {
	set hexa_modals(mesh_type) 1
} else {
	set hexa_modals(mesh_type) 3
}
set banner_disables_menus 1
set use_hexa_banner 1

#{options       button "Options"        0,3 file_options 0}

#    {unput         button "Run unput"      0,5 file_unput ""
#	"Quit Hexa and run Unput."}

set tool_phase_info(file) {
    {load          button "Load blocking"  0,1 file_load ""
	"Load blocking from a\nHexa blocking file."}
    {save          button "Save blocking"  0,2 "file_save 0" }
    {renew         button "Check / fix"    0,3 file_renew ""
	"Verify the current blocking\nand fix any problems found."}
    {quit          button "Quit"           0,4 file_quit ""
	"Exit from the program."}
    {replay        button "Replay"         0,6 file_replay ""
	"Replay a previously defined\nsequence of Hexa commands."}
    
    {multiblock    button "Multiblock"     1,1 file_multiblock "" "" \
     hexa_modals(mesh_struct_active)}
    {unstructured  button "Unstructured"   1,2 file_unstructured "" "" \
     hexa_modals(mesh_unstruct_active)}
    {unstruct2d    button "Unstr surface"  1,3 file_unstruct2d "" "" \
     hexa_modals(mesh_unstruct_active)}
    {superdomain   button "Superdomain"    1,5 file_superdomain "" "" \
     hexa_modals(mesh_struct_active)}
    {boco          button "Boundary conds"      0,5 file_edit_boco }
    {hardcopy      button "Hardcopy"       1,6 file_hardcopy}

    {import	   menu "Load Mesh" 1,4 {
	{ command -label "Unstructured" -command file_read_unstruct }
	{ command -label "Structured" -command file_read_struct }
    }}
    {modes         menu   "Mesh type"         2,1 {
	{radio -label "Unstructured or structured" -variable hexa_modals(mesh_type) -value 3 \
	 -command "toggle_hexa_meshtype"}
	{radio -label "Unstructured mesh" -variable hexa_modals(mesh_type) \
	     -value 1 -command "toggle_hexa_meshtype"}
	{radio -label "Structured mesh" -variable hexa_modals(mesh_type) \
	     -value 2 -command "toggle_hexa_meshtype"}
    } "Enable/disable entries based on Struct/Unstruct mesh"}
    {mouse        button  "Mouse buttons"    2,2 change_mouse_buttons }
}

set tool_phase_info(blocking) {
    {innero        button "O grid"          0,1 block_innero}
    {rescale       button "Rescale O grid"  0,2 block_rescale}
    {split         button "Split"           0,3 block_split}
    {extsplit      menu "Extend split"    0,4 {
	{command -label "Select        " -command block_extsplit }
	{command -label "All           " -command block_decimate }
    } }
	
    {delete        button "Delete station"  0,5 block_delete_station}
    {collapse      menu   "Collapse"        0,6 {
	{command -label "Block        " -command block_collapse }
	{command -label "Edge         " -command "edge_collapse collapse" }
	{command -label "Join Vertices" -command "edge_collapse join" }
	{command -label "Vertex to Edge" -command "vertex_to_edge" }
    } }
    {initmesh      menu   "Init blocking"    1,1 {
	{command -label "3-D" -command "block_initmesh {}" }
	{command -label "2-D" -command "block_initmesh 2d" }
	{cascade "Surface Mesh" {
		{ command -label "All Unstructured" \
				-command "block_initmesh surface -unstruct" }
		{ command -label "Some Mapped" \
				-command "block_initmesh surface -mixed" }
		{ command -label "Mostly Mapped" \
				-command "block_initmesh surface -mapped" }
	} }
	{command -label "Auto" -command "block_initmesh auto" }
    } }
    {merge_face    button "Merge face"       1,2 block_merge_face}
    {material      menu   "Material"         1,4 {
	{command -label "2 corners"  -command "block_material ijk"}
	{command -label "Select Block" -command "block_material select"}
	{command -label "Block Numbers" -command "block_material numbers"}
	{command -label "All Displayed" -command "block_material all_disp"}
	{command -label "Query Material" -command "block_material query"}
    } }
    
    {periodic      button "Periodic nodes"   1,5 block_periodic}
    {create_block  menu "Create Block"     1,6 {
	{ command -label "Hexa" -command "block_create hexa" }
	{ command -label "Quarter O-grid" -command "block_create yblock" }
	{ command -label "Degenerate" -command "block_create degen" }
	{ command -label "Unstructured" -command "block_create unstruct" }
	{ command -label "Split Unstructured Face" -command \
			"block_create split_unstruct" }
	{ command -label "Swept Block" -command "block_create_swept" }
	{ command -label "Split Unstructured Volume" -command \
			"split_unstruct_volume" }
	{ command -label "Add Interior Loop" -command \
			"add_interior_loop" }
    } }
    
    {split_face    menu "Split face"       2,1 {
	{command -label "2 corners" -command "block_split_face corners" }
	{command -label "select face" -command "block_split_face select" }
    } }

    {extrude_faces button "Extrude face(s)" 2,2 block_extrude_faces}

    {wing_grid     button "Wing grid"        2,3 block_wing_grid}

    {convert   menu "Convert Block"     2,4 {
	{ command -label "Mapped to Free" \
		-command "block_convert unstruct select" }
	{ command -label "Free to Mapped" \
		-command "block_convert struct select" }
	{ command -label "Mapped to Swept" \
		-command "block_convert swept select" }
	{ command -label "Free Face to Mapped face" \
		-command "block_convert struct_face select" }
	{ command -label "Mapped to Y-Block" \
		-command "block_convert yblock select" }
    } }

    {uncollapse    button "Uncollapse"       2,5 block_uncollapse}
}

	lappend tool_phase_info(blocking) { 
	deleteb         menu   "Delete blocks"     2,6 {
	    {command -label "2 corners"      -command "block_delete ijk" }
	    {command -label "Select blocks"    -command "block_delete sel" }
	    {command -label "Block Numbers"    -command "block_delete two" }
	    {command -label "Blanked"        -command "block_delete blanked" }
	} }

#    {curves_blocks button "Extrude blocks" 1,6 block_extrude_blocks}


    if [info exists env(HEXA_TOYOTA)] {
	lappend tool_phase_info(blocking) {
	    primitive_block  menu "Toyota"     2,4 {
		{ command -label "T Junc" -command "block_primitive T" }
		{ command -label "Y Junc" -command "block_primitive Y" }
		{ command -label "Base Block" -command "block_primitive_base" }
		{ command -label "Twist revision" -command "block_extrude_twist" }
		{ command -label "Merge Vertices" -command "merge_vertices" }
		{ command -label "Move face" -command "move_face" }
		{ command -label "Rotate Multiple Nodes" -command "rotate_multiple" }
	    }
	}
    }

	lappend tool_phase_info(blocking) { 
	merge         menu   "Merge blocks"     1,3 {
	    {command -label "2 corners"      -command "block_merge ijk" }
	    {command -label "Select blocks"    -command "block_merge sel" }
	    {command -label "Block Numbers"    -command "block_merge two" }
	    {command -label "Automatic"        -command "block_merge auto" }
	} }

#    {modify        button "Move vertex"       0,3 edge_modify_single}
#    {modify_mult   button "Move options"      0,4 edge_modify}

set tool_phase_info(edges) {
    {split         menu "Split edge"        0,1 {
	{ command -label "single edge - smooth interpolation" \
					-command { edge_split interpolate } }
	{ command -label "single edge - b-spline control points" \
					-command { edge_split smooth } }
	{ command -label "single edge - select tangents" \
					-command { edge_split tangents } }
	{ command -label "single edge - piecewize linear" \
					-command { edge_split } }
	{ command -label "single edge - auto" -command { split_edges select } }
	{ command -label "all at vertex - smooth" \
					-command { edge_split vertex_smooth } }
	{ command -label "all displayed edges - smooth" \
				-command { split_edges all_displayed smooth } }
	{ command -label "all displayed edges - auto" \
				-command { split_edges all_displayed } }
	{ command -label "all edges   - smooth" \
				-command { split_edges all smooth } }
	{ command -label "all edges   - auto" \
				-command { split_edges all } }
	} }
    {unsplit       menu "Unsplit edge"      0,2 {
	{ command -label "single edge - 1 point" \
				-command { edge_unsplit single } }
	{ command -label "single edge - all points"  \
				-command { edge_unsplit one_edge } }
	{ command -label "all displayed"  \
				-command { edge_unsplit all_displayed } }
	{ command -label "all edges"  \
				-command { edge_unsplit all } }
        } }
    {modify	   button "Move vertex"	      0,3 edge_modify_single }
    {single	   button "Move multiple"     0,4 edge_modify_mult }
    {set_pos       button "Set location"      0,5 edge_set_pos}
    {make_pres	   button "Make prescribed"   0,6 edge_modify_make_prescribed }
    {comp  	   menu   "Group curve" 1,1 {
        {command -label "screen select" -command {comp_screen } }
        {command -label "region select" -command {comp_region } }
        {command -label "1 family"   -command {comp_families} }
        {command -label "1 per family"   -command {comp_families all } }
        {command -label "all tangent"   -command {comp_tangent } }
    } }

    {uncomp        button "Ungroup curve"     1,2 edge_uncomp}
    {isocurve      button "Create isocurve"   1,3 edge_create_iso}
    {setlength     button "Set edge length"   1,4 edge_set_length}
    {align	   button "Align Vertices"    1,5 edge_modify_align_verts }
    {options       menu "Edges options"     1,6 {
	{ checkbutton -label "Move Dependent" \
				-variable edge_modify_dependent }
	{ command -label "Make an edge interpolatory" \
			-command { edge_make interpolatory } }
	{ command -label "Make an edge smooth" \
			-command { edge_make smooth } }
	{ command -label "Make an edge linear" \
			-command { edge_make linear } }
    } }
    {linkshape     button "Link shape"        2,1 edge_link_shape}
    {unlinkshape   button "Unlink shape"    2,2 edge_unlink_shape}
    {reset_ogrid   button "Reset Ogrid Ortho"  2,3 reset_ogrid_ortho }
    {ortho_offset  button "Ortho Offset"  2,4 edge_ortho_offset }
}

#    {movedep	   checkbutton "Mv Dependent"   1,6 edge_modify_dependent }
#    {csystem       button "Coord. Systems"    2,3 coordinate_systems}


set tool_phase_info(projection) {
    {vert          button "Project displayed"  0,1 proj_vert }
    {modify        button "Move vertex"        0,2 edge_modify_single}
    {prescr        button "Vertex -> point"    0,3 proj_vpoint}
    {vcurve	   button "Vertex -> curve"    0,4 proj_vcurve}
    {vsurf	   button "Vertex -> surface"  0,5 "proj_nvert surface"}
    {nvert         button "Vertex -> none"     0,6 "proj_nvert none" }

    {shared_wall   button "Shared walls"       1,1 proj_shared_wall}
    {active_surfs  button "Active families"    1,2 proj_active_surfs}
    {query         button "Query projection"   1,3 proj_query}

    {edgec         menu   "Edge -> curve"      1,4 {
	{command -label "Project" -command proj_edge_to_curve}
	{checkbutton -label "Edge -> curve -> surface" -variable project_flag(intersection)}
	{checkbutton -label "Proj end to intersection of 2 curves" -variable project_flag(curve_intersection)}}}
    {edges         button "Edge -> surface"    1,5 proj_edge_to_surface}
    {edgen         button "Edge -> none"       1,6 proj_edge_to_none}
    
    {update_blocking button "Update Blocking"  2,3 proj_update_blocking }
    {update_assoc  menu   "Update proj" 2,4 {
	{command -label "Update all"      -command {proj_update update {}} }
	{command -label "Update vertices" -command {proj_update update vertices} }
	{command -label "Update edges"    -command {proj_update update edges} }
	{command -label "Update faces"    -command {proj_update update faces} }
	separator
	{command -label "Clear all"       -command {proj_update clear {}} }
	{command -label "Clear vertices"  -command {proj_update clear vertices} }
	{command -label "Clear edges"     -command {proj_update clear edges} }
	{command -label "Clear faces"     -command {proj_update clear faces} }
    } }
    
    {proj_modals   menu "Modals"	       2,1 {
	{ checkbutton -label "Move ogrid nodes with projected nodes" \
			-variable move_ogrid_nodes }}
    }
    {link_face	   menu "Link face" 	     2,2 {
	{ command -label "2 corners"  -command { link_face corners }  }
        { command -label "Select face" -command { link_face select }  }
    } }
    {face          menu "Face->surface"      2,5 {
	{ command -label "2 corners"  -command { proj_face corners }  }
	{ command -label "Select face" -command { proj_face select }  }
    } }
    {nface         menu "Face -> none"     2,6 {
	{ command -label "2 corners" -command { proj_nface corners } } 
	{ command -label "Select face" -command { proj_nface select } }
    } }
}

foreach i {intersection curve_intersection} {
    set project_flag($i) 0
}

# {proj_lim      button "Projection limit"   2,1 proj_limit}
# {command -label "Unstruct mesh"	-command {mesh_smooth_glsm_unstruct 0}}
# {command -label "Struct mesh" 	-command {mesh_smooth_glsm_struct}}

set tool_phase_info(meshing) {
    {recompute     button "Compute"            0,1 mesh_recompute}
    {check_size    button "Mesh size"          0,2 mesh_check_size}
    {params        button "Surface params"     0,3 mesh_params}
    {cparams       button "Curve params"       0,4 mesh_cparams}
    {eparams       button "Edge params"        0,5 mesh_eparams}
    {assign_sizes  button "Reassign sizes"     0,6 mesh_assign_sizes}
   
    {quality       menu   "Quality check" 1,1 {
        {command -label "Determinant (2x2x2 stencil)" \
                        -command {mesh_diag determinant Determinants 1}}
        {command -label "Determinant (3x3x3 stencil)"  \
                 -command {mesh_diag determinant_27 Determinants 1}}
        {command -label Angle       -command {mesh_diag angle Angles 1}}
        {command -label Volume      -command {mesh_diag volume Volumes 1}}
        {command -label Warpage     -command {mesh_diag warpage Warpage 1}}
        {command -label "Min side"  -command {mesh_diag min_side "Min Edge" 1}}
        {command -label "Constant radius"     -command {check_const_r }}
        {command -label "Eriksson skewness"  \
                 -command {mesh_diag eriksson Eriksson 1}}
        {command -label "Workbench shape"  \
                 -command {mesh_diag workbench Workbench 1}}
    }}
    {refinement    button "Refinement"         1,3 mesh_refinement}
    {renumber      button "Renumber domains"   1,4 mesh_renumber_domains \
         "" "" hexa_modals(mesh_struct_active)}
    {smooth       menu   "Smoothing" 1,5 {
        {command -label "Blocks"        -command {mesh_smooth_glsm_unstruct 1}}
    }}
    {match_edges   button "Match edges"        1,6 mesh_match_edges}

    {scale_mesh    button "Scale mesh size"    2,1 mesh_scale}
    {init_output   button "Init output blocks" 2,2 mesh_init_output \
         "" "" hexa_modals(mesh_struct_active)}
    {options       button "Meshing options"    2,3 mesh_options}
}

    lappend tool_phase_info(meshing) {
        invert           menu   "Invert block"         1,2 {
            {command -label "Select blocks"  \
                        -command "block_ijk select invert" }
            {command -label "Block Numbers"   \
                        -command "block_ijk nums invert" }
            {command -label "2 corners"     \
                        -command "block_ijk ijk invert" }
            {command -label "All displayed"  \
                        -command "block_ijk all_displayed invert" }
            {command -label "All"  -command "block_ijk all invert" }
            {command -label "Inverted" \
                        -command "block_ijk inverted invert" }
            {command -label "Align normals (select)" \
                        -command "block_ijk select align" }
            {command -label "Align normals (number)" \
                        -command "block_ijk nums align" }
        } }

    lappend tool_phase_info(meshing) {
        ijk           menu   "IJK -> KIJ"         2,4 {
            {command -label "Select blocks"    -command "block_ijk select rotate" }
            {command -label "Block Numbers"    -command "block_ijk nums rotate"
}

        } "" hexa_modals(mesh_struct_active)}

    lappend tool_phase_info(meshing) {
        origin           menu   "Set Origin"         2,5 {
            {command -label "Select blocks" -command "block_set_origin select" }            {command -label "Block Numbers" -command "block_set_origin nums" }
        } "" hexa_modals(mesh_struct_active)}

    lappend tool_phase_info(meshing) {
        transfer        menu "Transfer Blocks"  2,6 {
            { command -label "2 corners" -command "block_transfer ijk" }
            { command -label "Select blocks" -command "block_transfer sel" }
            { command -label "Block Numbers" -command "block_transfer nums" }
        } }

# {current       button "Set current"        0,1 top_set_current}
# {insert        button "Insert"             0,1 top_insert}

set tool_phase_info(topology) {
    {delete        button "Delete"             0,1 top_delete}
    {extract       button "Extract"            0,2 top_extract}
    {rename        button "Rename"             0,3 top_rename}
    {invert        button "Invert blocks"      0,4 top_invert}
    {merge         button "Merge"              0,5 top_merge}
    {surface	   button "3D->2D"             0,6 top_surface}
    {copy          button "Copy"               1,1 top_copy}
    {transform     button "Transform"          1,2 top_transform}
    {load          button "Load sub-topo"      1,3 top_load}
    {save          button "Save topology"      1,4 top_save}
    {select        button "Change Topology"    1,5 top_select_current}
    {extrude	   menu   "2D->3D"	       1,6  {
        {command -label "translate    " -command "top_extrude translate" }
        {command -label "rotate       " -command "top_extrude rotate" } 
        {command -label "fill         " -command "top_fill" } 
    } }
}

set tool_phase_info(import) {
    {load_desc     button "Load surface file" 0,1 import_load_desc}
    {load_boco     button "Load boco"         0,2 import_load_boco}
    {load_domain   button "Load domains"      0,3 import_load_domain}
    {edit_data     button "Edit data"         0,4 import_edit_data}
    {save          button "Save all"          0,5 import_save}

    {setfam        button "Change families"   1,1 import_setfam}
    {extract_curve button "Extract curves"    1,2 "import_extract curve"}
    {extract_point button "Extract points"    1,3 "import_extract point"}
    {load_material button "Material point"    1,4 import_load_material}
    {modify_curve  button "Modify curve"      1,5 import_modify_curve}
}

set tool_phase_command(import) import_switch

# set tool_phase_command(topology) top_set_current_if
# set tool_phase_command(topology) top_set_current_list_if

proc toggle_hexa_meshtype {} {
    global hexa_modals
    
    set mesh_type $hexa_modals(mesh_type)
    
    # use switch statement instead of bit pattern for better
    # visual efficiency (don't unset just to set again)
    
    switch $mesh_type {
	0 {
	    set hexa_modals(mesh_unstruct_active) 0
	    set hexa_modals(mesh_struct_active) 0
	}
	
	1 {
	    set hexa_modals(mesh_unstruct_active) 1
	    set hexa_modals(mesh_struct_active) 0
	}
	
	2 {
	    set hexa_modals(mesh_unstruct_active) 0
	    set hexa_modals(mesh_struct_active) 1
	}
	
	3 {
	    set hexa_modals(mesh_unstruct_active) 1
	    set hexa_modals(mesh_struct_active) 1
	}
    }
    disp_hexa_meshtype $mesh_type
}


proc import_switch {on} {
    global tool_bin2 tool_id tool_id2 tool_prompt tool_prompt2
    global desc_num
    
    if ![info exists desc_num(surf)] {
	import_init_vars
    }

    set cmd $tool_bin2
    
    if $on {
	if [start_process $tool_id2 .v.view $cmd $tool_prompt2] {
	    command "orient +z"
	}
    } else {
	if ![tk_dialog .save "Save" "Save surface data?" "" 0 Yes No] {
	    import_save
	}
	start_process $tool_id .v.view "" $tool_prompt
    }
    import_display_window $on
}

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 == "x" || $c == "y" || $c == "z" } {
	    dl_orient $c
	} elseif {$c == "s" } {
	    dl_automax
	} elseif {$c == "F9"} {
	    send_event k O $wx $wy 0
	}
    }
}

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

set tool_extra(2) {button Print file_hardcopy}
set tool_extra(3) {button Undo undo_action}
set tool_extra(4) {button Redo redo_action}
set tool_extra(6) {button Shell "file_shell ."}
set extra_position(extra6) 3,2


proc set_scan_plane_state {} {
    global last_line
    command "mesh_dimension"
    if { $last_line == "2" } {
        .e.buts.scan configure -state disabled
    } else {
        .e.buts.scan configure -state normal
    }
}

if [file exists $env(ICEM_ACN)/hexa_custom_script.tcl] {
    source $env(ICEM_ACN)/hexa_custom_script.tcl
}
if [file exists hexa_custom_script.tcl] {
    source hexa_custom_script.tcl
}


