proc aie_solver_param {} {

	setupSolverParamDEZ show
}
proc aie_solver_type {} {

	setupNastranRunDEZ show
}

proc onSetupNastranRunDEZLoad {} {
    # Initialize with the default values
    global  env solver_setup solver_type solver_setup_global
    global params_data params_type


    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format4 $solve_tmp

#----------- Begin NASTRAN Stuff -----------------------------------------------------------------------    
    foreach {n v} {
	sol      101
	time     99999
	maxlines 99999
	echo     NONE
	autospc  1
	maxratio 1.0e7
	wtmass   1.0
	grdpnt   1
	k6rot    0.0
         eigrl ""
         eigb ""
         nlparm ""
         dload ""
         freq ""
         tstep ""
         sdamp ""
	coupmass   -1
	g   0.0
	w3   0.0
	lgdisp   -1
         include_exec ""
    } {
	set $n $v
    }
    
    # Copy in the values from the .par file
    catch {unset vals}
    
    array set vals [ic_param_get_values :EXEC:SOL]
    if [info exists vals(SOL)] {
	set sol 999
	if {[string match *101* $vals(SOL)] || [string match *SESTATIC* $vals(SOL)]} {
		set sol 101
	}
	if {[string match *103* $vals(SOL)] || [string match *SEMODES* $vals(SOL)]} {
		set sol 103
	}
         if {[string match *105* $vals(SOL)] || [string match *SEBUCKL* $vals(SOL)]} {
		set sol 105
	}
          if {[string match *106* $vals(SOL)] || [string match *NLSTATIC* $vals(SOL)]} {
		set sol 106
	}
          if {[string match *108* $vals(SOL)] || [string match *SEDFREQ* $vals(SOL)]} {
		set sol 108
	}
          if {[string match *109* $vals(SOL)] || [string match *SEDTRAN* $vals(SOL)]} {
		set sol 109
	}
          if {[string match *111* $vals(SOL)] || [string match *SEMFREQ* $vals(SOL)]} {
		set sol 111
	}
   }
    
    array set vals [ic_param_get_values :Output_Selection:Line_Control]
    if [info exists vals(MAXLINES)] { set maxlines $vals(MAXLINES) }
    
    array set vals [ic_param_get_values :Output_Selection:Bulk_Data_Echo]
    if [info exists vals(DESCR)] { set echo $vals(DESCR) }
    set echoList {NONE SORT UNSORT BOTH PUNCH}
    
    if 0 {
	array set vals [ic_param_get_values :Output_Selection:Point_Output]
	if [info exists vals(DISPLACEMENT)] {
	    set disp [expr {$vals(DISPLACEMENT) == "ALL"}]
	}
	
	array set vals [ic_param_get_values :Output_Selection:Element_Output]
	if [info exists vals(STRESS)] {
	    set stress [expr {$vals(STRESS) == "ALL"}]
	}
	if [info exists vals(STRAIN)] {
	    set strain [expr {$vals(STRAIN) == "ALL"}]
	}
	if [info exists vals(ESE)] {
	    set ese [expr {$vals(ESE) == "ALL"}]
	}
    }
    
    array set vals [ic_param_get_values :EXEC:TIME]
    if [info exists vals(Max_CPU_Minutes)] { set time $vals(Max_CPU_Minutes) }
 
    array set vals [ic_param_get_values :EXEC:INCLUDE]
    if [info exists vals(INCLUDE)] {set include_exec $vals(INCLUDE)}
   
    array set vals [aie_get_param_cards]
    if {[info exists vals(AUTOSPC)] && ($vals(AUTOSPC) == "NO")} {
	set autospc 0
    }
    if [info exists vals(MAXRATIO)] { set maxratio $vals(MAXRATIO) }
    if [info exists vals(WTMASS)] { set wtmass $vals(WTMASS) }
    if [info exists vals(GRDPNT)] { set grdpnt $vals(GRDPNT) }
    if [info exists vals(K6ROT)] { set k6rot $vals(K6ROT) }
    if [info exists vals(COUPMASS)] { set coupmass $vals(COUPMASS) }
    if [info exists vals(G)] { set g $vals(G) }
    if [info exists vals(W3)] { set w3 $vals(W3) }
    if [info exists vals(LGDISP)] { set lgdisp $vals(LGDISP) }
    
    foreach {val n} {EIGRL 0 EIGB 1 NLPARM 2 TSTEP 3 FREQ 4} {
        set val_temp {}
        foreach name [array names params_data] {
	  if {$params_type($name) == $n} { set val_temp [concat $val_temp $name]}
        }
        set ${val}List [lsort $val_temp]
    }

    set OptionsList {NONE ALL}

    set DLOADList {}
    foreach name [array names params_data] {
	if {$params_type($name) == 6 || $params_type($name) == 7} { set DLOADList [concat $DLOADList $name]}
    }
    set DLOADList [lsort $DLOADList]

    set SDAMPList {}
    foreach name [array names material_tbl_data] {
	if {$material_tbl_type($name) == 3 || $material_tbl_type($name) == 4} {set SDAMPList [concat $SDAMPList $name]}
    }
    set SDAMPList [lsort $SDAMPList]

    set runTypeList {
	{Linear Static (Sol 101)}
	{Modal (Sol 103)}
         {Buckling Analysis (Sol 105)}
        {Nonlinear Static (Sol 106)}
        {Direct Frequency Response (Sol 108)}
        {Direct Transient Response (Sol 109)}
        {Modal Frequency Response (Sol 111)}
	{Legacy(Sol 999)}
    }
    set runType 0
    foreach rt $runTypeList {
	if [string match *$sol* $rt] break
	    incr runType
    }
    
    ::AIEnv::GUIManagement::ExclusiveGroup::DropDownHandler \
	setupNastranEXG runType
    
    subcaseGetOrSet 1 0
    #subcaseGetOrSet_old 1 0

    set LOADList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 8} { set LOADList [concat $LOADList [lindex $params_data($name) 0]]}
	if {$params_type($name) == 12} { set LOADList [concat $LOADList [lindex $params_data($name) 0]]}
    }
    append Load_SetList " $LOADList"
    set Load_SetList [lsort $Load_SetList]
    set SPCList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 9} { set SPCList [concat $SPCList [lindex $params_data($name) 0]]}
    }
    append Single_Point_ConstraintsList " $SPCList"
    set Single_Point_ConstraintsList [lsort $Single_Point_ConstraintsList]
    set MPCList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 10} { set MPCList [concat $MPCList [lindex $params_data($name) 0]]}
    }
    append Multi_Point_ConstraintsList " $MPCList"
    set Multi_Point_ConstraintsList [lsort $Multi_Point_ConstraintsList]
  
    if {$METHOD != ""} {
       set eigtype 0
       set eigrl [aie_params_name_from_number $METHOD $eigtype]
   }
    if {$METHOD != ""} {
       set eigtype 1
       set eigb [aie_params_name_from_number $METHOD $eigtype]
   }
    if {$NLPARM != ""} {
       set nlparmtype 2
       set nlparm [aie_params_name_from_number $NLPARM $nlparmtype]
   }
     if {$FREQUENCY != ""} {
       set freqtype 4
       set freq [aie_params_name_from_number $FREQUENCY $freqtype]
   }
   if {$TSTEP != ""} {
       set tsteptype 3
       set tstep [aie_params_name_from_number $TSTEP $tsteptype]
   }
   if {$DLOAD != ""} {
       set dloadtype 7
       set dload [aie_params_name_from_number $DLOAD $dloadtype]
       if {$dload == ""} {
          set dloadtype 6
          set dload [aie_params_name_from_number $DLOAD $dloadtype]   
       }      
   }
   if {$SDAMPING != ""} {
       set sdamp [aie_material_tbl_name_from_number $SDAMPING]
   }   

#----------- End NASTRAN Stuff -----------------------------------------------------------------------

#----------- Begin ANSYS Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_ANSYS)] {setup_ansys_load}
#----------- End ANSYS Stuff -----------------------------------------------------------------------

#----------- Begin LS-Dyna Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_LSDYNA)] {setup_lsdyna_load}
#----------- End LS-Dyna Stuff -----------------------------------------------------------------------

#----------- Begin Abaqus Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_ABAQUS)] {setup_abaqus_load}
#----------- End Abaqus Stuff -----------------------------------------------------------------------

#----------- Begin AUTODYN Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_AUTODYN)] {setup_autodyn_load}
#----------- End AUTODYN Stuff -----------------------------------------------------------------------

}

proc onSetupNastranRunDEZApply {} {
    global  env file_name solver_setup
    global params_data params_type

#----------- Begin NASTRAN Stuff -----------------------------------------------------------------------
if {$solve_format4 == "0"} {
    set solver_setup 0
    set sol [lindex {101 103 105 106 108 109 111 999} $runType]
    if {$sol == 103 || $sol == 105} {
	#set STRESS 0
	#set STRAIN 0
	set STRESS ""
	set STRAIN ""
    }
    set METHOD ""
    set NLPARM ""
    set FREQUENCY ""
    set DLOAD ""
    set TSTEP ""
    set SDAMPING ""
    
    ic_param_set_values :EXEC:SOL [list SOL $sol]

    ic_param_set_values :EXEC:TIME [list Max_CPU_Minutes $time]
    
    if { $include_exec != ""} {
       ic_param_set_values :EXEC:INCLUDE [list INCLUDE $include_exec]
    } else {
       ic_param_delete_node :EXEC:INCLUDE     
    }
    
    ic_param_set_values :Output_Selection:Line_Control [list MAXLINES $maxlines]
    
    ic_param_set_values :Output_Selection:Bulk_Data_Echo [list DESCR $echo]
    
    if {$sol == 106} {
       aie_set_param_cards \
	[list AUTOSPC [expr {$autospc ? "YES" : "NO"}] \
	     WTMASS $wtmass GRDPNT $grdpnt K6ROT $k6rot MAXRATIO $maxratio \
	     OGEOM YES POST -1 COUPMASS $coupmass LGDISP $lgdisp]
    } elseif {$sol == 109} {
       aie_set_param_cards \
	[list AUTOSPC [expr {$autospc ? "YES" : "NO"}] \
	     WTMASS $wtmass GRDPNT $grdpnt K6ROT $k6rot MAXRATIO $maxratio \
	     OGEOM YES POST -1 COUPMASS $coupmass G $g W3 $w3]
    } elseif {$sol == 108 || $sol == 111} {
       aie_set_param_cards \
	[list AUTOSPC [expr {$autospc ? "YES" : "NO"}] \
	     WTMASS $wtmass GRDPNT $grdpnt K6ROT $k6rot MAXRATIO $maxratio \
	     OGEOM YES POST -1 COUPMASS $coupmass G $g]
    } else {
       aie_set_param_cards \
	[list AUTOSPC [expr {$autospc ? "YES" : "NO"}] \
	     WTMASS $wtmass GRDPNT $grdpnt K6ROT $k6rot MAXRATIO $maxratio \
	     OGEOM YES POST -1 COUPMASS $coupmass]
    }

    if {$sol == 103 || $sol == 111} {
        if {$eigrl != ""} {set METHOD [lindex $params_data($eigrl) 0]}
    }    

    if {$sol == 105} {
        if {$eigb != ""} {set METHOD [lindex $params_data($eigb) 0]}
    }

    if {$sol == 106} {
        if {$nlparm != ""} {set NLPARM [lindex $params_data($nlparm) 0]}
    }

    if {$sol == 108} {
        if {$freq != ""} {set FREQUENCY [lindex $params_data($freq) 0]}
        if {$dload != ""} {set DLOAD [lindex $params_data($dload) 0]}
   }

    if {$sol == 109} {
        if {$tstep != ""} {set TSTEP [lindex $params_data($tstep) 0]}
        if {$dload != ""} {set DLOAD [lindex $params_data($dload) 0]}
    }

    if {$sol == 111} {
        if {$freq != ""} {set FREQUENCY [lindex $params_data($freq) 0]}
        if {$dload != ""} {set DLOAD [lindex $params_data($dload) 0]}
        if {$sdamp != ""} {set SDAMPING [lindex $material_tbl_data($sdamp) 0]}
   }    
    subcaseGetOrSet 0 0
    #subcaseGetOrSet_old 0 0
}
#----------- End NASTRAN Stuff -----------------------------------------------------------------------

#----------- Begin ANSYS Stuff -----------------------------------------------------------------------
    if {$solve_format4 == "1"}  {
    	set solver_setup 1
		setup_ansys_apply
	}
#----------- End ANSYS Stuff -----------------------------------------------------------------------

#----------- Begin LS-Dyna Stuff -----------------------------------------------------------------------
    if {$solve_format4 == "2"}  {
    	set solver_setup 2
		setup_lsdyna_apply
	}
#----------- End LS-Dyna Stuff -----------------------------------------------------------------------

  if [info exists env(AI_ENV_ABAQUS)] {
#----------- Begin Abaqus Stuff -----------------------------------------------------------------------
    if {$solve_format4 == "3"}  {
    	set solver_setup 3
		setup_abaqus_apply
	}
#----------- End Abaqus Stuff -----------------------------------------------------------------------

#----------- Begin AUTODYN Stuff -----------------------------------------------------------------------
    if {$solve_format4 == "4"}  {
    	set solver_setup 4
		setup_autodyn_apply
	}
#----------- End AUTODYN Stuff -----------------------------------------------------------------------
  } else {
#----------- Begin AUTODYN Stuff -----------------------------------------------------------------------
    if {$solve_format4 == "3"}  {
    	set solver_setup 4
		setup_autodyn_apply
	}
#----------- End AUTODYN Stuff -----------------------------------------------------------------------
  }

}

