proc aie_geo_import_mesh {files {segment 1}} {
    global tdv_default aie_geoTypeVis fam_visible
    global aie_ShowCurveAttachment

    if ![ic_geo_is_loaded] {
        ic_empty_tetin
        make_default_file_name tetin .tin
    }

    busy 1
    ic_geo_import_mesh $files $segment 1

    set aie_geoTypeVis(surface) 2
    if {[ic_geo_num_objects curve] == 0} {
        # This might be a really huge surface.
        set tott 0
        foreach s [ic_geo_get_objects surface ""] {
            incr tott [lindex [ic_geo_num_segments surface $s] 0]
        }
        if {$tott > 64000} {
            mess "Warning: new surfaces have $tott triangles - \
                    by default not displaying them\n" red
            set aie_geoTypeVis(surface) 0
            set turn_off surface
        }
    }

    aie_afterStuffLoaded
    if [info exists turn_off] {
        aie_initGeoVis "" $turn_off
        unset turn_off
    } else {
        aie_initGeoVis
    }
    busy 0

    mess "Done importing data.\n"
}

proc aie_geo_import_from_mesh {type {infiles ""} {suffix *} {multiple 0}} {
    global file_name

    aie_clear_dez
    if ![file isdirectory $file_name(project_dir)] {
        set_project_dir .
    }
    set file_name(domains_dir) $file_name(project_dir)
    set file_name(mesh_dir) $file_name(project_dir)

    if {$infiles == "" && $type != "current"} {
        set ftypes [list [list "$type files" $suffix] [list "All infiles" *]]
        set infiles [tk_getOpenFile -initialdir $file_name(project_dir) \
                         -filetypes $ftypes -multiple $multiple]
        if {$infiles == ""} { return "" }
    }
    if !$multiple {set infiles [list $infiles]}

    switch $type {
        domain {
            if {[ic_geo_is_loaded] && [llength $infiles] == "1"} {
               set opt [tk_dialog .tetinOverwrite "Geometry Exists" "Geometry Already Exists" \
               "" 1 "Merge" "Replace" "Cancel" ]
               switch -- $opt {
                  -1 { 
                    if [winfo exists .tetinOverwrite] { top_destroy .tetinOverwrite }
                    return
                  }
                  0 { }
                  1 {
                     aie_close_geom 0
                     mess "\n   --- Unloading existing geometry\n\n"
                  }
                  2 { return }
               }
            }
            aie_geo_import_mesh $infiles
        }
        current {
            if [ic_geo_is_loaded] {
                if ![ic_yes_or_no "Geometry data already exists.  \
                        Add the data from the current mesh to the \
                        existing geometry?"] {
                    mess "Not importing geometry.\n" red
                    return
                }
            }
            aie_geo_import_mesh {}
        }
        nastran - patran - stl - vrml - capri - plot3d {
            set ddir $file_name(domains_dir)
            # geo_import_$type returns either "" or a list where 
            # 1 = unstr and
            # 2 = str,
            # followed by the domains.
            
            if {[ic_geo_is_loaded] && [llength $infiles] == "1"} {
                set opt [tk_dialog .tetinOverwrite "Geometry Exists" "Geometry Already Exists" "" 1 "Merge" "Replace" "Cancel"]
	            switch -- $opt {
	                -1 { 
	                    if [winfo exists .tetinOverwrite] { top_destroy .tetinOverwrite }
	                    return
	                }
	                0 { }
	                1 {
		                aie_close_geom 0
		                mess "\n   --- Unloading existing geometry\n\n"
	                }
	                2 { return }
	            }
            }
            busy 1
            tdv_active 0
            if {$type == "stl"} {
                set outfiles ""
                for {set i 0} {$i < [llength $infiles]} {incr i} {
                    lappend outfiles $ddir/tmpdomain${i}.uns
                }
                set ret [geo_import_$type $infiles $outfiles]
                if {$ret == ""} {
                    tdv_active 1
                    busy 0
                    return 
                }
                set files [lindex $ret 1]
                if {$files != ""} {
                    foreach domfile $files {
                        aie_geo_import_mesh [list $domfile]
                        eval file delete -force -- [list $domfile]
                    }
               }
               tdv_active 1
               busy 0
            } else {
                foreach infile $infiles {
                    if {$type == "vrml"} {
                        if ![ic_file_is_ascii $infile] {
			                mess "File $infile is binary. Only ASCII VRML files import is supported\n" red
			                tdv_active 1
			                busy reset
			                return
		                }
		            }
                    if {$type != "plot3d"} {
                        set ret [geo_import_$type [list $infile] [list $ddir/tmpdomain0.uns]]
                    } else {
                        set ret [geo_import_plot3d [list $infile] [list $ddir/tmpdomain0.uns] 1 1]
                        if {[lindex $ret 0] == 3} {
                           set ret [geo_import_plot3d [list $infile] [list $ddir/tmpdomain0.uns] 0 1 1] 
                        }
                    }
                    if {$ret == ""} {
                        tdv_active 1
                        busy 0
                        return 
                    }
                    if {[lindex $ret 0] == 2} {
                        set domains_dir [file dirname [lindex [lindex $ret 1] 0]]
                        set files [ic_convert_domains_to_unstruct $domains_dir]
                    } else  {
                        set files [lindex $ret 1]
                    }
                    if {$files != ""} {
                        foreach domfile $files {
                            aie_geo_import_mesh [list $domfile]
                            eval file delete -force -- [list $domfile]
                        }
                    }
                }
                tdv_active 1
                busy 0
            }
        }
        default {
            mess "Filetype $type not supported\n"
        }
    }
}

proc aie_geo_import_esf {} {
    global file_name

    set infile [med_file_select esf 0]

    if {$infile == ""} {
        mess "Invalid Input File\n"
        return
    }

    busy 1
    if [catch {ic_trans_read_esf_file $infile $esf_family} res] {
        mess "Error: $res\n" red
	busy reset
    } else {
        mess "Done.\n"
    }
    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc aie_geo_import_parasolid {} {
    global file_name

    set infile [med_file_select ps 0]

    if {$infile == ""} {
        mess "Invalid Input File\n"
        return
    }

    set tetinfile [file tail $infile]
    set tetinfile [file root $tetinfile]
    set tetinfile $file_name(project_dir)/$tetinfile.tin
    
    if { [ file exists $tetinfile ] || $tetinfile == "" } {
        set tmess "File $tetinfile exists - overwrite?"
        switch -- [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] {
            -1 {
	            if [winfo exists .overwrite] { top_destroy .overwrite }
	            return
	        }
	        1 {
                set tetinfile [med_file_select tetin 1 $tetinfile]
            }
        }
    }

    if {$tetinfile == ""} {
        mess "Invalid Output File\n"
        return
    }


    busy 1
    if [catch {ic_trans_ps_tetin $infile $tetinfile} res] {
        mess "Error: $res\n" red
	busy reset
        return
    }

    file_check_load_tetin $tetinfile
    mess "Done.\n"

    ic_geo_set_family_params PS_VERTEX emax 0.0
    ic_geo_set_family_params PS_EDGE emax 0.0
    ic_geo_set_family_params PS_FACE emax 0.0
    ic_set_meshing_params global 0 gref 1 gmax 512 gttol 0.001
    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}



proc aie_geo_import_parameters {} {
    global file_name

    set infile [med_file_select tin 0]

    if {$infile == ""} {
        mess "Invalid Input File\n"
        return
    }

    busy 1
    if [catch {ic_geo_map_tetin_sizes $infile} res] {
        mess "Error: $res\n" red
        busy reset
    } else {
        aie_geo_map_tetin_global_parameters $infile
        mess "Done.\n"
    }
    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc aie_geo_map_tetin_global_parameters {{infile ""}} {
    if {$infile == ""} return
    busy 1
    set f [open $infile r]
    while {![eof $f]} {
        set line [string trim [gets $f]]
        if {[lindex $line 0] == "global_meshing_parameter"} {
            set n [lindex $line 1]
            set v [lindex $line 2]
            ic_set_meshing_params variable 0 $n $v
        } elseif {[lindex $line 0] == "periodic"} {
            set n [lindex $line 1]
            if {$n == "translational"} {
                ic_geo_set_periodic_data "axis \{[lrange $line 2 4]\} type trans angle 0 base \{0 0 0\}"
            } else {
                ic_geo_set_periodic_data "axis \{[lrange $line 4 6]\} type rot angle [lindex $line 7]\
                                          base \{[lrange $line 1 3]\}"
            }
        }
    }
    busy 0
    close $f
}

proc ongeoImportCatiaDEZLoad {} {
    global file_name catia_trans

    set catia_trans(file) ""
    set catia_trans(file) [med_file_select catia 0 "" "" 1]

    set catia_trans(listing_file) catia/catia_listing

    set model ""
    regexp {^(.*)/([^/]*)(.model\}|.exp\}|.model|.exp)$} $catia_trans(file) tmpall \
            tmppath model
    if {$model == ""} {
        regexp {^(.*)/([^/]*)$} $catia_trans(file) tmpall tmppath model
    }
    set tetin_name $file_name(mesh_dir)/$model.tin
    set catia_trans(anf_name) $file_name(mesh_dir)/$model.anf

    set geoImportCatiaLayGGVal 0
    set filter_arg ""
    geoImpCatiaCMS select "SHOW" " CATIA Input "
    geoImpCatiaCMS select "PICK" " CATIA Input "
    geoImpCatiaCMS select "NOPICK" " CATIA Input "
    geoImpCatiaCMS select "No Points" " File Output "
    geoImpCatiaCMS select "No Curves" " File Output "

    set famnam "default"
    set entnam "default"
    set tritol 0.0004
    if [ic_geo_is_loaded] {
        foreach {n v} [ic_set_meshing_params global 0] {
            if {$n == "gttol"} {
	            set tritol $v
	            break
	        }
        }
    }
}

proc ongeoImportCatiaDEZAfterLoad {} {
    global catia_trans

    if {$catia_trans(file) == ""} {
        geoImportCatiaDEZ unload
        return
    }
    ::AIEnv::GUIManagement::ToggleGrey geoImportCatiaLayArg 1
    if {[trace vinfo geoImportCatiaLayGGVal] != ""} {
        trace vdelete geoImportCatiaLayGGVal w aie_geo_trace_import_catia_filter
    }
    trace variable geoImportCatiaLayGGVal w aie_geo_trace_import_catia_filter
}

proc aie_geo_trace_import_catia_filter {name element op} {
    global catia_trans
    
    if [geoImportCatiaDEZ get geoImportCatiaLayGGVal] {
        aie_get_catia4_filter_list
        if {$catia_trans(filter_arg) != ""} {
            geoImportCatiaDEZ set filter_arg $catia_trans(filter_arg)
        } else {
            geoImportCatiaDEZ set geoImportCatiaLayGGVal 0
            geoImportCatiaDEZ set filter_arg ""
            set catia_trans(filter_arg) ""
        }
    } else {
        geoImportCatiaDEZ set filter_arg ""
        set catia_trans(filter_arg) ""
    }
}

