
# ICEM CFD solver interface routine for the manager.
#
# Copyright (c) 1993-1995 ICEM Systems.


proc tascflow_write_input {args} {
    global confname file_name tascflow_output_path env icem_powermesh tcl_platform

    set domains [domain_select $file_name(domains_dir) "Select structured domains." struct 0]
    if {$domains == {}} return

    get_boco_topo

#    set outfile $file_name(transfer_dir)/tascflow
    set outfile [file root [file tail $file_name(settings)]]
    set outfile $outfile.tascflow


    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0
    set binary 1
    set full_dialog 1
    set warn_overwrite 0

    set lunu  "include \#/inter.lun\ninclude \#/systemu.lun"
    set lunf  "include \#/inter.lun\ninclude \#/systemf.lun"
    set defaultlun  "tri,5,formatted,\ntro,6,formatted,\nprm,11,formatted,*prm\ngrd,12,unformatted,*grd\nbcf,13,formatted,*bcf\nrsi,23,unformatted,*rsi\nrso,22,unformatted,*rso"

    # Now set the values from the arguments if any
    foreach {n v} $args { set $n $v }
    if ![info exists is_batch] { set is_batch [llength $args] }
    
    if !$is_batch {
	if $full_dialog {
	    if {![dialog .tascflow "CFX-TASCflow" \
		      "Please edit the following CFX-TASCflow options." {
			  {binary -opt {{Binary 1} {Ascii 0}} \
			       -title "Output type" -help "Output type"}
			  {topofile -type path -title "Topo file" \
			       -help "Topology file"}
			  {bocofile -type path -title "Boco file" \
			       -help "Boundary condition file"}
			  {outfile -type path -title "Output file" -help "Output file"}
			  {scale -opt {{"Yes" 1} {"No" 0}} \
			       -title "Scaling"\
			       -help "Scaling of coordinates of geometry file"}
			  {x -type string -active {$scale} -title "x scaling factor" \
			       -help "Enter x scaling factor"}
			  {y -type string -active {$scale} -title "y scaling factor" \
			       -help "Enter y scaling factor"}
			  {z -type string -active {$scale} -title "z scaling factor" \
			       -help "Enter z scaling factor"}
		      }]} return
	} else {
	    if {![dialog .tascflow "CFX-TASCflow" \
		      "Please edit the following CFX-TASCflow options." {
			  {binary -opt {{Binary 1} {Ascii 0}} \
			       -title "Output type" -help "Output type"}
			  {outfile -type path -title "Output file" -help "Output file"}
			  {scale -opt {{"Yes" 1} {"No" 0}} \
			       -title "Scaling"\
			       -help "Scaling of coordinates of geometry file"}
			  {x -type string -active {$scale} -title "x scaling factor" \
			       -help "Enter x scaling factor"}
			  {y -type string -active {$scale} -title "y scaling factor" \
			       -help "Enter y scaling factor"}
			  {z -type string -active {$scale} -title "z scaling factor" \
			       -help "Enter z scaling factor"}
		      }]} return
	}
    }
    
    if {$warn_overwrite && [file exists $outfile.grd]} {
	set mess "The file $outfile.grd already exists.  You can back\
 		up the existing file to $outfile.grd.old and then write\
 		the data, overwrite the old file, or cancel the command."
	switch [tk_dialog .overwrite "File exists" $mess "" -1 \
		    Backup Overwrite Cancel] {
			0 {
			    foreach p {grd log bcf} {
				cmd_mv $outfile.$p $outfile.$p.old
			    }
			}
			1 { }
			2 { return }
		    }
    }
    
    if $full_dialog {
	# Not for cfxhexa
	if {![file exists "name.lun"]} {
	    set fileId [open "name.lun" w]
	    puts $fileId "problem tascflow.\n$defaultlun"
	    close $fileId
	}
	if {![file exists "name.lun"]} {
	    errormsg "Cannot create name.lun"
	    return
	}
	
    } else {
	
	set fileId [open [file dirname $outfile]/name.lun w]
	
	if {$binary} {
	    puts $fileId "problem [file tail $outfile].\n$lunu"
	} else {
	    puts $fileId "problem [file tail $outfile].\n$lunf"
	}
	close $fileId
    }
    
    if {![file exists $topofile]} {
        errormsg "File $topofile doesn't exist"
        return
    }

    if {![file exists $bocofile]} {
        errormsg "File $bocofile doesn't exist"
        return
    }

    cmd_rm domain_list
    set text ""
    foreach d $domains { append text "$d\n" }
    ic_write_file domain_list $text
	if {$::tcl_platform(platform) == "windows" && [info exists env(ProgramFiles(x86))]} {
		#set tascflow_output_path [file dir $tascflow_output_path]/32bit/tascflow
	}
    set command "\"$tascflow_output_path\"  -dom_list domain_list \
                -b \"$bocofile\" -t \"$topofile\" -f \"$ftopofile\" -log"

    if {$binary} then {
        set command "$command -u \"$outfile\""
    } else {
        set command "$command \"$outfile\""
    }
    if $scale {set command "$command -scale $x,$y,$z"}
    runcom $command ICEM-CFD/TASCflow-Interface

#    cmd_rm "name.lun"
}
