
# Translator routines. ---------------------------------------------
#
# Copyright (c) 1997 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

#
#  Allow the user to import mesh sizes and family assignments
#  from an existing tetin file to the currently loaded model.
#  Data is assigned to curves, surfaces, etc. by matching entity
#  names.
#
proc do_map_tetin_sizes {} {
    global map_tetin_sizes file_name

    if {$map_tetin_sizes(tetin) == ""} {
        set  map_tetin_sizes(tetin) $file_name(tetin)
    }
    set buttons {
      {accept}
      {cancel}
    }
    set d [form_init .trans_map_tetin "Import Sizes From Tetin File" "" $buttons]
    if {$d == ""} return
	futil_fileentry $d.tetinfile map_tetin_sizes(tetin) $file_name(project_dir) {1 1} "" *.tin
	form_finish +300+300
    if ![form_wait .trans_map_tetin] return
    
    if [file exists $map_tetin_sizes(tetin)] {
        set what 0
        set what_type ""
        if $map_tetin_sizes(global) { append what_type "Global mesh params "; set what [expr {$what + 1}] }
        if $map_tetin_sizes(family) { append what_type "+ Part mesh params "; set what [expr {$what + 2}] }
        if $map_tetin_sizes(ppoint) { append what_type "+ Point parts "; set what [expr {$what + 4}] }
        if $map_tetin_sizes(pcurves) { append what_type "+ Curve parts "; set what [expr {$what + 8}] }
        if $map_tetin_sizes(mcurves) { append what_type "+ Curve mesh params "; set what [expr {$what + 16}] }
        if $map_tetin_sizes(psurfaces) { append what_type "+ Surface parts "; set what [expr {$what + 32}] }
        if $map_tetin_sizes(msurfaces) { append what_type "+ Surface mesh params "; set what [expr {$what + 64}] }
        if $map_tetin_sizes(thincuts) { append what_type "+ Thincuts "; set what [expr {$what + 128}] }
        if $map_tetin_sizes(densities) { append what_type "+ Densities volume "; set what [expr {$what + 256}] }
        if $map_tetin_sizes(subsets) { append what_type "+ Subsets "; set what [expr {$what + 512}] }
        if $map_tetin_sizes(mpoint) { 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
        }
        if [catch {ic_geo_map_tetin_sizes $map_tetin_sizes(tetin) $what} res] {
	        mess "$res\n" red
	    }
    } else {
	    mess "Cannot open $map_tetin_sizes(tetin)\n" red
    }
}

proc ddn_trans_iges_imp {} {
    global file_name iges_trans env
    
    if {[info exists iges_trans(file)] && $iges_trans(file) == ""} {
	if {$file_name(iges_dir) == ""} {
	    set file_name(iges_dir) $file_name(project_dir)/part.igs
	}
	set iges_trans(file) $file_name(iges_dir)/part.igs
    }
    
    set ch [med_file_select iges 0 $iges_trans(file) "to read in"]
    if {$ch == ""} return
    set iges_trans(file) $ch
    set file_name(iges_dir) [file dir $ch]
    
    set iges_trans(ddn_name) [file rootname [file tail $ch]]
    if {$iges_trans(listing_file) == ""} {
	set iges_trans(listing_file) iges/iges_listing
    }

    if ![file exists $file_name(parts_dir)] {
	cmd_mkdir $file_name(parts_dir)
    }

    set iges_trans(ddn_file) $file_name(parts_dir)/$iges_trans(ddn_name).ipa

    if {$iges_trans(dir_file_user) == ""} {
	set iges_trans(dir_file_user) $env(ICEM_ACN)/iges/dir_iges_general
    }
    set d [form_init .trans_iges_imp "IGES -> DDN"]
    if {$d != ""} {
	form_label $d.ddnnamel "DDN name" {0 0}
	form_entry $d.ddnname iges_trans(ddn_name) string {0 1}
	form_label $d.ddnfilel "File"  {1 0 -sticky e}
	futil_fileentry $d.ddnfile iges_trans(ddn_file) $file_name(parts_dir) \
	    {1 1} 
	form_label $d.dirl "Directive file" {2 0}
	form_radiobutton $d.dirdef Default iges_trans(dir_file) default {2 1}
	form_radiobutton $d.dirnone None iges_trans(dir_file) none {3 1}
	form_radiobutton $d.dircat Catia iges_trans(dir_file) catia {4 1}
	form_radiobutton $d.diruser Selected iges_trans(dir_file) user {5 1}
	form_label $d.diruserl "File"  {6 0 -sticky e}
	futil_fileentry $d.diruserf iges_trans(dir_file_user) $file_name(project_dir) \
	    {6 1} {{iges_trans(dir_file) user}}
	form_checkbutton $d.ipt "Run icempt" iges_trans(icempt) \
	    {10 0 -cols 2}
	form_checkbutton $d.dred "Data reduction" iges_trans(data_red) \
	    {11 0 -cols 2}
	form_checkbutton $d.slist "Save listing" iges_trans(listing) \
	    {12 0 -cols 2}
	form_label $d.slistl "File"  {13 0 -sticky e}
	futil_fileentry $d.slistf iges_trans(listing_file) $file_name(project_dir) \
	    {13 1} iges_trans(listing)
	form_checkbutton $d.transd "Translate DOS text" iges_trans(trans_dos) \
	    {14 0 -cols 2}
	form_finish +300+300
    }
    if ![form_wait .trans_iges_imp] return
    
    if [file exists $iges_trans(ddn_file)] {
	set tmess "Part $iges_trans(ddn_file) exists - overwrite?"
	if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] return
    }

    if 0 {
	ddn_trans_iges_imp_doit
    } else {
	if $iges_trans(listing) {
	    set listf $iges_trans(listing_file)
	} else {
	    set listf ""
	}
	ic_trans_iges_ddn $iges_trans(file) $iges_trans(ddn_name) $iges_trans(ddn_file) \
     		$iges_trans(dir_file) \
		$iges_trans(dir_file_user) $iges_trans(data_red) $listf \
		$iges_trans(icempt) $iges_trans(trans_dos)
    }
    mess "Done.\n"
}

proc old__ddn_trans_iges_imp_doit {} {
    global iges_trans file_name env
    
    if {[info exists file_name(parts_dir) ] && ($file_name(parts_dir) != "")} {
    	set part $file_name(parts_dir)/$iges_trans(ddn_name)
    } else {
    	set pdir [file_select "Select a directory for the DDN part." "" "IGES - DDN output" 1 1]
    	if {$pdir == ""} return
    	set part $pdir/$iges_trans(ddn_name)
    }
    if [file exists $part] {
	set mess "Part $iges_trans(ddn_name) exists - overwrite?"
	if [tk_dialog .overwrite Overwrite $mess "" -1 Yes No] return
    }
    
    switch $iges_trans(dir_file) {
	none    { set dfile "" }
	default { set dfile $env(ICEM_ACN)/iges/dir_iges_general }
	catia   { set dfile $env(ICEM_ACN)/iges/dir_from_catia }
	user    { set dfile $iges_trans(dir_file_user) }
    }
    
    if {$dfile != ""} {
	set tmp_dfile [tmp_filename]
	set fp [open $dfile r]
	set fp2 [open $tmp_dfile w]
	while {![eof $fp]} {
	    set line [gets $fp]
	    if !$iges_trans(data_red) {
		if [string match *CREATE_310_PART* $line] continue
		if [string match *MAX_BSP_SEG* $line] continue
		if [string match *BSP_RED_TOL* $line] continue
	    }
	    puts $fp2 $line
	}
	close $fp
	close $fp2
	set dfile_line d=$tmp_dfile
    } else {
	set dfile_line ""
    }
    
    if $iges_trans(listing) {
	set listing_line l=$iges_trans(listing_file)
    } else {
	set listing_line ""
    }
    
    if $iges_trans(trans_dos) {
	# ???
    }
    
    set command "i=$iges_trans(file) \
		o=$part.tmp name=$iges_trans(ddn_name) $listing_line \
		logit=yes $dfile_line"
    ic_run_application_exec . iges iges_post $command
    
    if $iges_trans(icempt) {
	set command "i=$part.tmp \
		o=$part t=BTA e=icempt_list"
	ic_run_application_exec . bin icempt $command
	rm_tmp_file $part.tmp
    } else {
	mess "Not running icempt on the output.\n"
	ic_mv $part.tmp $part
    }
    mess "Translation complete. Bring up a DDN application\
	from the Geometry menu to view/edit the part.\n"
    return $part
}

proc ddn_trans_iges_exp {} {
    global file_name iges_trans env
    
    set part [ddn_select_part both 1]
    if {$part == ""} return
    set iges_trans(ddn_name) [lindex $part 2]
    
    if {$iges_trans(file) == ""} {
	set iges_trans(file) $file_name(iges_dir)/[file tail $iges_trans(ddn_name)].igs
	regsub {.ipa} $iges_trans(file) {} iges_trans(file)
	regsub {.IPA} $iges_trans(file) {} iges_trans(file)
    }

    set igesdir [file dirname $iges_trans(file)]
    if ![file exists $igesdir] {
	cmd_mkdir $igesdir
    }

    set ch [med_file_select iges 1 $iges_trans(file) "to create"]
    set file_name(iges_dir) [file dir $ch]
    
    if {$ch == ""} return
    set iges_trans(file) $ch
    set file_name(iges_dir) [file dir $ch]
    
    if {$iges_trans(listing_file) == ""} {
	set iges_trans(listing_file) iges/iges_listing
    }
    if {$iges_trans(dir_file_user) == ""} {
	set iges_trans(dir_file_user) $env(ICEM_ACN)/iges/dir_to_catia
    }
    set d [form_init .trans_iges_exp "DDN -> IGES"]
    if {$d != ""} {
	form_checkbutton $d.dirl "Directive file" iges_trans(dir_file_exp) \
	    {1 0}
	futil_fileentry $d.diruserf iges_trans(dir_file_user) $file_name(project_dir) \
	    {1 1} iges_trans(dir_file_exp)
	form_checkbutton $d.slist "Save listing" iges_trans(listing) \
	    {11 0 -cols 2}
	form_label $d.slistl "File"  {12 0 -sticky e}
	futil_fileentry $d.slistf iges_trans(listing_file) $file_name(project_dir) \
	    {12 1} iges_trans(listing)
	form_finish +300+300
    }
    if ![form_wait .trans_iges_exp] return
    
    if [file exists $iges_trans(file)] {
	set mess "IGES file $iges_trans(file) exists - overwrite?"
	if [tk_dialog .overwrite Overwrite $mess "" -1 Yes No] return
    }
    
    #ddn_trans_iges_exp_doit
    ic_trans_ddn_iges $iges_trans(ddn_name) $iges_trans(file) \
	    $iges_trans(listing_file) $iges_trans(dir_file_user)
}

proc ddn_trans_iges_exp_doit {} {
    global iges_trans env    
    
    set env(CFD_IGES_ONLY) 1

    if $iges_trans(dir_file_exp) {
	set dfile_line d=$iges_trans(dir_file_user)
    } else {
	set dfile_line ""
    }
    
    if $iges_trans(listing) {
	set listing_line l=$iges_trans(listing_file)
    } else {
	set listing_line ""
    }
    
    set command "i=$iges_trans(ddn_name) \
	    o=$iges_trans(file) $listing_line $dfile_line"
    ic_run_application_exec . iges iges_pre $command
}

proc trans_ddn_tvda {} {
    global env file_name
    
    set part [ddn_select_part both 1]
    if {$part == ""} return
    set dir [lindex $part 0]
    set selected [lindex $part 1]
    set file [lindex $part 2]
    
    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {	
	set proj_dir $file_name(project_dir)
    }
    if [file exists $proj_dir/tvda] {
	set tvda_def_part $proj_dir/tvda/[]
    } else {
	set tvda_def_part $proj_dir/[nice_partname]
    }
    set outfile [file_select_lc "Enter name of the TVDA output file."\
		 "[pwd]/tvda/[nice_partname]" "TVDA Output" 0 1 ]
    if {$outfile == ""} return
    if {![file exists [file dirname $outfile]]} \
	{ ic_mkdir [file dirname $outfile] }
    
    if [file exists $outfile] then {
	if [tk_dialog .tvda_overwrite "TVDA" "File exists: overwrite?" \
		"" -1 Yes No] return
    } else {
	if {![file exists tvda]} {ic_mkdir tvda}
    }
    
    ic_trans_ddn_tvda $file $outfile $selected tvda_dir tvda_list    
}