proc aie_get_catia4_filter_list {} {
	global catia_trans env is_win

	if ![geoImportCatiaDEZ get geoImportCatiaLayGGVal] {
            geoImportCatiaDEZ set filter_arg ""
            set catia_trans(filter_arg) ""
            return
	}
        set tmpfile [tmp_filename]
        set c4tetin [list $env(ICEM_ACN)/bin/c4tetin]
	set filter_liste [eval exec $c4tetin -tcl --show-filter-only \'$catia_trans(file)\' 2>$tmpfile]
        if [file exists $tmpfile] {cmd_rm $tmpfile}
	set catia_trans(filter) "inactive"
	set buttons {
	    {Accept "set catia_trans(filter) active; form_done .select_filter"}
	    {Cancel "set catia_trans(filter) inactive; form_done .select_filter"}
	}
	set d [form_init .select_filter "CATIA V4 Layer Filter" "" $buttons]
	if {$d != ""} {
            form_listbox $d.list "Available Layer Filters" $filter_liste "" "" 30x10 {3 0} ""  0 0 1
            form_finish +300+300
	}
	form_wait .select_filter
	set filter_arg "--filter=\{"
	set nitems 0
	if {$catia_trans(filter) == "active"} {
            set idx [form_listbox_get_select_index $d.list]
            foreach item $idx {
                if {$nitems > 0} { append filter_arg "," }
                append filter_arg "\"[form_listbox_get $d.list $item]\""
                set nitems 1
            }
	}
	if {$nitems == 0} {
	    set filter_arg ""
	    set catia_trans(filter) "inactive"
	} else {
	    append filter_arg "\}"
	}
	set catia_trans(filter_arg) $filter_arg
	form_kill .select_filter
}

proc ongeoImportCatiaDEZOK {} {
    global catia_trans aie_geoTypeVis

    # Set the global vriables
    set catia_trans(do_tetin) "-one"
    set catia_trans(do_anf) "-no"
    set catia_trans(cpoints) ""
    set catia_trans(rcfile) ""
    set catia_trans(inhlayer) ""
    set catia_trans(ndoms) ""
    set catia_trans(filter_arg) ""
    set catia_trans(catiamif) ""
    
    if $geoImportCatiaLayGGVal {
	set catia_trans(filter_arg) $filter_arg
    } else {
	set catia_trans(filter_arg) ""
    }
    if $catiamif {
        set catia_trans(catiamif) "+catiamif-model"
	set_catiamif_defaults
    } else { set catia_trans(catiamif) "" }
    # if $preview {
    #     set catia_trans(preview) "--preview" 
    # } else { set catia_trans(preview) "" }
    if $range { 
        set catia_trans(range) "--ignore-out-of-range" 
    } else { set catia_trans(range) "" }
    if $bocu { 
        set catia_trans(bocu)  \
        "--dont-suppress-curves --with-boundary-curves --share-common-edges"
    } else { set catia_trans(bocu) "" }
    if $loops { 
        set catia_trans(loops) "--planes-as-loops $catia_trans(bocu)"
    } else { set catia_trans(loops) "" }
    if $bobox { 
        set catia_trans(bobox) "--write-bounding-box"
    } else { set catia_trans(bobox) "" }
    if $nodom { 
        set catia_trans(nodom) \
                "--with-polysolid-domains --without-polysolid-faces"
    } else { set catia_trans(nodom) "" }
    if $automaxsize { 
        set catia_trans(automaxsize) "--maximum_size=AUTO"
    } else { set catia_trans(automaxsize) "" }
    if $inhlayer { 
        set catia_trans(inhlayer) "--inherit-layer"
    } else { set catia_trans(inhlayer) "" }
    if $laxis { 
        set catia_trans(laxis) "--apply-axis-system"
    } else { set catia_trans(laxis) "" }

    foreach var {tetin_name famnam entnam tritol args} {
        set catia_trans($var) [set $var]
    }
    #===========================================
    if ![ic_geo_is_loaded] {
        ic_empty_tetin
    }
	ic_set_meshing_params global 0 gttol $tritol
    #===========================================
    if {$catia_trans(famnam) == "default"} {
        set catia_trans(famnam) ""
    }
    if {$catia_trans(entnam) == "default"} {
        set catia_trans(entnam) ""
    }

    if [geoImpCatiaCMS query "SHOW" " CATIA Input "] {
        set catia_trans(show) "+show"
    } else { set  catia_trans(show) "-show" }
    if [geoImpCatiaCMS query "NOSHOW" " CATIA Input "] {
        set catia_trans(noshow) "+noshow"
    } else { set  catia_trans(noshow) "-noshow" }
    if [geoImpCatiaCMS query "PICK" " CATIA Input "] {
        set catia_trans(pick) "+pick"
    } else { set  catia_trans(pick) "-pick" }
    if [geoImpCatiaCMS query "NOPICK" " CATIA Input "] {
        set catia_trans(nopick) "+nopick"
    } else { set  catia_trans(nopick) "-nopick" }
    
    if [geoImpCatiaCMS query "No Points" " File Output "] {
        set catia_trans(npnts) "--suppress-points"
    } else { set  catia_trans(npnts) "" }
    if [geoImpCatiaCMS query "No Curves" " File Output "] {
        set catia_trans(ncrvs) "--suppress-curves"
    } else { set  catia_trans(ncrvs) "" }
    if [geoImpCatiaCMS query "No Domains" " File Output "] {
        set catia_trans(nsrfs) "--suppress-domains"
    } else { set  catia_trans(nsrfs) "" }
    if [geoImpCatiaCMS query "No Surfaces" " File Output "] {
        set catia_trans(nsrfs) "--suppress-surfaces"
    } else { set  catia_trans(nsrfs) "" }

    busy 1
    trans_catia_tetin_doit

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportCatiaDEZCancel {} {
    if {[trace vinfo geoImportCatiaLayGGVal] != ""} {
        trace vdelete geoImportCatiaLayGGVal w aie_geo_trace_import_catia_filter
    }    
}

proc ongeoImportGemsDEZLoad {} {
    global file_name gems_trans is_win env

    set def gems.surf
    set gems_trans(file) [med_file_select gems 0 $def]

    set tetin_name $file_name(mesh_dir)/gems.tin
    set gttol 0.001
    set gtrel 1
}

proc ongeoImportGemsDEZAfterLoad {} {
    global gems_trans
    if {$gems_trans(file) == ""} {
        geoImportGemsDEZ unload
    }
}

proc ongeoImportGemsDEZOK {} {
    global gems_trans

    foreach var {tetin_name gttol gtrel} {
        set gems_trans($var) [set $var]
    }
    
    
    busy 1
    trans_gems_tetin_doit

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportStepDEZLoad {} {
    global file_name step_trans is_win env
    
    if {$is_win && ![file exists "$env(ICEM_ACN)/dif/iges/igestotin.exe"]} {
        mess "\nNote that the executable \"igestotin\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/dif/iges\"\n" blue
        if ![file exists "$env(ICEM_ACN)/dif/step/steptotin.exe"] {
            mess "Note that the executable \"steptotin\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/dif/step\"\n" blue
        }
        set step_trans(file) ""
        return
    }
    set step_trans(file) [med_file_select step 0 "" "" 1]
    if {$step_trans(file) == ""} return

    set tetinfile ""
    if {[llength $step_trans(file)] > 1} {
        set tetinfile [pwd]/Merged.tin
        mess "NOTE: More than one STEP/IGES file selected. Geometry files can\
              be merged after conversion.\n" blue
        ::AIEnv::GUIManagement::ToggleGrey geoImportStepPart 0
        ::AIEnv::GUIManagement::ToggleGrey geoImportStepMerge 0
        set step_part 1
        set step_merge 1
    } else {
        set tetinfile [file rootname [file tail $step_trans(file)]].tin
        ::AIEnv::GUIManagement::ToggleGrey geoImportStepPart 1
        ::AIEnv::GUIManagement::ToggleGrey geoImportStepMerge 1
        set step_part 0
        set step_merge 0
    }
    set step_ttol 0.001
    set step_faceted 0
    set step_version 0
    set use_healing 0
}

proc ongeoImportStepDEZAfterLoad {} {
    global step_trans

    if {$step_trans(file) == ""} {
        geoImportStepDEZ unload
    }
}

proc ongeoImportStepDEZOK {} {
    global step_trans

    set step_trans(step_toltype) 1    
    set step_trans(step_faceted) 0
    set step_trans(step_version) 0
    set step_trans(use_healing) 0

    foreach var {tetinfile step_faceted step_ttol step_part step_merge step_version use_healing} {
        set step_trans($var) [set $var]
    }
    set step_trans(step_abstol) $step_ttol

    if {$step_units != 0} {
	set step_trans(args) -nounits
    } else {
	set step_trans(args) ""
    }
    if $step_trans(use_healing) {
       append step_trans(args) " -heal"
    }

    busy 1
    if {[llength $step_trans(file)] > 1} {
        set all_part_names ""
        set step_files $step_trans(file)
        set tetin_files $step_trans(tetinfile)
        foreach f $step_files {
            set step_trans(file) $f
            set step_trans(tetinfile) "$file_name(mesh_dir)/[file rootname [file tail $f]].tin"
            trans_step_tetin_doit
            if $step_trans(step_part) {
                set new_name [file rootname [file tail $f]]
                set part [ic_geo_valid_name "$new_name" 1]
                foreach type {point curve surface} {
                    set names ""
                    foreach obj [ic_geo_get_objects $type ""] {
                        if {[lsearch "$all_part_names" [ic_geo_get_part $type $obj]] < 0} {
                            append names " $obj"
                        }
                    }
                    ic_geo_set_part $type "$names" "$part" 0
                }
                append all_part_names " $part"
            }
            mess "Converted geometry file $step_trans(tetinfile) created.\n"
            if !$step_trans(step_merge) {
                ic_unload_tetin 1
            }
        }
        set step_trans(file) $step_files
        set step_trans(tetinfile) $tetin_files
        if $step_trans(step_merge) {
            foreach f [ic_geo_list_families] {
                if [ic_geo_family_is_empty $f] {
                    ic_geo_delete_family $f
                }
            }
            if {[file exists $step_trans(tetinfile)]} {
	            set tmess "File $step_trans(tetinfile) exists - overwrite??"
                switch -- [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] {
                    1 {
		                set step_trans(tetinfile) [med_file_select tetin 1 $step_trans(tetinfile)]
		            }
                    -1 {
                        if [winfo exists .overwrite] { top_destroy .overwrite } 
                        busy 0
                        return
                    }
                }
            }
            if {$step_trans(tetinfile) != ""} {
                mess "Creating merged geometry file $step_trans(tetinfile).\n" blue
                ic_save_tetin $step_trans(tetinfile)
            }
        }
    } else {
	    set step_trans(file) [lindex $step_trans(file) 0]
        trans_step_tetin_doit
    }

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportIgesDEZLoad {} {
    global file_name iges_trans
    
    set def "part.igs"
    set iges_trans(file) [med_file_select iges 0 $def]
    if {$iges_trans(file) == ""} return

    set tetin_name $file_name(mesh_dir)/[file rootname \
            [file tail $iges_trans(file)]].tin

    set listing_file  $file_name(mesh_dir)/iges_listing
    set dir_file_user $file_name(mesh_dir)/user_dir_file
    set data_red      0
    set trans_dos     0
    set geoImportIgesListGGVal 0
}

proc ongeoImportIgesDEZOK {} {
    global iges_trans

    set iges_trans(icempt) 0
    switch $geoImportIgesFileGGVal {
        0 { set iges_trans(dir_file) default }
        1 { set iges_trans(dir_file) none }
        2 { set iges_trans(dir_file) catia }
        3 { set iges_trans(dir_file) user }
    }

    set iges_trans(listing) $geoImportIgesListGGVal

    foreach var {tetin_name listing_file dir_file_user data_red trans_dos} {
        set iges_trans($var) [set $var]
    }
        
    busy 1
    trans_iges_tetin_doit

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportProeDEZLoad {} {
    set [geoImportProeType get -key variable] 0
    geoImportProeDEZ set meth 0
}

proc ongeoImportProeDEZOK {} {
    global proe_trans is_win
 
    foreach var {type meth} {
        set proe_trans($var) [set $var]
    }
    

    busy 1
            trans_proe_tetin_action
   
 # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportUGDEZLoad {} {
    global env file_name ug_trans
        
    if ![info exists env(UGII_BASE_DIR)] {
        mess "The environment variable, UGII_BASE_DIR, is not defined.\n" red
        mess "The UG translator will not run unless Unigraphics is \n" red
        mess "installed and properly configured.\n" red
        return
    }
    
    set ug_dif_dir $env(ICEM_ACN)/dif/ug
    if ![file exists $ug_dif_dir] { 
        mess "UG Interface is not installed.\n" red 
        return 
    }
    
    set def "ug.prt"
    set ug_trans(file) [med_file_select ug 0 $def]
    if {$ug_trans(file) == ""} return

    set tetin_name $file_name(mesh_dir)/ug_import.tin
    set user_script ./user_script.tcl
    set geoImportUGFileGGVal 0
    set ug_usr_opts ""

}

proc ongeoImportUGDEZAfterLoad {} {
    global ug_trans

    if {$ug_trans(file) == ""} {
        geoImportUGDEZ unload
    }
}

proc ongeoImportUGDEZOK {} {
    global ug_trans

    switch $geoImportUGFileGGVal {
        0 { set ug_script_mode levels }
        1 { set ug_script_mode ug_default }
        2 { set ug_script_mode named }
        3 { set ug_script_mode user }
    }
    set ug_script ""
    if {$ug_script_mode == "user" } { set ug_script $user_script }

    busy 1
    ic_trans_ug_tetin $ug_trans(file) $tetin_name \
            $ug_script_mode $ug_script $ug_usr_opts

    file_check_load_tetin $tetin_name
    mess "Done.\n"

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc aie_mesh_import_from_result {source {source_label ""} {infile ""} {startup_args ""}} {
    global aie_backend file_name tcl_platform

    # setup aie_backend options
    set aie_backend(unstruct) 1
    set aie_backend(prec)  1
    set aie_backend(tstep) 0
    set aie_backend(args)  ""
    set aie_backend(create) 1
    # this could be more complicated
    if {[ic_uns_is_loaded] || [ic_str_is_loaded]} {
        set aie_backend(create) 0
    }
    set aie_backend(reader) [v3readers_get_reader_name $source]
    if { $startup_args != ""} {
        for {set i 0} {$i < [llength $startup_args]} {incr i} {
            set item [lindex $startup_args $i]
            if {$item == "startup_files"} {
                incr i
                set infile [lindex $startup_args $i]
            } else {
                if {$aie_backend(args) == ""} {
                    set aie_backend(args) $item
                } else {
                    set aie_backend(args) "$aie_backend(args) $item"
                }
            }
        }
    }
    if {$infile == ""} {
        set initialdir [pwd]
        if {$source == "cgns"} {
            set filetypes {{{CGNS files} *.cgns} {{All files} *}}
        } elseif {$source == "fluent"} {
            set filetypes {{{Fluent files} {*.cas *.msh}} {{All files} *}}
            set aie_backend(prec)  2
        } else {
            set filetypes {{{All files} *}}
        }
        set infile [tk_getOpenFile -title "Select the $source_label file." -parent . -initialdir $initialdir -filetypes $filetypes]
    }

    if {$infile == ""} {
        return 0
    }

    if {$tcl_platform(platform) == "windows"} {
        set infile [win_shortname $infile]
    }

    set aie_backend(files) $infile
    set status [import_reader_file $source $aie_backend(files) \
        $aie_backend(create) $aie_backend(prec) $aie_backend(tstep) \
        $aie_backend(args)]

    if {$status == ""} { return 0 }
    set fileinfo [lindex $status 0]
    set nunodes [lindex $fileinfo 0]
    set nsnodes [lindex $fileinfo 1]

    if {[ic_uns_is_loaded] && !$nunodes && $nsnodes} {
        ic_unload_uns_mesh
    }
    return 1
}

proc aie_mesh_check_reader_setup { {version aie} {res_dir ""} } {
    global env
    # If the setup scripts haven't been source already, source them
    if { [info commands aie_acu_dir_func] == "" } {
        if { $res_dir == "" } {
            set res_dir [file join $env(ICEM_ACN) icemcfd result-interfaces]
        }
        foreach f [glob -nocomplain -directory $res_dir *_format_${version}.tcl] {
            if ![file readable $f] continue
            uplevel \#0 source \"$f\"
        }
    }
}

proc aie_mesh_import {source {opt ""}} {
    global file_name tdv_default
    global AIEnvProduct aie_backend
    global backend_info options is_win 

    aie_clear_dez
    if ![ic_geo_is_loaded] ic_empty_tetin

    if ![file isdirectory $file_name(project_dir)] {
        set_project_dir .
    }
    set file_name(domains_dir) $file_name(project_dir)
    set file_name(mesh_dir) $file_name(project_dir)
    set from $file_name(project_dir)
    set to $file_name(project_dir)
    set merge 0

    if {$to == "." && $source == "cgns"} {
        if {[info exists options(temporary_directory)] && [file isdirectory $options(temporary_directory)]} {
            set to "$options(temporary_directory)"
        }
    }
    
    if $is_win {
        set to [win_shortname $to]
    }
    
    if {[ic_uns_is_loaded] || [ic_str_is_loaded]} {
       set numbcs 0
       if [ic_boco_is_loaded] {
          # Check if project is loaded (non-trivial bcs)
          foreach f [ic_boco_list_families] {
             incr numbcs [llength [ic_boco_get $f]]
          }
          if $numbcs {
             if {$opt == ""} {
                set opt [tk_dialog .unsOverwrite "Project Exists" "Project Already Exists" "" 1 "Replace" "Keep" "Cancel" ]
             }
             switch -- $opt {
	            -1 { 
	                if [winfo exists .unsOverwrite] { top_destroy .unsOverwrite }
	                return 0
	            }
                0 { aie_closeProject 0; mess "   --- Closing Project\n" }
                1 { }
                2 { return 0}
             }
          }             
       } 
       if {$numbcs == 0} {
          if [ic_str_is_loaded] {
             if {$opt == ""} {
                set opt [tk_dialog .unsOverwrite "Mesh Exists" "This will unload the existing structured mesh; \
                   Do you want to continue loading?" "" 1 "Yes" "No" ]
                switch -- $opt {
                    -1 { 
	                    if [winfo exists .unsOverwrite] { top_destroy .unsOverwrite }
	                    return 0
	               }
                   0 { ic_unload_mesh }
                   1 { return 0}
                 }
             }
          }
          if [ic_uns_is_loaded] {
             if {$opt == ""} {
                set opt [tk_dialog .unsOverwrite "Mesh Exists" "Mesh Already Exists" "" 1 "Merge" "Replace" "Cancel"]
             }
             switch -- $opt {
	            -1 { 
	                if [winfo exists .unsOverwrite] { top_destroy .unsOverwrite }
	                return 0
	            }
	            0 { set merge 1 }
                1 { aie_close_Mesh 0; mess "   --- Unloading existing mesh\n" }
                2 { return 0}
             }
          }
       }
    }

    set unstruct 1
    aie_mesh_check_reader_setup
    switch $source {
        acusolve {
            global acu acu_user_stream_data
            set backend_info(acusolve) { AcuSolve readacusolve acu_user_stream_data }
            array set acu_user_stream_data {
                title Acusolve
                position;x position;x
                position;y position;y
                position;z position;z
                time time
            }
            array set acu {
                prb ""
                run 0
                dir ""
                root_dir ""
                prblist ""
                runlist ""
                did_set_prb_list 0
                did_set_run_list 0
                prbWidget ""
                runWidget  ""
            }
            set infile [file_select "Select the Acusolve directory." $from "" 1 0 aie_acu_ok_dir_func "" "" "" aie_acu_dir_func]
            if {$infile == ""} return
            set sup [aie_setup_acusolve]
            if {$sup == ""} return
            if ![aie_mesh_import_from_result acusolve Acusolve "" $sup] {
                return
            }
            set domains ""
        }
        abaqus {
            aie_import_abaqus
            return
        }
        ansys {
            set backend_info(ansys) { "Ansys" readansys ansysinfo }
            set infile [med_file_select ansys 0]
            if {$infile == ""} return

            set ext [file extension $infile]
            if { $ext == ".rst" || $ext == ".rfl" || $ext == ".rth" || $ext == ".rmg" } {
                #Import mesh from result file
                if ![aie_mesh_import_from_result ansys Ansys $infile] {
                    return
                }
                set domains ""
            } else {
                if {$AIEnvProduct == "fea2.0"} {
                    aie_do_import_ansys $infile
                    aie_after_open_mesh 1 1
                    return
                } else {
                    set files tmpdomain
                    set domain $to/$files

                    if {[geo_import_ansys \"$infile\" \"$domain\"] == ""} return
                    set domains [list "${domain}.uns"]
                }
            }
        }
        cart3d {
            aie_cart3d_load_mesh
            return
        }
        cfdxx {
            set files tmpdomain.uns
            set domain $to/$files
            set infile [med_file_select cfdxx 0]
            if {$infile == ""} return
            set domains [list $infile]

        }
        cfx4 {
            set backend_info(cfx4) { CFX4 readcfx cfx4info }
            if ![aie_mesh_import_from_result cfx4 CFX4] {
                return
            }
            set domains ""
        }
        cfx5 {
            set files tmpdomain.uns
            set domain $to/$files
            set infile [med_file_select cfx5 0]
            if {$infile == ""} return
            set outfile [geo_import_cfx5 $infile $domain]
            if {$outfile == ""} return
            set domains [list [lindex $outfile 1]]
        }
        cgns {
            set domfile "$to/___tmpdomain[pid]___.uns"
            set backend_info(cgns) {"CGNS" readcgns cgnsinfo}

            set cgns_arg "dom $domfile"
            if ![aie_mesh_import_from_result cgns CGNS "" $cgns_arg] {
                return
            }
            set domains ""
            if [file exists $domfile] {
                if [ic_uns_is_loaded] { ic_unload_mesh 1 }
                set domains [list $domfile]
            }
        }
        fast {
            set backend_info(fast) { Fast readfast fastinfo }
            if ![aie_mesh_import_from_result fast "Fast"] {
                return
            }
            set domains ""
        }
        fenfloss {
            set backend_info(fenfloss) { Fenfloss readfenfloss fenfloss }
            if ![aie_mesh_import_from_result fenfloss "Fenfloss"] {
                return
            }
            set domains ""
        }
        fensap {
            set backend_info(fensap) { Fensap readfensap fensapinfo }
            if ![aie_mesh_import_from_result fensap "Fensap"] {
                return
            }
            set domains ""
        }
        fidap {
            set backend_info(fidap) { Fidap readfidap fidapinfo }
            if ![aie_mesh_import_from_result fidap "Fidap"] {
                return
            }
            set domains ""
        }
        fieldview {
            set backend_info(fieldview) { Fieldview readfieldview fieldviewinfo }
            if ![aie_mesh_import_from_result fieldview "Fieldview"] {
                return
            }
            set domains ""
        }
        fluent {
            if {$merge && [ic_uns_is_loaded]} {
                set tempfile [cmd_tempfile fluent_temp .uns]
                ic_save_unstruct $tempfile 1 "" "" "" "" 0 1
                ic_unload_mesh 1
            }
            set backend_info(fluent) { Fluent readfluent fluentinfo }
            if ![aie_mesh_import_from_result fluent "Fluent cas"] {
                return
            }
            if {$merge && [ic_uns_is_loaded]} {
                ic_uns_load $tempfile 3 1
                if {$tempfile != ""} { cmd_rm $tempfile }
                ic_delete_empty_parts
            }
            set domains ""
        }
        lsdyna {
             aie_import_lsdyna_new
             return
        }
        nastran {
            aie_import_nastran_new
            return
        }
        patran {
            set files tmpdomain.uns
            set domain $to/$files

            set infile [med_file_select patran 0]
            if {$infile == ""} return
            if {[geo_import_patran \"$infile\" \"$domain\" 0] == ""} return
            set domains [list $domain]
        }
        plot3d {
            set files tmpdomain.uns
            set domain $to/$files
            set infile [med_file_select plot3d 0]
            if {$infile == ""} return
            set n [geo_import_plot3d [list $infile] [list $domain] 0 0]
            if {$n == ""} return
            switch [lindex $n 0] {
                1 { set unstruct 1 }
                2 { set unstruct 0 }
            }
            set domains [list [lindex $n 1]]
            if $unstruct {
                tdv_active 0
            }
            ic_geo_new_family PART0
            set file_name(domain) "" ;# this will be a tmp name anyway
            aie_afterStuffLoaded
            aie_clear_dez
            if {$domains == ""} return
        }
        sauna {
            set files tmpdomain.uns
            set domain $to/$files

            set tfile [file_select "Select the SAUNA topology file" $from]
            if {$tfile == ""} return
            set gfile [file_select "Select the SAUNA grid file" $from]
            if {$gfile == ""} return
            if {[geo_import_sauna $tfile $gfile $domain] == ""} return
        }
        starcd {
            set infile [med_file_select starcd 0]
            if {$infile == ""} return
            set ext [file extension $infile]
            if { $ext == ".inp" || $ext == ".cel" || $ext==".vert" ||  $ext == ".vrt" || $ext==".bnd" } {
                # Starcd input file
                set files tmpdomain.uns
                set domain $to/$files
                if { $ext == ".cel" || $ext==".vert" || $ext == ".vrt" || $ext==".bnd" } {
                    set root [file rootname $infile]
                    set cfile "${root}.cel"
                    set vfile "${root}.vert"
                    set bfile "${root}.bnd"
                    set ifile "${root}.inp"
                    if [file exists $ifile] {
                        if {[geo_import_starcd $domain $ifile] == ""} return
                    } else {
                        if ![file exists $cfile] return
                        if ![file exists $vfile] {
                            set vfile "${root}.vrt"
                            if ![file exists $vfile] return
                        }
                        if ![file exists $bfile] {
                            set bfile ""
                        }
                        if {[geo_import_starcd $domain "" $cfile $vfile $bfile] == ""} return
		            }
                } else {
                    if {[geo_import_starcd $domain $infile] == ""} return
                }
                set domains [list $domain]
            } else {
                set ln [string length $infile]
                set en [string range $infile [expr {$ln-2}] end]

                if { $en != "09" && $en != "16" && $en != "29" } {
                    #We don't support this case
                        set prostarmsg \
"\nStarCD 3.1 and greater result file formats are not supported\n\
\n\
Prostar can now export an ICEMCFD domain file.\n\
\n\
While in Prostar, please load the pst or pstt file, and from the:\n\
\n\
Star Guide > Navcenter > Export Data > Export Results\n\
\n\
select ICEM option.\n\
\n\
This will create create a casename domain file which is in \
ICEMCFD domain format and can be loaded in Visual 3 or MED."

                    mess "$prostarmsg\n"
                    tk_messageBox -message $prostarmsg
                    return
                }
                global env
                set backend_info(starcd) { Starcd readstarcd starcdinfo}
                
                set env(FORT_CONVERT16) BIG_ENDIAN
                set env(FORT_CONVERT29) BIG_ENDIAN
                set env(FORT_CONVERT9) BIG_ENDIAN

                if ![aie_mesh_import_from_result starcd "StarCD" $infile] {
                    return
                }
                set domains ""
            }
        }
        stl {
            set infiles [med_file_select stl 0 {} {} 1]
            if {$infiles == ""} return
            if {$opt == ""} {
                if [ic_uns_is_loaded] {
                    set opt [tk_dialog .unsOverwrite "Mesh Exists" "Mesh Already Exists" "" 1 "Merge" "Replace" "Cancel"]
                    switch -- $opt {
	                    -1 { 
	                        if [winfo exists .unsOverwrite] { top_destroy .unsOverwrite }
	                        return
	                    }
	                    0 { }
                        1 { aie_close_Mesh 0; mess "   --- Unloading existing mesh\n" }
                        2 { return }
                    }
                }
            }
            set n 0
            set domain1 ""
            foreach if $infiles {
                lappend domain1 $to/tmpdomain$n.uns
                incr n
            }
            set domains [lindex [geo_import_stl $infiles $domain1] 1]
            if {$domains == ""} return
        }
        tascflow {
            set backend_info(tascflow) { TascFlow readtascflow tascfinfo}
            if ![aie_mesh_import_from_result tascflow "Tascflow GRD"] {
                return
            }
            set domains ""
        }
        tecplot {
            set backend_info(tecplot) { Tecplot readtecplot tecplotinfo}
            if ![aie_mesh_import_from_result tecplot "Tecplot"] {
                return
            }
            set domains ""
        }
        ugrid {
            set files tmpdomain.uns
            set domain $to/$files

            set infile [med_file_select ugrid 0]
            if {$infile == ""} return
            if {[geo_import_ugrid \"$infile\" \"$domain\"] == ""} return
            set domains [list $domain]
        }
        uh3d {
            importUH3DMeshFileDEZ show
            return
        }
        vectis {
            set files tmpdomain.uns
            set domain $to/$files

            set infile [file_select "Select the Vectis file (.sdf)." $from]
            if {$infile == ""} return
            set args "-sdf $infile $domain"
            ic_run_application_exec . icemcfd/output-interfaces vectis2df $args
            if ![file exists $domain] { return "" }
            set domains $domain
        }
        vtk {
            if ![aie_mesh_import_from_result vtk "VTK"] {
                return
            }
            set domains ""
        }
        default {
            mess "Input mesh format $source not supported\n" red
            return
        }
    }

    if {$unstruct} {
	    set resetpart ""
	    if {$domains != "" && ([llength $domains] > 1) && \
            ![tk_dialog .question "Separate parts" "More than one mesh file has been loaded. \
                              Put each mesh into a separate part, and ignore any part \
                              information that might be loaded from the mesh files?" "" -1 Yes No]} {
	        set resetpart PART
	    }

	    if {$domains != ""} {
            ic_uns_load $domains 3 0 $resetpart
            ic_uns_families_changed
	    }
    }
    if [ic_str_is_loaded] {
        aie_afterStuffLoaded
        aie_initial_strmesh_visibility
        foreach type {subface edge vertex} {
            set nums ""
            foreach n [ic_str_list_entities $type] {
                set info [ic_str_get_topo_info $type $n 0]
                if {[lindex [lindex $info 0] 2] == ""} {
                    lappend nums $n
                }
            }
            if {$nums != ""} {
                ic_str_change_family $type $nums DEFAULT
            }
        }
        ::AIEnv::GUIManagement::Tree::Parts show
    } else {
        tdv_active 0
        if [ic_uns_is_loaded] {
            ic_geo_new_family PART0
            ic_uns_move_family_elements ORFN PART0
            set file_name(domain) "" ;# this will be a tmp name anyway
            aie_after_open_mesh 1 1
            ic_uns_families_changed
            if [ic_wb2_is_running] {
                if {$domains != ""} {
                    eval file delete -force -- $domains
                }
            } else {
                if {$domains != "" && $source != "cfdxx" && $source != "cfx5"} {
                    eval file delete -force -- $domains
                }
            }
        }
    }
}

proc aie_setup_acusolve { {args ""} } {
    global acu str_data is_win

    if {$args != ""} {
        return [list startup_files $args]
    }
    if {![info exist acu(dir)] || \
        ![info exist acu(prb)] || \
        ![info exist acu(run)]} {return ""}

    if {$acu(dir) == ""} {return ""}
    if {$acu(prb) == ""} {return ""}
    set str_data(userstream) 1
    if { [string first "\{" $acu(dir)] != -1 } {
        set acu(dir) [lindex $acu(dir) 0]
    }
    set acu(dir) [string trimright $acu(dir) "\\"]
    set acu(dir) [string trimright $acu(dir) "\/"]
    if $is_win {
       set acu(dir) [win_get_short_pathname $acu(dir)]
    } 
    cd [file join $acu(dir) ..]
    return [list startup_files "$acu(prb) $acu(run) $acu(dir) *" ]
}

proc acu_ok_dir_func { {dir ""} } {
    return [aie_acu_ok_dir_func $dir]
}

proc acu_dir_func { {dir ""} } {
    aie_acu_dir_func $dir
}

proc aie_acu_ok_dir_func { {dir ""} } {
    global form_entry_datatype
    global acu
    set d [file tail $dir]
    if { [string toupper $d] == "ACUSIM.DIR" } {
        set acu(dir) $dir
        acu_set_prb_list
        acu_set_run_list
        return 1
    } else {
        return 0
    }
}

proc aie_acu_dir_func { {dir ""} } {
   global acu

   set acu(dir) $dir
   acu_set_prb_list
   acu_set_run_list
}

proc acu_prb_list {} {
    global acu
    acu_set_prb_list
    return [lsort $acu(prblist)]
}

proc acu_run_list {} {
    global acu
    acu_set_run_list
    return [lsort $acu(runlist)]
}

proc acu_set_prb_list {} {
    global acu

    set files [glob -nocomplain [file join $acu(dir) *.*.cli]]
    if {$files == ""} {
        set acu(prb) ""
        set acu(run) ""
        return
    }
    
    foreach file $files {
        if { [regexp {^(.+)/(.+)\.([0-9]+)\.cli$} $file a d p r] == 1 } {
            lappend pl($p) $r
        }
    }
    set acu(prblist) [lsort [array name pl]]
    set acu(prb) [lindex $acu(prblist) 0]
    set acu(runlist) [lsort -integer $pl($acu(prb))]
    set ind [expr [llength $acu(runlist)]-1]
    set acu(run) [lindex $acu(runlist) $ind]
}

proc acu_set_run_list {} {
    global acu
    
    set files [glob -nocomplain [file join $acu(dir) *.*.cli]]
    foreach file $files {
        if { [regexp {^(.+)/(.+)\.([0-9]+)\.cli$} $file a d p r] == 1 } {
            lappend rl($r) $r
        }
    }
    set acu(runlist) [lsort -integer [array name rl]]
    set ind [expr [llength $acu(runlist)]-1]
    set acu(run) [lindex $acu(runlist) $ind]
}

proc ongeoExportIgesDEZLoad {} {
    global file_name iges_trans
    
    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname \
            [file tail $tetin]].igs
    set iges_trans(file) [med_file_select iges 1 $def]
    if {$iges_trans(file) == ""} return

    set geoExportIgesFileGGVal 0
    set geoExportIgesListGGVal 0
}

proc ongeoExportIgesDEZOK {} {
    global iges_trans file_name

    busy 1
    ic_trans_tetin_step $file_name(tetin) $iges_trans(file)
    mess "Done.\n"
    busy 0
}

proc geoExportIgesDoit {} {
    global file_name iges_trans is_win env

    if {$is_win && ![file exists "$env(ICEM_ACN)/dif/iges/igestotin.exe"]} {
        mess "\nNote that the executables \"dif/iges/igestotin.exe\" and \"dif/step/steptotin.exe\" need to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)\"\n" blue
        return 
    }

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname [file tail $tetin]].igs
    set iges_trans(file) [med_file_select iges 1 $def]
    if {$iges_trans(file) == ""} return

    busy 1
    ic_trans_tetin_step $file_name(tetin) $iges_trans(file)
    mess "Done.\n"
    busy 0
}

proc geoExportDDNDoit {} {
    global file_name

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(parts_dir)/[file rootname \
            [file tail $tetin]].ipa
    set ddn_trans(file) [med_file_select ddn 1 $def]
    if {$ddn_trans(file) == ""} return

    busy 1
    ic_trans_tetin_ddn $file_name(tetin) $ddn_trans(file)
    mess "Done.\n"
    busy 0
}

proc geoExportPsDoit {} {
    global file_name env is_win

    if {$is_win && ![file exists "$env(ICEM_ACN)/bin/tintops.exe"]} {
        mess "\nNote that the executable \"tintops.exe\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/bin\"\n" blue
        return 
    }

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname [file tail $tetin]].x_t
    set ps_trans(file) [med_file_select ps 1 $def]
    if {$ps_trans(file) == ""} return

    busy 1
    ic_trans_tetin_ps $file_name(tetin) $ps_trans(file)
    mess "Done.\n"
    busy 0
}

proc geoExportStepDoit {} {
    global file_name

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname \
            [file tail $tetin]].stp
    set step_file [med_file_select step 1 $def]
    if {$step_file == ""} return

    busy 1
    ic_trans_tetin_step $file_name(tetin) $step_file
    mess "Done.\n"
    busy 0
}

proc geoExportSTLDoit {} {
    global file_name env is_win

    set use_current 0
    if [ic_geo_is_loaded] {
	    set opt [tk_dialog .tetinx "Geometry Exists" "Geometry already exists: how do you wish to proceed?" "" 1 "Close" "Export current" "Cancel"]
        switch -- $opt {
            -1 { 
                if [winfo exists .tetinx] { top_destroy .tetinx }
                return
            }
	        0 {
	            aie_close_geom 0
                mess "   --- Unloading existing geometry\n"
	        }
	        1 {
	            set use_current 1
	            mess "Using current loaded geometry for STL export\n"
	        }
	        2 { return }
        }
    }
    if [ic_uns_is_loaded] {
        set opt [tk_dialog .meshx "Mesh Exists" "Warning, this operation must remove the existing mesh before it can proceed?" "" 1 "Continue" "Cancel" ]
        switch -- $opt {
            -1 { 
                if [winfo exists .meshx] { top_destroy .meshx }
                return
            }
            0 {
                aie_close_Mesh
                mess "   --- Unloading existing mesh\n"
            }
            1 { return }
        }
    }
    
    if $use_current {
        if {$file_name(tetin) != ""} {
            set reload_tetin $file_name(tetin)
            set tetin [cmd_tempfile to_stl\_temp .tin]    
        } else {
            set tetin [aie_save_geom 1]
            if {$tetin != ""} {
                set reload_tetin $file_name(tetin)
                set tetin [cmd_tempfile to_stl\_temp .tin]
            } else {
                return
            }
       }
    } else {
        set tetin $file_name(tetin)
        set tetin [med_file_select tetin 0 $tetin]
        if {$tetin == ""} return
        set file_name(tetin) $tetin
	    if [catch {ic_load_tetin [list $file_name(tetin)]} res] {
	        mess "$res\n" red
	        return
	    }
    }
    set stl $file_name(project_dir)/[file rootname [file tail $tetin]].stl
    set stl_trans(file) [med_file_select stl 1 $stl]
    if {$stl_trans(file) == ""} return
    
    set file_name(tetin) $tetin
    set ascii 0
    if {![dialog .stl "STL export options" "STL file options" {
        {use_original -opt {{"Ascii" 1} {"Binary" 0}} -title "File format"}
        }]} { return "" }
    if $use_original { set ascii 1 } else { set ascii 0 }

    busy 1
    if $use_current { aie_save_geom  }
    mess "Creating STL file $stl_trans(file)...\n" blue
    set surfaces [ic_geo_get_objects surface]
    if {$surfaces == ""} {
        mess "Error: Geometry file contains no surfaces.\n" red
        busy 0
        return
    }
    foreach obj $surfaces {
        set gtype [ic_geo_stats surface $obj]
        if {[lindex [split $gtype ,] 0] == "mesh"} continue
        if {[ic_facetize_geometry surface $obj] == ""} {
            busy 0
            return
        }
    }
    ic_geo_export_to_mesh 1 "" 1
    aie_close_geom 0
    set fm __tmp_stl_export_mesh[pid].uns
    ic_save_unstruct $fm 1 "" "" ""  
    aie_close_Mesh
    if $ascii {
        if $is_win {
            ic_run_application_exec . icemcfd/output-interfaces stl "$fm \"$stl_trans(file)\" ascii no_shift"
        } else {
            ic_run_application_exec . icemcfd/output-interfaces stl "$fm $stl_trans(file) ascii no_shift"
        }
    } else {
        if $is_win {
            ic_run_application_exec . icemcfd/output-interfaces stl "$fm \"$stl_trans(file)\" no_shift"
        } else {
            ic_run_application_exec . icemcfd/output-interfaces stl "$fm $stl_trans(file) no_shift"
        }
    }
    ic_rm $fm
    if $use_current {
        set file_name(tetin) $reload_tetin
	    if [catch {ic_load_tetin [list $file_name(tetin)]} res] {
	        mess "$res\n" red
	        return
	    }
	    aie_afterStuffLoaded 0
	    aie_initGeoVis
	    aie_update_icons geom
        aie_init_geom_subsets
        aie_create_parts_in_tree_if_needed 
    }
    mess "Done. STL file saved as $stl_trans(file)\n" blue
    busy 0
}

proc geoExportIgesDEZDoit {} {
    global file_name iges_trans

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname \
            [file tail $tetin]].igs
    set iges_trans(file) [med_file_select iges 1 $def]
    if {$iges_trans(file) == ""} return

    busy 1
    ic_trans_tetin_step $file_name(tetin) $iges_trans(file)
    mess "Done.\n"
    busy 0
}

proc geoExportStepDEZDoit {} {
    global file_name

    set tetin $file_name(tetin)
    set tetin [med_file_select tetin 0 $tetin]
    if {$tetin == ""} return
    set file_name(tetin) $tetin

    set def $file_name(project_dir)/[file rootname \
            [file tail $tetin]].stp
    set step_file [med_file_select step 1 $def]
    if {$step_file == ""} return

    busy 1
    ic_trans_tetin_step $file_name(tetin) $step_file
    mess "Done.\n"
    busy 0
}

proc aie_nastran_file_export_doit {} {

     if {[info proc aie_nastran_file_export] == ""} {
        mess "You need to choose a FEA-Version via \"Settings->Product\" to run \"File->Export Mesh->To Nastran\"\n" red
        return
     }
     aie_nastran_file_export
}

proc aie_import_nastran_new {} {

   importNastranDEZ show
}

proc onimportNastranDEZLoad {} {
    global  env file_name

    set nas_in_file ""

    set rbe3 "Unchanged"
    importNastranDEZ set rbe3List {
        {Unchanged}
        {Ignore all weights}
          {Group in one part}
    }
    set bars "Unchanged"
    importNastranDEZ set barsList {
        {Unchanged}
        {Group in one part}
    }
    set shells "Unchanged"
    importNastranDEZ set shellsList {
        {Unchanged}
        {Group in one part}
    }

}

proc onimportNastranDEZApply { } {
    global file_name env bindir tcl_platform

    if {$nas_in_file == ""} {set nas_in_file [importNastranDEZ get nas_in_file]}
    if {$rbe3 == ""} {set rbe3 [importNastranDEZ get rbe3]}
    if {$bars == ""} {set bars [importNastranDEZ get bars]}

    if {$nas_in_file == ""} {
        mess "error: NO input file selected\n" red
        return
    }

    set do_load 1

    switch $rbe3 {
        "Unchanged" { set rbe3_option 0 }
        "Ignore all weights" { set rbe3_option 1 }
        "Group in one part" { set rbe3_option 2 }
    }
    switch $bars {
        "Unchanged" { set bar_option 0 }
        "Group in one part" { set bar_option 2 }
    }
    switch $shells {
         "Unchanged" { set shell_option 0 }
        "Group in one part" { set shell_option 2 }
    }
    aie_import_nastran $nas_in_file 1 0 $rbe3_option $bar_option $shell_option

#    mess "Translating the Nastran Input File.  Please Wait....\n"

#    set conv $env(ICEM_ACN)/icemcfd/output-interfaces/IcedNastran
#    regsub -all \\\\ $conv / conv

#    if {![file exists $conv] && ![file exists $conv.exe]} {
#        set conv $bindir/IcedNastran
#         regsub -all \\\\ $conv / conv
#    }

#    # revert back to copying the nastrn file to the working directory.  Nastran file could be in read only directory.
#    #set inputDir [file dirname $nas_in_file]
#    #if {$inputDir != ""} {ic_chdir $inputDir}

#    set pname [aie_unused_project_name]
#    file copy $nas_in_file $pname.dat
#    set file $pname.dat

#    if { $tcl_platform(platform) == "windows" } {
#        regsub -all / $file \\\\\\\\ nasdat_file_new
#         regsub -all / $conv \\\\\\\\ conv_new
#         set  nas_cmd "\"$conv\" \"$nasdat_file_new\" $rbe3_option $bar_option $shell_option"
#    } else {
#         set  nas_cmd "$conv $file $rbe3_option $bar_option $shell_option"
#    }
#
#    mess "$nas_cmd\n"
#
#    if [catch {eval exec $nas_cmd} pid] {
#        mess "Error: $pid\n" red
#    } else {
#         mess "$pid\n" blue
#    }
#
#    #mess "$conv $file\n"
#    #busy 1
#    #set data [ic_import_nastran $conv $args]
#    #mess $data blue
#
#    if $do_load {
#        set file_name(domain) [file rootname $file].uns
#        aie_open_Mesh_new
#
#          set file_name(solver_params) [file rootname $file].par
#        set file_name(solver_params_loaded) 1
#        aie_open_params 0 0
#
#        set file_name(family_boco) [file rootname $file].fbc
#        set file_name(family_boco_loaded) 1
#        aie_open_attr 0 0
#
#        set file_name(settings) [file rootname $file].prj
#        aie_afterStuffLoaded
#        aie_initial_unsmesh_visibility
#    }

}

proc onimportLsdynaDEZLoad {} {
    set lsdyna_in_file ""

	set lsdynaIncFlagList {Data Links}
	if {$lsdynaIncFlag==""} {
		set lsdynaIncFlag Data
	}
}

proc onimportLsdynaDEZApply { } {
    if {$lsdyna_in_file == ""} {
        mess "error: Please select keyword file.\n" red
        return
    }

    set do_load 1

    set include 0
	if {$lsdynaIncFlag=="Links"} {set include 1}
	
    aie_import_lsdyna $lsdyna_in_file $do_load $include
}

proc onimportUH3DMeshFileDEZLoad {} {
    global uh3dinfo

    array set uh3dinfo {
        grd        ""
        rso        ""
        asBendian  1
        dir_loc    ""
    }
    set uh3dinfo_grd ""
    set uh3dinfo_rso ""
    set uh3dinfo(bendian) 1
}

proc onimportUH3DMeshFileDEZApply { } {
    global uh3dinfo backend_info
    
    set backend_info(uh3d) {Uh3d readuh3d uh3dinfo}
    if {$uh3dinfo_grd == ""} {set uh3dinfo_grd [importUH3DMeshFileDEZ get uh3dinfo_grd]}
    if {$uh3dinfo_rso == ""} {set uh3dinfo_rso [importUH3DMeshFileDEZ get uh3dinfo_rso]}
    set uh3dinfo(grd) $uh3dinfo_grd
    set uh3dinfo(rso) $uh3dinfo_rso
    set sup [aie_setup_uh3d]
    if {$sup == ""} return
    if ![aie_mesh_import_from_result uh3d UH3D "" $sup] {
        return
    }
    ic_geo_new_family PART0
    ic_uns_move_family_elements ORFN PART0
    set file_name(domain) "" ;# this will be a tmp name anyway
    aie_after_open_mesh 1 1
}

proc aie_setup_uh3d { {args ""} } {
    global uh3dinfo
    if {$args != ""} {
        return [list startup_files $args]
    }
    if {$uh3dinfo(grd) == "" || $uh3dinfo(rso) == "" } {
        # Returning a blank means "cancel".
        return ""
    }
    return [list startup_files "$uh3dinfo(grd) $uh3dinfo(rso)" \
                     backend_option $uh3dinfo(asBendian)]
}

proc ongeoImportBGPDEZLoad {} {
    global bgplus_trans

    set bgplus_trans(file) [med_file_select bgplus 0]
    if {$bgplus_trans(file) == ""} return

    set tetinfile [file rootname $bgplus_trans(file)].tin
    set unsfile [file rootname $bgplus_trans(file)].uns

    set geoImportBGPtinGGVal 1
    ::AIEnv::GUIManagement::ToggleGrey geoImportBGPtinGG 0
    if {$sources == ""} {set sources "density"}
}

proc ongeoImportBGPDEZAfterLoad {} {
    global bgplus_trans
    if {$bgplus_trans(file) == ""} {
        geoImportBGPDEZ unload
    }
}

proc ongeoImportBGPDEZOK {} {
    global env is_win bgplus_trans file_name

    if {!$geoImportBGPtinGGVal && !$geoImportBGPunsGGVal} return
    if {![info exists env(BGP2TIN_BIN)] || $env(BGP2TIN_BIN) == ""} {
        set prog bgp2tin
    } elseif {$is_win} {
        set prog [join [split $env(BGP2TIN_BIN) \\] /]
    } else {
        set prog $env(BGP2TIN_BIN)
    }
    if {![file exists $prog] && [catch {get_program bin $prog} prog]} {
        mess "Error: can't find bgp2tin program\n" red
        return
    }
    set cmd [list $prog]

    set args "-v"
    if {$geoImportBGPtinGGVal} {
        append args t
        if {$join} {append args j}
        if {$family} {append args o}
        if {$sources == "spacing"} {append args S}
        if {$sources == "density"} {append args D}
        if {$LEgap != ""} {
            if [catch {expr double($LEgap)} gap] {
                mess "Error: invalid value for LE Gap\n" red
                return
            }
            append args g$gap
            if {$TEgap != ""} {
                if [catch {expr double($TEgap)} gap] {
                    mess "Error: invalid value for TE Gap\n" red
                    return
                }
                append args ,$gap
            }
        } else {
            if {$TEgap != ""} {
                if [catch {expr double($TEgap)} gap] {
                    mess "Error: invalid value for TE Gap\n" red
                    return
                }
                append args g$gap
            }
        }
    }
    lappend cmd $args
    if {$geoImportBGPunsGGVal} {lappend cmd -d}
    lappend cmd $bgplus_trans(file)
    if {$geoImportBGPtinGGVal} {lappend cmd $tetinfile}
    if {$geoImportBGPunsGGVal} {lappend cmd $unsfile}

    busy 1
    mess "Running $cmd\n"  
    if {[catch {eval ic_exec $cmd} out]} {
        mess "Error: $out\n" red
        busy 0
        return
    } else {
        mess "$out\n" blue
        mess "Done import\n"
    }
    
    if {$geoImportBGPtinGGVal && [file exists $tetinfile]} {
        file_check_load_tetin "$tetinfile"
        aie_initGeoVis
    }
    if {$geoImportBGPunsGGVal && [file exists $unsfile]} {
        set file_name(domain) $unsfile
        aie_open_Mesh 0 ""
    }
    aie_afterStuffLoaded
    busy 0
}

proc ongeoImport3DMDEZLoad {} {
    global rhino_trans

    set rhino_trans(3dmfile) [med_file_select 3dm 0]
    if {$rhino_trans(3dmfile) == ""} return

    set tetinfile [file rootname $rhino_trans(3dmfile)].tin
}

proc ongeoImport3DMDEZAfterLoad {} {
    global rhino_trans

    if {$rhino_trans(3dmfile) == ""} {
        geoImport3DMDEZ unload
    }
}

proc ongeoImport3DMDEZOK {} {
    global env is_win rhino_trans

    if {![info exists env(RHINO2TIN_BIN)] || $env(RHINO2TIN_BIN) == ""} {
        set prog 3dm2tin
    } elseif {$is_win} {
        set prog [join [split $env(RHINO2TIN_BIN) \\] /]
    } else {
        set prog $env(RHINO2TIN_BIN)
    }
    if {![file exists $prog] && [catch {get_program bin $prog} prog]} {
        mess "Error: can't find 3dm2tin program\n" red
        return
    }

    if [file exists $tetinfile] {
	    set opt [tk_dialog .overwrite "Geometry File Exists" "Geometry File\n$tetinfile\nAlready Exists" "" 1 "Replace" "Cancel"]
	    switch -- $opt {
	        -1 { if [winfo exists .overwrite] { destroy .overwrite; return } }
	        0 {}
	        1 { return }
	    }
    }

    if [ic_geo_is_loaded] {
        mess "   --- Unloading existing geometry\n"
        ic_unload_tetin
    }

    set args "-a"
    if {$geoImport3DMgeomGGVal} {
        if {$points} { append args p }
        if {$curves} { append args c }
        if {$surfaces} { append args s }
        if {$breps} { append args b }
        if {$meshes} { append args m }
    }
    if {$names} { append args n }
    if {$hidden} { append args h }
    if {$divide} { append args d }
    if {$extract} { append args e }
    
    set cmd [list $prog $args $rhino_trans(3dmfile) $tetinfile]
    busy 1
    mess "Running $cmd\n"  
    if {[catch {eval ic_exec $cmd} out]} {
        mess "Error: $out\n" red
        busy 0
        return
    } else {
        mess "$out\n" blue
        mess "Done import\n"
    }
    mess "Loading $tetinfile\n"
    ic_load_tetin "$tetinfile"
    aie_initGeoVis
    aie_afterStuffLoaded
    busy 0
}

proc ongeoExport3DMDEZLoad {} {
    global rhino_trans file_name

    set rhino_trans(tinfile) [med_file_select tetin 0 $file_name(tetin)]
    if {$rhino_trans(tinfile) == ""} return

    set file3dm [file rootname $rhino_trans(tinfile)].3dm
    if {![info exists units] || $units == ""} {
        set unitList {
            microns millimeters centimeters meters kilometers
            microinches mils inches feet miles
        }
    }
    set units [ic_geo_get_units 1]
    if {$units == ""} { set units meters }
    set tolerance [expr 0.1 * [aie_default_topo_tolerance]]
}

proc ongeoExport3DMDEZAfterLoad {} {
    global rhino_trans

    if {$rhino_trans(tinfile) == ""} {
        geoExport3DMDEZ unload
    }
}

proc ongeoExport3DMDEZOK {} {
    global env is_win rhino_trans

    if {![info exists env(TIN2RHINO_BIN)] || $env(TIN2RHINO_BIN) == ""} {
        set prog tin23dm
    } elseif {$is_win} {
        set prog [join [split $env(TIN2RHINO_BIN) \\] /]
    } else {
        set prog $env(TIN2RHINO_BIN)
    }
    if {![file exists $prog] && [catch {get_program bin $prog} prog]} {
        mess "Error: can't find tin23dm program\n" red
        return
    }
    
    if [file exists $file3dm] {
	    set opt [tk_dialog .overwrite "Rhino 3DM File Exists" "Rhino 3DM File\n$file3dm\nAlready Exists" "" 1 "Replace" "Cancel"]
	    switch -- $opt {
	        -1 { if [winfo exists .overwrite] { destroy .overwrite; return } }
	        0 {}
	        1 { return }
	    }
    }

    set args "-a"
    if {$geoExport3DMgeomGGVal} {
        if {$points} { append args p }
        if {$curves} { append args c }
        if {$surfaces} { append args s }
        if {$meshes} { append args m }
    }
    if {$dormant} { append args i }
    if {$notrim} { append args d }
    if {$rebuild} { append args r }
    if {$version2} { append args 2 }

    set cmd [list $prog $args]
    if {$units != ""} { lappend cmd -u$units }
    if {$tolerance != ""} { lappend cmd -t$tolerance }

    if $is_win {
        set rhino_trans(tinfile) [win_shortname $rhino_trans(tinfile)]
    }

    if [ic_geo_is_loaded] {
        mess "   --- Unloading existing geometry\n"
        ic_unload_tetin
    }

    mess "Loading $rhino_trans(tinfile)\n"
    ic_load_tetin $rhino_trans(tinfile)
    mess "Converting geometry file $rhino_trans(tinfile) to geometry file version 10\n"
    set version10_file [file rootname $rhino_trans(tinfile)].version10.[pid].tin
    ic_save_tetin $version10_file 0 0 {} {} 1 0 1

    set orig_file $rhino_trans(tinfile)
    set rhino_trans(tinfile) $version10_file

    lappend cmd $rhino_trans(tinfile) $file3dm
    busy 1
    mess "Running $cmd\n"  
    if {[catch {eval ic_exec $cmd} out]} {
        mess "Error: $out\n" red
        ic_rm $version10_file
        busy 0
        return
    } else {
        mess "$out\n" blue
        mess "Done export\n"
    }
    ic_rm $version10_file
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportINPUTDEZLoad {} {
    if {$INPUTfile == ""} {
	    set ppart PNTS
	    set cpart CRVS
	    set spart SURFS
	    set ppartp pnt
	    set cpartp crv
	    set spartp srf
	    set tol 0.001
	    set format 0
	    set points 1
        set curves 1
	    set surfaces 1
    }
}

proc ongeoImportINPUTDEZAfterLoad {} {
    set INPUTfile [med_file_select "" 0]
}

proc ongeoImportINPUTDEZOK {} {
    if $format {
	set how plot3d
    } else {
	set how input
    }
    ic_mess "Loading data file $INPUTfile\n"
    set pnam $ppartp
    if !$points   { set pnam "" }
    set cnam $cpartp
    if !$curves   { set cnam "" }
    set snam $spartp
    if !$surfaces { set snam "" }
    # Fix for onyx 54127
    if [ic_geo_is_loaded] {
       set opt [tk_dialog .tetinOverwrite "Geometry Exists" "Geometry Already Exists" "" 1 "Merge" "Replace" "Cancel"]
       switch -- $opt {
            -1 { 
                if [winfo exists .tetinOverwrite] { top_destroy .tetinOverwrite }
                return
            }
            0 { }
            1 {
                aie_close_geom 0
                mess "   --- Unloading existing geometry\n"
            }
            2 { return }
       }
    }
    ic_geo_cre_geom_input $INPUTfile $tol $how $ppart $pnam $cpart $cnam $spart $snam
    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    ic_mess "Done\n"
}
# ---------------------------------------

# Here is some stuff to support the "legacy" output interfaces

proc command_set_text {args} {
    # not relevant for ai_env
}

proc set_title {args} {
    # not relevant for ai_env
}

proc aie_wb_load_import_wb_readers_gui {} {
    geoImportWBDEZ unload
    geoImportWBDEZ show
}

proc onsetImportModelOptionsDEZLoad {} {
    global import_model_options
    
    set from_source $import_model_options(from_source) 
    set always_convert $import_model_options(always_convert) 
    set always_ref_key $import_model_options(always_ref_key) 
    set convert_to $import_model_options(convert_to) 
    set named_sel_only $import_model_options(named_sel_only) 
    set always_import $import_model_options(always_import) 
    set always_create_mpoint $import_model_options(always_create_mpoint) 
    set always_import_blocking $import_model_options(always_import_blocking) 
}

proc onsetImportModelOptionsDEZAfterLoad {} {
    global import_model_options
    
    set ConvertToUnitList [list "Nanometers" "Micrometers" "Millimeters" "Centimeters" "Meters" "Kilometers" "Microinches" "Mils (thousands)" "Inches" "Feet" "Miles"]
    if !$import_model_options(always_convert) {
        set import_model_options(convert_to) ""
        ::AIEnv::GUIManagement::ToggleGrey importModelConvertUnit 1
    } else {
        ::AIEnv::GUIManagement::ToggleGrey importModelConvertUnit 0
    }
}

proc onsetImportModelOptionsDEZApply {} {
    global import_model_options beta_mode

    set import_model_options(from_source) $from_source
    set import_model_options(always_convert) $always_convert 
    set import_model_options(always_ref_key) $always_ref_key
    set import_model_options(convert_to) $convert_to
    set import_model_options(named_sel_only) $named_sel_only
    set import_model_options(always_import) $always_import
    set import_model_options(always_create_mpoint) $always_create_mpoint
    set import_model_options(always_import_blocking) $always_import_blocking
    if !$import_model_options(always_convert) {
        set import_model_options(convert_to) ""
    }
    if {$import_model_options(convert_to) == ""} {
        set import_model_options(always_convert) 0
        ::AIEnv::GUIManagement::ToggleGrey importModelConvertUnit 1
    }
    aie_set_options
}

proc ongeoImportWBDEZLoad {} {
    global env file_name wb_trans
  
    set def ""
    if {[info exists ::wb_trans(file)] && $::wb_trans(file) != ""} {
        set def $::wb_trans(file)
    }
    set ::wb_trans(file) [med_file_select "Import Model" 0 $def]
    if {$::wb_trans(file) == ""} return
    if {[file extension $::wb_trans(file)] == ".wbpj"} {
        mess "Searching for model file to import within the workbench project: $::wb_trans(file)\n"
    } else {
        mess "Current model file to import: $::wb_trans(file)\n"
    }
    set dir [ic_get_global file_name project_dir]
    if [ic_wb2_is_running] {
        set tetin [aie_get_global wb_initProjName].tin
        if {[file exists "$dir/$tetin"]  && [file isfile "$dir/$tetin"]} {
            set wb_tetin_sizes_file "$dir/$tetin"
        } else {
            set wb_tetin_sizes_file [file root $::wb_trans(file)].tin
        }
    } else {
        set tetin [aie_get_global file_name tetin]
        if {[file exists "$dir/$tetin"] && [file isfile "$dir/$tetin"]} {
            set wb_tetin_sizes_file "$dir/$tetin"
        } else {
            set wb_tetin_sizes_file [file root $::wb_trans(file)].tin
        }
    }
}

proc ongeoImportWBDEZAfterLoad {} {
    global wb_trans vid_options file_name wb_import_options_set env is_win beta_mode
    
    if {$::wb_trans(file) == ""} {
        geoImportWBDEZ unload
        return
    }
    
    set importWBFileList ""
    set file_to_convert ""
    
    set ext [file extension $::wb_trans(file)]
    
    if {$ext == ".scdoc" && $is_win} {
        catch { unset ::wb_trans(geom); unset ::wb_trans(blocking); unset ::wb_trans(unstruct) }
        if ![info exists env(AWP_ROOT)] {
            set env(AWP_ROOT) $env(ICEM_ACN)/../..
            mess "Setting environment variable AWP_ROOT to $env(ICEM_ACN)\n" 
        }
        if [file exists $env(AWP_ROOT)/installer/util/7zip/7z.exe] {
            set zip [win_shortname $env(AWP_ROOT)/installer/util/7zip/7z.exe]
            set files [exec $zip l $::wb_trans(file)]"
            regsub -all \\\\ $files / files
            foreach line $files {
                if [string match "SpaceClaim/Blockings/*.tin" $line] {
                    lappend ::wb_trans(geom) "$line"
                }
                if [string match "SpaceClaim/Blockings/*.blk" $line] {
                    lappend ::wb_trans(blocking) "$line"
                }
                if [string match "SpaceClaim/Blockings/*.uns" $line] {
                    lappend ::wb_trans(unstruct) "$line"
                }
            }
        }
        if {[info exists ::wb_trans(geom)] && [info exists ::wb_trans(blocking)]} {
            mess "Found [llength $::wb_trans(geom)] blocking(s)\n"
        }
    }
     
    if {$ext == ".wbpj"} {
        set reader_files [ic_load_project_file $::wb_trans(file) 1]
        if {$reader_files == ""} {
            mess "No model file found to import in Workbench project file $::wb_trans(file)\n" red
            geoImportWBDEZ unload
            return
        } else {
            ::AIEnv::GUIManagement::ToggleGrey geoImportWBFromProjectFile 0
            foreach file $reader_files {
                set file_to_convert $file
                lappend importWBFileList $file
            }
            mess "Found [llength $reader_files] model file(s)"
            if {[llength $reader_files] > 1} {
                mess ".  Use the \"Choose project\" option to select the model file you want to import\n"
            } else {
                mess "\n"
            }
            mess "Current model file to import: $file_to_convert\n"
        }
        set ::wb_trans(file) $file_to_convert
    } else {
        catch {
        ::AIEnv::GUIManagement::ToggleGrey geoImportWBFromProjectFile 1
        }
    }
    if {$ext != ".mshdb"} {
        set ::vid_options(wb_import_geom) 1
        set ::vid_options(wb_import_mesh) 0
    } else {
        set ::vid_options(wb_import_geom) 0
        set ::vid_options(wb_import_mesh) 1
    }
    
    if ![info exists wb_import_options_set] { 
        aie_wb_set_import_defaults
        set wb_import_options_set 1
    }
    
    set tritolabs 0
    set tri_tolerance 0 
    foreach {n v} [ic_set_meshing_params global 0] {
        if {$n == "gttol"} { set ::vid_options(wb_import_tritol) $v }
        if {$n == "gtrel"} { set tritolabs $v }	
    } 
    
    if $beta_mode {
        if $tri_tolerance {
            ::AIEnv::GUIManagement::ToggleGrey geoImportGeomTriTol 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportGeomTriTolAbs 0
        } else {
            ::AIEnv::GUIManagement::ToggleGrey geoImportGeomTriTol 1
            ::AIEnv::GUIManagement::ToggleGrey geoImportGeomTriTolAbs 1
        }
    }
    
    set geoImportWBGGVal 0
    if {[aie_get_global import_model_options always_import] == "1" && ([file exists $wb_tetin_sizes_file] && [file isfile "$wb_tetin_sizes_file"])} {
        set geoImportWBGGVal 1
    }
    if {[aie_get_global import_model_options always_create_mpoint] == "1"} {
        set ::vid_options(wb_import_mat_points) 1
    }
    if {[aie_get_global import_model_options always_ref_key] == "1"} {
        set ::vid_options(wb_import_reference_key) 1
    }
    if {[info exists ::wb_trans(geom)] && [info exists ::wb_trans(blocking)]} {
        if {[aie_get_global import_model_options always_import_blocking] == "1"} {
            set ::vid_options(wb_import_mesh) 1
        }
    }
    
    set unitsList [list "Default" "Nanometers" "Micrometers" "Millimeters" "Centimeters" "Meters" "Kilometers" "Microinches" "Mils (thousands)" "Inches" "Feet" "Miles"]   
    set unit "Default"
    
    if {[aie_get_global import_model_options always_convert] == "1"} {
        set ::vid_options(wb_import_scale_geo) [aie_get_global import_model_options convert_to]
        set unit [aie_get_global import_model_options convert_to]
    }
     
    set ::vid_options(wb_NS_only) [aie_get_global import_model_options named_sel_only]
    
    if {$file_name(settings) != "" && ![aie_wb2_is_running]} {
        set settings [file rootname [file tail $file_name(settings)]]
        if [file exists $file_name(project_dir)/$settings.pmdb] {
            set ::vid_options(wb_import_cad_associativity) 1
        }
    }
    
    if {[trace vinfo file_to_convert] != ""} {
        trace vdelete file_to_convert w aie_wb_trace_file_to_convert
    }
    trace variable file_to_convert w aie_wb_trace_file_to_convert 
    if {[trace vinfo ::vid_options(wb_import_geom)] != ""} {
        trace vdelete ::vid_options(wb_import_geom) w aie_wb_trace_update_gui_state
    }
    trace variable ::vid_options(wb_import_geom) w aie_wb_trace_update_gui_state
    if {[trace vinfo ::vid_options(wb_import_mesh)] != ""} {
        trace vdelete ::vid_options(wb_import_mesh) w aie_wb_trace_update_gui_state
    }
    trace variable ::vid_options(wb_import_mesh) w aie_wb_trace_update_gui_state
    if {[trace vinfo ::vid_options(wb_import_sel_proc)] != ""} {
        trace vdelete ::vid_options(wb_import_sel_proc) w aie_wb_trace_update_gui_state
    }
    trace variable ::vid_options(wb_import_sel_proc) w aie_wb_trace_update_gui_state
    aie_wb_trace_update_gui_state
    if {[trace vinfo ::vid_options(wb_import_cad_associativity)] != ""} {
        trace vdelete ::vid_options(wb_import_cad_associativity) w aie_wb_trace_update_pmdb_state
    }
    trace variable ::vid_options(wb_import_cad_associativity) w aie_wb_trace_update_pmdb_state
    aie_wb_trace_update_pmdb_state
    
    if {$ext == ".prt" || $ext == ".asm" || $ext == ".prt*" || $ext == ".asm*" || $ext == ".par" || $ext == ".psm" || $ext == ".pwd" || \
        $ext == ".CATPart" || $ext == ".CATProduct" || $ext == ".3dxml" || $ext == ".sldasm" || $ext == ".sldprt" || $ext == ".ipt" || $ext == ".iam"} {
        if $::vid_options(wb_import_geom) {
            catch {
            ::AIEnv::GUIManagement::ToggleGrey geoImportUseAssociativity 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportReferenceKey 0
            }
        }
    } else {
        catch {
        ::AIEnv::GUIManagement::ToggleGrey geoImportUseAssociativity 1
        if !$::vid_options(wb_import_geom) {
            ::AIEnv::GUIManagement::ToggleGrey geoImportReferenceKey 1
        } else {
            ::AIEnv::GUIManagement::ToggleGrey geoImportReferenceKey 0
        }
        }
    }
}

proc aie_wb_set_import_defaults {} {
    global vid_options save_my_vid_options
    
    array set save_my_vid_options [array get ::vid_options]
    set ::vid_options(wb_import_mat_points) 0
    set ::vid_options(wb_NS_to_subset) 0
    set ::vid_options(wb_NS_to_entity_parts) 0
    set ::vid_options(wb_import_solid_bodies) 1
    set ::vid_options(wb_import_surface_bodies) 1
    set ::vid_options(wb_import_line_bodies) 0
    set ::vid_options(wb_import_cad_att_trans) 1
    set ::vid_options(wb_import_cad_att_pre) "SDFEA;DDM"
    set ::vid_options(wb_import_lcs) 0
    set ::vid_options(wb_import_sel_proc) 1
    set ::vid_options(wb_import_sel_pre) ""
    set ::vid_options(wb_import_mix_res) -1
    set ::vid_options(wb_import_en_sym_proc) 1
    set ::vid_options(wb_import_mix_res_solid) 0
    set ::vid_options(wb_import_mix_res_surface) 0
    set ::vid_options(wb_import_mix_res_line) 0
    set ::vid_options(wb_import_mix_res_point) 0
    set ::vid_options(wb_import_scale_geo) "Default"
    set ::vid_options(wb_import_work_points) 0
    set ::vid_options(wb_import_delete_solids) 0
    set ::vid_options(wb_import_create_solids) 0
    set ::vid_options(wb_import_save_pmdb) ""
    set ::vid_options(wb_import_load_pmdb) ""
    set ::vid_options(wb_import_refresh_pmdb) 0
    set ::vid_options(wb_import_cad_associativity) 0
    set ::vid_options(wb_import_save_partfile) 0
    set ::vid_options(wb_import_pluginname) "" 
    set ::vid_options(wb_NS_only) 0
    set ::vid_options(wb_import_associativity_model_name) ""
    set ::vid_options(wb_import_tritol) 0.001
    set ::vid_options(wb_import_reference_key) 0
    if [ic_geo_is_loaded] {
        foreach {n v} [ic_set_meshing_params global 0] {
            if {$n == "gttol"} { set ::vid_options(wb_import_tritol) $v; break }
        }
    }
}

proc aie_wb_reset_import_defaults {} {
    global vid_options save_my_vid_options
    
    if [info exists save_my_vid_options] {
        array set ::vid_options [array get save_my_vid_options]
        unset save_my_vid_options
    }
}

proc aie_wb_trace_file_to_convert {{name ""} {element ""} {op ""}} {
    global wb_trans
    
    if {[geoImportWBDEZ get file_to_convert] == ""} return
    if {$::wb_trans(file) == [geoImportWBDEZ get file_to_convert]} return
    set ::wb_trans(file) [geoImportWBDEZ get file_to_convert]
    aie_wb_trace_update_gui_state
    mess "Current model file to import: $::wb_trans(file)\n"
}

proc aie_wb_trace_update_pmdb_state {{name ""} {element ""} {op ""}} {
    global wb_trans file_name vid_options

    set ext [file extension $::wb_trans(file)]
    if {$ext == ".prt" || $ext == ".asm" || $ext == ".prt*" || $ext == ".asm*" || $ext == ".par" || $ext == ".psm" || $ext == ".pwd" || \
        $ext == ".CATPart" || $ext == ".CATProduct" || $ext == ".3dxml" || $ext == ".sldasm" || $ext == ".sldprt" || $ext == ".ipt" || $ext == ".iam"} {
        if $::vid_options(wb_import_cad_associativity) {
            if {$file_name(settings) == ""} {
                mess "Error: using option \"Use Associativity\" requires an existing project name\n" red
                set ::vid_options(wb_import_cad_associativity) 0
                return
            }
            set settings [file rootname [file tail $file_name(settings)]]
            set ::vid_options(wb_import_save_pmdb) $file_name(project_dir)/$settings.pmdb
            if [file exists $::vid_options(wb_import_save_pmdb)] {
                set ::vid_options(wb_import_refresh_pmdb) 1
                set ::vid_options(wb_import_load_pmdb) $::vid_options(wb_import_save_pmdb)
                mess "Associating geometry \"[file tail $::wb_trans(file)]\" via \"$::vid_options(wb_import_save_pmdb)\"\n"
            } else {
                set ::vid_options(wb_import_refresh_pmdb) 0
                set ::vid_options(wb_import_load_pmdb) ""
                mess "Associated geometry will be stored as \"$::vid_options(wb_import_save_pmdb)\"\n"
            }
        } else {
            set ::vid_options(wb_import_refresh_pmdb) 0
            set ::vid_options(wb_import_save_pmdb) ""
            set ::vid_options(wb_import_load_pmdb) ""
        }
    } else {
        if $::vid_options(wb_import_cad_associativity) {
            mess "Error: file extension \"$ext\" not supported by \"Use Associativity\"\n" red
            set ::vid_options(wb_import_cad_associativity) 0
        }
        catch {
            ::AIEnv::GUIManagement::ToggleGrey geoImportUseAssociativity 1
        }
        set ::vid_options(wb_import_refresh_pmdb) 0
        set ::vid_options(wb_import_save_pmdb) ""
        set ::vid_options(wb_import_load_pmdb) ""
    }
}

proc aie_wb_trace_update_gui_state {{name ""} {element ""} {op ""}} {
    global wb_trans vid_options beta_mode

    if ![ic_wb2_is_running] {
        if [geoImportWBDEZ get geoImportWBGGVal] {
            ::AIEnv::GUIManagement::ToggleGrey geoImportWBTetinSizes 0
        } else {
            ::AIEnv::GUIManagement::ToggleGrey geoImportWBTetinSizes 1
        }
    }
    
    if $vid_options(wb_import_geom) {
        set on 0
    } else {
        set on 1
        set ::vid_options(wb_import_sel_proc) 0
    }
    catch { ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPro $on }
     
    set ext [file extension $::wb_trans(file)]
    catch {
    switch -- $ext {
        ".pmdb" - ".cmdb" - ".dsdb" - ".mshdb" - ".mechdat" - ".mshdat" {
            if {$ext == ".mshdb"} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportAutoBodies 1
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportWBGeom 0
                ::AIEnv::GUIManagement::ToggleGrey geoImportAutoBodies 0
            }
            if {$vid_options(wb_import_mesh) || $ext == ".mshdb"} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNS_to_subset 1
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNS_to_subset $on
            }
            if {$ext != ".mshdb"} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportAutoBodies $on
            }
            ::AIEnv::GUIManagement::ToggleGrey geoImportSolidBodies 1
            set ::vid_options(wb_import_solid_bodies)  0
            ::AIEnv::GUIManagement::ToggleGrey geoImportSurfaceBodies 1
            set ::vid_options(wb_import_surface_bodies) 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportLineBodies 1
            set ::vid_options(wb_import_line_bodies) 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportWorkPoints 1
            set ::vid_options(wb_import_work_points) 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportCADAttTra 1
            ::AIEnv::GUIManagement::ToggleGrey geoImportCADAttPre 1
            ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPro $on
            if {$vid_options(wb_import_sel_proc) && !$on} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPre 0
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPre 1
                set ::vid_options(wb_import_sel_proc) 0
                set ::vid_options(wb_NS_only) 0
                set ::vid_options(wb_NS_to_subset) 0
                set ::vid_options(wb_NS_to_entity_parts) 0
                set ::vid_options(wb_import_sel_pre) ""
            }
            ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPre 1
            set ::vid_options(wb_import_cad_att_pre) ""
            ::AIEnv::GUIManagement::ToggleGrey geoImportEnSymProc 1
            set ::vid_options(wb_import_en_sym_proc) 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportMixRes 1
            set ::vid_options(wb_import_mix_res_solid) 0
            set ::vid_options(wb_import_mix_res_surface) 0
            set ::vid_options(wb_import_mix_res_line) 0
            set ::vid_options(wb_import_mix_res_point) 0
            if {$ext == ".pmdb"} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportWBMesh 1
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportWBMesh 0
            }
            after 10 { ::AIEnv::GUIManagement::ToggleGrey geoImportUseAssociativity 1 }
        }
        default {
            ::AIEnv::GUIManagement::ToggleGrey geoImportWBGeom 0
            ::AIEnv::GUIManagement::ToggleGrey geoImportAutoBodies 0
            if {$vid_options(wb_import_mesh) && !$vid_options(wb_import_geom)} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNS_to_subset 1
                set ::vid_options(wb_NS_to_subset) 0
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNS_to_subset $on
            }
            ::AIEnv::GUIManagement::ToggleGrey geoImportAutoBodies $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportCADAttTra $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportEnSymProc $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportMixRes $on
            if $beta_mode { ::AIEnv::GUIManagement::ToggleGrey geoImportGeomTriTolerance $on }
            if {$vid_options(wb_import_cad_att_trans) && !$on} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportCADAttPre 0
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportCADAttPre 1
            }
            if {$vid_options(wb_import_sel_proc) && !$on} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPre 0
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportNSelPre 1
                set ::vid_options(wb_NS_only) 0
                set ::vid_options(wb_NS_to_subset) 0
                set ::vid_options(wb_NS_to_entity_parts) 0
                set ::vid_options(wb_import_sel_pre) ""
            }
            if {$ext == ".agdb"} { set on 1 }
            ::AIEnv::GUIManagement::ToggleGrey geoImportSolidBodies $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportSurfaceBodies $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportLineBodies $on
            ::AIEnv::GUIManagement::ToggleGrey geoImportWorkPoints $on
            if {$ext == ".agdb" || $ext == ".sat" || $ext == ".iam" || $ext == ".iges" || \
                $ext == ".ipt" || $ext == ".CATPart" || $ext == ".CATProduct" || $ext == ".model" || $ext == ".3dxml" || \
                $ext == ".x_t" || $ext == ".x_txt" || $ext == ".xmt_b" || $ext == ".xmt_bin" || $ext == ".prt" || \
                $ext == ".asm" || $ext == ".sdpc" || $ext == ".sldasm" || $ext == ".sldprt" || $ext == ".scdoc" || \
                $ext == ".stp" || $ext == ".step" || $ext == ".igs" || $ext == ".sat" || $ext == ".mcnp" || $ext == ".pkg" || \
                $ext == ".bdl" || $ext == ".ses" || $ext == ".sda" || $ext == ".sdp" || $ext == ".sdac" || $ext == ".sdpc" || \
                $ext == ".fmdb"} {
                if {$ext == ".scdoc" && ([info exists ::wb_trans(geom)] || [info exists ::wb_trans(blocking)])} {
                    ::AIEnv::GUIManagement::ToggleGrey geoImportWBMesh 0
                } else {
                    ::AIEnv::GUIManagement::ToggleGrey geoImportWBMesh 1
                }
                if {$ext == ".agdb"} {
                    ::AIEnv::GUIManagement::ToggleGrey geoImportMixRes 1
                    ::AIEnv::GUIManagement::ToggleGrey geoImportReferenceKey 1
                } else {
                    ::AIEnv::GUIManagement::ToggleGrey geoImportMixRes 0
                    ::AIEnv::GUIManagement::ToggleGrey geoImportReferenceKey 0
                }
            } else {
                ::AIEnv::GUIManagement::ToggleGrey geoImportWBMesh 0
                ::AIEnv::GUIManagement::ToggleGrey geoImportMixRes 0
            }
            if {$ext == ".prt" || $ext == ".asm" || $ext == ".prt*" || $ext == ".asm*" || $ext == ".par" || $ext == ".psm" || $ext == ".pwd" || \
                $ext == ".CATPart" || $ext == ".CATProduct" || $ext == ".3dxml" || $ext == ".sldasm" || $ext == ".sldprt" || $ext == ".ipt" || $ext == ".iam"} {
                ::AIEnv::GUIManagement::ToggleGrey geoImportUseAssociativity 0
            }
        }
    }
    }
}

