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

proc cfx4_write_input {args} {
    global confname file_name cfx4_output_path icem_fluent icem_powermesh
    global file_name
   
    # Set the default values of all the parameters first
    set topo 0
    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0
    set thickness 1.0
    
    set outputfile $file_name(transfer_dir)/cfx4

    set bocofile ""
    set topofile ""
    set domain ""
    
    set warn_overwrite 0
    
    # 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 {
	set domain [domain_select $file_name(domains_dir) \
			 "Select structured domains." struct 0]
	if {$domain == {}} return
	
	get_boco_topo
	
	if {![dialog .cfx4 "CFX-4" \
		  "Please edit the following CFX options." {
		      {topofile -type path -title "Topology file"}
		      {bocofile -type path -title "Boundary condition file"}
		      {outputfile -type path -title "CFX-4 Geometry 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"}
		      {thickness -type string \
			   -title "Thickness (only for planar 2D grids)" \
			   -help "Define a thickness (only for planar 2D grids)"} \
			  {topo -opt {{"Yes" 1} {"No" 0}} \
			       -title "Create CFX command file"}
		  }]} return
    }
    if {![file exists $topofile]} {
        errormsg "Topology file doesn't exist!"
        return
    }
    if {![file exists $bocofile]} {
        errormsg "Boundary condition file doesn't exist!"
        return
    }

    cmd_rm domain_list
#   set fp [open domain_list w]
#   foreach d $domain { puts $fp $d }
#   close $fp
    set text ""
    foreach d $domain { append text "$d\n" }
    ic_write_file domain_list $text

    if {$warn_overwrite && [file exists $outputfile.geo]} {
	set mess "The file $outputfile.geo already exists.  You can back\
 		up the existing file to $outputfile.geo.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 {cmd geo log} {
				cmd_mv $outputfile.$p $outputfile.$p.old
			    }
			}
			1 { }
			2 { return }
		    }
    }
    set command "\"$cfx4_output_path\" -t \"$topofile\" -f \"$ftopofile\" -b \"$bocofile\""
    if $scale {set command "$command -scale $x,$y,$z"}
    if $topo {set command "$command -topo"}
    set command "$command -thick $thickness"
    set command "$command -dom_list domain_list \"$outputfile\""

    runxcom CFX-4 $command "" 1 0
    cmd_rm domain_list

}