proc aie_get_param_cards {} {
    set ret ""
    foreach ch [ic_param_list_children :Parameter] {
	set name [string trim [lindex [split $ch /] end]]
	if [string match Param* $name] { set name [string range $name 5 end] }
	catch {unset vals}
	array set vals [ic_param_get_values $ch]
	if ![info exists vals(Real_Part)] continue
	lappend ret $name [string trim $vals(Real_Part)]
    }
    return $ret
}

proc aie_set_param_cards {vals} {
    foreach mat [ic_param_list_children :Parameter] {
        ic_param_delete_node $mat
    }
    foreach {name val} $vals {
        ic_param_add_node :Parameter $name [list Create 1 Real_Part $val Imaginary_Part ""]
        ic_param_set_modified
    }
}

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

proc subcaseGetOrSet_old {get {id ""}} {
    array set opts {
         LABEL ""
	SPC ""
	MPC ""
	LOAD ""
	TEMP ""
          METHOD ""
	DISP 1
	STRESS 1
	STRAIN 1
	ESE 1
	NLPARM ""
         VELOCITY 1
         TSTEP ""
         FREQUENCY ""
         DLOAD ""
         ACCELERATION 1
         SDAMPING ""
         INCLUDE ""
         TITLE ""
         SUBTITLE ""
    }
    
    set allcsets [aie_list_sets impdisp]
    set allmsets [aie_list_sets mpcdisp]
    set alllsets [aie_list_sets load]
    set alltsets [aie_list_sets temperature]
    
    if $get {
	array set vals [ic_param_get_values :Subcase/$id]
	foreach v [array names opts] {
	    if [info exists vals($v)] {
		set x $vals($v)
	    } else {
		set x $opts($v)
	    }
	    if [lmember {DISP STRESS STRAIN ESE VELOCITY ACCELERATION} $v] {
		if {$x == "ALL"} { set x 1 } else { set x 0 }
	    }
	    uplevel 1 set $v \"$x\"
	}
	uplevel 1 set Single_Point_ConstraintsList \"$allcsets\"
	uplevel 1 set Multi_Point_ConstraintsList \"$allmsets\"
	uplevel 1 set Load_SetList \"$alllsets\"
	uplevel 1 set Temp_SetList \"$alltsets\"
	
    } else {
	set vals ""
	foreach v [array names opts] {
	    set x [uplevel 1 set $v]
	    if {$x == ""} continue
	    if [lmember {DISP STRESS STRAIN ESE VELOCITY ACCELERATION} $v] {
		if $x { set x ALL } else { set x NONE }
	    }
	    lappend vals $v $x
	}
	# Make sure the old values are cleared out
	ic_param_delete_node :Subcase/$id
	ic_param_set_values :Subcase/$id $vals
    }
}

proc subcaseGetOrSet {get {id ""}} {
    array set opts {
         ACTIVE ""
         LABEL ""
	SPC ""
	MPC ""
	LOAD ""
	TEMP ""
          METHOD ""
	DISP ""
	STRESS ""
	STRAIN ""
	ESE ""
	NLPARM ""
         VELOCITY ""
         TSTEP ""
         FREQUENCY ""
         DLOAD ""
         ACCELERATION ""
         SDAMPING ""
         INCLUDE ""
         TITLE ""
         SUBTITLE ""
   }
    
    set allcsets [aie_list_sets impdisp]
    set allmsets [aie_list_sets mpcdisp]

    set alllsets [aie_list_sets load]
    set alltsets [aie_list_sets temperature]
    
    if $get {
	array set vals [ic_param_get_values :Subcase/$id]
	foreach v [array names opts] {
	    if [info exists vals($v)] {
		set x $vals($v)
	    } else {
		set x $opts($v)
	    }
	    uplevel 1 set $v \"$x\"
	}
	uplevel 1 set Single_Point_ConstraintsList \"$allcsets\"
	uplevel 1 set Multi_Point_ConstraintsList \"$allmsets\"
	uplevel 1 set Load_SetList \"$alllsets\"
	uplevel 1 set Temp_SetList \"$alltsets\"
	
    } else {
	set vals ""
	foreach v [array names opts] {
	    set x [uplevel 1 set $v]
	    if {$x == ""} continue
	    lappend vals $v $x
	}
	# Make sure the old values are cleared out
	ic_param_delete_node :Subcase/$id
	ic_param_set_values :Subcase/$id $vals
    }
}

proc onSubcaseDEZLoad {} {
    global current_subcase_to_load
    global solver_setup solver_setup_global
    global params_data params_type
    global subcase_data

    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format5 $solve_tmp

    foreach {n v} {
         subcase_active 1
         eigs ""
         nlparm ""
         dload ""
         freq ""
         tstep ""
         sdamp ""
    } {
	set $n $v
    }

    foreach {val n} {NLPARM 2 TSTEP 3 FREQ 4} {
        set val_temp {}
        foreach name [array names params_data] {
	  if {$params_type($name) == $n} { set val_temp [concat $val_temp $name]}
        }
        set ${val}List [lsort $val_temp]
    }

    set EIGSList {}
    foreach name [array names params_data] {
	if {$params_type($name) == 0 || $params_type($name) == 1} { set EIGSList [concat $EIGSList $name]}
    }
    set EIGSList [lsort $EIGSList]

    set OptionsList {NONE ALL}

    set DLOADList {}
    foreach name [array names params_data] {
	if {$params_type($name) == 6 || $params_type($name) == 7} { set DLOADList [concat $DLOADList $name]}
    }
    set DLOADList [lsort $DLOADList]

    set SDAMPList {}
    foreach name [array names material_tbl_data] {
	if {$material_tbl_type($name) == 3 || $material_tbl_type($name) == 4} {set SDAMPList [concat $SDAMPList $name]}
    }
    set SDAMPList [lsort $SDAMPList]

    if [info exists current_subcase_to_load] {
	set Subcase_ID $current_subcase_to_load
    } else {
	#set Subcase_ID [ic_param_unused_child_name :Subcase ""]
	foreach i [array names subcase_data] {
	    set used($i) 1
	    set used([lindex $subcase_data($i) 0]) 1
	}
	for {set i 1} {[info exists used(Subcase$i)] || [info exists used($i)]} {incr i} { }
	set Subcase_ID $i
    }

    subcaseGetOrSet 1 $Subcase_ID

    set LOADList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 8} { set LOADList [concat $LOADList [lindex $params_data($name) 0]]}
	if {$params_type($name) == 12} { set LOADList [concat $LOADList [lindex $params_data($name) 0]]}
    }
    append Load_SetList " $LOADList"
    set Load_SetList [lsort $Load_SetList]
    set SPCList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 9} { set SPCList [concat $SPCList [lindex $params_data($name) 0]]}
    }
    append Single_Point_ConstraintsList " $SPCList"
    set Single_Point_ConstraintsList [lsort $Single_Point_ConstraintsList]
    set MPCList " "
    foreach name [array names params_data] {
	if {$params_type($name) == 10} { set MPCList [concat $MPCList [lindex $params_data($name) 0]]}
    }
    append Multi_Point_ConstraintsList " $MPCList"
    set Multi_Point_ConstraintsList [lsort $Multi_Point_ConstraintsList]
  
    if {$ACTIVE != ""} {
       set subcase_active $ACTIVE
   }
    if {$METHOD != ""} {
       set eigtype 0
       set eigs [aie_params_name_from_number $METHOD $eigtype]
       if {$eigs == ""} {
          set eigtype 1
          set eigs [aie_params_name_from_number $METHOD $eigtype]
       }
   }
    if {$NLPARM != ""} {
       set nlparmtype 2
       set nlparm [aie_params_name_from_number $NLPARM $nlparmtype]
   }
     if {$FREQUENCY != ""} {
       set freqtype 4
       set freq [aie_params_name_from_number $FREQUENCY $freqtype]
   }
   if {$TSTEP != ""} {
       set tsteptype 3
       set tstep [aie_params_name_from_number $TSTEP $tsteptype]
   }
   if {$DLOAD != ""} {
       set dloadtype 7
       set dload [aie_params_name_from_number $DLOAD $dloadtype]
       if {$dload == ""} {
          set dloadtype 6
          set dload [aie_params_name_from_number $DLOAD $dloadtype]   
       }      
   }
   if {$SDAMPING != ""} {
       set sdamp [aie_material_tbl_name_from_number $SDAMPING]
   }   

}

proc onSubcaseDEZApply {} {
    global solver_setup
    global params_data params_type
    global subcase_data

   set METHOD ""
    set NLPARM ""
    set FREQUENCY ""
    set DLOAD ""
    set TSTEP ""
    set SDAMPING ""

    set name Subcase$Subcase_ID
    set subcase_data($name) $Subcase_ID

    set ACTIVE $subcase_active

    if {$eigs != ""} {set METHOD [lindex $params_data($eigs) 0]}

    if {$nlparm != ""} {set NLPARM [lindex $params_data($nlparm) 0]}

    if {$freq != ""} {set FREQUENCY [lindex $params_data($freq) 0]}

    if {$dload != ""} {set DLOAD [lindex $params_data($dload) 0]}

    if {$tstep != ""} {set TSTEP [lindex $params_data($tstep) 0]}

    if {$sdamp != ""} {set SDAMPING [lindex $material_tbl_data($sdamp) 0]}
 
    subcaseGetOrSet 0 $Subcase_ID
    ::AIEnv::GUIManagement::Tree::PutSubcases $Subcase_ID 0

#----------- Begin ANSYS Stuff -----------------------------------------------------------------------
    if {$solve_format5 == "1"}  {
    	set solver_setup 1
	}
#----------- End ANSYS Stuff -----------------------------------------------------------------------

#----------- Begin LS-Dyna Stuff -----------------------------------------------------------------------
    if {$solve_format5== "2"}  {
    	set solver_setup 2
	}
#----------- End LS-Dyna Stuff -----------------------------------------------------------------------

  if [info exists env(AI_ENV_ABAQUS)] {
#----------- Begin Abaqus Stuff -----------------------------------------------------------------------
    if {$solve_format5== "3"}  {
    	set solver_setup 3
	}
#----------- End Abaqus Stuff -----------------------------------------------------------------------

#----------- Begin AUTODYN Stuff -----------------------------------------------------------------------
    if {$solve_format5== "4"}  {
    	set solver_setup 4
	}
#----------- End AUTODYN Stuff -----------------------------------------------------------------------
  } else {
#----------- Begin AUTODYN Stuff -----------------------------------------------------------------------
    if {$solve_format5== "3"}  {
    	set solver_setup 4
	}
#----------- End AUTODYN Stuff -----------------------------------------------------------------------
  }

}

proc aie_edit_subcase {name} {
    global current_subcase_to_load
    catch {subcaseDEZ accept} ;# in case it's already there
    set current_subcase_to_load $name
    subcaseDEZ show
    unset current_subcase_to_load
}

proc aie_load_subcase {} {
    global env
    
    if [info exists env(AI_ENV_CFX)] return  ;# for now
    
    ::AIEnv::GUIManagement::Tree::PutSubcases [aie_list_subcases] 1
}

proc aie_list_subcases {} {
    global subcase_data

    set ids ""
    foreach ch [ic_param_list_children :Subcase] {
	set n [lindex [split $ch /] end]
         set name Subcase$n
	set subcase_data($name) $n
	if $n { lappend ids $n }
    }
    return $ids
}