proc trans_tvda_ddn {} {
    global env file_name
     
    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {	
	set proj_dir $file_name(project_dir)
    }
    if [file exists $proj_dir/tvda] {
	set tvda $proj_dir/tvda
    } else {
	set tvda $proj_dir
    }
    set infile [ file_select "Select an input TVDA file" $tvda ]
    if {$infile == ""} return
    
    set file [question_box .tvda_ddn_name "DDN part name" \
	"Enter name of the DDN part\n(in the parts directory)." \
		  [file tail $infile]]
    if {$file == ""} return
    set file $file_name(parts_dir)/$file
    
    if [file exists $file] {
	if {[tk_dialog .tvda_overwrite "TVDA" "Part exists: overwrite?" "" -1 Yes No ] == \
	    "1"} return
    }

    ic_trans_tvda_ddn $infile $file tvda_dir tvda_list
    update_partlist - 1
}

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

proc trans_ddn_dxf {} {
    global env is_win
    
    set part [ddn_select_part both 1]
    if {$part == ""} return
    set dir [lindex $part 0]
    set selected [lindex $part 1]
    set file [lindex $part 2]

    if {$file == ""} {errormsg "No part selected!"; return}
    
    if [partfile_is_binary $file] {
 	errormsg "The part must be in ASCII format before it can be converted\
                   to DXF.  Please edit the part using DDN, and change the \"Fmt\"\
                   option to \"B > A\", and save the part.  You can then convert it\
                   to DXF."
 	return
    }
    set lower [ string tolower $file ]
    set remove_file 0
    if {  $file != $lower } {
    	ic_cp $file $lower
	set file $lower
	set remove_file 1
    }
    
    set outfile [file_select_lc "Enter name of the DXF output file."\
		     "[pwd]/dxf/[nice_partname]" "DXF Output" 0 1 ]
    if {$outfile == ""} return
    if {![file exists [file dirname $outfile]]} \
 	{ic_mkdir [file dirname $outfile]}
    
    if [file exists $outfile] then {
 	if [tk_dialog .dxf_overwrite "DXF" \
		"File exists: overwrite?" "" -1 Yes No ] return
    } else {
 	if {![file exists dxf]} {ic_mkdir dxf}
    }
    
    set dirf ""
    set resp [file_select_lc "Directive file?  (Cancel if none)" ""]
    
    if 0 {
	if {$resp != ""} {set dirf "D=$resp"}       
	if $is_win {
	    set command "tm=AD I=$file O=$outfile L=dxf_list $dirf"
	} else {
	    set command "I=$file O=$outfile L=dxf_list $dirf"
	}
	ic_run_application_exec . dxf dxf_pre $command ;#OLD
    }
    
    ic_trans_ddn_dxf $file $outfile "" $resp
    
    if { $remove_file } { rm_tmp_file $file }
}

proc trans_dxf_ddn {} {
    global part_selected env parts_dir is_win file_name
    
    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {	
	set proj_dir $file_name(project_dir)
    }

    if [file exists $proj_dir/dxf] {
	set def_dir $proj_dir/dxf
    } else {
	set def_dir $proj_dir
    }
    set infile [file_select "Select the input DXF file." $def_dir ]    
    if {$infile == ""} return
    
    set part [ddn_select_part both 0]
    if {$part == ""} return
    set dir [lindex $part 0]
    set selected [lindex $part 1]
    set file [lindex $part 2]

    if [file exists $file] {
	if {[tk_dialog .dxf_overwrite "DXF" \
		"Part exists: overwrite?" "" -1 Yes No ] ==  1 } {
	    return
	}
    }
    
    set resp [file_select_lc "Directive file?  (Cancel if none)" ""]
    if 0 {
	set dirf ""
	if {$resp != ""} {set dirf "D=$resp"}
	
	if $is_win {
	    set command "tm=DA I=$infile O=$file L=dxf_list $dirf"
	} else {
	    set command "I=$infile O=$file L=dxf_list $dirf"
	}	
	ic_run_application_exec . dxf dxf_post $command ; # OLD
    } else {
	#set tdxf_list "dxf_list"
	ic_trans_dxf_ddn $infile $file "" $resp
    }
    update_partlist - 1
}

proc trans_prebfc_ddn {} {
    global env prebfc_infile prebfc_scale file_name
    
    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {	
	set proj_dir $file_name(project_dir)
    }
    if [file exists $proj_dir/prefbc] {
	set prebfc $proj_dir/prefbc
    } else {
	set prebfc $proj_dir
    }
    set prebfc_infile [ file_select "Select a PreBFC input file" $prebfc ]
    
    if {$prebfc_infile == ""} return

    set prebfc_infile [ file tail $prebfc_infile ]
    
    set outfile [question_box .prebfc_ddn_name "DDN part name" \
		     "Enter name of the DDN part\n(in the parts directory)." \
		     $prebfc_infile]
    if {$outfile == ""} return
    
    set part $file_name(parts_dir)/$outfile
    if [file exists $part] {
	set mess "Part $outfile exists - overwrite?"
	if [tk_dialog .overwrite Overwrite $mess "" -1 Yes No] return
    }
    
    set prebfc_scale [question_box .prebfc_ddn_scale "Scale" "Enter scale." 1.0]
    if {$prebfc_scale == ""} return

    ddn_run prebfc $outfile
    update_partlist - 1

}
proc ddn_app_before_prebfc { parts partname } {
    global file_name env prebfc_infile prebfc_scale

    set rst ""
    lappend rst F.5.13.5.6
    lappend rst $env(ICEM_ACN)/common/gpl
    lappend rst F.5.13.7.prebfc
    lappend rst $prebfc_infile
    lappend rst $prebfc_scale
    lappend rst F.4.3

    return $rst
}

proc ddn_app_after_prebfc { parts partname } {
}

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

proc trans_iges_tetin {} {
    global file_name iges_trans
    
    if {$file_name(iges_dir) == ""} { 
	set file_name(iges_dir) $file_name(project_dir)
    }    

    if {$file_name(iges_dir) != ""} {
	set file_name(iges_dir) $file_name(project_dir)
	set iges_trans(file) $file_name(iges_dir)/part.igs
    }
    
    set ch [med_file_select iges 0 $iges_trans(file) "to read in"]
    if {$ch == ""} return
    set iges_trans(file) $ch
    set file_name(iges_dir) [file dir $ch]
    set igesroot [file rootname [file tail $ch]]
    
    if {$file_name(mesh_dir) != ""} {
	set iges_trans(tetin_name) $file_name(mesh_dir)/${igesroot}.tin
    } else {
        set iges_trans(tetin_name) ./${igesroot}.tin
    }
    
    set iges_trans(listing)       0
    set iges_trans(listing_file)  $file_name(iges_dir)/iges_listing
    set iges_trans(dir_file)      default
    set iges_trans(dir_file_user) $file_name(iges_dir)/user_dir_file    
    set iges_trans(icempt)        0
    set iges_trans(data_red)      0
    set iges_trans(trans_dos)     0
    
    set buts {{Accept {trans_iges_tetin_doit}} reset cancel}
    set d [form_init .trans_iges_tetin_imp "Import IGES" "" $buts]
    if {$d != ""} {
	form_label $d.dirl "Directive file" {11 0}
	form_radiobutton $d.dirdef Default iges_trans(dir_file) default {11 1}
	form_radiobutton $d.dirnone None iges_trans(dir_file) none {12 1}
	form_radiobutton $d.dircat Catia iges_trans(dir_file) catia {13 1}
	form_radiobutton $d.diruser Selected iges_trans(dir_file) user {14 1}
	form_label $d.diruserl "File"  {15 0 -sticky e}
	futil_fileentry $d.diruserf iges_trans(dir_file_user) $file_name(project_dir) \
	    {15 1} {{iges_trans(dir_file) user}}
	form_checkbutton $d.dred "Data reduction" iges_trans(data_red) \
	    {20 0 -cols 2}
	form_checkbutton $d.slist "Save listing" iges_trans(listing) \
	    {21 0 -cols 2}
	form_label $d.slistl "File"  {22 0 -sticky e}
	futil_fileentry $d.slistf iges_trans(listing_file) $file_name(project_dir) \
	    {22 1} iges_trans(listing)
	form_checkbutton $d.transd "Translate DOS text" iges_trans(trans_dos) \
	    {23 0 -cols 2}
	form_finish +300+300
    }
    
    form_wait .trans_iges_tetin_imp     
}

proc trans_iges_tetin_doit {} {
    global iges_trans
    
    ic_trans_iges_tetin $iges_trans(file) $iges_trans(tetin_name) \
	    $iges_trans(dir_file) $iges_trans(dir_file_user) \
	    $iges_trans(data_red) $iges_trans(listing_file) \
	    $iges_trans(icempt) $iges_trans(trans_dos)
    
    file_check_load_tetin $iges_trans(tetin_name)
    mess "Done.\n"
}

proc trans_ddn_tetin {} {
    global file_name env is_win
    
    if {$is_win && ![file exists "$env(ICEM_ACN)/bin/ssish.exe"]} {
        mess "\nNote that the executable \"ssish.exe\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/bin\"\n" blue
        mess "Note that the directory \"tcl8.3.3\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/lib\"\n" blue
        return 
    }
    
    if {$file_name(parts_dir) == ""} {
	set $file_name(parts_dir) ./parts
    }
    if {$file_name(project_dir) == "" } {
        set project_dir .
    } else {
	set project_dir $file_name(project_dir)
    }
    set ddn [med_file_select ddn 0]
    if {$ddn == ""} return
    set def [file rootname [file tail $ddn]].tin
    set tetin [med_file_select tetin 1 $project_dir/$def "to create"]
    if {$tetin == ""} return
    
    ic_trans_ddn_tetin $ddn $tetin
    file_check_load_tetin $tetin
    mess "Done.\n"
}

proc trans_ug_tetin {} {
    global env file_name ug_trans ug_script_mode map_tetin_sizes
        
    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 }

    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {
	set proj_dir $file_name(project_dir)
    }
    if [file exists $proj_dir/ugparts] {
	set ug_trans(file) $proj_dir/ugparts/ug.prt
    } else {
	set ug_trans(file) $proj_dir/ug.prt
    }

    set ch [med_file_select ug 0 $ug_trans(file) "to read in"]

    if {$ch == ""} return
    set ug_trans(file) $ch

    set ug_trans(tetin_name) $file_name(project_dir)/ug_import.tin
    set user_script ./ugparts/user_script.tcl
    set ug_script_mode levels
    set ug_usr_opts ""

    set d [form_init .trans_ug_tetin_imp "UG -> Tetin"]
    if {$d != ""} {
        form_label $d.tetnamel "Tetin file" {0 0}
        futil_fileentry $d.tetname ug_trans(tetin_name) \
            "" {0 1}
        form_label $d.dirl "Input Option" {10 0}
        form_radiobutton $d.scrLevel Levels           ug_script_mode levels  {10 1}
        form_radiobutton $d.scrDef   "UG Plugin"      ug_script_mode ug_default {11 1}
        form_radiobutton $d.scrName  Named            ug_script_mode named   {12 1}
        form_radiobutton $d.scrUser  Selected         ug_script_mode user    {13 1}
        form_label       $d.scrUserl "File" {14 0 -sticky e}
        futil_fileentry  $d.scrUserf user_script ./ugparts              {14 1} \
                {{ug_script_mode user}}
        form_label       $d.scrUsrOl "User Options"     {15 0 -sticky e}
        form_entry       $d.scrUsrOp ug_usr_opts        string          {15 1} \
                {{ug_script_mode user}}
	form_checkbutton $d.map_sizes "Import Sizes From Tetin File" map_tetin_sizes(doit) {16 0}

        form_finish +300+300
    }
    if ![form_wait .trans_ug_tetin_imp] return

    set ug_script ""
    if {$ug_script_mode == "user" } { set ug_script $user_script }
    ic_trans_ug_tetin $ug_trans(file) $ug_trans(tetin_name) \
	    $ug_script_mode $ug_script $ug_usr_opts

    if [catch {file_check_load_tetin $ug_trans(tetin_name)} res] {
	mess "$res\n" red
    } elseif [file exists $ug_trans(tetin_name)] {
        if $map_tetin_sizes(doit) {
	    if [catch {do_map_tetin_sizes} res] {
	        mess "$res\n" red
	    }
    	}
    }
    mess "Done.\n"
}

