proc set_autodyn_file {} {
	global file_name lsdyna_param_arrays
	global lsdyna_Volumes lsdyna_Shells lsdyna_Bars lsdyna_Points
	global thickness_defined
   	if ![info exists lsdyna_param_arrays] { set_lsdyna_params }
  
	set tmp [writeNastranDEZ get autodyn_file]  
	if {$tmp == ""} {
	if {$file_name(settings) == "" } {
		set base autodyn
		writeNastranDEZ set autodyn_file "autodyn.exp"
	} else {
		set base [file tail [file root $file_name(settings)]]
		writeNastranDEZ set autodyn_file $base.exp
	}
	}
 
#CDI doesn't want to see the temp files that get created.
#	set tmp [writeNastranDEZ get autodyn_fbc]  
#	if {$tmp == ""} {
#		writeNastranDEZ set autodyn_fbc $base.autodyn.fbc
#	}
  
#	set tmp [writeNastranDEZ get autodyn_par]  
#	if {$tmp == ""} {
#		writeNastranDEZ set autodyn_par $base.autodyn.par
#	}
    writeNastranDEZ set lsdyna_volumeList {Defined All}
	set tmp [writeNastranDEZ get lsdyna_volume]  
	if {$tmp == ""} {
		writeNastranDEZ set lsdyna_volume Defined
		set lsdyna_Volumes Defined
	}
	 
    writeNastranDEZ set lsdyna_shellList {Defined All None}
	set tmp [writeNastranDEZ get lsdyna_shell]  
	if {$tmp == ""} {
		writeNastranDEZ set lsdyna_shell Defined
		set lsdyna_Shells Defined
	}
	 
    writeNastranDEZ set lsdyna_barList {Defined All None}
	set tmp [writeNastranDEZ get lsdyna_bar]  
	if {$tmp == ""} {
		writeNastranDEZ set lsdyna_bar Defined
		set lsdyna_Bars Defined
	}
	 
    writeNastranDEZ set lsdyna_pointList {Defined All None}
	set tmp [writeNastranDEZ get lsdyna_point]  
	if {$tmp == ""} {
		writeNastranDEZ set lsdyna_point Defined
		set lsdyna_Points Defined
	}
	 
    aie_check_shell_thicknesses
	if ![info exists thickness_defined] {set thickness_defined 0}
    writeNastranDEZ set lsdyna_thickList {BCDefined Distributed Averaged}
	set tmp [writeNastranDEZ get lsdyna_thick]  
	if {$tmp == ""} {
		if {$thickness_defined == 0} {writeNastranDEZ set lsdyna_thick BCDefined
		} elseif {$thickness_defined == 1} {writeNastranDEZ set lsdyna_thick Distributed
		} elseif {$thickness_defined == 2} {writeNastranDEZ set lsdyna_thick Averaged
		}
	} else {
		if {$thickness_defined && $tmp == "BCDefined"} {
			writeNastranDEZ set lsdyna_thick [lindex [writeNastranDEZ get lsdyna_thickList] $thickness_defined]
		}
	}
	 
}

proc setup_autodyn_load {} {
	namespace eval ::AIEnv::DEZ::setupNastranRunDEZ {
    	global env lsdyna_param_arrays
    	if ![info exists lsdyna_param_arrays] { set_lsdyna_params }
    	foreach {n v} [array get lsdyna_param_arrays] {
			array set $n $v
		}
		
		set autodyn_title $title(title)
		if {$autodyn_title==""} { set autodyn_title "AI*Environment AUTODYN Project" }
		foreach mat {lsdynaMatSolidVal lsdynaMatShellVal lsdynaMatBeamVal lsdynaFormBeamVal} {
			if { ![info exists $mat] || [set $mat]=="" } {
				set $mat 0
			}
		}
		if {$lsdyna_rho==""} { set lsdyna_rho 0.0 }
		foreach mat {solid_sigy solid_fail shell_sigy shell_fail} {
			if { ![info exists $mat] || [set $mat]=="" } {
				set $mat 0.0
			}
		}
	}
	
}

proc setup_autodyn_apply {} {
    global env lsdyna_param_arrays

# get the lsdyna params settings..

	set title [setupNastranRunDEZ get autodyn_title]

# update lsdyna params settings
	set lsdyna_param_arrays(title) [list title $title]

}

proc set_autodyn_solve {} {
    global env tcl_platform

}

proc solve_autodyn_file {} {
    global  env solver_setup solver_type 
    global  file_name
    global  is_win

    set solver_setup 4
	
    return
}

proc update_autodyn_setup {} {
    global  env solver_setup solver_type

    set solver_setup 4
    	
}