proc aie_delete_subcase {name} {
    global subcase_data

    mess "Deleting subcase \"$name\"\n"
    
    set subcase_name Subcase$name
    catch {unset subcase_data($subcase_name)}
    ic_param_delete_node :Subcase/$name
    ::AIEnv::GUIManagement::Tree::PutSubcases [aie_list_subcases] 1
}

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

proc aie_lsdyna_file {} {
    global solver_setup
    global from_export
    global lsdyna_Volumes lsdyna_Shells lsdyna_Bars lsdyna_Points
    
    set solver_setup 2
    set from_export 0

    writeNastranDEZ show
    
    writeNastranDEZ set lsdyna_volume All
    set lsdyna_Volumes All
    
    writeNastranDEZ set lsdyna_shell All
    set lsdyna_Shells All
    
    writeNastranDEZ set lsdyna_bar All
    set lsdyna_Bars All
    
    writeNastranDEZ set lsdyna_point All
    set lsdyna_Points All
}

proc aie_ansys_file {} {
    global solver_setup
    global from_export
    global ansys_Volumes ansys_Shells ansys_Bars
    
    set solver_setup 1
    set from_export 0
    
    writeNastranDEZ show
    
    writeNastranDEZ set ansys_volume All
    set ansys_Volumes All
    
    writeNastranDEZ set ansys_shell All
    set ansys_Shells All
    
    writeNastranDEZ set ansys_bar All
    set ansys_Bars All
}

proc aie_abaqus_file {} {
    global solver_setup from_export
    global abaqus_Volumes abaqus_Shells abaqus_Bars abaqus_Points
    
    set solver_setup 3
    set from_export 0
    
    writeNastranDEZ show
    
    writeNastranDEZ set abaqus_volume All
    set abaqus_Volumes All
    
    writeNastranDEZ set abaqus_shell All
    set abaqus_Shells All
    
    writeNastranDEZ set abaqus_bar All
    set abaqus_Bars All
    
    writeNastranDEZ set abaqus_point All
    set abaqus_Points All
}

proc aie_autodyn_file {} {
    global solver_setup
    global from_export
    global lsdyna_Volumes lsdyna_Shells lsdyna_Bars
    
    set solver_setup 4
    set from_export 0
    
    writeNastranDEZ show
    
    writeNastranDEZ set lsdyna_volume All
    set lsdyna_Volumes All
    
    writeNastranDEZ set lsdyna_shell All
    set lsdyna_Shells All
    
    writeNastranDEZ set lsdyna_bar All
    set lsdyna_Bars All
}

proc aie_nastran_file {} {
    global from_export

    set from_export 0
    writeNastranDEZ show
}

proc aie_nastran_file_export {} {
    global solver_setup
    global from_export

    set from_export 1
    set solver_setup 0
    writeNastranDEZ show
    writeNastranDEZ set volume "All"
    writeNastranDEZ set shell "All"
    writeNastranDEZ set bar "All"
}

proc onwriteNastranDEZLoad {} {
    global  env file_name solver_setup solver_setup_global

    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format $solve_tmp

    set_nastran_file
    if [info exists env(AI_ENV_ANSYS)] {set_ansys_file}
    if [info exists env(AI_ENV_LSDYNA)] {set_lsdyna_file}
    if [info exists env(AI_ENV_ABAQUS)] {set_abaqus_file}
    if [info exists env(AI_ENV_AUTODYN)] {set_autodyn_file}

}

proc set_nastran_file {} {
    global  file_name  thickness_defined
    global from_export
    global write_mesh_option

    aie_check_shell_thicknesses
    
    set nas_file_tmp [writeNastranDEZ get nas_file]
    if {$nas_file_tmp == ""} {
	if {$file_name(settings) == "" } {
 	    writeNastranDEZ set nas_file "nastran.dat"
	} else {
	    set nas_file_tmp [file root $file_name(settings)]
	    append nas_file_tmp \.dat
             writeNastranDEZ set nas_file $nas_file_tmp
	} 
         writeNastranDEZ set large_format 0
         writeNastranDEZ set cont_flag 0
    }

    set volume_tmp [writeNastranDEZ get volume]
    if {!$from_export && [info exists write_mesh_option]} {
           writeNastranDEZ set volume [lindex $write_mesh_option(nastran) 0]
    } elseif {$volume_tmp != ""} {
           writeNastranDEZ set volume $volume_tmp
    } else {
          writeNastranDEZ set volume "Defined"
     }
    writeNastranDEZ set volumeList {
	{Defined}
	{All}
          {None}
    }
    set shell_tmp [writeNastranDEZ get shell]
    if {!$from_export && [info exists write_mesh_option]} {
           writeNastranDEZ set shell [lindex $write_mesh_option(nastran) 1]
    } elseif {$shell_tmp != ""} {
           writeNastranDEZ set shell $shell_tmp
    } else {
          writeNastranDEZ set shell "Defined"
     }
    writeNastranDEZ set shellList {
	{Defined}
	{All}
          {None}
    }
    set bar_tmp [writeNastranDEZ get bar]
    if {!$from_export && [info exists write_mesh_option]} {
           writeNastranDEZ set bar [lindex $write_mesh_option(nastran) 2]
    } elseif {$bar_tmp != ""} {
           writeNastranDEZ set bar $bar_tmp
    } else {
          writeNastranDEZ set bar "Defined"
     }
    writeNastranDEZ set barList {
	{Defined}
	{All}
          {None}
    }

    writeNastranDEZ set thickList {
	{BCDefined}
	{Distributed}
          {Averaged}
    }
    set thick_tmp [writeNastranDEZ get thick]
    if {$thick_tmp != ""} {
           writeNastranDEZ set thick $thick_tmp
           if {$thickness_defined > 0 && $thick_tmp == "BCDefined"} {writeNastranDEZ set thick [lindex [writeNastranDEZ get thickList] $thickness_defined]}
           if {$thickness_defined == 0 && $thick_tmp != "BCDefined"} {writeNastranDEZ set thick "BCDefined"}
    } else {
          if {$thickness_defined == 0} {writeNastranDEZ set thick "BCDefined"}
          if {$thickness_defined == 1} {writeNastranDEZ set thick "Distributed"}
          if {$thickness_defined == 2} {writeNastranDEZ set thick "Averaged"}
    }

}

proc onwriteNastranDEZApply {} {
    global  env file_name solver_setup

    #aie_saveProject 0 1

    set nas_file [writeNastranDEZ get nas_file]
    if {$file_name(solver_params) == ""} {set file_name(solver_params) [file rootname $nas_file].par}    
    if {![file exists $file_name(solver_params)]} {
	aie_save_params
    }
    if {$file_name(family_boco) == "" || $file_name(attributes) == ""} {
       set file_name(family_boco) [file rootname $nas_file].fbc
       set file_name(attributes) [file rootname $nas_file].atr
    }    
    if {![file exists $file_name(family_boco)] || ![file exists $file_name(attributes)]} {
	aie_save_attr
    }

    writeNastranDEZ setclean

    if {$solve_format == "0"}  {write_nastran_file}
    if {$solve_format == "1"}  {write_ansys_file}
    if {$solve_format == "2"}  {write_lsdyna_file}
    if [info exists env(AI_ENV_ABAQUS)] {
       if {$solve_format == "3"}  {write_abaqus_file}
       if {$solve_format == "4"}  {write_autodyn_file}
    } else {
       if {$solve_format == "3"}  {write_autodyn_file}
    }

}

proc write_nastran_file {} {
    global env file_name tcl_platform thickness_defined
    global from_export
    global write_mesh_option
    global beta_mode

    set solver_setup 0
    set nas_file [writeNastranDEZ get nas_file]
    set volume [writeNastranDEZ get volume]
    set shell [writeNastranDEZ get shell]
    set bar [writeNastranDEZ get bar]
    set thick [writeNastranDEZ get thick]
    set view_file [writeNastranDEZ get view_file]
    set large_format [writeNastranDEZ get large_format]
    set cont_flag [writeNastranDEZ get cont_flag]

    if {!$from_export} {set write_mesh_option(nastran) [list $volume $shell $bar]}

    if {$thickness_defined != 0 && $thick != "BCDefined" && ![ic_uns_thickness_exists_mesh]} {
       mess "Computing thinckness.  Please Wait....\n"
       ic_uns_thickness create
    }
    aie_saveProject 0 1
    
    switch $volume {
	Defined { set vol_v 1 }
	All { set vol_v 2 }
	None { set vol_v 0 }
    }
    switch $shell {
	Defined { set shell_v 1 }
	All { set shell_v 2 }
	None { set shell_v 0 }
    }
    switch $bar {
	Defined { set bar_v 1 }
	All { set bar_v 2 }
	None { set bar_v 0 }
    }

    switch $thick {
	BCDefined { set thick_v 0 }
	Distributed { set thick_v 1 }
	Averaged { set thick_v 2 }
    }
    
    mess "Creating the Nastran Input File.  Please Wait....\n"
    
    if 0 {
        if ![aie_export_nastran $nas_file $file_name(domain) \
	     $file_name(family_boco) $file_name(solver_params) \
	     $large_format $vol_v $shell_v $bar_v $thick_v $cont_flag] return
    }
    
    if ![aie_export_nastran $nas_file $file_name(domain) \
	 $file_name(attributes) $file_name(solver_params) \
	 $large_format $vol_v $shell_v $bar_v $thick_v $cont_flag] return
    
    if $view_file {
	edit_file $nas_file 1
    }
}

proc close_write_file {} {
   writeNastranDEZ set nas_file ""
   writeNastranDEZ set ansys_file ""
   writeNastranDEZ set ansys_atr ""
   writeNastranDEZ set ansys_par ""
   writeNastranDEZ set lsdyna_file ""
   writeNastranDEZ set lsdyna_fbc ""
   writeNastranDEZ set lsdyna_par ""
   writeNastranDEZ set abaqus_file ""
   writeNastranDEZ set abaqus_fbc ""
   writeNastranDEZ set abaqus_par ""
   writeNastranDEZ set autodyn_file ""
}

proc aie_nastran_post {} {

	postNastranDEZ show
}

proc onpostNastranDEZLoad {} {
    global  env file_name solver_setup solver_setup_global

    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format3 $solve_tmp

    if {![info exists post_option]} {set post_option 0}

    set_nastran_post
    if [info exists env(AI_ENV_ANSYS)] {set_ansys_post}
    if [info exists env(AI_ENV_LSDYNA)] {set_lsdyna_post}
    if [info exists env(AI_ENV_ABAQUS)] {set_abaqus_post}
    if [info exists env(AI_ENV_AUTODYN)] {set_autodyn_post}

}