proc trans_gems_tetin {} {
    global env file_name gems_trans

    if {(![info exists file_name(project_dir)]) || \
	    ($file_name(project_dir) == "")} {
	set proj_dir .
    } else {
	set proj_dir $file_name(project_dir)
    }
    if [file exists $proj_dir/gemsparts] {
	set gems_trans(file) $proj_dir/gemsparts/gems.sur
    } else {
	set gems_trans(file) $proj_dir/gems.sur
    }
    set ch [med_file_select gems 0 $gems_trans(file) "to read in"]
    if {$ch == ""} return
    set gems_trans(file) $ch

    set mesh_dir $file_name(mesh_dir)
    if ![file exists $mesh_dir] {
        if [file exists ./mesh] {
            set mesh_dir ./mesh
        } else {
            set mesh_dir .
        }
    }
    set gems_trans(tetin_name) $mesh_dir/gems.tin
    set gems_trans(gttol)      0.001
    set gems_trans(gtrel)      1
    set gems_usr_opts ""

    set buts {accept reset cancel}
    set d [form_init .trans_gems_tetin_imp "GEMS -> Tetin" "" $buts]
    if {$d != ""} {
	form_label $d.ttoll "Tri Tol" "13 0 -cols 2"
	form_entry $d.ttol gems_trans(gttol) float "13 1"
	form_checkbutton $d.trel "" gems_trans(gtrel) "13 2"
	form_label $d.trell "unitless" "13 3 -cols 2"
	form_label $d.ltetname "Tetin File"  {1 0 -sticky e}
        futil_fileentry $d.tetname gems_trans(tetin_name) \
            "" {1 1} "" *.tin
        form_finish +300+300
    }
    if [form_wait .trans_gems_tetin_imp] {
	trans_gems_tetin_doit
    }
}

proc trans_gems_tetin_doit {} {
    global gems_trans

    if { [ file exists $gems_trans(tetin_name) ] || $gems_trans(tetin_name) == "" } {
	    set tmess "File $gems_trans(tetin_name) exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set gems_trans(tetin_name) [med_file_select tetin 1 $gems_trans(tetin_name)]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }

    set dir [file dirname $gems_trans(tetin_name)]

    if { $gems_trans(tetin_name) == "" || ![file exists $dir] } {
 	mess "Invalid output filename: \"$gems_trans(tetin_name)\"\n" red
    } else {
        ic_trans_gems_tetin $gems_trans(file) $gems_trans(tetin_name) \
	    $gems_trans(gttol) $gems_trans(gtrel)

        file_check_load_tetin $gems_trans(tetin_name)
        mess "Done.\n"
    }
}

proc trans_tetin_iges {} {
    global file_name iges_trans env

    if {$file_name(tetin) != ""} {
	set tetin $file_name(tetin)
    } else {
	set tetin $file_name(mesh_dir)/
	set tetin [med_file_select tin 0 "" "to convert"]
	if {$tetin == ""} return
    }

    if {$iges_trans(file) == ""} {
	set iges_trans(file) file_name(iges_dir)/part.igs
    }
    set iges [med_file_select iges 1 "" "to create"]
    if {$iges == ""} return

    ic_trans_tetin_step $tetin $iges

    mess "Done.\n"
}

proc old_trans_tetin_iges {} {
    global file_name iges_trans env

    if {$file_name(tetin) != ""} {
	set tetin $file_name(tetin)
    } else {
	set tetin $file_name(mesh_dir)/
	set tetin [med_file_select tin 0 "" "to convert"]
	if {$tetin == ""} return
    }

    set ddn $file_name(parts_dir)/tmp_export_part

    ic_trans_tetin_ddn $tetin $ddn

    if {$iges_trans(file) == ""} {
	set iges_trans(file) file_name(iges_dir)/part.igs
    }
    set iges [med_file_select iges 1 "" "to create"]
    if {$iges == ""} return

    set iges_trans(file) $iges
    set file_name(iges_dir)   [file dir $iges]

    if {$iges_trans(listing_file) == ""} {
	set iges_trans(listing_file) iges/iges_listing
    }
    if {$iges_trans(dir_file_user) == ""} {
	set iges_trans(dir_file_user) $env(ICEM_ACN)/iges/dir_to_catia
    }
    set d [form_init .trans_iges_exp "DDN -> IGES"]
    if {$d != ""} {
	form_checkbutton $d.dirl "Directive file" iges_trans(dir_file_exp) \
	    {1 0}
	futil_fileentry $d.diruserf iges_trans(dir_file_user) $file_name(project_dir) \
	    {1 1} iges_trans(dir_file_exp)
	form_checkbutton $d.slist "Save listing" iges_trans(listing) \
	    {11 0 -cols 2}
	form_label $d.slistl "File"  {12 0 -sticky e}
	futil_fileentry $d.slistf iges_trans(listing_file) $file_name(project_dir) \
	    {12 1} iges_trans(listing)
	form_finish +300+300
    }
    if ![form_wait .trans_iges_exp] return

    set iges_trans(ddn_name) $ddn
    ddn_trans_iges_exp_doit
    mess "Done.\n"
}

proc trans_tetin_ddn {} {
    global file_name

    if {$file_name(tetin) != ""} {
	set tetin $file_name(tetin)
    } else {
	if {$file_name(mesh_dir) == ""} {
	    set tetin ./tetin1
  	} else {
	    set tetin $file_name(mesh_dir)/
	}
    }
    set tetin [med_file_select tin 0]
    if {$tetin == ""} return
    if {$file_name(parts_dir) == ""} {
	set default_parts_dir ./parts
    } else {
	set default_parts_dir $file_name(parts_dir)
    }
    set ddn [med_file_select ddn 1 "" "to write"]
    if {$ddn == ""} return

    set file_name(part_name) [string toupper [file rootname [file tail $tetin]]]
    set file_name(units)  -mm
    set d [form_init .trans_tetin_ddn "Tetin -> DDN"]
    if {$d != ""} {
  	form_label $d.ddnnamel "DDN name"       {0 0}
	form_entry $d.ddnname  file_name(part_name) string {0 1}
        form_label $d.ddnunitsl "DDN Units"     {1 0}
        form_radiobutton $d.units_mm MM      	 file_name(units) -mm     {1 1}
        form_radiobutton $d.units_in Inches  	 file_name(units) -inches {2 1}
        form_radiobutton $d.units_ft Inches/feet file_name(units) -feet   {3 1}
        form_finish +300+300
    }
    if ![form_wait .trans_tetin_ddn] return

    ic_trans_tetin_ddn $tetin $ddn $file_name(part_name) $file_name(units)
    mess "Done.\n"
}

proc trans_tetin_brepxml {} {
    global file_name

    if {$file_name(tetin) != ""} {
	set tetin $file_name(tetin)
    } else {
	if {$file_name(mesh_dir) == ""} {
	    set tetin ./tetin1
  	} else {
	    set tetin $file_name(mesh_dir)/
	}
    }
    set tetin [med_file_select tin 0]
    if {$tetin == ""} return

    if {$file_name(mesh_dir) == ""} {
	set brepxml_file ./tetin1.xml
    } else {
	set brepxml_file $file_name(mesh_dir)/
    }
    set brepxml_file [file_selection -default $brepxml_file \
	    -message "Please name a BrepXML file (*.xml) to write" \
	    -new_ok 1]
    if {$brepxml_file == ""} return

    set light_export ""
    # we could add the arg "-light" to for graphics only export
    ic_trans_tetin_brepxml $tetin $brepxml_file $light_export
    mess "Done.\n"
}

proc get_catia4_filter_list {} {
	global file_name catia_trans env filter

	if {$catia_trans(filter) == "inactive"} {
		set catia_trans(filter_arg) ""
		return
	}
	set filter_liste [eval exec $env(ICEM_ACN)/bin/c4tetin -tcl --show-filter-only $catia_trans(file) 2>/dev/null]
#	puts $filter_liste
#	puts "Filters:"
#	foreach item $filterlist {
#	    append filter_liste "$item\n"
#	    puts "$item"
#        }
#	puts "-------"
	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 "" "" 10x10 {3 0} ""  0 0 1
		#form_listbox_set $d.list [lsort $filter_liste]
		#form_listbox_set $d.list {$filter_liste}
		form_finish +300+300
	#} else {
	#	puts "form_init failed"
	}
	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

	#puts "filter_arg=$filter_arg"
	form_kill .select_filter

	#puts "$catia_trans(filter)"
	#puts "-------"

	#-----------------------------------------------------------------------------------------------------
}

proc set_catiamif_defaults {} {
	global catia_trans env
	if {$catia_trans(catiamif) != ""} {
		set catia_trans(famnam) "--famnam=%f"
		set catia_trans(entnam) "--entnam=ID%I"
		if [file exists $env(HOME)/.catmeshrc] {
			set catia_trans(rcfile) "+catiamif-rcfile"
		}
	}
}