proc aie_wb_set_empty_solid_state {on} {
    global vid_options
    if $on {
        set vid_options(wb_import_delete_solids) 0
    } else {
        set vid_options(wb_import_create_solids) 0
    }
}

proc ongeoImportWBDEZOK {} {
    global wb_trans geo_cad wb_tetin_sizes_file import_model_options

    if {[ic_gui_set vid_options(wb_import_mesh)] && ([file extension $::wb_trans(file)] == ".scdoc") && \
        ([info exists ::wb_trans(geom)] && [info exists ::wb_trans(blocking)])} {
        if [ic_geo_is_loaded] ic_unload_tetin
        if [ic_hex_blocking_loaded] ic_hex_unload_blocking
        if [ic_uns_is_loaded] { ic_unload_mesh 1 }
    }
    
    set wb_merge_geom 0
    if [ic_gui_set vid_options(wb_import_geom)] {
        if [ic_geo_is_loaded] {
            set opt [tk_dialog .tetinOverwrite "Geometry Exists" "Geometry Already Exists" "" 1 "Merge" "Replace" "Cancel"]
            switch -- $opt {
                -1 { 
                    if [winfo exists .tetinOverwrite] { top_destroy .tetinOverwrite }
                    return
                }
                0 { set wb_merge_geom 1 }
                1 {
                    mess "\n   --- Unloading existing geometry\n\n"
                    aie_close_geom 0
                }
                2 { return }
            }
        }
    }
    set wb_merge_mesh 0
    if [ic_gui_set vid_options(wb_import_mesh)] {
        if [ic_uns_is_loaded] {
            set opt [tk_dialog .meshOverwrite "Mesh Exists" "Mesh Already Exists" "" 1 "Merge" "Replace" "Cancel"]
            switch -- $opt {
                -1 { 
                    if [winfo exists .meshOverwrite] { top_destroy .meshOverwrite }
                    return
                }
                0 {
                    set wb_merge_mesh 1
                }
                1 {
                    mess "\n   --- Unloading existing mesh\n\n"
                    aie_close_Mesh 0
                }
                2 { return }
            }
        }
    }
    
    if {$::vid_options(wb_import_geom) == "1" && $::vid_options(wb_import_mesh)== "0"} {
        if [ic_uns_is_loaded] {
            set wb_merge_mesh 1
        }
    }
    if !$wb_merge_mesh {
        if [ic_uns_is_loaded] {
            aie_close_Mesh 0
        }
    }

    if {![ic_gui_set vid_options(wb_import_geom)] && ![ic_gui_set vid_options(wb_import_mesh)]} {
        return
    }
    
    busy 1

    set wb_import_mix_res 0
    if $::vid_options(wb_import_mix_res_solid) {
        incr wb_import_mix_res 1
    }
    if $::vid_options(wb_import_mix_res_surface) {
       incr wb_import_mix_res 10
    }
    if $::vid_options(wb_import_mix_res_line) {
        incr wb_import_mix_res 100
    }
    if $::vid_options(wb_import_mix_res_point) {
        incr wb_import_mix_res 1000
    }
    switch -- $wb_import_mix_res {
        0     { set ::vid_options(wb_import_mix_res) -1 }
        1     { set ::vid_options(wb_import_mix_res) 1 }
        10    { set ::vid_options(wb_import_mix_res) 2 }
        11    { set ::vid_options(wb_import_mix_res) 5 }
        100   { set ::vid_options(wb_import_mix_res) 3 }
        101   { set ::vid_options(wb_import_mix_res) 6 }
        110   { set ::vid_options(wb_import_mix_res) 8 }
        111   { set ::vid_options(wb_import_mix_res) 14 }
        1000  { set ::vid_options(wb_import_mix_res) 4 }
        1001  { set ::vid_options(wb_import_mix_res) 7 }
        1010  { set ::vid_options(wb_import_mix_res) 9 }
        1011  { set ::vid_options(wb_import_mix_res) 13 }
        1100  { set ::vid_options(wb_import_mix_res) 10 }
        1101  { set ::vid_options(wb_import_mix_res) 12 }
        1110  { set ::vid_options(wb_import_mix_res) 11 }
        1111  { set ::vid_options(wb_import_mix_res) 15 }
        default { set ::vid_options(wb_import_mix_res) -1 }
    }
    
    set vid_options(wb_import_scale_geo) $unit

    set import_tritol $::vid_options(wb_import_tritol)
    
    if $beta_mode {
        if $tri_tolerance {
            if {$tritolabs == 0 && ([string is double $import_tritol] || [string is integer $import_tritol])} {
                set import_tritol [expr $import_tritol * -1.0]
            }
        } else {
            if [ic_geo_is_loaded] {
                foreach {n v} [ic_set_meshing_params global 0] {
                    if {$n == "gttol"} { set import_tritol $v; break }
                }
            }
        }
    }
    
    # for replay
    ic_wb_set_parameters $::vid_options(wb_import_geom) $::vid_options(wb_import_mesh) $::vid_options(wb_import_mat_points) $::vid_options(wb_NS_to_subset) \
        $::vid_options(wb_NS_to_entity_parts) $::vid_options(wb_import_solid_bodies) $::vid_options(wb_import_surface_bodies) $::vid_options(wb_import_line_bodies) \
        $::vid_options(wb_import_cad_att_trans) $::vid_options(wb_import_cad_att_pre) $::vid_options(wb_import_sel_proc) $::vid_options(wb_import_sel_pre) \
        $::vid_options(wb_import_mix_res) $::vid_options(wb_import_en_sym_proc) $::vid_options(wb_import_scale_geo) $::vid_options(wb_import_work_points) \
        $::vid_options(wb_import_delete_solids) $::vid_options(wb_import_create_solids) $::vid_options(wb_import_save_pmdb) $::vid_options(wb_import_load_pmdb) \
        $::vid_options(wb_import_refresh_pmdb) $::vid_options(wb_import_cad_associativity) $::vid_options(wb_import_save_partfile) $::vid_options(wb_import_pluginname) \
        $::vid_options(wb_NS_only) $import_tritol $vid_options(wb_import_reference_key) \
        $::vid_options(wb_import_lcs)

    geoImportWBDEZ setclean
    set vid_options(wb_import_from_gui) 1
    set error 0
    if {[ic_gui_set vid_options(wb_import_mesh)] && ([file extension $::wb_trans(file)] == ".scdoc") && \
        ([info exists ::wb_trans(geom)] && [info exists ::wb_trans(blocking)])} {
        ic_wb_import_blockings $::wb_trans(file)
        if [ic_hex_blocking_loaded] { switch_topology root; aie_after_blocking_loaded }
    } else {
        mess "\nImporting file $::wb_trans(file)\n\n" blue
        if [catch {ic_wb_brep_read $::wb_trans(file) 1 $wb_merge_geom $::vid_options(wb_import_mesh)} res] {
            busy reset
            set error 1
            mess "Error: $res\n" red
        } else {
            if {$res == "1"} {
                set error 1
            }
        }
    }
    catch { unset ::wb_trans(geom); unset ::wb_trans(blocking); unset ::wb_trans(unstruct) }
    if [info exists vid_options(wb_import_from_gui)] { unset vid_options(wb_import_from_gui) }
    
    # Map family assignments and sizes
    
    if [ic_wb2_is_running] {
        set geoImportWBGGVal $import_model_options(always_import)
    }
    if $geoImportWBGGVal {
        if !$error {     
            aie_wb_import_parameters $wb_tetin_sizes_file
        }
    }

    if {[ic_gui_set vid_options(wb_import_mesh)] && [ic_uns_is_loaded]} {
        if {[ic_uns_non_empty_families] == ""} {
            mess "Warning: \"Import mesh\" created an empty mesh\n" red
        } else {
            InitUnsMeshDisplaySettings
            aie_initial_unsmesh_visibility 0 0
            aie_update_parts_in_tree_if_needed 1
        }
    }
    aie_afterStuffLoaded 
    if {[ic_gui_set vid_options(wb_import_geom)] && [ic_geo_is_loaded]} {
        # Init Geo visibility
        aie_initGeoVis
        aie_init_geom_subsets
        aie_viewInfo geometry
    }
    if {[ic_gui_set vid_options(wb_import_mesh)] && [ic_uns_is_loaded]} {
        if {[ic_uns_non_empty_families] == ""} {
            mess "Mesh contains no elements, closing\n"
            aie_close_Mesh 0
        } else {
            ic_uns_print_info summary
        }
    }
    tdv_home current
    if $error {
        mess "\nError in importing model file $::wb_trans(file)\n" red
    } else {
        mess "\nDone\n" blue
    }
    geoImportWBDEZ unload
    busy reset
}