proc set_nastran_post {} {

	set iset 0
	set iset_1 0
	#set [postNasOptions get -key variable] 0

#Option 1

	#set entform_tmp [postNastranDEZ get entform]
	#if { $entform_tmp != ""} {
	#	postNastranDEZ set entform $entform_tmp
	#	set iset 1
	#} else {
	#	set entform_tmp [solveNastranDEZ get entform_1]
	#	if { $entform_tmp != ""} {
	#		postNastranDEZ set entform $entform_tmp
	#		set iset 1
	#	} else {
	#		postNastranDEZ set entform 0
	#		set iset 0
	#	}
	#}

	set dom_tmp_file [postNastranDEZ get nasdom_file]
	if { $dom_tmp_file != ""} {
                postNastranDEZ set nasdom_file $dom_tmp_file
         } else {
               postNastranDEZ set nasdom_file "postop2.dom"
         }

	set op2_tmp_file [postNastranDEZ get nasop2_file]
	if { $op2_tmp_file != ""} {
		postNastranDEZ set nasop2_file $op2_tmp_file
	} else {
		set op2_tmp_file [postNastranDEZ get nasop2_file_1]
		if { $op2_tmp_file != ""} {
			postNastranDEZ set nasop2_file $op2_tmp_file
		} else {
			set nas_input_file [writeNastranDEZ get nas_file]	
			if { $nas_input_file != ""} {
				set nasop2_file1 [file root $nas_input_file]
			} else {
				set nas_input_file [solveNastranDEZ get nasdat_file]
				if { $nas_input_file != ""} {
					set nasop2_file1 [file root $nas_input_file]
				} else {
					set nasop2_file1 "nastran"
				}
			}
			append nasop2_file1 \.op2
                            postNastranDEZ set nasop2_file $nasop2_file1
		}
	}
          

#Option 3

	#set entform_tmp [postNastranDEZ get entform]
	#if { $entform_tmp != ""} {
	#	postNastranDEZ set entform_1 $entform_tmp
	#	set iset_1 1
	#} else {
	#	set entform_tmp [solveNastranDEZ get entform_1]
	#	if { $entform_tmp != ""} {
	#		postNastranDEZ set entform_1 $entform_tmp
	#		set iset_1 1
	#	} else {
	#		postNastranDEZ set entform_1 0
	#		set iset_1 0
	#	}
	#}

          #postNastranDEZ set nasdom_file "postop2.dom"

	set op2_tmp_file [postNastranDEZ get nasop2_file]
	if { $op2_tmp_file != ""} {
		postNastranDEZ set nasop2_file_1 $op2_tmp_file
	} else {
		set op2_tmp_file [postNastranDEZ get nasop2_file_1]
		if { $op2_tmp_file != ""} {
			postNastranDEZ set nasop2_file_1 $op2_tmp_file
		} else {
			set nas_input_file [writeNastranDEZ get nas_file]	
			if { $nas_input_file != ""} {
				set nasop2_file_11 [file root $nas_input_file]
			} else {
				set nas_input_file [solveNastranDEZ get nasdat_file]
				if { $nas_input_file != ""} {
					set nasop2_file_11 [file root $nas_input_file]
				} else {
					set nasop2_file_11 "nastran"
				}
			}
			append nasop2_file_11 \.op2
                            postNastranDEZ set nasop2_file_1 $nasop2_file_11
		}
	}

#Option 2
	
	#set nas_post_stat [solveNastranDEZ get solvePostProcessVal]
	#if { $nas_post_stat == 1} {
	#	if  {$iset == 0} {
	#		set entform_tmp [solveNastranDEZ get entformb]
	#		if { $entform_tmp != ""} {postNastranDEZ set entform $entform_tmp}
	#	}
	#	if  {$iset_1 == 0} {
	#		set entform_tmp [solveNastranDEZ get entformb]
	#		if { $entform_tmp != ""} {postNastranDEZ set entform_1 $entform_tmp}
	#	}
	#}
	
}

proc onpostNastranDEZOK {} {
    global  env file_name solver_setup

    if {$solve_format3 == "0"} {
              #if {$postNasOptionsVal == 0} {post_nastran_file {0}}
              #if {$postNasOptionsVal == 1} {post_nastran_file {1}}
              #if {$postNasOptionsVal == 2} {post_nastran_file {2}}
	    if {$post_option == 0} {post_nastran_file {0}}
              if {$post_option == 1} {post_nastran_file {1}}
              if {$post_option == 2} {post_nastran_file {2}}
    }
    if {$solve_format3 == "1"}  {post_ansys_file}
    if {$solve_format3 == "2"}  {post_lsdyna_file_v3}
    if [info exists env(AI_ENV_ABAQUS)] {
       if {$solve_format3 == "3"}  {post_abaqus_file}
       if {$solve_format3 == "4"}  {post_autodyn_file}
    } else {
       if {$solve_format3 == "3"}  {post_autodyn_file}
    }

}

proc post_nastran_file {{post_opt ""}} {
     global  env file_name solver_setup

         set solver_setup 0

         #set [postNasOptions get -key variable] 0
         set nasdom_file [postNastranDEZ get nasdom_file]

	set v3_opts "-type nastran "
         append v3_opts "$nasdom_file"

	if {$post_opt == "0"} {aie_nastran_op2 {interactive}}
	if {$post_opt == "1"} {
		if ![file exists $nasdom_file] {
          			mess "Can't find Domain file $nasdom_file\n" red
			return
		}
		#aie_start_visual3p $v3_opts
                   aie_v3p_startup_cmd -data_format nastran -startup_files [list $nasdom_file]
	}
	if {$post_opt == "2"} {aie_nastran_op2 {interactive_1}}
}
proc aie_nastran_op2 {{opt ""}} {
   global env tcl_platform solver_setup solver_type

	set file_dom "postop2.dom"
	mess "option: $opt\n"
	if {$opt == "interactive"} {
		set nas_op2_file [postNastranDEZ get nasop2_file]
		#set nas_op2_form [postNastranDEZ get entform]
                   set nas_op2_form $solver_type                  
	}
	if {$opt == "interactive_1"} {
		set nas_op2_file [postNastranDEZ get nasop2_file_1]
		#set nas_op2_form [postNastranDEZ get entform_1]
                   set nas_op2_form $solver_type
	}
	if {$opt == "batch"} {
		set nas_dat_file [solveNastranDEZ get nasdat_file]
		set nas_op2_file "nastran"
		if { $nas_dat_file != ""} {set nas_op2_file [file root $nas_dat_file]}
		append nas_op2_file \.op2
		#set nas_op2_form [solveNastranDEZ get entformb]
                   set nas_op2_form $solver_type
	}

	if ![file exists $nas_op2_file] {
          		mess "Can't find OP2 file $nas_op2_file\n" red
		return
	}

	if { $nas_op2_file != ""} {
		set file_dom [file root $nas_op2_file]
		append file_dom \.dom
	}

	mess "OP2 File: $nas_op2_file \n Domain File: $file_dom \n Format: $nas_op2_form\n"

         set postop2 [file join $env(ICEM_ACN) icemcfd result-interfaces postop2]

          if { $tcl_platform(platform) == "windows" } {
                set postop2 "${postop2}.exe"
          }
	regsub -all \\\\ $postop2 / postop2

	if ![file executable $postop2] {
          		error "Can't find OP2 Converter executable $postop2"
		return
       	}

         if {$nas_op2_form == -1} {
             msg ERROR "No NASTRAN format is provided.\nPlease set the NASTRAN format type under Setting -> Solver"
             return
          }
          if { $tcl_platform(platform) == "windows" } {
		if {$nas_op2_form == 0} { set postop2_cmd "\"$postop2\" -ai \"$nas_op2_file\" \"$file_dom\""}
		if {$nas_op2_form == 1} { set postop2_cmd "\"$postop2\" -msc \"$nas_op2_file\" \"$file_dom\""}
         } else {
		if {$nas_op2_form == 0} { set postop2_cmd "$postop2 -ai $nas_op2_file $file_dom"}
		if {$nas_op2_form == 1} { set postop2_cmd "$postop2 -msc $nas_op2_file $file_dom"}
         }

	mess "executing $postop2_cmd\n"
	mess "Processing OP2 File.  Please Wait....\n"

	if {![catch {eval ic_exec $postop2_cmd} pid]} {
      		mess "$pid\n"
		if {$opt !=  "interactive_1"} {
			if ![file exists $file_dom] {
          				mess "Can't find Domain file $file_dom\n" red
				return
			}
			set v3_opts "-type nastran "
         			append v3_opts "$file_dom"
			#aie_start_visual3p $v3_opts
                            aie_v3p_startup_cmd -data_format nastran -startup_files [list $file_dom]
		}
	} else {
		mess "Error \n $pid\n" red
	}
}

# moved to aie_ansys.tcl
#proc post_ansys_file {} {
#     global  env file_name solver_setup
#
#         set solver_setup 1
#
#         set ansys_rslt_file [postNastranDEZ get ansys_rslt_file]
#
#	set v3_opts "-type ansys "
#         append v3_opts "$ansys_rslt_file"
#
#	if ![file exists $ansys_rslt_file] {
#          		mess "Can't find Ansys Result file $ansys_rslt_file\n" red
#		return
#	}
#	aie_start_visual3p $v3_opts
#
#}

proc aie_nastran_solve {} {

	solveNastranDEZ show
}

proc onsolveNastranDEZLoad {} {
    global  env solver_setup solver_type solver_setup_global

    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format2 $solve_tmp

    set_nastran_solve
    if [info exists env(AI_ENV_ANSYS)] {set_ansys_solve}
    if [info exists env(AI_ENV_LSDYNA)] {set_lsdyna_solve}
    if [info exists env(AI_ENV_ABAQUS)] {set_abaqus_solve}
    if [info exists env(AI_ENV_AUTODYN)] {set_autodyn_solve}

}

proc set_nastran_solve {} {

	set iset 0

	#set nas_post_stat [solveNastranDEZ get solvePostProcessVal]
	#if { $nas_post_stat != ""} {
	#	solveNastranDEZ set solvePostProcessVal $nas_post_stat
	#} else {
	#	solveNastranDEZ set solvePostProcessVal 0
	#}
	#set entform_tmp [solveNastranDEZ get entformb]
	#if { $entform_tmp != ""} {
	#	solveNastranDEZ set entformb $entform_tmp
	#	set iset 1
	#} else {
	#	solveNastranDEZ set entformb 0
	#	set iset 0
	#}
         
	set nas_tmp_file [solveNastranDEZ get nasdat_file]
	if { $nas_tmp_file != ""} {
		solveNastranDEZ set nasdat_file $nas_tmp_file
		#if  {$iset == 0} {
		#	set entform_tmp [postNastranDEZ get entform]
		#	if { $entform_tmp != ""} {solveNastranDEZ set entformb $entform_tmp}
		#}
	} else {
                  set nas_file_tmp [writeNastranDEZ get nas_file]
    		if {$nas_file_tmp != ""} {
                        solveNastranDEZ set nasdat_file $nas_file_tmp
                   } else {
		     solveNastranDEZ set nasdat_file "nastran.dat"
                   }
	}

	set option_solver_tmp [solveNastranDEZ get option_solver]
	if { $option_solver_tmp != ""} {
		solveNastranDEZ set option_solver $option_solver_tmp
	} else {
		solveNastranDEZ set option_solver ""
	}
 
}

proc onsolveNastranDEZOK {} {
global  env file_name solver_setup

    if {$solve_format2 == "0"} {solve_nastran_file}
    if {$solve_format2 == "1"}  {solve_ansys_file}
    if {$solve_format2 == "2"}  {solve_lsdyna_file}
    if [info exists env(AI_ENV_ABAQUS)] {
       if {$solve_format2 == "3"}  {solve_abaqus_file}
       if {$solve_format2 == "4"}  {solve_autodyn_file}
    } else {
       if {$solve_format2 == "3"}  {solve_autodyn_file}
    }

}