proc trans_catia_tetin {} {
	#
        # 12/13/2000 - az: adjusted for c4tetin V1.7.2
        # 01/14/2001 - az: prepared for c4tetin V1.7.22
	# 01/10/2002 - az: repair of garbled source and restoring comments
	# 11/19/2002 - az: accept a few settings from CATIAMIF .catmeshrc
	# 12/09/2002 - az: use .catmeshrc (V1.15.25)
	# 02/11/2003 - az: support CATIA Layer Filter with GUI support (requires at least V1.16 or V1.16beta#80)
        #
	global file_name catia_trans env active_filter_liste filter

	set catia_trans(bocu) ""
	set catia_trans(loops) ""
	set catia_trans(nodom) ""
	set catia_trans(cpoints) ""

	if {($catia_trans(file) == "") || ($catia_trans(project_dir) != $file_name(project_dir))} {
		set catia_trans(file) $file_name(project_dir)/catia
	}
	set catia_trans(project_dir) $file_name(project_dir)
	set ch [file_select "Select a CATIA file." $catia_trans(file)]
	if {$ch == ""} return
	set catia_trans(file) $ch

	if {$catia_trans(listing_file) == ""} {
		set catia_trans(listing_file) catia/catia_listing
	}
	set model ""
	regexp {^(.*)/([^/]*)(.model|.exp)$} $catia_trans(file) tmpall tmppath model
	if {$model == ""} {
		regexp {^(.*)/([^/]*)$} $catia_trans(file) tmpall tmppath model
	}
	if {$file_name(mesh_dir) == ""} {
		set catia_trans(tetin_name) ./$model.tin
		set catia_trans(anf_name) ./$model.anf
	} else {
		set catia_trans(tetin_name) $file_name(mesh_dir)/$model.tin
		set catia_trans(anf_name) $file_name(mesh_dir)/$model.anf
	}
	set catia_trans(famnam) ""
	set catia_trans(entnam) ""
	set catia_trans(npnts) "--suppress-points"
	set catia_trans(ncrvs) "--suppress-curves"
	set catia_trans(ndoms) ""
	set catia_trans(nsrfs) ""
	set catia_trans(range) ""
	set catia_trans(do_anf) "-no"
	set catia_trans(rcfile) ""
	set catia_trans(laxis) ""
	set catia_trans(catiamif) ""
	set catia_trans(inhlayer) ""
	set catia_trans(filter_arg) ""
	set catia_trans(filter) "inactive"
	#set do_tetin "-one"
	set catia_trans(do_tetin) "-one"

	#
	# fetch some settings from the .catmeshrc file
	#
	set catmeshrc $env(HOME)/.catmeshrc
	#mess ".catmeshrc is $catmeshrc\n"

	set without_catmeshrc {{catia_trans(rcfile) ""}}
	set without_catiamif {{catia_trans(catiamif) ""}}

	get_catia4_filter_list

	set buts {{Accept {trans_catia_tetin_doit}} reset cancel}
	set md [form_init .trans_catia_tetin_imp "CATIA V4 -> Tetin" "" $buts]
	if {$md != ""} {
		set d $md.main
	form_frame $md.main flat 0 {0 0}
#		if [file exists $env(HOME)/.catmeshrc] {
#		    form_frame $d.rcfiles ridge 3 {1 0 -cols 2}
#			set cd $d.rcfiles
#			form_checkbutton $cd.catmeshrc "Use $catmeshrc" catia_trans(rcfile) {1 0} "" "" 1
#			$cd.catmeshrc configure -onvalue "+catiamif-rcfile" -offvalue "" -anchor nw
#		}
		form_frame $d.tetin ridge 0 {2 0 -cols 2}
			set td $d.tetin
			#form_checkbutton $td.tetnamel "TETIN file" catia_trans(do_tetin) {0 0} "" "" 1
			#$td.tetnamel configure -onvalue "-one" -offvalue "-no" -anchor nw
			form_label $td.label "TETIN File" {0 0}
			futil_fileentry $td.tetname catia_trans(tetin_name) $file_name(mesh_dir) {0 1}

			#form_checkbutton $td.anfnamel "ANSYS ANF file" catia_trans(do_anf) {1 0} "" "" 1
			#$td.anfnamel configure -onvalue "" -offvalue "-no" -anchor nw
			#futil_fileentry $td.anfname catia_trans(anf_name) $file_name(mesh_dir) {1 1}


		form_frame $d.display ridge 3 {6 0}
			set dd $d.display
			form_label $dd.label "CATIA Input Filter" {0 0 -cols 2}
			form_checkbutton $dd.show "SHOW" catia_trans(show) {5 0} "" "" 1
			$dd.show configure -onvalue "+show" -offvalue "-show" -anchor nw
			form_checkbutton $dd.noshow "NOSHOW" catia_trans(noshow) {5 1} "" "" 1
			$dd.noshow configure -onvalue "+noshow" -offvalue "-noshow" -anchor nw
			form_checkbutton $dd.pick "PICK" catia_trans(pick) {7 0} "" "" 1
			$dd.pick configure -onvalue "+pick" -offvalue "-pick" -anchor nw
			form_checkbutton $dd.nopick "NOPICK" catia_trans(nopick) {7 1} "" "" 1
			$dd.nopick configure -onvalue "+nopick" -offvalue "-nopick" -anchor nw
			form_label $dd.empty " " {10 0}

			form_checkbutton $dd.filter "Use CATIA Layer Filter" catia_trans(filter) {8 0 -cols 2} "" get_catia4_filter_list 1
			$dd.filter configure -onvalue "active" -offvalue "inactive" -anchor nw
			form_entry $dd.filterargs catia_trans(filter_arg) string {9 0 -cols 2} {{catia_trans(filter) "active"}}
			#form_label $dd.filterargs $catia_trans(filter_arg) {9 0 -cols 2}

		form_frame $d.output ridge 3 {6 1}
			set od $d.output
			form_label $od.label "TETIN Output Filter" {0 0}
			form_checkbutton $od.npnts "No Points" catia_trans(npnts) {3 0} $without_catmeshrc "" 1
			$od.npnts configure -onvalue "--suppress-points" -offvalue "" -anchor nw
			form_checkbutton $od.ncrvs "No Curves" catia_trans(ncrvs) {4 0} $without_catmeshrc "" 1
			$od.ncrvs configure -onvalue "--suppress-curves" -offvalue "" -anchor nw
			form_checkbutton $od.ndoms "No Domains" catia_trans(ndoms) {5 0} $without_catmeshrc "" 1
			$od.ndoms configure -onvalue "--suppress-domains" -offvalue "" -anchor nw
			form_checkbutton $od.nsrfs "No Surfaces" catia_trans(nsrfs) {6 0} $without_catmeshrc "" 1
			$od.nsrfs configure -onvalue "--suppress-surfaces" -offvalue "" -anchor nw

		if [file exists $env(HOME)/.catmeshrc] {
			form_checkbutton $d.catmeshrc "Use $catmeshrc" catia_trans(rcfile) {11 0} "" "" 1
			$d.catmeshrc configure -onvalue "+catiamif-rcfile" -offvalue "" -anchor nw
		}
		form_checkbutton $d.catiamif "Apply CATIAMIF Preferences (Modals & Setup) from CATIA model file" catia_trans(catiamif) {12 0 -cols 2} "" set_catiamif_defaults 1
		$d.catiamif configure -onvalue "+catiamif-model" -offvalue "" -anchor nw
		#form_checkbutton $d.preview "Preview" catia_trans(preview) {13 0 -cols 2} "" "" 1
		#$d.preview configure -onvalue "--preview" -offvalue "" -anchor nw
		form_checkbutton $d.rangerr "Ignore Range Errors" catia_trans(range) {14 0 -cols 2} "" "" 1
		$d.rangerr configure -onvalue "--ignore-out-of-range" -offvalue "" -anchor nw
		form_checkbutton $d.bocu "With Boundary Curves" catia_trans(bocu) {15 0 -cols 2} "" "" 1
		set with_boundary_curves "--dont-suppress-curves --with-boundary-curves --share-common-edges"
		$d.bocu configure -onvalue "$with_boundary_curves" -offvalue "" -anchor nw
		# V1.7.22
		# form_checkbutton $d.loops "Loops instaed of Faces" catia_trans(loops) {16 0 -cols 2} "" "" 1
		# $d.loops configure -onvalue "--loops" -offvalue "" -anchor nw
		form_checkbutton $d.loops "Loops instead of Faces" catia_trans(loops) {16 0 -cols 2} "" "" 1
		$d.loops configure -onvalue "--planes-as-loops $with_boundary_curves" -offvalue "" -anchor nw
		form_checkbutton $d.bobox "Create Bounding Box" catia_trans(bobox) {17 0 -cols 2} $without_catmeshrc "" 1
		$d.bobox configure -onvalue "--write-bounding-box"  -offvalue "" -anchor nw
		form_checkbutton $d.nodom "Use QUAD to mesh Polyhedral Solids/Faces" catia_trans(nodom) {18 0 -cols 2} "" "" 1
		$d.nodom  configure -onvalue "--with-polysolid-domains --without-polysolid-faces" -offvalue "--with-polysolid-faces --without-polysolid-domains" -anchor nw
		form_checkbutton $d.automax "Estimate Maximum Size" catia_trans(automaxsize) {19 0 -cols 2} $without_catmeshrc "" 1
		$d.automax  configure -onvalue "--maximum_size=AUTO" -offvalue "" -anchor nw
		form_checkbutton $d.inhlayer "Take Layer from Shell/Skin/Solid" catia_trans(inhlayer) {20 0 -cols 2} "" "" 1
		$d.inhlayer  configure -onvalue "--inherit-layer" -offvalue "" -anchor nw
		form_checkbutton $d.laxis "Transform to active Axis System" catia_trans(laxis) {21 0 -cols 2} "" "" 1
		$d.laxis  configure -onvalue "--apply-axis-system" -offvalue "" -anchor nw
		# form_checkbutton $d.cpoints "Anchor Vertices (Solids)" catia_trans(cpoints) {20 0 -cols 2} "" "" 1
		# $d.cpoints configure -onvalue "+cpoints" -offvalue "" -anchor nw

		form_frame $d.family ridge 1 {25 0}
			set fd $d.family
			form_label $fd.title "Part Naming" {0 0 -cols 4}
			form_radiobutton $fd.famdef "Default" catia_trans(famnam) "" {1 0} $without_catmeshrc
			form_radiobutton $fd.famlay "Part by Layer" catia_trans(famnam) "--family-by-layer" {2 0} $without_catmeshrc
			form_radiobutton $fd.famcol "Part by Color" catia_trans(famnam) "--family-by-color" {3 0} $without_catmeshrc
			form_radiobutton $fd.fammif "CATIAMIF-Parts" catia_trans(famnam) "--famnam=%f" {4 0} $without_catmeshrc
		form_frame $d.entity ridge 1 {25 1}
			set fd $d.entity
			form_label $fd.title "Entity Naming" {0 0 -cols 4}
			form_radiobutton $fd.entdef "Default" catia_trans(entnam) "" {1 0} $without_catmeshrc
			form_radiobutton $fd.entcad "CATIA-Names" catia_trans(entnam) "--entnam=%c_@subid" {2 0} $without_catmeshrc
			form_radiobutton $fd.entmif "CATIAMIF-Names" catia_trans(entnam) "--entnam=ID%I" {4 0} $without_catmeshrc
			#form_radiobutton $fd.entcol "Part by Color" entnam "--entily-by-color" {3 0} $without_catmeshrc
			#form_radiobutton $fd.entmif "CATIAMIF-Parts" entnam "--entnam=%f" {4 0} $without_catmeshrc

		form_label $d.xtritol "Triangulation Tolerance" {26 0} $without_catmeshrc
		form_entry $d.tritol catia_trans(tritol) float {26 1} $without_catmeshrc

		form_label $d.xargs "Additional Options" {27 0}
		form_entry $d.args catia_trans(args) string {27 1}

		form_finish +300+300
	}
	form_wait .trans_catia_tetin_imp
	return
}

proc trans_catia_tetin_doit {} {
    global file_name catia_trans batch_mode

    set c4args "--overwrite \
		--fix-icemcfd-face-surface-nloops-bug \
		--icemcfd=50 \
		$catia_trans(rcfile) \
		$catia_trans(catiamif) \
		-$catia_trans(do_tetin)-tetin-file=\"{$catia_trans(tetin_name)}\" \
		-$catia_trans(do_anf)-anf-file=\"{$catia_trans(anf_name)}\" \
		$catia_trans(inhlayer) \
		$catia_trans(laxis) \
		$catia_trans(automaxsize) \
		$catia_trans(bobox) \
		$catia_trans(npnts) \
		$catia_trans(nsrfs) \
		$catia_trans(ndoms) \
		$catia_trans(ncrvs) \
		$catia_trans(bocu) \
		$catia_trans(loops) \
		$catia_trans(nodom) \
		$catia_trans(cpoints) \
		$catia_trans(famnam) \
		$catia_trans(entnam)  \
		--triangulation-tolerance=$catia_trans(tritol) \
		$catia_trans(show) \
		$catia_trans(noshow) \
		$catia_trans(pick) \
		$catia_trans(nopick) \
		$catia_trans(args) \
		$catia_trans(filter_arg) \
		\"$catia_trans(file)\""
    # exec rm -f $catia_trans(tetin_name)
    if [file exists $catia_trans(tetin_name)] {
	ic_mv $catia_trans(tetin_name) $catia_trans(tetin_name).bak
    }
    ic_run_application_exec . bin c4tetin "-tcl $c4args"
    #run_application_exec "c4tetin -tcl $c4args"
    if [file exists $catia_trans(tetin_name)] {
	# ic_unload_tetin
	# orig:
	# ic_load_tetin $catia_trans(tetin_name)
	# version 1
	if {[file_continue_loading tetin] == "cancel"} {
		mess "Not loading tetin file $catia_trans(tetin_name)\n"
		return
	}
	file_do_load_tetin [list $catia_trans(tetin_name)]
	if [file exists $catia_trans(tetin_name).bak] {
		set foundit "Found existing tetin file, $catia_trans(tetin_name).bak.  Transfer mesh parameters?"
                if {$batch_mode} {
                    set doit yes
                } else {
		    set doit [tk_messageBox -type yesno -message $foundit]
                }
		if {$doit == "yes"} {
			mess "Transferring meshing parameters from old tetin db at $catia_trans(tetin_name).bak\n"
			if [catch {ic_geo_map_tetin_sizes $catia_trans(tetin_name).bak} res] {
				mess "Warning: $res\n"
			}
		}
		ic_rm $catia_trans(tetin_name).bak
	}
	display_update geo
	# version 2
	# ic_load_tetin $catia_trans(tetin_name)
	# ic_geo_summary
	# disp_new_stuff
	mess "Ok.\n"
	form_kill .trans_catia_tetin_imp
    } else {
	mess "Conversion failed.\n"
    }
#	#
#	mess "Running c4tetin $c4args\n"
#	catch "exec c4tetin -tcl $c4args" output
#	set pos [string last "Ready" $output]
#	if {$pos == -1} {
#	mess "Error:\n" red
#		mess "$output\n" red
#	} else {
#		mess "$output\n" blue
#		if {$do_tetin == "-one"} {
#			ic_load_tetin $catia_trans(tetin_name)
#		}
#	}
#	mess "Done.\n"
}

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