proc aie_wb_import_parameters {file} {
    global vid_options
    
    if ![file exists $file] return
    
    set what 0
    set what_type ""
    set skip 0
    if $::vid_options(wb_import_cad_associativity) {
        if {[file exists $::vid_options(wb_import_save_pmdb)] && [file isfile $::vid_options(wb_import_save_pmdb)]} {
            set skip 1
        }
    }
    set what 0
    set what_type ""
    if {[ic_get_global map_tetin_sizes global] == "1"} { append what_type "Global mesh params "; set what [expr {$what + 1}] }
    if {[ic_get_global map_tetin_sizes family] == "1"} { append what_type "+ Part mesh params "; set what [expr {$what + 2}] }
    if {[ic_get_global map_tetin_sizes ppoint] == "1"} {
        if !$skip {
            append what_type "+ Point parts "; set what [expr {$what + 4}]
        } else {
            append what_type "- Point parts "
        }
    }
    if {[ic_get_global map_tetin_sizes pcurves] == "1"} {
        if !$skip {
            append what_type "+ Curve parts "; set what [expr {$what + 8}]
        } else {
            append what_type "- Curve parts "
        }
    }
    if {[ic_get_global map_tetin_sizes mcurves] == "1"} { append what_type "+ Curve mesh params "; set what [expr {$what + 16}] }
    if {[ic_get_global map_tetin_sizes psurfaces] == "1"} { 
        if !$skip {
            append what_type "+ Surface parts "; set what [expr {$what + 32}]
        } else {
            append what_type "- Surface parts "
        }
    }
    if {[ic_get_global map_tetin_sizes msurfaces] == "1"} { append what_type "+ Surface mesh params "; set what [expr {$what + 64}] }
    if {[ic_get_global map_tetin_sizes thincuts] == "1"} { append what_type "+ Thincuts "; set what [expr {$what + 128}] }
    if {[ic_get_global map_tetin_sizes densities] == "1"} { append what_type "+ Densities volume "; set what [expr {$what + 256}] }
    if {[ic_get_global map_tetin_sizes subsets] == "1"} { 
        if {[ic_get_global vid_options wb_NS_to_subset] == "0"} {
            append what_type "+ Subsets "; set what [expr {$what + 512}]
        } else {
            append what_type "- Subsets "
        }
    }
    if {[ic_get_global map_tetin_sizes mpoint] == "1"} { append what_type "+ Material points"; set what [expr {$what + 1024}] }   
    if {$what == 0} {
        mess "Nothing selected in \"Settings->Geometry Options->Import Parameters from Tetin file\"\n" red
        return
    }
    mess "Importing parameters \"$what_type\"\n"
    if [catch {ic_geo_map_tetin_sizes $file $what} res] {
	    mess "Error: $res\n" red
	    return
	}
	mess "Imported parameters from geometry file $file\n"
}