proc solve_nastran_file {} {
   global env tcl_platform solution_finished solver_setup solver_type

         set solver_setup 0
	#set nas_post_stat [solveNastranDEZ get solvePostProcessVal]
	set npost_ok 0
	set nexec_stat 0
	set i_format -1

         set nasdat_file [solveNastranDEZ get nasdat_file]
         set option_solver [solveNastranDEZ get option_solver]
         set nas_post_stat 0

	#if { $nas_post_stat == 1} {
	#	mess "Format: $res_format\n"
	#	switch $res_format {
	#		Select { 
	#			msg ERROR "Please select Result Format!"
	#			return }
	#		AI*Nastran { set i_format 0 }
	#		MSC Nastran { set i_format 1 }
    	#	}
	#}
         if { $nas_post_stat == 1} {
		if { $solver_type == -1} {
				msg ERROR "No NASTRAN format is provided.\nPlease set the NASTRAN format type under Setting -> Solver"
				return }
         }

	if [info exists env(NASTRAN_EXEC_PATH)] {
		set nas_app $env(NASTRAN_EXEC_PATH)
		regsub -all \\\\ $nas_app / nas_app
		if ![file exists $nas_app] {
          			error "Can't find Nastran executable $nas_app"
			return
       		} else {
			if { $tcl_platform(platform) == "windows" } {
				set file $nasdat_file
				regsub -all / $file \\\\\\\\ nasdat_file_new
				if {[file executable $nas_app] } {
					set nas_cmd "\"$nas_app\" \"$nasdat_file_new\""
				} else {
					set nas_cmd "Start /min $nas_app $nasdat_file_new"
					#set npost_ok 1
					set nexec_stat 1
				}
			} else {
				set nas_cmd "$nas_app $nasdat_file"
			}

			if {$option_solver != ""} {
				lappend nas_cmd $option_solver
			}

			if ![file exists $nasdat_file] {
          				mess "Can't find input file $nasdat_file\n" red
				return
			}

			set inputDir [file dirname $nasdat_file]
			set err_file [file root $nasdat_file]
			append err_file \.aie
			if { $tcl_platform(platform) == "windows" } {
				regsub -all / $inputDir \\\\\\\\ inputDir_new
				set inputDir $inputDir_new
				#regsub -all / $err_file \\\\\\\\ err_file_new
				#set err_file $err_file_new
			}
			set here [pwd]
	    		if {$inputDir != ""} {ic_chdir $inputDir}
			set f06_file [file root $nasdat_file]
			append f06_file \.f06

 			mess "executing $nas_cmd\n"
			mess "Running NASTRAN.  Please Wait....\n"
			#if {![catch {eval exec $nas_cmd} pid]} {
      			#	mess "$pid\n"
			#} else {
			#	mess "Error \n $pid\n" red
				#return
			#}
			catch {eval ic_exec $nas_cmd} pid
			mess "$pid\n"

			if {$nexec_stat == 1} {
				set fp_bat [file root $nasdat_file]
				append fp_bat \.bat
				#while {![file exists $fp_bat]} {
		    			#mess "Did not start\n" yellow
				#}
				#mess "Job Started\n" blue
				for {set s 0} {$s < 60} {incr s} {
					if [file exists $fp_bat] {
						mess "Job Started\n" blue
						break
					}
					after 1000
	    			}
				if {$s == 60} {
					mess "Warning: $fp_bat file wasn't created - maybe Nastran didn't start up.\n" red
					ic_chdir $here
					return
	    			}
				while {[file exists $fp_bat]} {
		    			#mess "Not Done\n" yellow
					after 1000
				}
				mess "Job Done\n" blue
			}

			# Now wait for the output file.
	    		for {set s 0} {$s < 60} {incr s} {
				if [file exists $f06_file] {
					mess "$f06_file file was created.\n" green
					break
				}
				after 1000
	    		}
	    		if {$s == 60} {
				mess "Warning: f06 file wasn't created - maybe Nastran didn't start up.\n" red
				ic_chdir $here
				return
	    		}

			ic_chdir $here

			set size [file size $f06_file] 
			set last [expr $size - 10000]
			if {$last < 0} { set last 0 }
			set fp [open $f06_file r]
			seek $fp $last
			set last10k [read $fp]
			close $fp
			if {[string first  {* * * END OF JOB * * *} $last10k]!=-1} {
				eval cmd_rm [glob -nocomplain $err_file]
				set grep_exec "grep"
          				if { $tcl_platform(platform) == "windows" } {
					set grep_exec [file join $env(ICEM_ACN) bin grep]
                					set grep_exec "${grep_exec}.exe"
          				}
				regsub -all \\\\ $grep_exec / grep_exec
				if ![file executable $grep_exec] {
          					error "Can't find grep executable $grep_exec"
					return
       				}
                                      if { $tcl_platform(platform) == "windows" } {
					set grep_cmd "\"$grep_exec\" -i \"\ FATAL\" \"$f06_file\" > \"$err_file\""
                                      } else {
                                               set grep_cmd "$grep_exec -i \"\ FATAL\" $f06_file >$err_file"
                                      }
				catch {eval ic_exec $grep_cmd} pid
				set errs 0
	    			if [file exists $err_file] {
					set fp [open $err_file r]
					while {![eof $fp]} {
		    				set line [gets $fp]
		    				if {[string first {FATAL} $line] !=-1} {
							mess "$line\n" red
							incr errs
		    				}
					}
				         close $fp
				}
	    			if $errs {
					mess "\nNastran finished running but there were errors\n" red
					set npost_ok 1
	    			} else {
					mess "\nNastran run completed successfully.\n" blue
	    			}
			} else {
   			 	mess "Nastran finished prematurely\n"	 red
				set npost_ok 1
			}
		}
       	} else {
		mess "NO Nastran executable is set.\n  Please set the NASTRAN_EXEC_PATH env. variable\n" red
                   mess "to point the the Nastran executable\n" red
		return
	}

	if {$nas_post_stat == 1 && $npost_ok==0} {aie_nastran_op2 {batch}}

}

proc update_nastran_setup {} {
    global  env solver_setup solver_type

    set solver_setup 0
    	
}

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

proc aie_famboco_edit {solver} {
    global solution famboco_info famboco_data
    global fam_visible file_name solution
    
    aie_set_solver $solver
    
    set sol $solution(solver)
    
    foreach f [ic_boco_list_unused] { set unused($f) 1 }
    
    catch {unset famboco_data}
    foreach f [array names fam_visible] {
	if [info exists unused($f)] continue
	set famboco_data($f) [ic_boco_get $f]
    }
    
    if ![famboco_edit $sol boco_edit_sol_show_fam 1] return
    
    foreach f [array names famboco_data] {
	ic_boco_set $f $famboco_data($f)
    }
}

proc aie_params_edit {solver} {
    global solution famboco_info famboco_data
    global fam_visible file_name solution param_edit
    
    if {$file_name(solver_params) == ""} {
	set file_name(solver_params) [file rootname $file_name(settings)].par
	if {$file_name(solver_params) == ""} {
	    user_error "No project is loaded"
	    return
	}
    }
    set param_edit(save_file) $file_name(solver_params)
    
    aie_set_solver $solver
    famboco_edit_params "" 2
}

proc aie_set_solver {ch} {
    global solution solver_info env famboco_edit
    
    get_solver_info
    
    if ![info exists solver_info($ch)] {
	error "Unknown solver $ch"
    }
    
    set si $solver_info($ch)
    set sol [lindex $si 1]
    set tdir [lindex $si 4]
    set proc ${sol}_write_input
    
    if ![file exists $tdir/$sol.tcl] {
	if [file exists ./$sol.tcl] {
	    set tdir .
	} else {
	    error "GUI file for $sol doesn't exist in either . or $tdir"
	}
    }
    uplevel #0 source [list $tdir/$sol.tcl]
    
    if [file exists $tdir/$sol.bcinfo] {
	famboco_load_bcinfo $tdir/$sol.bcinfo
    } elseif [file exists ./$sol.bcinfo] {
	famboco_load_bcinfo ./$sol.bcinfo
    } else {
	famboco_load_bcinfo ""
    }
    
    set solution(solver) $ch    
    set famboco_edit(solver) $ch
    ic_boco_solver $ch
}

proc aie_check_shell_thicknesses {} {
    global thickness_defined

    #set thickness_defined 0
    #set thickness_defined [ic_uns_thickness_exists_mesh]
    if [catch {set thickness_defined [ic_uns_thickness_exists_mesh]}] {
       set thickness_defined 0
    } else {      
       foreach part [ic_boco_list_families] {
	 set val ""
  	 foreach {n v} [aie_parse_bcs_element_prop_2d [ic_boco_get $part ISHE]] {
 	    if {$n == "m_thick"} { set val $v; break }
	 }
	 if {$val == 0} {set thickness_defined 1}
       }
    }
}

proc onSetupSolverParamDEZLoad {} {
    global  env solver_setup solver_type solver_setup_global
    global current_params_to_load
    global params_data params_type
    global material_tbl_data material_tbl_type
    global modify_param
    global lcs_data

    if {$solver_setup != "-1"} {
       set solve_tmp $solver_setup
    } else {
       set solve_tmp $solver_setup_global
    }

    if {![info exists env(AI_ENV_ABAQUS)] && $solve_tmp > 3} {set solve_tmp 3}
    set solve_format_param $solve_tmp

    if [info exists current_params_to_load(type)] {
        set solver_param $current_params_to_load(type)
    } else {
        set solver_param 0
    }

    set new_param 1

    foreach {n v} {
         name ""
         sid 1
	minFreq  ""
	maxFreq  ""
	nmodes   20
         method_type 0
         method SINV
         l1 0.0
         l2 10.0
         nep 10
         ndp 30
         ndn 30
	norm_type 0
         norm MAX
         gridpnt ""
         compnum ""
         ninc 10
         dt 0.0
         kmethod "AUTO"
         kstep 5
         maxiter 25
         conv "PW"
         intout "NO"
         epsu 1.0e-2
         epsp 1.0e-2
         epsw 1.0e-2
         maxdiv 3
         maxqn 25
         maxls 4
         fstress 0.2
         lstol 0.5
         maxbis 5
         maxr 20.0
         rtolb 20.0
         steps_num 1
         dynamic_type 0
         freq_type 0
         f1 ""
         df ""
         ndf ""
         gridpnt1 ""
         compnum1 ""
         at1 0.0
         gridpnt2 ""
         compnum2 ""
         at2 0.0
         dynamic_excite_type 0
         darea_id ""
         delay_id ""
         dphase_id ""
         excite_type ""
         table1_id ""
         t1 ""
         t2 ""
         freq ""
         phase ""
         exp_coef ""
         g_coef ""
         table2_id ""
         s_factor ""
         eigr_type 0
         include_type 2
         include_file ""
         temp ""
         cid 0
         x 0.0
         y 0.0
         z 0.0
         scale 1.0
         mb 0
    } {
	if [info exists current_params_to_load($n)] {
             set new_param 0
             switch -- $n { 
                 darea_id { set $n [aie_params1_name_from_number $current_params_to_load($n) 5 0]}
                 delay_id { set $n [aie_params1_name_from_number $current_params_to_load($n) 5 1]}
                 dphase_id { set $n [aie_params1_name_from_number $current_params_to_load($n) 5 2]}
                 table1_id { set $n [aie_material_tbl_name_from_number $current_params_to_load($n)]}
                 table2_id { set $n [aie_material_tbl_name_from_number $current_params_to_load($n)]}
                 default {set $n $current_params_to_load($n) }
             }	    
	} else {
	    set $n $v
	}
    }

    set methodList {SINV INV}
    set normList {MAX POINT}
    set kmethodList {AUTO ITER SEMI}
    set convList {U P W UP UW PW UPW}
    set intoutList {YES NO ALL}
    set method1List {LAN AHOU INV SINV GIV NGIV HOU MHOU AGIV}

    set LCSList [lsort [array names lcs_data]]
    set LCS [aie_get_lcs_name $cid]

    foreach {val n} {DAREA 0 DELAY 1 DPHASE 2} {
        set val_temp {}
        foreach name1 [array names params_data] {
	  if {($params_type($name1) == 5) && ([lindex $params_data($name1) 1] == $n)} {set val_temp [concat $val_temp $name1]}
        }
        set ${val}List [lsort $val_temp]
    }

    set TABLEDList {}
    foreach name1 [array names material_tbl_data] {
	if {$material_tbl_type($name1) == 3} {set TABLEDList [concat $TABLEDList $name1]}
    }
    set TABLEDList [lsort $TABLEDList]

    if {$new_param} {
	foreach i [array names params_data] {
	    set used($i) 1
	    set used([lindex $params_data($i) 0]) 1
	}
	for {set i 1} {[info exists used(EIGRL$i)] || [info exists used(EIGB$i)] || [info exists used(LOAD$i)] || [info exists used(NLPARM$i)]\
              || [info exists used(TSTEP$i)] || [info exists used(DAREA$i)] || [info exists used(DELAY$i)] || [info exists used(DPHASE$i)]\
              || [info exists used(FREQ_$i)] || [info exists used(FREQ1_$i)] || [info exists used(FREQ2_$i)]\
              || [info exists used(TLOAD1_$i)] || [info exists used(TLOAD2_$i)] || [info exists used(RLOAD1_$i)] || [info exists used(RLOAD2_$i)]\
              || [info exists used(DLOAD$i)] || [info exists used(EIGR$i)] || [info exists used(SPCADD$i)] || [info exists used(MPCADD$i)]\
              || [info exists used(TEMPD$i)] || [info exists used(Gravity_Load$i)] || [info exists used(INCLUDE$i)] || [info exists used($i)]} {incr i} { }
	set sid $i
    }

    set modify_param 0
    foreach {n} {EIGRL NLPARM TSTEP FREQ FREQ1 FREQ2 DYNAMICType1 DYNAMICType2 DYNAMICType3
                         DYNAMICEXCITEType1 DYNAMICEXCITEType2 DYNAMICEXCITEType3 DYNAMICEXCITEType4
                         DLOAD LOAD EIGR SPCADD MPCADD TEMPD Gravity_Load INCLUDE} {
        ::AIEnv::GUIManagement::ToggleGrey ${n}Name [info exists current_params_to_load(name)]
        if {[info exists current_params_to_load(name)] == 1} {set modify_param 1}
    }

#----------- Begin ANSYS Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_ANSYS)] {
    	   foreach {n} {Gravity_Load} {
	      ::AIEnv::GUIManagement::ToggleGrey ${n}ANSYSName [info exists current_params_to_load(name)]
	   }
	   params_ansys_load
	}
#----------- End ANSYS Stuff -----------------------------------------------------------------------

#----------- Begin LS-Dyna Stuff -----------------------------------------------------------------------
	if [info exists env(AI_ENV_LSDYNA)] {
    	   foreach {n} {Gravity_Load} {
	      ::AIEnv::GUIManagement::ToggleGrey ${n}LSDYNAName [info exists current_params_to_load(name)]
	   }
	   params_lsdyna_load
	}