proc trans_cadds_tetin {} {
    global env is_win file_name

    if $is_win {
        mess "Not supported on windows.\n"
        return
    }

    if [info exists env(CVPATH)] {
    if [info exists env(CVDORS)] {
        set cvpath $env(CVPATH)
        set partdir [lindex [split $cvpath =] 0]
        set partfile [file_selection -get_dir 1 \
                                     -default $partdir \
                                     -new_ok 0 \
                                     -mkdir_button 0 \
                                     -message "Select Cadds Part"]

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

        set tetinfile $file_name(project_dir)/[file rootname $partfile]

        set tetinfile [med_file_select tetin 1 $tetinfile.tin]

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

        ic_run_application_exec . bin cadds2tin "$partfile $tetinfile"

        mess "Done.\n"

        file_check_load_tetin $tetinfile

        return
    }
    }

    mess "Please set CVDORS to locate cvdors installation and \
          CVPATH to locate Cadds parts\n"
}

proc trans_ideas_tetin {} {
    global env is_win file_name

    if [info exists env(SDRC_INSTL)] {

    set ideasfile [med_file_select ideas 0 $file_name(project_dir)]

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

    set tetinfile $file_name(project_dir)/[file tail [file rootname $ideasfile]]

    set tetinfile [med_file_select tetin 1 $tetinfile.tin]

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

    # Make a full path in case of relative path

    if {[file pathtype $ideasfile] == "relative"} {
        set ideasfile [pwd]/$ideasfile
    }

    if {[file pathtype $tetinfile] == "relative"} {
        set tetinfile [pwd]/$tetinfile
    }

    set currdir [pwd]
    cd [file dirname $ideasfile]
    set ideasfile [pwd]/[file tail $ideasfile]
    cd [file dirname $tetinfile]
    set tetinfile [pwd]/[file tail $tetinfile]
    cd $currdir


    # On Windows, change all forward slashes to back slashes
    if $is_win {
        regsub -all {\/} $ideasfile {\\\\} ideasfile
    }

    ic_run_application_exec . bin ideas2tin "$ideasfile $tetinfile"

    mess "Done.\n"

    file_check_load_tetin $tetinfile
    } else {
        mess "Please set SDRC_INSTL to SDRC installation directory\n"
    }
}

proc trans_idi_tetin {} {
    global env is_win file_name map_tetin_sizes


    set idifile [med_file_select idi 0 $file_name(project_dir)]

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

    set tetinfile $file_name(project_dir)/[file rootname $idifile]

    set tetinfile [med_file_select tetin 1 $tetinfile.tin]

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

    if [catch {ic_trans_idi_tetin $idifile $tetinfile} res] {
    	mess "Error:  $res\n"
    } else {
	    file_check_load_tetin $tetinfile
        if [file exists $tetinfile] {
            set tmess "Import families, sizes from existing tetin file?"
            if {[tk_dialog .import_sizes "Import sizes" $tmess "" -1 Yes No] == 0} { 
	            if [catch {do_map_tetin_sizes} res] {
	                mess "$res\n" red
	            }
            }
            if [winfo exists .import_sizes] { top_destroy  .import_sizes; return }
        }
	mess "Done.\n"
    }
}

