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

proc wm_write_input {} {
    global confname file_name wm_output_path icem_fluent icem_powermesh
    global file_name

    set sel [domain_select $file_name(domains_dir) "Select structured domains." struct 0]    
    if {$sel == {}} return
#    if {([llength $sel] != 1)} {
#	errormsg "Select only one superdomain file!"
#	return
#    }

    get_boco_topo

    cmd_rm domain_list
    set text ""
    foreach d $sel { append text "$d\n" }
    ic_write_file domain_list $text
       
    set numbers ""
#    errormsg "sel = $sel"
    foreach d $sel {
        lappend numbers [string range [file extension $d] 1 end]
#        errormsg "numbers =$numbers"
    }
    set supername [file join . super_domain.1]
    set listname  [file join . sup_domain_list]
    
    set cmd [list -dom_list domain_list -t $topofile -o $supername \
		 -list $listname]

    set stat [catch {ic_run_application_exec . icemcfd supdomain $cmd } res] 

    if $stat {
    return -code error $res
    }

        
    set outfile [file rootname $file_name(settings)]
    set outfile $outfile.txt
    
    set scale 0
    set x 1.0
    set y 1.0
    set z 1.0
    
    set msg "Please edit the following WindMaster options. \
	    The topology file and the boundary condition file \
            must be selected.  \
            The suffixes are _grid.dat, _block.dat and _flag.dat \
            respectively."
    if {![dialog .wm "Wind Master" $msg {
	{bocofile -type path -title "Boco file" \
		-help "Boundary condition file"}
	{topofile -type path -title "Topo file" \
		-help "Topology file"}
	{scale -opt {{"Yes" 1} {"No" 0}} -title "Scaling" \
                -help "Scaling of coordinates (enter 3 values for x, y & z"}
        {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"}
	{outfile -type path -title "Output file" -help "Output file"}
    }]} return
	
    if {![file exists $bocofile]} {
	errormsg "Boundary condition file doesn't exist!"
	return
    }
    if {![file exists $topofile]} {
	errormsg "Topology file doesn't exist!"
	return
    }
    if {[file exists $outfile] && \
	[query_question .query "Overwrite $outfile?"] != "*okay*"} return
	
    set command "\"$wm_output_path\" -dom $supername -b \"$bocofile\" -t \"$topofile\" -f \"$ftopofile\" \"$outfile\""  
    if $scale {set command "$command -scale $x,$y,$z"}

    runcom $command ""
}