#----------- End LS-Dyna Stuff -----------------------------------------------------------------------

}

proc onSetupSolverParamDEZApply {} {
   global  env solver_setup solver_type
   global current_layers_to_load
   global params_data params_type
   global material_tbl_data material_tbl_type
   global param_name_list
   global modify_param
   global lcs_data

    if {[info exists modify_param] && $modify_param != 1} {
       if {$name != ""} {
          set name2 [ic_geo_valid_name $name 1]
          if {$name2 != $name} {
             mess "Name \"$name\" is not valid - changed to \"$name2\"\n" red
             set name $name2
          }
          if {[info exists param_name_list] && [lsearch $param_name_list $name] >= 0} {
             user_error "The Name entered is already used.\nPlease enter another Name."
             return
          }
       }
    }

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

  if {$solve_format_param == "0"} {
     
    set solver_setup 0

    if {$darea_id != ""} { set darea_id [lindex $params_data($darea_id) 0]}
    if {$delay_id != ""} { set delay_id [lindex $params_data($delay_id) 0]}
    if {$dphase_id != ""} { set dphase_id [lindex $params_data($dphase_id) 0]}
    if {$table1_id != ""} { set table1_id [lindex $material_tbl_data($table1_id) 0]}
    if {$table2_id != ""} { set table2_id [lindex $material_tbl_data($table2_id) 0]}

    if {$scale == ""} {set scale 1.0}
    if {$LCS != ""} {
	set cid [lindex $lcs_data($LCS) 0]
    } else {
	set cid 0
    }

    if {$solver_param == 0} {
        if {$eigr_type == 0} {
           if {$name == ""} {
              set paramname EIGRL$sid
           } else {
              set paramname $name
           }
           aie_set_params_type $paramname [list $solver_param]
           aie_set_params_data $paramname [list $sid $eigr_type $minFreq $maxFreq $nmodes]
       } else {
          if {$name == ""} {
             set paramname EIGR$sid
          } else {
             set paramname $name
          }
          if {$norm_type == 0} { set norm "MAX"}
          if {$norm_type == 1} { set norm "POINT"}
          aie_set_params_type $paramname [list $solver_param]
          aie_set_params_data $paramname [list $sid $eigr_type $method $l1 $l2 $nep $ndp $norm $gridpnt $compnum]
       }
    }

    if {$solver_param == 1} {
        if {$name == ""} {
           set paramname EIGB$sid
        } else {
           set paramname $name
        }
        if {$method_type == 0} { set method "SINV"}
        if {$method_type == 1} { set method "INV"}
        if {$norm_type == 0} { set norm "MAX"}
        if {$norm_type == 1} { set norm "POINT"}
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $method $l1 $l2 $nep $ndp $ndn $norm $gridpnt $compnum]
    }

    if {$solver_param == 2} {
        if {$name == ""} {
           set paramname NLPARM$sid
        } else {
           set paramname $name
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $ninc $dt $kmethod $kstep $maxiter $conv $intout $epsu $epsp \
                     $epsw $maxdiv $maxqn $maxls $fstress $lstol $maxbis $maxr $rtolb]
    }

    if {$solver_param == 3} {
        if {$name == ""} {
           set paramname TSTEP$sid
        } else {
           set paramname $name
        }
        set name1 steps$sid
        if ![info exists current_layers_to_load($name1)] {
            mess "Error no steps were defined!\n" red
            setupSolverParamDEZ setdirty
            return
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $steps_num $current_layers_to_load($name1)]
    }

    if {$solver_param == 4} {
        if {$freq_type == 0} {
           if {$name == ""} {
              set paramname FREQ_$sid
           } else {
              set paramname $name
           }
           set name1 freqs$sid
           if ![info exists current_layers_to_load($name1)] {
               mess "Error no frequencies were defined!\n" red
               setupSolverParamDEZ setdirty
               return
           }
           aie_set_params_type $paramname [list $solver_param]
           aie_set_params_data $paramname [list $sid $freq_type $steps_num $current_layers_to_load($name1)]
       } else {
           if {$name == ""} {
              if {$freq_type == 1} {set paramname FREQ1_$sid}
              if {$freq_type == 2} {set paramname FREQ2_$sid}
           } else {
              set paramname $name
           }
           aie_set_params_type $paramname [list $solver_param]
           aie_set_params_data $paramname [list $sid $freq_type $f1 $df $ndf]
       }
    }

    if {$solver_param == 5} {
        if {$name == ""} {
           if {$dynamic_type == 0} {set paramname DAREA$sid}
           if {$dynamic_type == 1} {set paramname DELAY$sid}
           if {$dynamic_type == 2} {set paramname DPHASE$sid}
        } else {
           set paramname $name
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $dynamic_type $gridpnt1 $compnum1 $at1 $gridpnt2 $compnum2 $at2]
    }

    if {$solver_param == 6} {
        if {$dynamic_excite_type == 0} {
           if {$name == ""} {
               set paramname TLOAD1_$sid
           } else {
              set paramname $name
           }
            aie_set_params_type $paramname [list $solver_param]
            aie_set_params_data $paramname [list $sid $dynamic_excite_type $darea_id $delay_id $excite_type $table1_id]
       }
        if {$dynamic_excite_type == 1} {
           if {$name == ""} {
               set paramname TLOAD2_$sid
           } else {
              set paramname $name
           }
            aie_set_params_type $paramname [list $solver_param]
            aie_set_params_data $paramname [list $sid $dynamic_excite_type $darea_id $delay_id $excite_type $t1 $t2 \
                                                                          $freq $phase $exp_coef $g_coef]
       }
        if {($dynamic_excite_type == 2) || ($dynamic_excite_type == 3)} {
           if {$name == ""} {
               if {$dynamic_excite_type == 2} {set paramname RLOAD1_$sid}
               if {$dynamic_excite_type == 3} {set paramname RLOAD2_$sid}
           } else {
              set paramname $name
           }
            aie_set_params_type $paramname [list $solver_param]
            aie_set_params_data $paramname [list $sid $dynamic_excite_type $darea_id $delay_id $dphase_id $table1_id  \
                                                                          $table2_id $excite_type]
       }
    }

    if {$solver_param == 7} {
        if {$name == ""} {
           set paramname DLOAD$sid
        } else {
           set paramname $name
        }
        set name1 loads$sid
        if ![info exists current_layers_to_load($name1)] {
            mess "Error no loads were defined!\n" red
            setupSolverParamDEZ setdirty
            return
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $s_factor $steps_num $current_layers_to_load($name1)]
    }

    if {$solver_param == 8} {
        if {$name == ""} {
           set paramname LOAD$sid
        } else {
           set paramname $name
        }
        set name1 loads$sid
        if ![info exists current_layers_to_load($name1)] {
            mess "Error no loads were defined!\n" red
            setupSolverParamDEZ setdirty
            return
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $s_factor $steps_num $current_layers_to_load($name1)]
    }

    if {$solver_param == 9} {
        if {$name == ""} {
           set paramname SPCADD$sid
        } else {
           set paramname $name
        }
        set name1 spcs$sid
        if ![info exists current_layers_to_load($name1)] {
            mess "Error no SPCs were defined!\n" red
            setupSolverParamDEZ setdirty
            return
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $steps_num $current_layers_to_load($name1)]
    }

    if {$solver_param == 10} {
        if {$name == ""} {
           set paramname MPCADD$sid
        } else {
           set paramname $name
        }
        set name1 mpcs$sid
        if ![info exists current_layers_to_load($name1)] {
            mess "Error no MPCs were defined!\n" red
            setupSolverParamDEZ setdirty
            return
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $steps_num $current_layers_to_load($name1)]
    }

    if {$solver_param == 11} {
        if {$name == ""} {
           set paramname TEMPD$sid
        } else {
           set paramname $name
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $temp]
    }

    if {$solver_param == 12} {
        if {$name == ""} {
           set paramname GRAV$sid
        } else {
           set paramname $name
        }
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $cid $scale $x $y $z $mb 1]
    }

    if {$solver_param == 13} {
        if {$name == ""} {
           set paramname INCLUDE$sid
        } else {
           set paramname $name
        }
        set include_type 2
        aie_set_params_type $paramname [list $solver_param]
        aie_set_params_data $paramname [list $sid $include_type $include_file]
    }

    if {[info exists param_name_list] && [lsearch $param_name_list $paramname] < 0} {
       lappend param_name_list $paramname
    }
  }

  if {$solve_format_param == "1"} {     
    set solver_setup 1
    if {$solver_ansys_param == "1"} {
        set solver_param 12
        if {[aie_param_exists $solver_param $name]} {
           user_error "Gravity Parameter already exists!\nPlease use the modify option to update the parameter entries."
           return
        } else {
           if {$name == ""} {
              set paramname GRAV$sid
           } else {
              set paramname $name
           }
           aie_set_params_type $paramname [list $solver_param]
           aie_set_params_data $paramname [list $sid $cid $scale $x $y $z $mb 1]
        }
    } elseif $solver_ansys_param {
	params_ansys_apply
    }
  }

  if {$solve_format_param == "2"} {     
    set solver_setup 2
    if {$solver_lsdyna_param == "1"} {
        set solver_param 12
        if {[aie_param_exists $solver_param $name]} {
           user_error "Gravity Parameter already exists!\nPlease use the modify option to update the parameter entries."
           return
        } else {
           if {$name == ""} {
              set paramname GRAV$sid
           } else {
              set paramname $name
           }
           aie_set_params_type $paramname [list $solver_param]
           aie_set_params_data $paramname [list $sid $cid $scale $x $y $z $mb 1]
        }
    } elseif {$solver_lsdyna_param == "2"} {
	params_lsdyna_apply
    }
  }

  if [info exists env(AI_ENV_ABAQUS)] {
     if {$solve_format_param == "3"} {     
       set solver_setup 3
     }

     if {$solve_format_param == "4"} {     
       set solver_setup 4
     }
  } else {
     if {$solve_format_param == "3"} {     
       set solver_setup 4
     }
  }

}

proc onSetupSolverParamDEZCancel {} {
    global lsdyna_params_data
	catch {unset lsdyna_params_data(iid)}
}

proc aie_set_params_type {name vals} {
    global params_data params_type
    set params_type($name) $vals
}

proc aie_set_params_type1 {name type} {
    global params_data params_type

    if {$type == "EIGRL"|| $type == "EIGR"} {set params_format 0}
    if {$type == "EIGB"} {set params_format 1}
    if {$type == "NLPARM"} {set params_format 2}
    if {$type == "TSTEP"} {set params_format 3}
    if {$type == "FREQ" || $type == "FREQ1" || $type == "FREQ2"} {set params_format 4}
    if {$type == "DAREA" || $type == "DELAY" || $type == "DPHASE"} {set params_format 5}
    if {$type == "TLOAD1" || $type == "TLOAD2" || $type == "RLOAD1" || $type == "RLOAD2"} {set params_format 6}
    if {$type == "DLOAD"} {set params_format 7}
    if {$type == "LOAD"} {set params_format 8}
    if {$type == "SPCADD"} {set params_format 9}
    if {$type == "MPCADD"} {set params_format 10}
    if {$type == "TEMPD"} {set params_format 11}
    if {$type == "Gravity_Load"} {set params_format 12}
    if {$type == "INCLUDE"} {set params_format 13}
    set params_type($name) $params_format
}

proc aie_set_params_data {name vals} {
    global params_data params_type
    set params_data($name) $vals
    ::AIEnv::GUIManagement::Tree::PutParams [list $name] 0
    ic_param_set_modified
}