proc ongeoImportWBDEZUnLoad {} {
    global vid_options
    
    if {[trace vinfo file_to_convert] != ""} {
        trace vdelete file_to_convert w aie_wb_trace_file_to_convert
    }
    if {[trace vinfo ::vid_options(wb_import_geom)] != ""} {
        trace vdelete ::vid_options(wb_import_geom) w aie_wb_trace_update_gui_state
    }
    if {[trace vinfo ::vid_options(wb_import_mesh)] != ""} {
        trace vdelete ::vid_options(wb_import_mesh) w aie_wb_trace_update_gui_state
    }
    if {[trace vinfo ::vid_options(wb_import_sel_proc)] != ""} {
        trace vdelete ::vid_options(wb_import_sel_proc) w aie_wb_trace_update_gui_state
    }
    if {[trace vinfo ::vid_options(wb_import_cad_associativity)] != ""} {
        trace vdelete ::vid_options(wb_import_cad_associativity) w aie_wb_trace_update_pmdb_state
    }
}

proc ongeoImportParasolidDEZLoad {} {
    global env file_name ps_trans is_win

    if {$is_win && ![file exists "$env(ICEM_ACN)/bin/pstotin.exe"]} {
        mess "\nNote that the executable \"pstotin\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/bin\"\n" blue
        return 
    }

    set ps_trans(file) [med_file_select ps 0]
    if {$ps_trans(file) == ""} return

    set ps_name $ps_trans(file)
}