proc write_autodyn_file {} {
    global env file_name tcl_platform solver_setup solver_type
    global thickness_defined

    set use_lsdyn_exe 1
    if [regexp Multiblock $file_name(domain)] {
        set use_lsdyn_exe 0
    }
    if $use_lsdyn_exe {
        set solver_setup 4
        aie_saveProject 0 1
        set autodyn_file [writeNastranDEZ get autodyn_file]
        set view_file [writeNastranDEZ get view_file]
        set base [file root [file tail $autodyn_file]]
        set afbc $base.autodyn.fbc
        set apar $base.autodyn.par
#        set afbc [writeNastranDEZ get autodyn_fbc]
#        set apar [writeNastranDEZ get autodyn_par]
    
        if ![writeNastranDEZ get lsdynaEditOptionVal] {
            aie_check_shell_thicknesses
            if {$thickness_defined && [writeNastranDEZ get lsdyna_thick] != "BCDefined" && ![ic_uns_thickness_exists_mesh]} {
                mess "Computing thinckness.  Please Wait....\n"
                ic_uns_thickness create
            }
            aie_saveProject 0 1
            mess "Creating the AUTODYN attribute & parameter files....\n"
            if ![aie_convert_nastran_lsdyna_files $afbc $apar] return
        }
    
        mess "Creating the AUTODYN input file.  Please wait...\n"
        # This runs the translator
        if ![aie_export_lsdyna $autodyn_file $file_name(domain) $afbc $apar] return
        if $view_file {
            edit_file $autodyn_file 1
        }
        autodyn_setup_workbench $autodyn_file
    } else {
        mess "Struct mesh file detected...using multiblock info to create the AUTODYN input file\n"
        writeNastranDEZ unload
        multiblock_write_autodyn_input
    }
}

proc multiblock_write_autodyn_input {} {
    global env is_win file_name

    if {$file_name(project_dir) != "" && $file_name(project_dir) != "."} { 
        set domains [multiblock_domain_list]
        if {$domains == {}} {
            errormsg "No multiblock mesh available!"
            return
        }
        get_boco_topo
        set outfile $file_name(project_dir)/autodyn.info
    } else {
        set domains [domain_select $file_name(domains_dir) "Select structured domains." struct 0]
        if {$domains == {}} {
            errormsg "No multiblock mesh available!"
            return
        }
        set dir [file dir [lindex $domains 0]]
        set outfile $dir/autodyn.info
        set files [glob -nocomplain $dir/*.fbc]
        set len [llength $files]
        if {$files != ""} {
            set bocofile [lindex $files 0]
            if {$len > 1} {
                mess "Multiple boco files detected. Select the correct one\n" red
            }
        }
        set files [glob -nocomplain $dir/*.top]
        set len [llength $files]
        if {$files != ""} {
            set topofile [lindex $files 0]
            if {$len > 1} {
                mess "Multiple topo files detected. Select the correct one\n" red
            }
        }
        set files [glob -nocomplain $dir/*.fto]
        if {$files != ""} {
            set ftopofile [lindex $files 0]
        }
    }
    
    set multi 0
    
    if {![dialog .multiblock "MULTIBLOCK" \
          "Please edit the following MULTIBLOCK options." {
	{topofile -type path -title "Topo file" -help "Topology file"}
	{bocofile -type path -title "Boco file" -help "Boundary condition file"}
	{outfile -type path -title "Output file" -help "Output file"}
        {multi -opt {{"Yes" 1} {"No" 0}} \
               -title "Write one grid file per block?" }
    }]} return
	
    if {![file exists $topofile]} {
        errormsg "Topology file doesn't exist!"
        return
    }
    if {![file exists $bocofile]} {
        errormsg "Boundary condition file doesn't exist!"
        return
    }
    mess "Creating the AUTODYN input file.  Please wait...\n"
    cmd_rm domain_list
    set text ""
    foreach d $domains { append text "$d\n" }
    ic_write_file domain_list $text
    set multiblock_output_path $env(ICEM_ACN)/icemcfd/output-interfaces/multiblock
    if $is_win { append multiblock_output_path ".exe" }
    set command "\"$multiblock_output_path\" -b \"$bocofile\" -t \"$topofile\" -f \"$ftopofile\""
    set command "$command -dom_list domain_list \"$outfile\""
    if $multi {set command "$command -multi" }
    runcom $command ""
    cmd_rm domain_list
}

proc autodyn_setup_workbench {autodyn_file {is_batch 0}} {
    # Create entry in Workbench Project Page
    global use_workbench
    if { ![info exists use_workbench] } {
        set use_workbench 0
        return 0
    }

    if { $use_workbench && [info exists autodyn_file] && 
            $autodyn_file != "" && [file exists $autodyn_file] } {
        global env tcl_platform wb_autodyn_app_location
        if {[info exists wb_autodyn_app_location] && $wb_autodyn_app_location != ""} {
            set path $wb_autodyn_app_location
        } else {
            catch {load $env(ICEM_ACN)/bin/icemtcl84.dll registry}
            catch {registry get "HKEY_LOCAL_MACHINE\\SOFTWARE\\ANSYS, Inc.\\v110\\APFramework\\Addins\\AUTODYN" "InstallDir"} path
            regsub "%AWP_ROOT110%" $path $env(AWP_ROOT110) path
        }

        if {![file exists $path]} {
            mess "Can't find AUTODYN installation\n" orange
        } else {
            global wb_autodyn_file
            set wb_autodyn_file $autodyn_file
            set p [pwd]
            # Use an absolute pathname
            if {[string index $wb_autodyn_file 0] == "."} {
                regsub "." $wb_autodyn_file $p wb_autodyn_file
            } elseif { [file dirname $wb_autodyn_file] == "." } {
                set wb_autodyn_file [file join $p $wb_autodyn_file]
            }
            set wb_autodyn_file $wb_autodyn_file
            AIEWBSendToWBScript "AIERegisterAUTODYN" $is_batch
        }
    }
    return 1
}
    
proc set_autodyn_post {} {
}

proc post_autodyn_file {} {
    global  env solver_setup solver_type
    global  is_win

    set solver_setup 4

    return
}


#------------------------------------------------------------------------------