proc aie_edit_params {name} {
    global current_params_to_load
    global params_data params_type
    global current_layers_to_load
    
    set current_params_to_load(name) $name

    set params_format [lindex $params_type($name) 0]

   set current_params_to_load(type) $params_format

    if {$params_format == 0} {
       if {[lindex $params_data($name) 1] == 0} {
          foreach {p n} {
	    sid 0 eigr_type 1 minFreq 2 maxFreq 3 nmodes 4
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
          }
       } else {
          foreach {p n} {
	    sid 0 eigr_type 1 method 2 l1 3 l2 4 nep 5 ndp 6 norm 7 gridpnt 8 compnum 9
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
              if {$p == "norm"} {
                  if {[lindex $params_data($name) $n] == "MAX"} { set current_params_to_load(norm_type) 0}
                  if {[lindex $params_data($name) $n] == "POINT"} { set current_params_to_load(norm_type) 1}
              }
         }
       }
    }

    if {$params_format == 1} {
       foreach {p n} {
	    sid 0 method 1 l1 2 l2 3 nep 4 ndp 5 ndn 6 norm 7 gridpnt 8 compnum 9
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
              if {$p == "method"} {
                  if {[lindex $params_data($name) $n] == "SINV"} { set current_params_to_load(method_type) 0}
                  if {[lindex $params_data($name) $n] == "INV"} { set current_params_to_load(method_type) 1}
              }
              if {$p == "norm"} {
                  if {[lindex $params_data($name) $n] == "MAX"} { set current_params_to_load(norm_type) 0}
                  if {[lindex $params_data($name) $n] == "POINT"} { set current_params_to_load(norm_type) 1}
              }
      }
    }

    if {$params_format == 2} {
       foreach {p n} {
	    sid 0 ninc 1 dt 2 kmethod 3 kstep 4 maxiter 5 conv 6 intout 7 epsu 8 epsp 9 epsw 10 maxdiv 11
             maxqn 12 maxls 13 fstress 14 lstol 15 maxbis 16 maxr 17 rtolb 18
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
       }
    }

    if {$params_format == 3} {
       foreach {p n} {
	    sid 0 steps_num 1 steps 2
       } {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
              if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 steps$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
       }
    }

    if {$params_format == 4} {
       if {[lindex $params_data($name) 1] == 0} {
          foreach {p n} {sid 0 freq_type 1 steps_num 2 steps 3} {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
              if {$p == "freq_type"} {
                     set current_params_to_load($p) [lindex $params_data($name) $n]
               }
              if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 freqs$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
           }
       } else {
          foreach {p n} {
	    sid 0 freq_type 1 f1 2 df 3 ndf 4
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
          }
       }
    }

    if {$params_format == 5} {
       foreach {p n} {
	    sid 0 dynamic_type 1 gridpnt1 2 compnum1 3 at1 4 gridpnt2 5 compnum2 6 at2 7
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
       }
    }

    if {$params_format == 6} {
       if {[lindex $params_data($name) 1] == 0} {
          foreach {p n} {
	    sid 0 dynamic_excite_type 1 darea_id 2 delay_id 3 excite_type 4 table1_id 5
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
          }
       }
       if {[lindex $params_data($name) 1] == 1} {
          foreach {p n} {
	    sid 0 dynamic_excite_type 1 darea_id 2 delay_id 3 excite_type 4 t1 5 t2 6 freq 7 phase 8 exp_coef 9 g_coef 10
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
          }
       }
       if {[lindex $params_data($name) 1] == 2 || [lindex $params_data($name) 1] == 3} {
          foreach {p n} {
	    sid 0 dynamic_excite_type 1 darea_id 2 delay_id 3 dphase_id 4 table1_id  5 table2_id 6 excite_type 7
          } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
          }
       }
    }

    if {$params_format == 7} {
       foreach {p n} {
	    sid 0 s_factor 1 steps_num 2 steps 3
       } {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
               if {$p == "s_factor"} {
                     set current_params_to_load($p) [lindex $params_data($name) $n]
               }
             if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 loads$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
       }
    }

    if {$params_format == 8} {
       foreach {p n} {
	    sid 0 s_factor 1 steps_num 2 steps 3
       } {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
               if {$p == "s_factor"} {
                     set current_params_to_load($p) [lindex $params_data($name) $n]
               }
             if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 loads$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
       }
    }

    if {$params_format == 9} {
       foreach {p n} {
	    sid 0 steps_num 1 steps 2
       } {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
             if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 spcs$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
       }
    }

    if {$params_format == 10} {
       foreach {p n} {
	    sid 0 steps_num 1 steps 2
       } {
              if {$p == "sid"} {
                     set lid [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lid
               }
             if {$p == "steps_num"} {
                     set lstep [lindex $params_data($name) $n]
                     set current_params_to_load($p) $lstep
              }
              if {$p == "steps"} {
                 set name1 mpcs$lid
                 set current_layers_to_load($name1) [lindex $params_data($name) $n]
              } 
       }
    }

    if {$params_format == 11} {
       foreach {p n} {
	    sid 0 temp 1
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
       }
    }

    if {$params_format == 12} {
       foreach {p n} {
	    sid 0 cid 1 scale 2 x 3 y 4 z 5 mb 6
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
       }
    }

    if {$params_format == 13} {
       foreach {p n} {
	    sid 0 include_type 1 include_file 2
       } {
	    set current_params_to_load($p) [lindex $params_data($name) $n]
      }
    }

    setupSolverParamDEZ show
    
    unset current_params_to_load

}

proc aie_delete_params {name} {
    global params_data params_type
    global param_name_list
    
    catch {unset params_data($name)}
    catch {unset params_type($name)}
    set pos [lsearch $param_name_list $name]
    if { $pos >= 0} {
       set param_name_list [lreplace $param_name_list $pos $pos]
    }
    ::AIEnv::GUIManagement::Tree::DeleteParams [list $name]
    ic_param_set_modified
}

proc aie_rename_params {name} {
    global params_data params_type
    global param_name_list
    
    set newname [question_box .newname "New name" "New name for paramater $name" $name]
    if {$newname == ""} return

    #if ![aie_valid_name $newname 1] {return}
    #set newname [string toupper $newname]
    set newname [aie_get_valid_name $newname]

    if {[info exists param_name_list] && [lsearch $param_name_list $newname] >= 0} {
          user_error "The Name entered is already used.\nPlease enter another Name."
          return
    }
   
    set params_data($newname) $params_data($name)
    set params_type($newname) $params_type($name)
    unset params_data($name)
    unset params_type($name)
    set pos [lsearch $param_name_list $name]
    if { $pos >= 0} {
       set param_name_list [lreplace $param_name_list $pos $pos]
    }
    if {[info exists param_name_list] && [lsearch $param_name_list $newname] < 0} {
       lappend param_name_list $newname
    }
    ::AIEnv::GUIManagement::Tree::RenameParams $name $newname
    ic_param_set_modified
}

proc aie_tree_load_params {clear} {
    global params_data env
    global params_type
    global param_name_list
    
    if [info exists env(AI_ENV_CFX)] return  ;# for now
    
    if $clear { catch {unset params_data} }

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

    set params {}
   
    foreach type {EIGRL EIGB NLPARM TSTEP FREQ FREQ1 FREQ2 DAREA DELAY DPHASE TLOAD1 TLOAD2 RLOAD1 RLOAD2 DLOAD LOAD EIGR SPCADD MPCADD TEMPD Gravity_Load INCLUDE} {
        set params1 [aie_tree_load_one_param $type]
        if {[llength $params1] != 0} {
           set params [concat $params $params1]
           if {[info exists param_name_list] && [lsearch $param_name_list $params1] < 0} {
              lappend param_name_list $params1
           }
       }
    }

    if {[llength $params] != 0} {::AIEnv::GUIManagement::Tree::PutParams $params $clear}
        
}

proc aie_tree_load_one_param {type} {
    global params_data env
    global params_type

    set params {}

    foreach param [ic_param_list_children :$type] {
	set name [lindex [split $param /] 1]
          if {$name != ""} {
	    set p [aie_params_from_param $type $name]
	    if {$p == ""} continue
 	    set params_data($name) $p
	    aie_set_params_type1 $name $type
	    lappend params [concat $name $params_data($name)]
         } else {
             array set val [ic_param_get_values :$type]
             set ret ""
             set name1 ""
             foreach n [aie_params_order $type] {
	        if [info exists val($n)] {
                    if {$n == "SID" || $n == "ID"} {
                       set sid $val($n)
                       set name1 $type$sid
                    }
	           lappend ret $val($n)
	       } else {
	          lappend ret ""
	       }
            }
            if {$ret == ""} continue
            set params_data($name1) $ret
	  aie_set_params_type1 $name $type
	  lappend params [concat $name1 $params_data($name1)]
            break
        }
    }

    return $params

}

proc aie_params_order {type} {

    if  {$type == "EIGRL"} {
       return {
	SID EIGR_type Min_Vibration_Frequency Max_Vibration_Frequency Desired_Number_of_Roots
      }
    }

   if  {$type == "EIGR"} {
       return {
          SID EIGR_type Method Min_Frequency Max_Frequency Estimated_Number_of_Roots
          Desired_Number_of_Roots Normalization_Method
          Normalization_Point DOF_Number
      }
    }

   if  {$type == "EIGB"} {
       return {
          SID Method From_Eigenvalue To_Eigenvalue Estimate_Number_of_Positive_Roots
          Desired_Number_of_Positive_Roots Desired_Number_of_Negative_Roots Normalization_Method
          Normalization_Point DOF_Number
      }
    }

   if  {$type == "NLPARM"} {
       return {
	ID Number_of_Increments Incremental_Time_Intervals kMethod Number_Iterations Maximum_Iterations
         CONV INTOUT Error_Tolerence_for_Displacement Error_Tolerence_for_Load Error_Tolerence_for_Work
         Maximum_Divergence Maximum_Number_of_Quasi-Newton Maximum_Number_of_Line_Search
         Fraction_of_Effective_Stress Line_Search_Tolereance Maximum_Number_of_Bisections Maximum_Ratio
         Maximum_Value_of_Incremental_Rotation
      }
    }

   if  {$type == "TSTEP"} {
       return {
          SID Number_of_Steps Steps_Data
      }
    }

   if  {$type == "FREQ"} {
       return {
          SID Frequency_type Number_of_Frequencies Frequencies_Data
      }
    }

   if  {$type == "FREQ1"} {
       return {
          SID Frequency_type First_Frequency Frequency_Increment Number_Frequency_Increment
      }
    }

   if  {$type == "FREQ2"} {
       return {
          SID Frequency_type First_Frequency Last_Frequency Number_Logarithmic_Interval
      }
    }

   if  {$type == "DAREA"} {
       return {
          SID Dynamic_Type Point_1 DOF_1 Scale_1 Point_2 DOF_2 Scale_2
      }
    }

   if  {$type == "DELAY"} {
       return {
          SID Dynamic_Type Point_1 DOF_1 Delay_1 Point_2 DOF_2 Delay_2
      }
    }

   if  {$type == "DPHASE"} {
       return {
          SID Dynamic_Type Point_1 DOF_1 Phase_1 Point_2 DOF_2 Phase_2
      }
    }

   if  {$type == "TLOAD1"} {
       return {
          SID Dynamic_Excitation_Type EXCITATION_ID DELAY_ID TYPE TABLE_ID
      }
    }

   if  {$type == "TLOAD2"} {
       return {
          SID Dynamic_Excitation_Type EXCITATION_ID DELAY_ID TYPE Time_T1 Time_T2 Frequency 
          Phase Exponential_Coef Growth_Coef
      }
    }

 if  {$type == "RLOAD1"} {
       return {
          SID Dynamic_Excitation_Type EXCITATION_ID DELAY_ID DPHASE_ID TABLE_TC_ID TABLE_TD_ID TYPE
      }
    }

 if  {$type == "RLOAD2"} {
       return {
          SID Dynamic_Excitation_Type EXCITATION_ID DELAY_ID DPHASE_ID TABLE_TB_ID TABLE_TP_ID TYPE
      }
    }

   if  {$type == "DLOAD"} {
       return {
          SID Global_Scale_Factor Number_of_Loads Loads_Data
      }
    }

   if  {$type == "LOAD"} {
       return {
          SID Global_Scale_Factor Number_of_Loads Loads_Data
      }
    }

   if  {$type == "SPCADD"} {
       return {
          SID Number_of_SPCs SPCs_Data
      }
    }

   if  {$type == "MPCADD"} {
       return {
          SID Number_of_MPCs MPCs_Data
      }
    }

   if  {$type == "TEMPD"} {
       return {
          SID Default_Temperature
      }
    }

   if  {$type == "Gravity_Load"} {
       return {
          SID CID Acceleration_Vector_Scale Acceleration_Vector_Component_1 Acceleration_Vector_Component_2 Acceleration_Vector_Component_3 Bulk_Data_Section Create
      }
    }

   if  {$type == "INCLUDE"} {
       return {
          SID Include_Type Include_File
      }
    }

}

proc aie_params_from_param {type name} {
    set vals [ic_param_get_values :$type/$name]
    if {$vals == ""} {
	puts "no values for $type $name"
	return ""
    }
    array set val $vals
    set ret ""
    foreach n [aie_params_order $type] {
	if [info exists val($n)] {
	    lappend ret $val($n)
	} else {
	    lappend ret ""
	}
    }
    return $ret
}

