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

proc uh3d_write_input {} {
    global confname file_name uh3d_output_path 
    global file_name

    get_boco_topo

    set outputfile $file_name(transfer_dir)/uh3d
    set inputfile $file_name(mesh_dir)/surface_pp
    set domainfile $file_name(domains_dir)/surface_domain.1
    set unstructdom $file_name(domains_dir)/cut_domain.1

    set wrt_usm 1
    set unstr 0

    if {![dialog .uh3d "UH3D" \
      "Please edit the following UH3D options. \
       Piercing Point Data will be written unless you specify \
       'Write Surface Mesh,' in which case an unstructured. \
       surface mesh will be written." {
	{unstr -opt {{"Yes" 0} {"No" 1}} -title "Write Grid File" }
        {inputfile -type path -active {!$unstr} -title "Cartesian grid input"}
	{bocofile -type path -title "Boco file" \
                -help "Boundary condition file"}
        {outputfile -type path -title "Output file"}
	{wrt_usm -opt {{"Yes" 1} {"No" 0}} -active {!$unstr} -title "Write Surface Mesh"}

		
    }]} return

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

#if cartesian grid 
    if {(!$unstr)} {	#write grid file ONLY-> YES (=0)
        if {![file exists $inputfile]} {
            errormsg "Input Global-UH3D file $inputfile doesn't exist"
            return
	}

        set command "\"$uh3d_output_path\" -ifile \"$inputfile\" -b \"$bocofile\" \"$outputfile\""
    
        if {$wrt_usm} {
	    set domainfile [list [domain_select $file_name(domains_dir) "Select a Surface Domain File." unstruct 0]] 
	    if {$domainfile == {}} return

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

	    if {![file exists $domainfile]} {
	        errormsg "Domainfile $domainfile doesn't exist"
	        return
	    }
	    set command "$command -dom \"$domainfile\""
        }

    } else {		#write grid file -> NO (= 1)

	set unstructdom [list [domain_select $file_name(domains_dir) "Select an unstructured domain." unstruct 0]]
        if {$unstructdom == {}} return

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


	if {![file exists $unstructdom]} {
	    errormsg "Domainfile $unstructdom doesn't exist"
	    return
	}
	set command "\"$uh3d_output_path\" -dom \"$unstructdom\" -b \"$bocofile\" \"$outputfile\""
    }

    runxcom ICEM-CFD/UH3D-Interface $command "" 1 0
    cmd_rm domain_list
}