proc ongeoImportParasolidDEZAfterLoad {} {
    global ps_trans file_name

    if {![info exists ps_trans(file)] || $ps_trans(file) == ""} {
        geoImportParasolidDEZ unload
        return
    }
    set tetin_file_name $file_name(mesh_dir)/[file rootname [file tail $ps_trans(file)]].tin 
}

proc ongeoImportParasolidDEZOK {} {
    global ps_trans

    switch $geoImportParasolidFileGGVal {
        0 { set ps_unit meter }
        1 { set ps_unit inch }
        2 { set ps_unit centimeter }
        3 { set ps_unit foot }
        4 { set ps_unit millimeter}
    }        

    busy 1
    if {[file exists $tetin_file_name] || $tetin_file_name == ""} {
        set tmess "File $tetin_file_name exists - overwrite?"
        switch -- [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] {
            -1 { 
                if [winfo exists .overwrite] { top_destroy .overwrite }
                return
            }
            1 {
                set tetin_file_name [med_file_select tetin 1 $tetin_file_name]
            }
        }
    }
    if {$tetin_file_name == ""} {
        mess "Invalid Output File\n"
        return
    }
    if [catch {ic_trans_ps_tetin $ps_name $tetin_file_name $ps_unit} res] {
        mess "Error: $res\n" red
    }
    
    file_check_load_tetin $tetin_file_name
    mess "Done.\n"
    
    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc ongeoImportIDIDEZLoad {} {
    global file_name idi_trans is_win env

    if {$is_win && ![file exists "$env(ICEM_ACN)/bin/idi2tetin.exe"]} {
        mess "\nNote that the executable \"idi2tetin\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/bin\"\n" blue
        set idi_trans(file) ""
        return 
    }

    set idi_trans(file) [med_file_select idi 0 "" "" 0]
    if {$idi_trans(file) == ""} return

    set tetinfile $file_name(mesh_dir)/[file rootname \
            [file tail $idi_trans(file)]].tin

    set part_name 0
    set assem_name 0
    set noasm 0
    set fmrg 0
    set tria 0
    set ng 0
    set part_assem 0
}

proc ongeoImportIDIDEZAfterLoad {} {
    global idi_trans
    if {$idi_trans(file) == ""} {
        geoImportIDIDEZ unload
    }
}

proc ongeoImportIDIDEZOK {} {
    global idi_trans

    foreach var {tetinfile part_assem part_name assem_name noasm fmrg tria ng} {
        set idi_trans($var) [set $var]
    }
    
    busy 1
    trans_idi_tetin_doit

    # Init Geo visibility
    aie_afterStuffLoaded
    aie_initGeoVis
    busy 0
}

proc aie_fe_modeler_file_export {} {
    global env

    if ![ic_uns_is_loaded] {
        mess "No Mesh is loaded!\n" red
        return
    }
    if [file exists $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_elem_props.tcl] {
        source $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_elem_props.tcl
    } else {
        mess "Error loading $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_elem_props.tcl\n" red
        return
    }
    if [file exists $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_materials.tcl] {
        source $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_materials.tcl
    } else {
        mess "Error loading $env(ICEM_ACN)/lib/ai_env/scripts/fea2.0/aie_materials.tcl\n" red
        return
    }
    writeFeModelerDEZ show
}

proc onwriteFeModelerDEZAfterLoad {} {
    global file_name thickness_defined

    catch { unset thickness_defined }
    if {[writeFeModelerDEZ get femodeler_file] == ""} {
        if {$file_name(settings) == ""} {
            writeFeModelerDEZ set femodeler_file "FEModeler.dat"
        } else {
            set femodeler_file_tmp [file root $file_name(settings)]
            append femodeler_file_tmp \.dat
            writeFeModelerDEZ set femodeler_file $femodeler_file_tmp
        }
    }
    writeFeModelerDEZ setdirty
}

proc aie_check_fe_modeler_shell_thicknesses {} {
    global thickness_defined

    if [catch {set thickness_defined [ic_uns_thickness_exists_mesh]}] {
        set thickness_defined 0
    } else {
        foreach part [ic_boco_list_families] {
            set val ""
            foreach {n v} [aie_parse_bcs_element_prop_2d [ic_boco_get $part ISHE]] {
                if {$n == "m_thick"} { set val $v; break }
            }
            if {$val == 0} { set thickness_defined 1 }
        }
    }
}

proc onwriteFeModelerDEZApply {} {
    global env file_name solver_setup

    aie_check_fe_modeler_shell_thicknesses
    set femodeler_file [writeFeModelerDEZ get femodeler_file]
    if [file exists $femodeler_file] {
        set tmess "File $femodeler_file exists - overwrite?"
        switch -- [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] {
            -1 {
                if [winfo exists .overwrite] { top_destroy .overwrite }
                return    
            }
            1 { return }
            0 {
                eval file delete -force -- $femodeler_file
            }
        }
    }
    if {$file_name(solver_params) == ""} {
        set file_name(solver_params) [file rootname $femodeler_file].par
    }
    if {$file_name(family_boco) == "" || $file_name(attributes) == ""} {
       set file_name(family_boco) [file rootname $femodeler_file].fbc
       set file_name(attributes) [file rootname $femodeler_file].atr
    }
    aie_write_fe_modeler_file
}

proc aie_write_fe_modeler_file {} {
    global env file_name tcl_platform thickness_defined solution

    set femodeler_file [writeFeModelerDEZ get femodeler_file]
    if {$thickness_defined != 0 && ![ic_uns_thickness_exists_mesh]} {
       mess "Computing thickness.  Please wait....\n" blue
       busy 1
       ic_uns_thickness create
       busy 0
    }
    if {![info exists solution(solver)] || $solution(solver) != "Nastran"} {
        ic_boco_solver Nastran
        ic_solver_mesh_info Nastran
    }
    aie_saveProject 1 1
    if {![file exists $file_name(solver_params)]} {
        aie_save_params
    }
    aie_save_attr

    set vol_v 2
    set shell_v 2
    set bar_v 2
    set cont_flag 0
    set large_format 1
    set thick_v $thickness_defined
    mess "Creating the Input File for FE Modeler.  Please wait....\n" blue
    if ![aie_export_nastran $femodeler_file $file_name(domain) \
        $file_name(attributes) $file_name(solver_params) \
        $large_format $vol_v $shell_v $bar_v $thick_v $cont_flag] return
    if {![file exists $femodeler_file] || ![file size $femodeler_file]} {
        mess "Error in creating file $femodeler_file\n" red
        return
    } else {
        mess "\nInput File $femodeler_file for FE Modeler created\n" blue
    }
    writeFeModelerDEZ setclean
}


set backend_info(cgns) { "CGNS" readcgns cgnsinfo}

array set cgnsinfo {
   file       ""
   dir_loc    ""
}

proc aie_setup_cgns { {args ""} } {
    global cgnsinfo

    if {$args != ""} {
        return [list startup_files $args]
    }
    
    if {$cgnsinfo(file) == ""} {
        # Returning a blank means "cancel".
        return ""
    }
    return [list startup_files $cgnsinfo(file)]
}