proc aie_save_sol_params {} {
    global params_data params_type
    
    foreach m { :EIGRL :EIGR :EIGB :NLPARM :TSTEP :FREQ :FREQ1 :FREQ2 :DAREA :DELAY :DPHASE \
                :TLOAD1 :TLOAD2 :RLOAD1 :RLOAD2 :DLOAD :LOAD :SPCADD :MPCADD :TEMPD :Gravity_Load :INCLUDE } {
        foreach mat [ic_param_list_children $m] {
            ic_param_delete_node $mat
        }
    }

    foreach param [array names params_data] {
	      set values ""
          set param_type [lindex $params_type($param) 0] 
          if {$param_type == 0 && [lindex $params_data($param) 1] == 0} {set type "EIGRL"}
          if {$param_type == 0 && [lindex $params_data($param) 1] == 1} {set type "EIGR"}
          if {$param_type == 1} {set type "EIGB"}
          if {$param_type == 2} {set type "NLPARM"}
          if {$param_type == 3} {set type "TSTEP"}
          if {$param_type == 4 && [lindex $params_data($param) 1] == 0} {set type "FREQ"}
          if {$param_type == 4 && [lindex $params_data($param) 1] == 1} {set type "FREQ1"}
          if {$param_type == 4 && [lindex $params_data($param) 1] == 2} {set type "FREQ2"}
          if {$param_type == 5 && [lindex $params_data($param) 1] == 0} {set type "DAREA"}
          if {$param_type == 5 && [lindex $params_data($param) 1] == 1} {set type "DELAY"}
          if {$param_type == 5 && [lindex $params_data($param) 1] == 2} {set type "DPHASE"}
          if {$param_type == 6 && [lindex $params_data($param) 1] == 0} {set type "TLOAD1"}
          if {$param_type == 6 && [lindex $params_data($param) 1] == 1} {set type "TLOAD2"}
          if {$param_type == 6 && [lindex $params_data($param) 1] == 2} {set type "RLOAD1"}
          if {$param_type == 6 && [lindex $params_data($param) 1] == 3} {set type "RLOAD2"}
          if {$param_type == 7} {set type "DLOAD"}
          if {$param_type == 8} {set type "LOAD"}
          if {$param_type == 9} {set type "SPCADD"}
          if {$param_type == 10} {set type "MPCADD"}
          if {$param_type == 11} {set type "TEMPD"}
          if {$param_type == 12} {set type "Gravity_Load"}
          if {$param_type == 13} {set type "INCLUDE"}
	      foreach n [aie_params_order $type] v $params_data($param) {
	         lappend values $n $v
	     }
	     ic_param_add_node :$type $param $values
	     ic_param_set_modified
    }
}

proc aie_params_name_from_number {n type} {
    global params_data params_type
    foreach name [array names params_data] {
	if {$n == [lindex $params_data($name) 0] && $params_type($name) == $type} { return $name }
    }
    return ""
}

proc aie_params1_name_from_number {n type subtype} {
    global params_data params_type
    foreach name [array names params_data] {
	if {$n == [lindex $params_data($name) 0] && $params_type($name) == $type \
             && [lindex $params_data($name) 1] == $subtype} { 
              return $name 
         }
    }
    return ""
}

proc aie_params_set_steps {} {
    global current_layers_to_load
    global layers_data
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
        
    catch {form_kill .tstep_layers}
    
    set buts [list [list OK "aie_tstep_layers_doit $lid $layers"]  cancel]
    set d [form_init .tstep_layers "Set Steps" "" $buts]
    if {$d != ""} {
	form_label $d.layer " Number of time steps" {0 1}
	form_label $d.t "Time increment" {0 2}
	form_label $d.no "Skop Factor" {0 3}
         set name1 steps$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 [expr int($length)/3] 
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 layer$i
             if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr (($i-1)*3)]]
             } else {
                 set layers_data($name2) $i
             }
	    form_entry $d.layer$i layers_data($name2) int "$i 1 -sticky ewn -padx 0"
	    set name2 t$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr (($i-1)*3)+1]]
             } else {
                 set layers_data($name2) 0.0
             }
	    form_entry $d.t$i layers_data($name2) float "$i 2 -sticky ewn -padx 0"
	    set name2 no$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr (($i-1)*3)+2]]
             } else {
                 set layers_data($name2) 1
             }
	    form_entry $d.no$i layers_data($name2) int "$i 3 -sticky ewn -padx 0"
	}

	form_finish  middle 1 1
    }
    
}

proc aie_tstep_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data

   set name1 steps$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 layer$i
         lappend  layers_list $layers_data($name2)
         set name2 t$i
         lappend  layers_list $layers_data($name2)
         set name2 no$i
         lappend  layers_list $layers_data($name2)
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .tstep_layers

}

proc aie_params_set_freqs {} {
    global current_layers_to_load
    global layers_data
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
        
    catch {form_kill .freq_layers}
    
    set buts [list [list OK "aie_freq_layers_doit $lid $layers"]  cancel]
    set d [form_init .freq_layers "Set Frequency" "" $buts]
    if {$d != ""} {
	form_label $d.layer " Frequencies" {0 1}
         set name1 freqs$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 $length
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 layer$i
             if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr $i-1]]
             } else {
                 set layers_data($name2) ""
             }
	    form_entry $d.layer$i layers_data($name2) float "$i 1 -sticky ewn -padx 0"
         }
         form_finish  middle 1 1
    }

}

proc aie_freq_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data

   set name1 freqs$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 layer$i
         lappend  layers_list $layers_data($name2)
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .freq_layers

}

proc aie_params_set_dload {} {
    global current_layers_to_load
    global layers_data
    global params_data params_type
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
    
    set LOADList {}
    foreach name2 [array names params_data] {
	if {$params_type($name2) == 6} { set LOADList [concat $LOADList $name2]}
    }
    set LOADList [lsort $LOADList]
    
    catch {form_kill .dload_layers}
    
    set buts [list [list OK "aie_dload_layers_doit $lid $layers"]  cancel]
    set d [form_init .dload_layers "Set Loads" "" $buts]
    if {$d != ""} {
	form_label $d.layer "Scale Factor" {0 1}
	form_label $d.mid "Load Set ID" {0 2}
         set name1 loads$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 [expr int($length)/2] 
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 s$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr (($i-1)*2)]]
             } else {
                 set layers_data($name2) ""
             }
	    form_entry $d.s$i layers_data($name2) float "$i 1 -sticky ewn -padx 0"
	    set name2 mid$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set loadid [lindex $current_layers_to_load($name1) [expr (($i-1)*2)+1]]
                 set layers_data($name2) [aie_params_name_from_number $loadid 6]
             } else {
                 set layers_data($name2) ""
             }
	    futil_menuentry $d.mid$i layers_data($name2) [concat list $LOADList] "$i 2 -sticky ewn -padx 0"
        }
        form_finish  middle 1 1
    }

}

proc aie_dload_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data
    global params_data params_type

   set name1 loads$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 s$i
         lappend  layers_list $layers_data($name2)
         set name2 mid$i
         lappend  layers_list [lindex $params_data($layers_data($name2)) 0]
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .dload_layers

}

proc aie_params_set_sload {} {
    global current_layers_to_load
    global layers_data
    global params_data params_type
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
    
    set LOADList [aie_list_sets load]
   
    set LOADList [lsort $LOADList]
    
    catch {form_kill .sload_layers}
    
    set buts [list [list OK "aie_sload_layers_doit $lid $layers"]  cancel]
    set d [form_init .sload_layers "Set Loads" "" $buts]
    if {$d != ""} {
	form_label $d.layer "Scale Factor" {0 1}
	form_label $d.mid "Load Set ID" {0 2}
         set name1 loads$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 [expr int($length)/2] 
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 s$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set layers_data($name2) [lindex $current_layers_to_load($name1) [expr (($i-1)*2)]]
             } else {
                 set layers_data($name2) ""
             }
	    form_entry $d.s$i layers_data($name2) float "$i 1 -sticky ewn -padx 0"
	    set name2 mid$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set loadid [lindex $current_layers_to_load($name1) [expr (($i-1)*2)+1]]
                 set layers_data($name2) $loadid
             } else {
                 set layers_data($name2) ""
             }
	    futil_menuentry $d.mid$i layers_data($name2) [concat list $LOADList] "$i 2 -sticky ewn -padx 0"
        }
        form_finish  middle 1 1
    }

}

proc aie_sload_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data
    global params_data params_type

   set name1 loads$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 s$i
         lappend  layers_list $layers_data($name2)
         set name2 mid$i
         lappend  layers_list $layers_data($name2)
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .sload_layers

}

proc aie_params_set_spcadd {} {
    global current_layers_to_load
    global layers_data
    global params_data params_type
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
    
    set SPCList [aie_list_sets impdisp]
   
    set SPCList [lsort $SPCList]
    
    catch {form_kill .sload_layers}
    
    set buts [list [list OK "aie_spcadd_layers_doit $lid $layers"]  cancel]
    set d [form_init .sload_layers "Set SPCs" "" $buts]
    if {$d != ""} {
	form_label $d.layer "SPC Set ID" {0 1}
         set name1 spcs$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 $length 
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 mid$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set spcid [lindex $current_layers_to_load($name1) [expr ($i-1)]]
                 set layers_data($name2) $spcid
             } else {
                 set layers_data($name2) ""
             }
	    futil_menuentry $d.mid$i layers_data($name2) [concat list $SPCList] "$i 1 -sticky ewn -padx 0"
        }
        form_finish  middle 1 1
    }

}

proc aie_spcadd_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data
    global params_data params_type

   set name1 spcs$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 mid$i
         lappend  layers_list $layers_data($name2)
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .sload_layers

}

proc aie_params_set_mpcadd {} {
    global current_layers_to_load
    global layers_data
    global params_data params_type
    
    set layers [setupSolverParamDEZ get steps_num]
    set lid [setupSolverParamDEZ get sid]
    
    set MPCList [aie_list_sets mpcdisp]
   
    set MPCList [lsort $MPCList]
    
    catch {form_kill .sload_layers}
    
    set buts [list [list OK "aie_mpcadd_layers_doit $lid $layers"]  cancel]
    set d [form_init .sload_layers "Set MPCs" "" $buts]
    if {$d != ""} {
	form_label $d.layer "MPC Set ID" {0 1}
         set name1 mpcs$lid
	if {[info exists current_layers_to_load($name1)]} {
             set length [llength $current_layers_to_load($name1)] 
             set layers1 $length 
         }
	for {set i 1} {$i  <= $layers} {incr i} {
	    set name2 mid$i
	    if {[info exists current_layers_to_load($name1)] && ($i <= $layers1)} {
                 set mpcid [lindex $current_layers_to_load($name1) [expr ($i-1)]]
                 set layers_data($name2) $mpcid
             } else {
                 set layers_data($name2) ""
             }
	    futil_menuentry $d.mid$i layers_data($name2) [concat list $MPCList] "$i 1 -sticky ewn -padx 0"
        }
        form_finish  middle 1 1
    }

}

proc aie_mpcadd_layers_doit {lid layers} {
   global current_layers_to_load
   global layers_data
    global params_data params_type

   set name1 mpcs$lid
   set layers_list ""
   for {set i 1} {$i  <= $layers} {incr i} {
         set name2 mid$i
         lappend  layers_list $layers_data($name2)
   }
   aie_set_Layers_data $name1 $layers_list

   form_kill .sload_layers

}

proc aie_load_include_file {{opt 0}} {

   switch $opt { 
      0 { 
           set include_name [setupSolverParamDEZ get include_file]
           set par_name [setupSolverParamDEZ get name]
          	if [file exists $include_name] {
              set rootn [aie_import_nastran $include_name 0 0 0 0 0 1]
              aie_open_Mesh 0 "" 0 $rootn.uns
              ic_param_load $rootn.par 0
              ic_boco_load_atr $rootn.atr
              aie_afterStuffLoaded
              aie_initial_unsmesh_visibility
              aie_clear_dez
              aie_delete_params $par_name
           } else {
              mess "file $include_name does not exist\n" red
           }
      }
      default {return}
   }	    
   
}

proc aie_param_exists {type name} {
    global params_data params_type

    foreach param [array names params_data] {
        set param_type [lindex $params_type($param) 0] 
        if {($param != $name) && ($param_type == $type)} {return 1}
    }
    
    return 0

}














































