proc trans_idi_tetin_doit {} {
    global idi_trans

    if { [ file exists $idi_trans(tetinfile) ] || $idi_trans(tetinfile) == "" } {
	    set tmess "File $idi_trans(tetinfile) exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set idi_trans(tetinfile) [med_file_select tetin 1 $idi_trans(tetinfile)]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }

    set dir [file dirname $idi_trans(tetinfile)]
    set tol 0.0001
    set isRel 1
    set idi_options ""

    if {$idi_trans(tria) == 1} {append idi_options "-tri "}
    if {$idi_trans(ng) == 1} {append idi_options "-ng "}
    if {$idi_trans(part_name) == 1} {append idi_options "-fnum "}
    if {$idi_trans(part_assem) == 1} {
       if {$idi_trans(assem_name) == 0} {append idi_options "-pname "}
       if {$idi_trans(assem_name) == 1} {append idi_options "-pnum "}
       if {$idi_trans(noasm) == 1} {append idi_options "-noasm "}
       if {$idi_trans(fmrg) == 1} {append idi_options "-fmrg "}
    }

    if { $idi_trans(tetinfile) == "" || ![file exists $dir] } {
 	mess "Invalid output filename: \"$idi_trans(tetinfile)\"\n" red
    } else {
        ic_trans_idi_tetin $idi_trans(file) $idi_trans(tetinfile) $tol $isRel $idi_options
        file_check_load_tetin $idi_trans(tetinfile)
        mess "Done.\n"
    }
}

proc trans_proe_tetin {} {
    global env is_win file_name proe_trans

    set buttons { {Button_name button_fcn} {Reset reset} {Done done} }
    set Version [ic_version 0]
    set valid_exec_path 1
    if [catch {set exec_path $env(PROE_START_CMD$Version) } res] {
        set valid_exec_path 0
        mess "Invalid PROE_START_CMD$Version.  Please set this environment variable to the script you use to start Pro/Engineer." red
    }

    set proe_trans(type) 0

        set proe_trans(meth) 0
        if !$proe_trans(type) {
                set buts {{Accept {trans_proe_tetin_action}} reset cancel}
                set d [form_init .trans_proe_meth "ProE -> Tetin" "" $buts]
                if {$d != ""} {
                        form_radiobutton $d.dump "Dump Model to Tetin"  proe_trans(meth) 0 {1 1}
                        form_radiobutton $d.fams "Use Predefined ICEM Families"  proe_trans(meth) 1 {2 1}
                        form_radiobutton $d.lyrs "ProE Layers to ICEM Families"  proe_trans(meth) 2 {3 1}
                        form_finish +200+200
                }
                form_wait .trans_proe_meth
        } else {
            trans_proe_tetin_action
        }
}


proc trans_proe_tetin_action {} {
   global env is_win file_name proe_trans pro_com_path

    set Version [ic_version 0]
    if ![info exists env(PROE_START_CMD$Version)] {
        mess "Please set the environment variable PROE_START_CMD$Version to the full path\
            of your Pro/Engineer start script. This is typically located in the bin\
            directory of your Pro/Engineer installation\n" red	 
            return
    }
    if ![info exists env(PROMIF_ACN$Version)] {
        mess "Your PROMIF_ACN$Version variable is not set, without this the import will likely fail.\n" red	 
    }
    if $is_win {
        set promif_dll_file $env(PROMIF_ACN$Version)/promif.dll
        if ![file exists $promif_dll_file] {
            mess "Your PROMIF_ACN$Version variable may not be set correctly to load the translator module.\n" red
        }
    }
    set start_parse 0
    set open_proe_str ""
    set prtfile [med_file_select proe 0 $file_name(project_dir)]
    if {$prtfile == ""} {
        return
    }
    update
    regsub -all {\\} $prtfile {\\\\} prtfile
    if $is_win {
        regsub -all {\/} $prtfile {\\\\} argprtfile
    } else {
        set argprtfile $prtfile
    }

    if $is_win {
        set cut_point [string last / $prtfile]
        set offset 1
        set cut_point "[expr $cut_point + $offset]"
        set tetfileloc  [string range $prtfile $cut_point end]
    } else {
        set tetfileloc [pwd]/[file tail $prtfile]
    }
    if {$tetfileloc == ""} {
        mess "Invalid Input File\n"
        return
    }
    set tetinfile $tetfileloc
    regsub -all .prt $tetinfile {} $tetinfile
    regsub -all {\\} $tetinfile {\\\\} tetinfile

#   automatically use the part file to generate the tetin file
    if $is_win {
        regsub -all {\/} $tetinfile {\\\\} argtetinfile
    } else {
        set argtetinfile $tetinfile
    }

    if $is_win {
        set part_name {~ Activate `main_dlg_cur` `File.psh_open`
~ Select `file_open` `Ph_list.Filelist` \\ 
 `Part`
~ Activate `file_open` `Inputname` \
 `Part`}
    } else {
        set part_name {~ Activate `main_dlg_cur` `File.psh_open`
~ Select `file_open` `Ph_list.Filelist` \\ 
 `Part`
~ Activate `file_open` `Inputname` \
 `Part`}
    }
    regsub -all Part $part_name $prtfile part_name
    set PF $part_name
    set TF $tetinfile
    regsub -all .tin $TF {} TF
    set cut_point [string last "/" $TF]
    set idx [expr $cut_point+1]
    set TF  [string range $TF $idx end]
    expr 5 / ( [string length "abcd"] + 0.0 )
    set PROJ $file_name(project_dir)
    set cut_point [string first "/" $TF]
    set idx [expr $cut_point+1]
    set PROJ  [string range $PROJ $idx end]
    
    set peVer $env(ANSYS_ICPEVER$Version)
    mess "Detected cad version: $peVer. \n" orange
      
    if {$peVer != "Creo"} {  
    mess "Using OLD trail format.\n" orange
    set proe_dump_trail {!trail file version No. 1151
!Pro/ENGINEER  TM  2001  (c) 1988-2001 by Parametric Technology Corporation  All Rights Reserved.
@@PARTNAME
#ICEM CFD
.
#DIRECT INTERFACE SETTINGS
~ Activate `settings` `optimizeButton` 1
~ Activate `settings` `autoGeomDumpButton` 1
~ Activate `settings` `OK`
#WRITE TETIN
@@TETFILE
#DEFAULT
#OVERWRITE EXISTING FILE
#DEFAULT
~ Select `main_dlg_cur` `MenuBar1` \
1  `File`
~ Close `main_dlg_cur` `MenuBar1`
~ Activate `main_dlg_cur` `File.psh_exit`
~ FocusIn `UI Message Dialog` `no`
~ FocusIn `UI Message Dialog` `yes`
~ Activate `UI Message Dialog` `yes`
!}
} else {
mess "Using NEW Creo trail format.\n" orange
set proe_dump_trail {!trail file version No. 1500
~ Activate `main_dlg_cur` `main_dlg_cur`
~ Activate `main_dlg_cur` `main_dlg_cur`
~ Select `main_dlg_cur` `appl_casc`
~ Close `main_dlg_cur` `appl_casc`
~ Command `ProCmdModelOpen` 
~ Trail `UI Desktop` `UI Desktop` `DLG_PREVIEW_POST` \
 `file_open`
~ Select `file_open` `Ph_list.Filelist` 1 `@@PARTNAME`
~ Trail `UI Desktop` `UI Desktop` `PREVIEW_POPUP_TIMER` \
 `file_open:Ph_list.Filelist:<NULL>`
~ Command `ProFileSelPushOpen@context_dlg_open_cmd` 
~ Timer `UI Desktop` `UI Desktop` `EmbedBrowserTimer`
#ICEM CFD
.
#DIRECT INTERFACE SETTINGS
~ Activate `settings` `autoGeomDumpButton` 1
~ Activate `settings` `OK`
#WRITE TETIN
@@TETFILE
#OVERWRITE EXISTING FILE
#DEFAULT
~ Select `main_dlg_cur` `appl_casc`
~ Close `main_dlg_cur` `appl_casc`
~ Command `ProCmdExit` 
~ FocusIn `UI Message Dialog` `yes`
~ Trail `UI Desktop` `UI Desktop` `PREVIEW_POPUP_TIMER` \
 `main_dlg_w1:PHTLeft.AssyTree:<NULL>`
~ Activate `UI Message Dialog` `yes`
!End of Trail File}
}

#        regsub -all @@PROJ      $proe_dump_trail $PROJ         proe_dump_trail
    regsub -all @@TETFILE  $proe_dump_trail $TF proe_dump_trail
    regsub -all @@PARTNAME $proe_dump_trail $PF proe_dump_trail
    set isasm [string first .asm $prtfile]
    if {$isasm != -1} {
        set sub {#WRITE ASSEMBLY
#SINGLE FILE}
        set replace_me {#WRITE TETIN}
        regsub -all $replace_me $proe_dump_trail $sub proe_dump_trail
    }
    #Dump Output
    mess "Generating trail file " blue
    if {$proe_trans(meth) == "0"} {
        mess "using geometry dump\n" blue
        set dump_str {`autoGeomDumpButton` 0}
        set sub {`autoGeomDumpButton` 1}
        regsub -all $dump_str $proe_dump_trail $sub proe_dump_trail
    }
    #Normal Output
    if {$proe_trans(meth) == "1"} {
        mess "using parts\n" blue
        set dump_str {`autoGeomDumpButton` 1}
        set sub {`autoGeomDumpButton` 0}
        regsub -all $dump_str $proe_dump_trail $sub proe_dump_trail
    }
    #Layers Output
    if {$proe_trans(meth) == "2"} {
        mess "using proe layers dump\n" blue
        set dump_str {`autoGeomDumpButton` 1}
        set sub {`autoGeomDumpButton` 0
~ Activate `settings` `writeAllLayersButton` 1
~ Activate `settings` `writeHiddenLayersButton` 1}
        regsub -all $dump_str  $proe_dump_trail $sub  proe_dump_trail
    }
    set fname tmp_trail.txt
    set fp [open $fname w]
    puts $fp "$proe_dump_trail"
    close $fp
    set env(NMSD_TIMEOUT) "15"
    if [info exists env(LM_LICENSE_FILE)] {
        if !$is_win {
            set lm_license_file $env(LM_LICENSE_FILE)
            unset env(LM_LICENSE_FILE)
        }
    }
    # clean out std* files.
    set file "std.err"
    if [file exists $file] {
        mess "cleaning up std.err\n"
        file delete $file
    }

    set file "std.out"
    if [file exists $file] {
        mess "cleaning up std.out\n"
        file delete $file
    }
    mess "Importing ProE Model, please wait ...\n" blue 
    set Version [ic_version 0]
    set exec_path $env(PROE_START_CMD$Version)
    busy 1
    if  $proe_trans(type) {
        set env(IS_PROTCL) TRUE
        ic_run_application_exec . bin proe2tin "$argprtfile $argtetinfile"
        unset env(IS_PROTCL)
    } else {
        catch {exec $exec_path -g:no_graphics $fname} res
    }
    set file "std.err"
    if [file exists $file] {
        mess "Possible Translation Errors listed in 'std.err':" red
        set x 0
        set f [open $file r]
        while (![eof $f]) {
            set line($x) [gets $f]
            mess "$f\n" red
            incr x
        }
        close $f
        set file std_out($file)
    }
    busy reset
    if [info exists lm_license_file] {
        if !$is_win { set env(LM_LICENSE_FILE) $lm_license_file }
    }
    mess "Translation/Import complete. Loading $tetinfile\n" blue
    set tetinfile $tetinfile.tin
    if [file exists $tetinfile] {
        file_check_load_tetin $tetinfile
    } else {
        mess "Import failed to generate a tetin file.  Please refer to the 'std.out' \n\
        file in your project directory for details concerning problems encountered\n\
        during the import.\n" red
    }
}

proc trans_proe_tetin_doit {} {
    global env is_win file_name proe_trans pro_com_path

    set Version [ic_version 0]
    if [info exists env(PROE_START_CMD$Version)] {

	if [info exists env(PRO_COMM_MSG_EXE)] {
		set pro_com_path $env(PRO_COMM_MSG_EXE)
		if ![file exists $pro_com_path] {
			if $is_win {
					mess "MED detected an invalid PRO_COMM_MSG_EXE setting.\n \
					Currently the variable is set to: \n\
					\t $env(PRO_COMM_MSG_EXE)\n \
					Set the variable to the location of the file \n\
					pro_comm_msg.exe.  \n\
					This is usually located in the i486_nt\\obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
					return;
			} else {
				if {$unix_type == "IRIX64"} {
					mess "MED detected an invalid PRO_COMM_MSG_EXE setting.\n \
					Currently the variable is set to: \n\
					\t $env(PRO_COMM_MSG_EXE)\n \
					Set the variable to the location of the file \n\
					pro_comm_msg.  \n\
					This is usually located in the sgi_mips4/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red 
					return;
					}
				if {$unix_type == "SunOS"} {
					mess "MED detected an invalid PRO_COMM_MSG_EXE setting.\n \
					Currently the variable is set to: \n\
					\t $env(PRO_COMM_MSG_EXE)\n \
					Set the variable to the location of the file \n\
					pro_comm_msg.  \n\
					This is usually located in the sun4_solaris/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
					return;
					}
				if {$unix_type == "HP-UX"} {
					mess "MED detected an invalid PRO_COMM_MSG_EXE setting.\n \
					Currently the variable is set to: \n\
					\t $env(PRO_COMM_MSG_EXE)\n \
					Set the variable to the location of the file \n\
					pro_comm_msg.  \n\
					This is usually located in the hp8k/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
					return;
					}
				}
			}
	} else  {
		set exec_path $env(PROE_START_CMD$Version)
		set unix_type [exec uname -s]
		
		if $is_win {
			set cut_point [string last "\\bin" $exec_path]
			set pro_com_path  [string range $exec_path 0 $cut_point]

			set quote "\""
			set first [string range $pro_com_path 0 0]

			if {$first == $quote} {
				regsub -all {\"} $pro_com_path {} pro_com_path
			}

			set append "i486_nt\\obj\\pro_comm_msg.exe"
			set pro_com_path "$pro_com_path$append"
			set env(PRO_COMM_MSG_EXE) $pro_com_path	
		} 

		if { $unix_type == "IRIX64"} {
			set cut_point [string last "/bin" $exec_path]
			set pro_com_path  [string range $exec_path 0 $cut_point]
			set pro_com_path "$pro_com_path/sgi_mips4/obj/pro_comm_msg"
			set env(PRO_COMM_MSG_EXE) $pro_com_path	
		}
		if {$unix_type == "SunOS"} {
			set cut_point [string last "/bin" $exec_path]
			set pro_com_path  [string range $exec_path 0 $cut_point]
			set pro_com_path "$pro_com_path/sun4_solaris/obj/pro_comm_msg"
			set env(PRO_COMM_MSG_EXE) $pro_com_path	
		}
		if {$unix_type == "HP-UX"} {
			set cut_point [string last "/bin" $exec_path]
			set pro_com_path  [string range $exec_path 0 $cut_point]
			set pro_com_path "$pro_com_path\\hp8k\\obj\\pro_comm_msg.exe"
			set env(PRO_COMM_MSG_EXE) $pro_com_path	
		}

		if ![file exists $env(PRO_COMM_MSG_EXE)] { 
			if $is_win {
					mess "Unable to determine PRO_COMM_MSG_EXE location\n \
					Tried:\t $env(PRO_COMM_MSG_EXE)\n \
					Set the environment variable PRO_COMM_MSG_EXE\n\
					to the location of the file pro_comm_msg.exe \n \
					This is usually located in the i486_nt\\obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
			} else {
				if {$unix_type == "IRIX64"} {
					mess "Unable to determine PRO_COMM_MSG_EXE location\n \
					Tried:\t $env(PRO_COMM_MSG_EXE)\n \
					Set the environment variable PRO_COMM_MSG_EXE\n\
					to the location of the file pro_comm_msg \n \
					This is usually located in the sgi_mips4/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red 
				}
				if {$unix_type == "SunOS"} {
					mess "Unable to determine PRO_COMM_MSG_EXE location\n \
					Tried:\t $env(PRO_COMM_MSG_EXE)\n \
					Set the environment variable PRO_COMM_MSG_EXE\n\
					to the location of the file pro_comm_msg \n \
					This is usually located in the sun4_solaris/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
					}
				if {$unix_type == "HP-UX"} {
					mess "Unable to determine PRO_COMM_MSG_EXE location\n \
					Tried:\t $env(PRO_COMM_MSG_EXE)\n \
					Set the environment variable PRO_COMM_MSG_EXE\n\
					to the location of the file pro_comm_msg \n \
					This is usually located in the hp8k/obj subdirectory\n\
					of your Pro/Engineer installation, but MED was unable to \n\
					locate it.  The failure is likely the result of an invalid\n\
					PROE_START_CMD$Version, please be sure that this is set to the full path\n\
					of your launch script." red
					}
			}
			return;
		}	
	}
    
   set prtfile [med_file_select proe 0 $file_name(project_dir)]
   if { [string length $prtfile] > 64} {
            mess "Error: Path buffer exceeded.  Due to limitation in Pro/Engineer the total number of characters in the" red
            mess "path and part name cannot exceed 64" red
            return
        } 	
    
 	set cut_point [string last "/" $prtfile]
	set tetfileloc  [string range $prtfile $cut_point end]	

	set mesh_dir $file_name(mesh_dir)
    if ![file exists $mesh_dir] {
        if [file exists ./mesh] {
            set mesh_dir ./mesh
        } else {
            set mesh_dir .
        }
    }

    set tetfileloc "$mesh_dir$tetfileloc"
    
    if {$tetfileloc == ""} {
        mess "Invalid Input File\n"
        return	
    }
    
    set tetinfile [file rootname $tetfileloc]
    
    set tetinfile [med_file_select tetin 1 $tetinfile.tin]
    
    if {$tetinfile == ""} {
        mess "Invalid Output File\n"
        return
    }

    if {[file pathtype $prtfile] == "relative"} {
        set prtfile [pwd]/$prtfile
    }

    if {[file pathtype $tetinfile] == "relative"} {
        set tetinfile [pwd]/$tetinfile
    }

    set currdir [pwd]
    cd [file dirname $prtfile]
    set prtfile [pwd]/[file tail $prtfile]
    cd [file dirname $tetinfile]
    set tetinfile [pwd]/[file tail $tetinfile]
    cd $currdir

    set ICEM_ACN_BAK $env(ICEM_ACN)

    set argprtfile $prtfile
    set argtetinfile $tetinfile

    # On Windows, change all forward slashes to back slashes
    if $is_win {
        regsub -all {\/} $prtfile {\\\\} argprtfile
        regsub -all {\/} $tetinfile {\\\\} argtetinfile
        regsub -all {\/} $env(ICEM_ACN) {\\} env(ICEM_ACN)
	}

    set fname [cmd_tempfile protcl_in .tmp]

    set fp [open $fname w]
	
	if $is_win {
		regsub -all {\\} $env(PROE_START_CMD$Version) {\\\\} proStartLine
		set line1 "proStartNoGraphics $proStartLine"
	} else {
		set line1 "proStartNoGraphics $env(PROE_START_CMD$Version)"
	}

	if {[string first ".asm" $argprtfile] > 0} {
		set line2 "proOpenAsm $argprtfile"
		set line3 "proWriteAsmSingleMed $argtetinfile"
	} else 	{

		set line2 "proOpenPart $argprtfile"
		set line3 "proWriteTetinMedDump $argtetinfile"

		if {$proe_trans(meth) == "1"} 	{
			set line3 "proWriteTetinMed $argtetinfile"
		}
		
		if {$proe_trans(meth) == "2"} {
			set line3 "proWriteTetinMedLyrs $argtetinfile"
		}
		
	}

	set line4 "proClosePart"
	set line5 "proEnd"
        set line6 "exit"

	puts $fp $line1
	puts $fp $line2
	puts $fp $line3
#	puts $fp $line4
	puts $fp $line5
        puts $fp $line6
	close $fp

    set env(NMSD_TIMEOUT) "15"
    set input_file "< $fname"
    if  $proe_trans(type) {
        ic_run_application_exec . bin proe2tin "$argprtfile $argtetinfile"
    } else {
	    ic_run_application_exec . bin protcl $input_file
	}
	if $is_win {
		set env(ICEM_ACN) $ICEM_ACN_BAK
	}
	
    set env(NMSD_TIMEOUT) "300"
    mess "Done.\n"

    file_check_load_tetin $tetinfile
    } else {
    mess "Please set PROE_START_CMD$Version to the full path\n\
	     of your Pro/Engineer start script.\n\
		 This is typically located in the bin directory\n\
		 of your Pro/Engineer installation\n" red
    }
}

proc trans_sw_tetin {} {
    global env is_win file_name
    
    set swfile [med_file_select sw 0 $file_name(project_dir)]
    
    if {$swfile == ""} {
       mess "Invalid Input File\n"
       return
    }

    ic_trans_sw_tetin $swfile
    set tetinfile [file tail $swfile]
    set tetinfile [ file root $tetinfile ]
    set tetinfile $file_name(project_dir)/$tetinfile.tin

    file_check_load_tetin $tetinfile
    mess "Done.\n"
}

proc trans_acis_tetin {} {
    global env is_win file_name beta_mode
    
    if {$is_win && !$beta_mode} return
    
    if {$is_win && ![file exists "$env(ICEM_ACN)/bin/acistotin.exe"]} {
        mess "\nNote that the executable \"acistotin.exe\" needs to be obtained from the ANSYS customer portal and copied to \"$env(ICEM_ACN)/bin\". " blue
        mess "It requires the Microsoft Visual C++ 2005 Redistributable Package which " blue
        mess "you can install at your own risk from \"http://www.microsoft.com/en-us/download/details.aspx?id=3387\" if necessary.\n" blue
        return 
    }
    
    set acisfile [med_file_select acis 0 $file_name(project_dir)]
    if {$acisfile == ""} {
        mess "Invalid Input File\n"
        return
    }
    set tetinfile [file tail $acisfile]
    set tetinfile [ file root $tetinfile ]
    set tetinfile $file_name(project_dir)/$tetinfile.tin
    if { [ file exists $tetinfile ] || $tetinfile == "" } {
	    set tmess "File $tetinfile exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set tetinfile [med_file_select tetin 1 $tetinfile]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }
    
    if {$tetinfile == ""} {
        mess "Invalid Output File\n"
        return
    }
    ic_run_application_exec . bin acistotin "-sat \"$acisfile\" -tin \"$tetinfile\"  -onefamilyperbody "
    file_check_load_tetin $tetinfile
    if [file exists $tetinfile] {
        set tmess "Import parts, sizes from existing tetin file?"
        if {[tk_dialog .import_sizes "Import sizes" $tmess "" -1 Yes No] == 0} { 
	        if [catch {do_map_tetin_sizes} res] {
	            mess "$res\n" red
	        }
        }
        if [winfo exists .import_sizes] { top_destroy .import_sizes; return }
    }
    mess "Done.\n"
}

proc trans_ps_tetin {} {
    global env is_win file_name
    
    set psfile [med_file_select ps 0 $file_name(project_dir)]
    
    if {$psfile == ""} {
        mess "Invalid Input File\n"
        return
    }
    
    set tetinfile [file tail $psfile]
    set tetinfile [ file root $tetinfile ]
    set tetinfile $file_name(project_dir)/$tetinfile.tin
    
    if { [ file exists $tetinfile ] || $tetinfile == "" } {
	    set tmess "File $tetinfile exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set tetinfile [med_file_select tetin 1 $tetinfile]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }
    
    if {$tetinfile == ""} {
        mess "Invalid Output File\n"
        return
    }
    
    # On Windows, change all forward slashes to back slashes
#    if $is_win {
#        regsub -all {\/} $psfile {\\} psfile
#        regsub -all {\/} $tetinfile {\\} tetinfile
#    }
    
    ic_run_application_exec . bin pstotin \
                                  "$psfile $tetinfile"
    

    file_check_load_tetin $tetinfile
    if [file exists $tetinfile] {
        set tmess "Import families, sizes from existing tetin file?"
        if {[tk_dialog .import_sizes "Import sizes" $tmess "" -1 Yes No] == 0} { 
	        if [catch {do_map_tetin_sizes} res] {
	            mess "$res\n" red
	        }
        }
        if [winfo exists .import_sizes] { top_destroy .import_sizes; return }
    }
    mess "Done.\n"
}

proc trans_tetin_ps {} {
    global env is_win file_name
    
    set tinfile [med_file_select tin 0 ""]

    if {$tinfile == ""} {
        mess "Invalid Input File\n"
        return
    }
    
    set psfile [file tail  $tinfile]
    set psfile [ file root $psfile ]
    set psfile $file_name(project_dir)/$psfile.x_t
    
    if { [ file exists $psfile ] || $psfile == "" } {
	    set tmess "File $psfile exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set psfile [med_file_select ps 1 $psfile]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }
    
    if {$psfile == ""} {
        mess "Invalid Output File\n"
        return
    }
    
    # On Windows, change all forward slashes to back slashes
#    if $is_win {
#        regsub -all {\/} $tinfile {\\} tinfile
#        regsub -all {\/} $psfile {\\} psfile
#    }
    
    ic_run_application_exec . bin tintops \
                                  "$tinfile $psfile"
    
    mess "Done.\n"
}

proc trans_autocad_tetin {} {
    global env is_win file_name


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

    if {(!$is_win) && (![file exists $env(ICEM_ACN)/bin/dwg2tin])} {
        user_error "DWG/DXF translator not available - please contact Technical Support for ICEM CFD / AI*Environment"
        return
    }
    
    set dwgfile [med_file_select dwg 0 $file_name(project_dir)]
    
    if {$dwgfile == ""} {
        mess "Invalid Input File\n"
        return
    }
    
    set tetinfile $file_name(project_dir)/[file rootname $dwgfile]
    
    set tetinfile [med_file_select tetin 1 $tetinfile.tin]
    
    if {$tetinfile == ""} {
        mess "Invalid Output File\n"
        return
    }
    
    # On Windows, change all forward slashes to back slashes
#    if $is_win {
#        regsub -all {\/} $dwgfile {\\} dwgfile
#        regsub -all {\/} $tetinfile {\\} tetinfile
#    }
    
    if [catch {ic_trans_dwg_tetin $dwgfile $tetinfile} res] {
        mess "Error: $res\n" red
    } else { 
        mess "Done.\n"
        file_check_load_tetin $tetinfile
    }
}


proc trans_step_tetin {} {
    global env is_win file_name step_trans 
    
    set step_trans(file) [med_file_select step 0 $file_name(project_dir)]
    
    if {$step_trans(file) == ""} {
        return
    }
    
    set step_trans(tetinfile) $file_name(project_dir)/[file tail [file rootname $step_trans(file)]].tin
    
    set step_trans(tetinfile) [med_file_select tetin 1 $step_trans(tetinfile)]
    
    if {$step_trans(tetinfile) == ""} {
        return
    }
        
    # On Windows, change all forward slashes to back slashes
#    if $is_win {
#        regsub -all {\/} $infile {\\} infile
#        regsub -all {\/} $tetinfile {\\} tetinfile
#    }

	set step_trans(step_toltype) 1
    set step_trans(step_faceted) 0
    set step_trans(step_abstol)  0.001
	set step_trans(step_reltol)  0.001

    set buts {
	{Accept "form_done .trans_step_tetin_imp; trans_step_tetin_doit"}
	reset cancel
    }

    set d [form_init .trans_step_tetin_imp "STEP/IGES -> Tetin" "" $buts]
    if {$d != ""} {	
#	form_radiobutton $d.abstol "Abs Tri Tol" step_trans(step_toltype) 1 {1 0}
#	form_radiobutton $d.reltol "Rel Tri Tol" step_trans(step_toltype) 2 {2 0}

#	form_entry $d.absttol step_trans(step_abstol) float "1 1"
#	form_entry $d.relttol step_trans(step_reltol) float "2 1"

	form_checkbutton $d.trel "Output faceted geometry" step_trans(step_faceted) "13 0 -cols 2"
	form_checkbutton $d.step "Use 5.1 Version Step Translator" step_trans(step_version) "14 0 -cols 2"
	# only possible to set when step_version = 0
	set step_trans(args) ""
	form_finish +300+300
    }
}

proc trans_step_tetin_doit {} {
    global step_trans

    set tolarg ""

#   if {$step_trans(step_toltype) == 1} { 
#	set tolarg "-atri $step_trans(step_abstol) "
#   } else {
#	set tolarg "-rtri $step_trans(step_reltol) "
#   }
    
    if { [ file exists $step_trans(tetinfile) ] || $step_trans(tetinfile) == "" } {
	    set tmess "File $step_trans(tetinfile) exists - overwrite?"
        if [tk_dialog .overwrite Overwrite $tmess "" -1 Yes No] { 
		    set step_trans(tetinfile) [med_file_select tetin 1 $step_trans(tetinfile)]
	    }
	    if [winfo exists .overwrite] { top_destroy .overwrite; return }
    }
    
    set dir [file dirname $step_trans(tetinfile)]

    if { $step_trans(tetinfile) == "" || ![file exists $dir] } {
 	mess "Invalid output filename: \"$step_trans(tetinfile)\"\n" red
    } else {
        if [catch {ic_trans_step_tetin $step_trans(file) \
		       $step_trans(tetinfile) $step_trans(step_faceted) $tolarg $step_trans(step_version) $step_trans(args)} res] {
            mess "Error: $res\n" red
        } else { 
            mess "Done file $step_trans(tetinfile).\n"
            if ![info exists step_trans(step_merge)] {
                file_check_load_tetin $step_trans(tetinfile)
            } else {
               if {[info exists step_trans(step_merge)] && $step_trans(step_merge)} {
                   ic_load_tetin $step_trans(tetinfile)
               } else {
                   file_check_load_tetin $step_trans(tetinfile)
               }
           }
        }
    }
}

proc trans_esf_tetin {} {
    global env is_win file_name esf_faceted esf_family
    
    set infile [med_file_select esf 0 $file_name(project_dir)]
    
    if {$infile == ""} {
        mess "Invalid Input File\n"
        return
    }
    
    set esf_family [file root [ file tail $infile]]
    set d [form_init .trans_esf_tetin_imp "ESF/XYZ -> Tetin"]
    if {$d != ""} {
	form_label $d.ttoll "Part" "1 0 -cols 2"
	form_entry $d.ttol esf_family string "1 2"
if 0 {
	form_checkbutton $d.trel "" esf_faceted "13 0"
	form_label $d.trell "Output faceted geometry" "13 1 -cols 2"
}
        form_finish +300+300
    }
    if ![form_wait .trans_esf_tetin_imp] return
    if [catch {ic_trans_read_esf_file $infile $esf_family} res] {
        mess "Error: $res\n" red
    } else { 
        mess "Done.\n"
    }
}



proc trans_input_tetin {} {
    global env is_win file_name input_faceted input_pFam
    global input_sFam input_cFam
    global input_sPre input_cPre input_pPre
    global input_sDo input_cDo input_pDo input_tol
    global input_mode
    
    set infile [med_file_select input 0 $file_name(project_dir)]
    
    if {$infile == ""} {
        mess "Invalid Input File\n"
        return
    }
    set input_pFam PNTS
    set input_cFam CRVS
    set input_sFam SRFS
    set input_pPre pnt
    set input_cPre crv
    set input_sPre srf
    set input_pDo  1
    set input_cDo  1
    set input_sDo  1
    set input_tol  0.001
    set input_mode input
    set d [form_init .trans_input_tetin_imp "ICEM Input -> Tetin"]
    if {$d != ""} {
	form_label $d.pFaml "Point Part" "1 0 -cols 2"
	form_entry $d.pFam  input_pFam string "1 2"
	form_label $d.cFaml "Curve Part" "2 0 -cols 2"
	form_entry $d.cFam  input_cFam string "2 2"
	form_label $d.sFaml "Surface Part" "3 0 -cols 2"
	form_entry $d.sFam  input_sFam string "3 2"
	form_label $d.pPrel "Point Prefix" "4 0 -cols 2"
	form_entry $d.pPre  input_pPre string "4 2"
	form_label $d.cPrel "Curve Prefix" "5 0 -cols 2"
	form_entry $d.cPre  input_cPre string "5 2"
	form_label $d.sPrel "Surface Prefix" "6 0 -cols 2"
	form_entry $d.sPre  input_sPre string "6 2"
	form_checkbutton $d.doP "Import Points" input_pDo {7 0 -cols 2}
	form_checkbutton $d.doC "Import Curves" input_cDo {8 0 -cols 2}
	form_checkbutton $d.doS "Import Surfaces" input_sDo {9 0 -cols 2}
        form_label $d.pTolL "Approximation tolerance" "10 0 -cols 2"
	form_entry $d.pTol input_tol  float "10 2"
        form_label $d.modL  "Input format" "11 0 -cols 2"
	form_radiobutton $d.modedef Default input_mode input {12 2}
	form_radiobutton $d.dirPlot3d Plot3d input_mode plot3d {13 2}
#	form_radiobutton $d.dirPDGS PDGS input_mode pdgs {14 2}
#	form_radiobutton $d.diruser  Contour input_mode contour {15 2}
#	form_radiobutton $d.diruser "Named Entities" input_mode named {16 2}
        form_finish +300+300
    }
    if ![form_wait .trans_input_tetin_imp] return
    if !$input_pDo {set input_pPre ""}
    if !$input_cDo {set input_cPre ""}
    if !$input_sDo {set input_sPre ""}
    if [catch {ic_geo_cre_geom_input $infile $input_tol $input_mode $input_pFam $input_pPre \
	$input_cFam $input_cPre $input_sFam $input_sPre} res] {
        mess "Error: $res\n" red
    } else {
        mess "Done.\n"
    }
}

proc trans_bgplus_tetin {} {
    global env is_win file_name bgplus_trans ai_environment

    set bgplus_trans(file) [med_file_select bgplus 0 $file_name(project_dir)]
    if {$bgplus_trans(file) == ""} return
    set bgplus_trans(tetinfile) "[file rootname $bgplus_trans(file)].tin"
    set bgplus_trans(unsfile) "[file rootname $bgplus_trans(file)].uns"

    set buts {
	{Accept "form_done .trans_bgplus_tetin_imp; trans_bgplus_tetin_doit"}
	reset cancel
    }

    set d [form_init .trans_bgplus_tetin_imp "CFX-BladeGen+" "" $buts]
    if {$d != ""} {
        array set bgplus_trans {
            import_geom 1
            join_surfaces 0
            mesh_families 0
            import_sources 0
            import_mesh 0
        }

        form_frame $d.f1 groove 3 "0 0 -padx 5 -pady 5"
        set f1 $d.f1

        set ln 0
        form_checkbutton $f1.geom "Import Geometry" \
            bgplus_trans(import_geom) "$ln 0"
        incr ln
	futil_fileentry $f1.tin bgplus_trans(tetinfile) \
            $file_name(project_dir) "$ln 0" bgplus_trans(import_geom) {*.tin}
        incr ln

        form_checkbutton $f1.join "Allow Blade Surface Merging" \
            bgplus_trans(join_surfaces) "$ln 0" bgplus_trans(import_geom)
        incr ln

        form_checkbutton $f1.fam "Use Mesh Part Names" \
            bgplus_trans(mesh_families) "$ln 0" bgplus_trans(import_geom)
        incr ln

        if {[info exists ai_environment] && $ai_environment} {
            set bgplus_trans(density_boxes) 0
        } else {
            form_label $f1.src "Import Mesh Controls as:" "$ln 0" \
                bgplus_trans(import_geom)
            incr ln
	    form_radiobutton $f1.none "None" \
                bgplus_trans(import_sources) 0 "$ln 0 -padx 10" \
                bgplus_trans(import_geom)
            incr ln
	    form_radiobutton $f1.crv "Curve/Surface Spacings" \
                bgplus_trans(import_sources) 1 "$ln 0 -padx 10" \
                bgplus_trans(import_geom)
            incr ln
	    form_radiobutton $f1.box "Line Densities" \
                bgplus_trans(import_sources) 2 "$ln 0 -padx 10" \
                bgplus_trans(import_geom)
        }

        form_frame $d.f2 groove 3 "1 0 -padx 5 -pady 5"
        set f2 $d.f2
        set ln 0

        form_checkbutton $f2.mesh "Import Mesh" \
            bgplus_trans(import_mesh) "$ln 0"
        incr ln
	futil_fileentry $f2.uns bgplus_trans(unsfile) \
            $file_name(project_dir) "$ln 0" bgplus_trans(import_mesh) {*.uns}
	form_finish +300+300
    }
}

proc trans_bgplus_tetin_doit {} {
    global env bgplus_trans file_name ai_environment

    if {$bgplus_trans(tetinfile) == ""} {set bgplus_trans(import_geom) 0}
    if {$bgplus_trans(unsfile) == ""} {set bgplus_trans(import_mesh) 0}
    if {!$bgplus_trans(import_geom) && !$bgplus_trans(import_mesh)} return

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

    set args "-v"
    if {$bgplus_trans(import_geom)} {
        append args t
        if {$bgplus_trans(join_surfaces)} {append args j}
        if {$bgplus_trans(mesh_families)} {append args o}
        if {$bgplus_trans(import_sources) == 1} {append args S}
        if {$bgplus_trans(import_sources) == 2} {append args D}
    }
    if {$bgplus_trans(import_mesh)} {append args d}
    lappend cmd $args
    lappend cmd $bgplus_trans(file)
    if {$bgplus_trans(import_geom)} {lappend cmd $bgplus_trans(tetinfile)}
    if {$bgplus_trans(import_mesh)} {lappend cmd $bgplus_trans(unsfile)}

    busy 1
    mess "$cmd\n"
    if {[catch {open |$cmd r} pipe]} {
        mess "Error: $pipe\n" red
        busy 0
        return
    }
    while {[gets $pipe line] >= 0} {
        mess "$line\n" blue
        update idletasks
    }
    if {[catch {close $pipe} msg]} {
        mess "Error: $msg\n" red
    } else {
        mess "Done.\n"
        if {[info exist ai_environment] && $ai_environment} {
            if {$bgplus_trans(import_geom)} {
                file_check_load_tetin "$bgplus_trans(tetinfile)"
                aie_initGeoVis
            }
            if {$bgplus_trans(import_mesh)} {
                set file_name(domain) $bgplus_trans(unsfile)
                aie_open_Mesh 0 ""
            }
            aie_afterStuffLoaded
        } else {
            if {$bgplus_trans(import_geom)} {
                file_check_load_tetin "$bgplus_trans(tetinfile)"
            }
            if {$bgplus_trans(import_mesh)} {
                file_unload domain
                gui_load_domains "{$bgplus_trans(unsfile)}"
                display_update unstruct
                ic_uns_set_modified 1
                med_home
            }
        }
    }
    busy 0
}

proc trans_3dm_tetin {} {
    global rhino_trans file_name

    set rhino_trans(3dmfile) [med_file_select 3dm 0 \
        $file_name(project_dir) "to read"]
    if {$rhino_trans(3dmfile) == ""} return
    set rhino_trans(tinfile) [med_file_select tin 1 \
        "[file rootname $rhino_trans(3dmfile)].tin" " to write"]
    if {$rhino_trans(tinfile) == ""} return

    set buts {
	{Accept "form_done .trans_3dm_tetin_imp; trans_3dm_tetin_doit"}
	reset cancel
    }

    set d [form_init .trans_3dm_tetin_imp "3DM->Tetin" "" $buts]
    if {$d != ""} {
        array set rhino_trans {
            select 0
            points 1
            curves 1
            surfaces 1
            breps 1
            meshes 1
            names 0
            hidden 0
            divide 0
            extract 0
        }

        form_frame $d.f1 groove 3 "0 0 -padx 5 -pady 5"
        set f1 $d.f1

        set ln 0
        form_checkbutton $f1.select "Selected Geometry" \
            rhino_trans(select) "$ln 0"
        incr ln
        foreach {i j} {points Points curves Curves surfaces Surfaces \
                       breps Solids meshes Meshes} {
            form_checkbutton $f1.$i $j rhino_trans($i) "$ln 0 -padx 20" \
                rhino_trans(select)
            incr ln
        }

        form_checkbutton $f1.layers "Use Object Names as Families" \
            rhino_trans(names) "$ln 0"
        incr ln
        form_checkbutton $f1.hidden "Include Hidden Layers" \
            rhino_trans(hidden) "$ln 0"
        incr ln
        form_checkbutton $f1.divide "Split Composite Curves" \
            rhino_trans(divide) "$ln 0"
        incr ln
        form_checkbutton $f1.extract "Include Brep Surface Trim Curves" \
            rhino_trans(extract) "$ln 0"
	form_finish +300+300
    }
}

proc trans_3dm_tetin_doit {} {
    global env rhino_trans ai_environment

    if {[info exists env(3DM2TIN_BIN)] && $env(3DM2TIN_BIN) != ""} {
        set prog [join [split $env(3DM2TIN_BIN) \\] /]
    } else {
        set prog 3dm2tin
    }
    if {![file exists $prog] && [catch {get_program bin $prog} prog]} {
        mess "Error: can't find 3dm2tin program\n" red
        return
    }
    set args "-a"
    if {$rhino_trans(select)} {
        foreach i {points curves surfaces breps meshes} {
            if {$rhino_trans($i)} {
                append args [string index $i 0]
            }
        }
    }
    foreach i {names hidden divide extract} {
        if {$rhino_trans($i)} {
            append args [string index $i 0]
        }
    }

    set cmd [list $prog $args $rhino_trans(3dmfile) $rhino_trans(tinfile)]
    mess "\n$cmd\n"
    if [catch {open |$cmd "r"} fp] {
        mess "Error: $fp\n" red
        return
    }
    while {[gets $fp line] >= 0} {
        mess "$line\n" blue
    }
    if [catch {close $fp} msg] {
        mess "Error: $msg\n" red
        return
    }
    mess "Done.\n"

    file_check_load_tetin "$rhino_trans(tinfile)"
    if {[info exist ai_environment] && $ai_environment} {
        aie_afterStuffLoaded
        aie_initGeoVis
    }
}

proc trans_tetin_3dm {} {
    global rhino_trans file_name

    set rhino_trans(tinfile) [med_file_select tin 0 \
        $file_name(project_dir) "to read"]
    if {$rhino_trans(tinfile) == ""} return
    set rhino_trans(3dmfile) [med_file_select 3dm 1 \
        "[file rootname $rhino_trans(tinfile)].3dm" " to write"]
    if {$rhino_trans(3dmfile) == ""} return

    set buts {
	{Accept "form_done .trans_tetin_3dm_imp; trans_tetin_3dm_doit"}
	reset cancel
    }

    set d [form_init .trans_tetin_3dm_imp "Tetin->3DM" "" $buts]
    if {$d != ""} {
        array set rhino_trans {
            select 0
            points 1
            curves 1
            surfaces 1
            meshes 1
            notrim 0
            rebuild 0
            abstol 0.000001
            reltol 0.00001
            units mm
            vers2 1
        }

        form_frame $d.f1 groove 3 "0 0 -padx 5 -pady 5"
        set f1 $d.f1

        set ln 0
        form_checkbutton $f1.select "Selected Geometry" \
            rhino_trans(select) "$ln 0"
        incr ln
        foreach {i j} {points Points curves Curves surfaces Surfaces \
                       meshes Meshes} {
            form_checkbutton $f1.$i $j rhino_trans($i) "$ln 0 -padx 20" \
                rhino_trans(select)
            incr ln
        }

        form_checkbutton $f1.notrim "Do Not Trim Surfaces" \
            rhino_trans(notrim) "$ln 0"
        incr ln
        form_checkbutton $f1.rebuild "Rebuild Coedges" \
            rhino_trans(rebuild) "$ln 0"
        incr ln
if 0 {
        form_checkbutton $f1.abstol "Absolute Tolerance" \
            rhino_trans(abstol) "$ln 0"
        incr ln
        form_checkbutton $f1.reltol "Relative Tolerance" \
            rhino_trans(reltol) "$ln 0"
        incr ln
}
        form_checkbutton $f1.vers2 "Version 2 Compatibility" \
            rhino_trans(vers2) "$ln 0"
	form_finish +300+300
    }
}

proc trans_tetin_3dm_doit {} {
    global env rhino_trans ai_environment

    if {[info exists env(TIN23DM_BIN)] && $env(TIN23DM_BIN) != ""} {
        set prog [join [split $env(TIN23DM_BIN) \\] /]
    } else {
        set prog tin23dm
    }
    if {![file exists $prog] && [catch {get_program bin $prog} prog]} {
        mess "Error: can't find tin23dm program\n" red
        return
    }
    set args "-a"
    if {$rhino_trans(select)} {
        foreach i {points curves surfaces meshes} {
            if {$rhino_trans($i)} {
                append args [string index $i 0]
            }
        }
    }
    if {$rhino_trans(notrim)} {append args d}
    if {$rhino_trans(rebuild)} {append args r}
    if {$rhino_trans(vers2)} {append args 2}

    set cmd [list $prog $args $rhino_trans(tinfile) $rhino_trans(3dmfile)]
    mess "\n$cmd\n"
    if [catch {open |$cmd "r"} fp] {
        mess "Error: $fp\n" red
        return
    }
    while {[gets $fp line] >= 0} {
        mess "$line\n" blue
    }
    if [catch {close $fp} msg] {
        mess "Error: $msg\n" red
        return
    }
    mess "Done.\n"
}

proc trans_cos_ccl {} {
    global file_name
    if {$file_name(tetin) != ""} {
	    set tetin $file_name(tetin)
    }
	if {$tetin == ""} return
    set nfile $file_name(mesh_dir)/[file tail $file_name(ccl_file)]
	set nfile [file_selection -message "File to save the CCL data." \
		       -default $nfile -title "Select CCL file" \
		       -warn_overwrite 1 \
		       -new_ok 1 -delete_button 1 -mkdir_button 1 \
		       -visible_func {has_suffix "CCL (.ccl)" .ccl}]
	if {$nfile == ""} return
    ic_trans_cos_ccl $nfile
    set file_name(ccl_file) $nfile
} 

