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

proc usmkv3v_write_input {} {
    global confname file_name usmkv3v_output_path icem_powermesh
    global structured
    global file_name

    get_boco_topo

    set outfile $file_name(transfer_dir)/usmkv3v
    
    set structured 1
    if {![dialog .usmkv3v "usmkv3v" \
          "Please edit the following USMKV3V options." {
	{structured -opt {{Structured 1} {Unstructured 0}} -title "Domain Type"}
	{topofile -type path -active {$structured} -title "Topology file"}
	{bocofile -type path -title "Boco file" -help "Boundary condition file"}
	{outfile -type path -title "Output file" -help "Prefix of output file"}
    }]} return

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

    if {$structured} {

#       set domains [multiblock_domain_list]
	set domains [domain_select $file_name(domains_dir) "Select structured domains." struct 0]
       if {$domains == {}} {
          errormsg "No multiblock mesh available!"
          return
       }

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

    } else {
        set domains [list [domain_select $file_name(domains_dir) "Select an unstructured domain." \
	unstruct 0]]
    }

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

    set command "\"$usmkv3v_output_path\" -b \"$bocofile\" "
    if {$structured} {
       set command "$command -t \"$topofile\" -f \"$ftopofile\""
    } else {
       set command "$command -unstr"
    }
    set command "$command \"$outfile\" -dom_list domain_list"
    
    runcom $command ""
    cmd_rm domain_list    
}
