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

proc cfx5_write_input {args} {
    global env file_name cfx5_output_path use_workbench

    # Set the default values of all the parameters first
    set scale 0
    set x 0.001
    set y 0.001
    set z 0.001
    set ascii 1
    set new_version 1
    set db 1
    set warn_overwrite 1
    set domain $file_name(domain)

    set root_name [file rootname [file tail $domain]]
    if { $root_name == "" && \
        [info exists file_name(settings)] && $file_name(settings) != "" } {
        set root_name [file root [file tail $file_name(settings)]]
    }
    if { $root_name == "" } {
        set root_name "project1"
    }

#   set domain $file_name(domains_dir)/hex.uns
#   set env(CFX5_UNITS_DIR) $env(ICEM_ACN)/icemcfd/output-interfaces
#   set env(CFX_DATA_DIR)   $env(ICEM_ACN)/icemcfd/output-interfaces
#   set env(CFX5BINDIR)     $env(ICEM_ACN)/icemcfd/output-interfaces
#   set env(CFX5EXECDIR)    $env(ICEM_ACN)/icemcfd/output-interfaces
    
    set transfer_dir $file_name(transfer_dir)
    if { $transfer_dir == "." } {
        set transfer_dir [pwd]
    }
    if { $transfer_dir != "" } {
        set cfx5file [file join $transfer_dir ${root_name}.cfx5]
        set gtmfile  [file join $transfer_dir ${root_name}.gtm]
    } else {
        set cfx5file [file join [pwd] ${root_name}.cfx5]
        set gtmfile  [file join [pwd] ${root_name}.gtm]
    }

    set lcs_exist 0
    set lcs 0

    if { ![info exists use_workbench] } {
        set use_workbench 0
    }
    set use_gtmfile 0
    if { $use_workbench } {
        global env tcl_platform use_workbench wb_gtmfile wb_advanced_cfd_app_location
        if {[info exists wb_advanced_cfd_app_location] && $wb_advanced_cfd_app_location != ""} {
            set path $wb_advanced_cfd_app_location
        } else {
            catch {load $env(ICEM_ACN)/bin/tclreg83.dll registry}
            catch {registry get "HKEY_LOCAL_MACHINE\\SOFTWARE\\ANSYS, Inc.\\v110\\APFramework\\Addins\\AdvancedCFD" "InstallDir"} path
        }
        set exe [file join $path bin cfx5gtmconv]

        if {$tcl_platform(platform) == "windows" } {
            set exe ${exe}.exe
        }
        if {![file exists $exe]} {
            mess "Can't find CFX5 installation:  Conversion to CFX5 only, not to GTM.\n" orange
        } else {
            set use_gtmfile 1
        }
    }
    
    get_boco_topo

    # 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] }
    
    set units [ic_geo_get_units]
    if { $units == "" } {
        set units "unknown"
    }
    switch $units {
        "unknown" {
        }
        "um" {
            #Microns
            set units "micron"
        }
        "uin" {
            #Microinches
            #CFX doesn't support
            set units "unknown"
        }
        "mm" {
            #Millimeters
            set units "mm"
        }
        "mil" {
            #Mils
            #CFX doesn't support
            set units "unknown"
        }
        "cm" {
            #centimeters
            set units "cm"
        }
        "in" {
            #Inches
            set units "in"
        }
        "m" {
            #meters
            set units "m"
        }
        "ft" {
            #feet
            set units "foot"
        }
        "km" {
            #kilometers
            #CFX doesn't support
            set units "unknown"
        }
        "mi" {
            #miles
            #CFX doesn't support
            set units "unknown"
        }
    }

    if !$is_batch {
      if { $use_gtmfile} {
        if {![dialog .cfx5 "GTM" \
                  "Please edit the following GTM options." {
                      {bocofile -type path -title "Boco file" \
                           -help "Boundary condition file"}
                      {gtmfile -type path -title "Output GTM 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 .cfx5 "CFX5" \
                  "Please edit the following CFX5 options." {
                      {bocofile -type path -title "Boco file" \
                           -help "Boundary condition file"}
                      {cfx5file -type path -title "Output CFX5 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"}
                      {lcs -opt {{"Global" 0} {"Local" 1}} \
                           -title "Coordinate system"}
                      {ascii -opt {{"ASCII" 1} {"BINARY" 0}} \
                           -title "ASCII or BINARY file" \
                           -help "ASCII or BINARY file"}
                      {db -opt {{"Single" 0} {"Double" 1}} \
                           -title "Single or Double Precision" \
                           -help "Single or Double Precision"}
                      {new_version -opt {{"Pre-5.5" 0} {"5.5 or later" 1}} \
                           -title "CFX-5 Version" \
                           -help "CFX5-5 Version"}
                  }]} return
      }
        
        if {$domain == {}} {
            global ai_environment
            set ai [expr {[info exists ai_environment] && $ai_environment}]
            if $ai {
                aie_saveProject 0
            } else {
                file_saveproject 1
            }
            set domain $file_name(domain)
        }
        if {$domain == {}} {
            set domain [domain_select $file_name(domains_dir) "Select an unstructured domain." unstruct 0]
        }
    }

    if {$domain == {}} {
        errormsg "No domain selected"
        return
    }
    if {![file exists $bocofile]} {
        errormsg "File $bocofile doesn't exist"
        return
    }
    if {$lcs} {
        set csystem [ic_csystem_get_current]
        if {$csystem != "global"} {
           set lcs_exist 1
           set lcs_args [ic_csystem_get $csystem]
           infomsg "Using LCS = $lcs_args\n"
        } else {
           errormsg "Local coordinate system doesn't exit, using global!\n"
        }
    }
    
    if { $use_gtmfile} {
        if {[file extension $gtmfile] == "" } {
                set gtmfile "${gtmfile}.gtm"
        }
        if {[file exists $gtmfile] && $warn_overwrite && \
            [query_question .query "Overwrite $gtmfile?"] != "*okay*"} return
    } else {
        if {[file exists $cfx5file] && $warn_overwrite && \
            [query_question .query "Overwrite $cfx5file?"] != "*okay*"} return
    }
    
    set command "\"$cfx5_output_path\" -dom \"$domain\" -b \"$bocofile\"" 
    if $scale {set command "$command -scale $x,$y,$z"}
    if $ascii {set command "$command -ascii"}
    if $db {set command "$command -db"}
    if $new_version {set command "$command -internal_faces"}
    if $lcs_exist {set command "$command -lcs \"$lcs_args\""}
    set command "$command \"$cfx5file\" "
    runcom $command ""

#convert CFX5 file to GTM file
    if { $use_gtmfile} {     
        if { [info exists use_workbench] && $use_workbench==1 } {
            if {[string index $gtmfile 0] == "."} {
                # The gtm command has to use an absolute pathname
                set p [pwd]
                regsub "." $gtmfile $p gtmfile
            }
            if { [string index [file dirname $gtmfile] end] == "." } {
                #gtmfile pathname is relative
                set p [pwd] 
                set gtmfile [file join $p $gtmfile]
            }
        }
        catch {
            if {$units != "unknown"} {
                set cmd "$exe -overwrite -generic $cfx5file -gtm $gtmfile -units $units"
                mess "$cmd\n" blue
                set message [exec $exe -overwrite -generic $cfx5file -gtm $gtmfile -units $units]
            } else {
                set cmd "$exe -overwrite -generic $cfx5file -gtm $gtmfile"
                mess "$cmd\n" blue
                set message [exec $exe -overwrite -generic $cfx5file -gtm $gtmfile]
            }
            mess "$message\n" orange
        } err
        if { [info exists err] &&  $err != "" } {
            mess "$err\n" orange
        }

        global use_workbench wb_gtmfile
        if { [info exists use_workbench] && $use_workbench==1 } {
            if {[string index $gtmfile 0] == "."} {
                # The gtm command has to use an absolute pathname
                set p [pwd]
                regsub "." $gtmfile $p gtmfile
            }
            set wb_gtmfile $gtmfile
            AIEWBSendToWBScript "AIERegisterGTM"
        }
    }
}
