
array set v3p_interp_select_vars {
}

array set v3p_interp_select_mats {
}

proc aie_v3p_interp_select {what what1""} {
    global v3p_interp_data
    global v3p_interp_select_mats
    global v3p_interp_select_vars

    switch -- $what {
        mats {
            set listName "Material List"
            set dialogName "Select Materials (optional)"
            set selectedName "Selected Material"
        }
        vars {
            set listName "Variable List"
            set dialogName "Select Variables"
            set selectedName "Selected Variable"
        }
    }

    #create listbox form
    set data_list [set v3p_interp_data($what)]
    set d [form_init .interp_file_${what} "$dialogName" "" {done cancel}]
    if { $d != "" } {
        form_label $d.lbl $listName {0 0}
        form_frame $d.vlst flat 0 {1 0 -rows 5 -sticky w -pady 2}
        check_box $d.vlst $data_list 10 v3p_interp_select_${what}
        form_finish +300+300
    }
    if ![form_wait .interp_file_${what}]  {return ""}

    # Print out user feedback and setup output data
    set selected [array get v3p_interp_select_${what}]
    set v3p_interp_data(selected_$what) ""
    set s 0
    foreach { data isSelected } $selected {
        if { $isSelected == 1 } {
            lappend v3p_interp_data(selected_$what) $data
            if {$s == 0} { 
                set s ""
            } else { 
                set s "s" 
            }
        }
    }

    if {$s == 0} { return 1 }

    #mess "$selectedName${s} $v3p_interp_data(selected_$what)\n"

    set v3p_interp_data(selected_$what) [list $v3p_interp_data(selected_$what)]
}

proc onv3InterpFileDEZLoad {} {
    global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
    global v3p_interp_select_interp_type
    global v3p_interp_data v3p_interp_ansys_select_command
    global v3p_interp_select_format v3p_interp_select_ansys_command
    global last_interp_var_list

    set interpolation_format 0

    set vList [V3_variables_get sv_namelist]
    if { [info exists last_interp_var_list] && \
        $vList != $last_interp_var_list } {
        # Model has changed
        set v3p_interp_data(selected_vars) ""
        set v3p_interp_data(selected_mats) ""
        form_kill .interp_file_vars
        form_kill .interp_file_mats
    }
    set last_interp_var_list $vList

    set v3p_interp_select_interp_type {"Surfaces Only" "Volumes Only" "Surfaces and Volumes"}
    if { ![info exists v3p_interp_data(type)] } {
        set v3p_interp_data(type) "Surfaces and Volumes"
    }
    set v3p_interp_select_format {"ASCII" "ANSYS"}
    if { ![info exists v3p_interp_data(format)] } {
        set v3p_interp_data(format) "ASCII"
    }
    set v3p_interp_select_ansys_command {"BF" "SF" "SFE"}
    if { ![info exists v3p_interp_data(ansys_command)] } {
        set v3p_interp_data(ansys_command) "BF"
    }
    if { ![info exists v3p_interp_data(ansys_label)] } {
        set v3p_interp_data(ansys_label) ""
    }

    if { ![info exists v3p_interp_data_input] } {
        set v3p_interp_data_input ""
    }

    if { ![info exists v3p_interp_data_output] } {
        set v3p_interp_data_output ""
    }

    if { ![info exists v3p_interp_data_conn] } {
        set v3p_interp_data_conn ""
    }

    set v3p_interp_data(vars) ""
    set v3p_interp_data(mats) ""
    set v3p_interp_data(conv_factor) 1.0
    foreach var $vList {
        if { [string first "Material_" $var] == -1 } {
            lappend v3p_interp_data(vars) $var
        } else {
            regsub -all "Material_" $var "" var
            lappend v3p_interp_data(mats) $var
        }
    }
  
    if {$v3p_interp_data(format) == "ASCII"} {set interpolation_format 0}
    if {$v3p_interp_data(format) == "ANSYS"} {set interpolation_format 1}

    if {$v3p_interp_data(ansys_command) != "SFE"} {
	::AIEnv::GUIManagement::ToggleGrey v3InterpConnFile 1
    } else {
	::AIEnv::GUIManagement::ToggleGrey v3InterpConnFile 0
    }
       
}

proc onv3InterpFileDEZApply {} {
    global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
    global v3p_interp_data 

    if {$interpolation_format == 0} {set v3p_interp_data(format) "ASCII"}
    if {$interpolation_format == 1} {set v3p_interp_data(format)  "ANSYS"}  

    if { ![info exists v3p_interp_data(selected_vars)] || \
        $v3p_interp_data(selected_vars) == "" } {
        mess "Variable(s) must be selected before interpolation can begin."
        return
    }

    if { $v3p_interp_data(format) == "ANSYS" && 
         $v3p_interp_data(ansys_command) == "SFE" \
        && $v3p_interp_data_conn == "" } {
        mess "The ANSYS command \"SFE\" requires a Connectivity File\n"
        return
    } elseif { $v3p_interp_data_conn != "" } {
        # Trim the braces if they exist.
        set v3p_interp_data_conn [string trimleft $v3p_interp_data_conn "{"]
        set v3p_interp_data_conn [string trimright $v3p_interp_data_conn "}"]
    }

    if { $v3p_interp_data(type) == "Surfaces Only." } {
        set it Shells
    } elseif { $v3p_interp_data(type) == "Volumes Only." } {
        set it Volumes
    } else {
        set it Mixed
    }

    if { $v3p_interp_data(format) == "ANSYS" && $v3p_interp_data(ansys_command) != "SFE"} {
        set interp_format "-format ANSYS -command $v3p_interp_data(ansys_command) \
 -label \"$v3p_interp_data(ansys_label)\" -factor $v3p_interp_data(conv_factor) "
    } elseif { $v3p_interp_data(format) == "ANSYS" && $v3p_interp_data(ansys_command) == "SFE"} {     
        set interp_format "-format ANSYS -command $v3p_interp_data(ansys_command) \
 -label \"$v3p_interp_data(ansys_label)\" -factor $v3p_interp_data(conv_factor) \
 -conn_file \"$v3p_interp_data_conn\""
    } else {
        set interp_format "-format ASCII"
    }

    if { ![info exists v3p_interp_data(selected_mats)] || \
        $v3p_interp_data(selected_mats) == "" } {
        set cmd "interpolate_file_to_file $v3p_interp_data_input \
            $v3p_interp_data_output $v3p_interp_data(selected_vars) \"\" \
            $it $interp_format"
    } else {
        set cmd "interpolate_file_to_file $v3p_interp_data_input \
            $v3p_interp_data_output $v3p_interp_data(selected_vars) \
            $v3p_interp_data(selected_mats) $it $interp_format"
    }


    mess "Starting Interpolation:\n"
    V3_client_cmd $cmd
    mess "Interpolation Done\n"
}

proc onCreateAnsComModify {dez val} {
    global v3p_interp_data 

    if {$v3p_interp_data(ansys_command) != "SFE"} {
	::AIEnv::GUIManagement::ToggleGrey v3InterpConnFile 1
    } else {
	::AIEnv::GUIManagement::ToggleGrey v3InterpConnFile 0
    }

}

proc load_v3InterpFileDEZ {} {
   global v3_started did_startup_v3p did_setup_v3p_first
   
   if {[info exist did_startup_v3p] && [info exist did_setup_v3p_first]} {
      v3InterpFileDEZ show
   } else {
      mess "Interpolation can only occur after Visual3 has started.\n"
   }
}



