
# You don't have to worry about pulling the variables
# mentioned in the .ttkrf files into scope with either
# a global or variable statement.  The framework takes
# care of that for you.

# ------------------------------------------------------
# This DEZ is defined in the file loadsDEZ.ttkrf

set ::aie_bctypes_used(load) {FORCE MOMENT PLOAD2 PLOAD4}
#set ::aie_bctypes_used(force) {FORCE MOMENT LSDYNA_FID}
#set ::aie_bctypes_used(temperature) {TEMP LSDYNA_TID}
set ::aie_bctypes_used(force) {FORCE MOMENT}
set ::aie_bctypes_used(temperature) {TEMP}
set ::aie_bctypes_used(pressure) {PLOAD2 PLOAD4}

set ::aie_bcclass_title(force) Force
set ::aie_bcclass_title(temperature) Temperature
set ::aie_bcclass_title(pressure) Pressure
set ::aie_bcclass_title(load) Load

set ::aie_prop_subtype(force) load
#set ::aie_prop_subtype(temperature) load
set ::aie_prop_subtype(pressure) load
#set ::aie_prop_subtype(directional_pressure) pressure


set ::aie_tree_nodes(load) Loads
set ::aie_tree_nodes(temperature) Temperatures

proc aie_edit_force {name} {
    aie_edit_attribute $name force Force
}

proc aie_parse_bcs_force {bcs} {
    set ret ""
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    MOMENT {
		foreach n {loadSet LCS scale MX MY MZ force_type} v [lrange $bc 2 8] {
                       if {$n == "LCS"} {
		       lappend ret $n [aie_get_lcs_name $v]
                      } else {
                          if {$n == "force_type"} {
                             if {$v == ""} {set v 0}
                          }
		       lappend ret $n $v
                       }
		}
	    }
	    FORCE {
		foreach n {loadSet LCS scale FX FY FZ force_type} v [lrange $bc 2 8] {
                       if {$n == "LCS"} {
		       lappend ret $n [aie_get_lcs_name $v]
                      } else {
                          if {$n == "force_type"} {
                             if {$v == ""} {set v 0}
                          }
		       lappend ret $n $v
                       }
		}
	    }
	    LSDYNA_FID {
			lappend ret lsdyna_nsid [lindex $bc 2]
	    }
	}
    }
    return $ret
}

# This function is called prior to the DEZ being displayed
foreach i { Point Curve Surf Subset Existing } {
    proc onCreate${i}ForceDEZLoad {} {
	global current_force_to_load lcs_data
	global  solver_setup solver_setup_global

	select_reset_parts_and_subsets
	
	foreach {n v} {
	    name "" loadSet 1 FX 0.0 FY 0.0 FZ 0.0 MX 0.0 MY 0.0 MZ 0.0 lsdyna_nsid ""
	    LCS "Global" scale 1.0 force_type 0
	} {
	    if [info exists current_force_to_load($n)] {
		set $n $current_force_to_load($n)
  	    } else {
		set $n $v
	    }
	}
	set LCSList [lsort [array names lcs_data]]
	catch {
	    # This is very sloppy
	    create1DElePropBarCYL setvalue @[lsearch $LCSList $LCS]
	    create3DElePropCYL setvalue @[lsearch $LCSList $LCS]
	}
	set loadSetList [aie_list_sets load]
	if {$loadSet == ""} { set loadSet [lindex $loadSetList 0] }
	set AllowActiveReload 1
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current] \
	   {FX FY FZ MX MY MZ} {FX FY FZ MX MY MZ}"
	aie_on_change_LCS [namespace current] {FX FY FZ MX MY MZ} {FX FY FZ MX MY MZ}
#VG>
          if {$force_type == ""} {set force_type 0}

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

         if {$solve_format == "2"} {
            ::AIEnv::GUIManagement::ToggleGrey lsdynaPointFNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveFNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfFNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingFNodeSetId 0
         } else {
            ::AIEnv::GUIManagement::ToggleGrey lsdynaPointFNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveFNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfFNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingFNodeSetId 1
         }
    }
    proc onCreate${i}ForceDEZApply {} "\
	global  solver_setup solver_setup_global;\
	if \{\$solver_setup != \"-1\"\} \{ \
		set solve_format \$solver_setup \
	\} else \{ \
		set solve_format \$solver_setup_global \
	\} ;\
	if \{\$solve_format == \"2\"\} \{ \
		if \{\$lsdyna_nsid == \"\" || \$lsdyna_nsid <= 0\} \{ \
			user_error \"Please enter a valid Node Set ID\" ;\
			set objList \{\} ;\
			return \
		\} \
	\} ;\
        if !\[info exists objList\] \{ set objList \{\} \} ;\
	foreach n \{FX FY FZ MX MY MZ\} \{if \{\[set \$n\] == \"\"\} \{set \$n 0\}\};\
	if \{\$scale == \"\"\} \{set scale 1.0\};\
	foreach {n v} \[applyForce \$FX \$FY \$FZ \$MX \$MY \$MZ [string tolower $i] \$objList \$name \$loadSet\ \$LCS \$scale \$force_type \$lsdyna_nsid] \{ set \$n \$v \} ;\
	set objList \{\} ;\
    "
}

proc onCreateForceDEZLoad {} {
   global current_force_to_load lcs_data
   global  solver_setup solver_setup_global

   select_reset_parts_and_subsets
	
   foreach {n v} {
      name "" loadSet "" FX 0.0 FY 0.0 FZ 0.0 MX 0.0 MY 0.0 MZ 0.0 lsdyna_nsid "" LCS "Global" scale 1.0 force_type 0
   } {
      if [info exists current_force_to_load($n)] {
         set $n $current_force_to_load($n)
      } else {
         set $n $v
      }
   }
   set LCSList [lsort [array names lcs_data]]
   catch {
      # This is very sloppy
      create1DElePropBarCYL setvalue @[lsearch $LCSList $LCS]
      create3DElePropCYL setvalue @[lsearch $LCSList $LCS]
   }
   set loadSetList [aie_list_sets load]
   set nlength [llength $loadSetList]
   if {$loadSet == "" && $nlength > 0} { 
       set loadSet [expr ([lindex [lsort $loadSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set loadSet 1
   }
   set AllowActiveReload 1
#VG<
   trace variable LCS "w" "aie_on_change_LCS [namespace current]\
      {FX FY FZ MX MY MZ} {FX FY FZ MX MY MZ}"
   aie_on_change_LCS [namespace current] {FX FY FZ MX MY MZ} {FX FY FZ MX MY MZ}
#VG>
   if {$force_type == ""} {set force_type 0}

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

   if {$solve_format == "2"} {
      ::AIEnv::GUIManagement::ToggleGrey lsdynacreateForceNodeSetId 0
   } else {
      ::AIEnv::GUIManagement::ToggleGrey lsdynacreateForceNodeSetId 1
   }
}

proc onCreateForceDEZApply {} {
   global  solver_setup solver_setup_global
  global bc_name_list

  set def_name 0

  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 bc_name_list] && [lsearch $bc_name_list $name] >= 0} {
            user_error "The Name entered is already used.\nPlease enter another Name."
            set objList {}
            return
      }
  } else {
     set def_name 1
  }


   switch $createForceVal { 
      0 { set entity "point"}
      1 { set entity "curve"}
      2 { set entity "surf"}
      3 { set entity "subset"}
      4 { set entity "part"}
      default {set entity ""}
   }

   if {$solver_setup != "-1"} {
      set solve_format $solver_setup
   } else { 
      set solve_format $solver_setup_global
   }
   if {$solve_format == "2"} { 
      if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
         user_error "Please enter a valid Node Set ID"
         set objList {}
         return
      } 
   } 
   if ![info exists objList] {set objList {}}
   foreach n {FX FY FZ MX MY MZ} {
      if {[set $n] == ""} {
         set $n 0.0
      }
   }
   if {$scale == ""} {set scale 1.0}
   foreach {n v} [applyForce $FX $FY $FZ $MX $MY $MZ $entity $objList $name $loadSet $LCS $scale $force_type $lsdyna_nsid] { 
      set $n $v 
   }
   set objList {}
   if {$def_name} {set name ""}
}

proc applyForce {FX FY FZ MX MY MZ type objects name loadset LCS scale force_type lsdyna_nsid} {
    foreach j {FX FY FZ MX MY MZ} {
       if ![string is double [set $j]] {
         mess "Entry \"$j\" is not valid - changed to 0\n" red
         set $j 0
       }
    }
    if {$force_type == ""} {set force_type 0}
    set lcsnum [aie_get_lcs_num $LCS]
#    set bcs [list [list 1 FORCE $loadset $lcsnum $scale $FX $FY $FZ $force_type] \
#		 [list 1 MOMENT $loadset $lcsnum $scale $MX $MY $MZ $force_type] \
#		 [list 1 LSDYNA_FID $lsdyna_nsid]]
	set frc [list 1 FORCE $loadset $lcsnum $scale $FX $FY $FZ $force_type]
	set mom [list 1 MOMENT $loadset $lcsnum $scale $MX $MY $MZ $force_type]
	if {$lsdyna_nsid != "" && $lsdyna_nsid > 0} {
		set bcs [list $frc $mom [list 1 LSDYNA_FID $lsdyna_nsid]]
	} else {
		set bcs [list $frc $mom]
	}

    aie_apply_attribute force $bcs FORCE $name $type $objects \
	[select_get_parts_and_subsets]
}

proc aie_apply_icons_force {fam obtype vis lvis} {
    global lcs_data
    
    foreach j {FX FY FZ MX MY MZ} { set $j 0 }
    set LCS "Global"
    set scale 1
    set name ""
    set force_type 0
    set bcs [ic_boco_get $fam]
    if {$bcs == ""} { return 0 }
    set parsed [aie_parse_bcs_force $bcs]
    if {$parsed == ""} { return 0 }
    foreach {n v} $parsed { set $n $v }
    #set lcsnum $LCS
    set lcsnum [aie_get_lcs_num $LCS]

    foreach i {FX FY FZ MX MY MZ} { set $i [expr {[set $i] * $scale}] }
    ic_boco_apply_generic_icon_to_group $fam force \
	[list $FX $FY $FZ $MX $MY $MZ $lcsnum 30] $vis
    return 1
}

proc aie_get_force_set {fam} {
    return [aie_get_bc_argument $fam force loadSet]
}

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

proc aie_edit_pressure {name} {
    aie_edit_attribute $name pressure Pres
}

proc aie_parse_bcs_pressure {bcs} {
    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    PLOAD2 {
		foreach n {loadSet pres} v [lrange $bc 2 3] {
		    lappend ret $n $v
		}
		set got_it 1
	    }
	    PLOAD4 {
		foreach n {loadSet LCS vect1 vect2 vect3 pres pres2 pres3 pres4} v [lrange $bc 2 10] {
                       if {$v == "-999999"} {set v 0}
                       if {$n == "LCS"} {
		       lappend ret $n [aie_get_lcs_name $v]
                      } else {
		       lappend ret $n $v
                      }
		}
		set got_it 1
	    }
	}
    }
    if $got_it {
	return $ret
    } else {
	return ""
    }
}

foreach i { Curve Surf Subset Existing } {
    proc onCreate${i}PresDEZLoad {} {
	global current_pressure_to_load
	global pressure_type
	global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
	global v3p_interp_select_interp_type
	global v3p_interp_data v3p_interp_ansys_select_command
	global v3p_interp_select_format v3p_interp_select_ansys_command
	global last_interp_var_list
	global icon_size1 env
	global beta_mode
	global v3_started did_startup_v3p did_setup_v3p_first
	
	select_reset_parts_and_subsets
	
	variable ::AIEnv::GUIManagement::CATRootDir

	foreach {n v} {
	    name "" loadSet 1 pres 0 pres2 0 pres3 0 pres4 0 LCS "Global" vect1 0 vect2 0 vect3 0 scale_factor 1.0
	} {
	    if [info exists current_pressure_to_load($n)] {
		set $n $current_pressure_to_load($n)
	    } else {
		set $n $v
	    }
	}
	set LCSList [lsort [array names lcs_data]]
         if [info exists pressure_type($name)] {
            set pres_type [lindex $pressure_type($name) 0]
         } else {
             set pres_type 0
         }
	set loadSetList [aie_list_sets load]
	if {$loadSet == ""} { set loadSet [lindex $loadSetList 0] }
	set AllowActiveReload 1

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

	set v3p_interp_select_interp_type {"Surfaces Only" "Volumes Only" "Surfaces and Volumes"}
	if { ![info exists v3p_interp_data(type)] } {
	   set v3p_interp_data(type) "Surfaces and Volumes"
	}

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

    }
    
    proc onCreate${i}PresDEZApply {} "\
        if !\[info exists objList\] \{ set objList \{\} \} ;\
	foreach n \{pres pres2 pres3 pres4\} \{if \{\[set \$n\] == \"\"\} \{set \$n 0\}\};\
	if \{\$scale_factor == \"\"\} \{set scale_factor 1.0\};\
	foreach {n v} \[applyPres \$pres_type \$pres \$pres2 \$pres3 \$pres4 \$LCS \$vect1 \$vect2 \$vect3 \$scale_factor [string tolower $i] \$objList \$name \$loadSet\] \{ set \$n \$v \} ;\
	set objList \{\} \
    "
}

proc onCreatePressureDEZLoad {} {
   global current_pressure_to_load
   global pressure_type
   global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
   global v3p_interp_select_interp_type
   global v3p_interp_data v3p_interp_ansys_select_command
   global v3p_interp_select_format v3p_interp_select_ansys_command
   global last_interp_var_list
   global icon_size1 env
   global beta_mode
   global v3_started did_startup_v3p did_setup_v3p_first
	
   select_reset_parts_and_subsets
	
   variable ::AIEnv::GUIManagement::CATRootDir

   foreach {n v} {
      name "" loadSet "" pres 0 pres2 0 pres3 0 pres4 0 LCS "Global" vect1 0 vect2 0 vect3 0 scale_factor 1.0
   } {
      if [info exists current_pressure_to_load($n)] {
         set $n $current_pressure_to_load($n)
      } else {
         set $n $v
      }
   }
   set LCSList [lsort [array names lcs_data]]
   if [info exists pressure_type($name)] {
      set pres_type [lindex $pressure_type($name) 0]
   } else {
      set pres_type 0
   }
   set loadSetList [aie_list_sets load]
   set nlength [llength $loadSetList]
   if {$loadSet == "" && $nlength > 0} { 
       set loadSet [expr ([lindex [lsort $loadSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set loadSet 1
   }
   set AllowActiveReload 1

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

   set v3p_interp_select_interp_type {"Surfaces Only" "Volumes Only" "Surfaces and Volumes"}
   if { ![info exists v3p_interp_data(type)] } {
      set v3p_interp_data(type) "Surfaces and Volumes"
   }

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

}

proc onCreatePressureDEZApply {} {
  global bc_name_list

  set def_name 0

  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 bc_name_list] && [lsearch $bc_name_list $name] >= 0} {
            user_error "The Name entered is already used.\nPlease enter another Name."
            set objList {}
            return
      }
  } else {
     set def_name 1
  }

   switch $createPressureVal { 
      0 { set entity "surf"}
      1 { set entity "subset"}
      2 { set entity "part"}
      default {set entity ""}
   }

   if ![info exists objList] {set objList {}}
   foreach n {pres pres2 pres3 pres4} {
      if {[set $n] == ""} {
         set $n 0.0
      }
   }
   if {$scale_factor == ""} {set scale_factor 1.0}
   foreach {n v} [applyPres $pres_type $pres $pres2 $pres3 $pres4 $LCS $vect1 $vect2 $vect3 $scale_factor $entity $objList $name $loadSet] {
      set $n $v 
   }
   set objList {} 
   if {$def_name} {set name ""}

}

proc applyPres {pres_type pres pres2 pres3 pres4 LCS vect1 vect2 vect3 scale_factor type objects name loadset} {
    global pressure_type
    global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
    global v3p_interp_data 
    global v3_started did_startup_v3p did_setup_v3p_first

    set pres_ok 1

    set cid [aie_get_lcs_num $LCS]

    set pressure_type($name) $pres_type

    if {$pres_type == 0} {
       set bcs [list [list 1 PLOAD2 $loadset $pres]]
    } 
    if {$pres_type == 1} {
      foreach n {vect1 vect2 vect3 pres pres2 pres3 pres4} {
	if {[set $n] == ""} {set $n 0}
      }
      if {$cid == ""} {
           if {($vect1 != 0 && $vect1 != "") || ($vect2 != 0 && $vect2 != "") || ($vect3 != 0 && $vect3 != "")} {set cid 0}
       }
       if {$cid == ""} {set cid -999999}
       if {($vect1 == 0 || $vect1 == "") && ($vect2 == 0 || $vect2 == "") && ($vect3 == 0 || $vect3 == "")} {set cid -999999}
       set bcs [list [list 1 PLOAD4 $loadset $cid $vect1 $vect2 $vect3 $pres $pres2 $pres3 $pres4]]
    }
    if {$pres_type == 2} {
       set bcs [list [list 1 PLOAD2 $loadset $pres]]
       ic_uns_write_node_list $objects node_list $scale_factor 
       ic_uns_write_elem_list $objects elem_list 
       if { $v3p_interp_data(type) == "Surfaces Only." } {
          set it Shells
       } elseif { $v3p_interp_data(type) == "Volumes Only." } {
         set it Volumes
       } else {
         set it Mixed
       }
       set interp_format "-format ASCII -command FACE -label PLOAD2 -factor $v3p_interp_data(conv_factor) -conn_file elem_list"
       if { ![info exists v3p_interp_data(selected_mats)] || $v3p_interp_data(selected_mats) == "" } {
         set cmd "interpolate_file_to_file node_list pressure_list $v3p_interp_data(selected_vars) \"\" $it $interp_format"
       } else {
         set cmd "interpolate_file_to_file node_list pressure_list $v3p_interp_data(selected_vars) $v3p_interp_data(selected_mats) $it $interp_format"
       }
       if {[info exist did_startup_v3p] && [info exist did_setup_v3p_first]} {
          mess "Starting Interpolation:\n"
          V3_client_cmd $cmd
          mess "Interpolation Done\n"
          set param_name "Nastran:PLOAD2:P"
          set on_nodes 0
          set colnum 1
          ic_uns_import_bcfield $param_name pressure_list $on_nodes $colnum $pres 
      } else {
         set pres_ok 0
	mess "Note: Interpolation can only occur after Post Results are loaded.\n" red
      }
    }
    if {$pres_ok} {
	aie_apply_attribute pressure $bcs PRES $name $type $objects \
	    [select_get_parts_and_subsets]
    }
}


proc aie_apply_icons_pressure {fam obtype vis lvis} {
    set name ""
    set vect1 ""
    set vect2 ""
    set vect3 ""
    set bcs [ic_boco_get $fam]
    if {$bcs == ""} { return 0 }
    set parsed [aie_parse_bcs_pressure $bcs]
    if {$parsed == ""} { return 0 }
    foreach {n v} $parsed {set $n $v}

    if {($vect1 != 0 && $vect1 != "") || ($vect2 != 0 && $vect2 != "") || ($vect3 != 0 && $vect3 != "")} {
            set cid [aie_get_lcs_num $LCS]
            ic_boco_apply_generic_icon_to_group $fam directional_pressure [list $vect1 $vect2 $vect3 $cid 30] $vis
    } else {
            ic_boco_apply_generic_icon_to_group $fam pressure [list $pres] $vis
    }
    return 1
}

proc aie_get_pressure_set {fam} {
    return [aie_get_bc_argument $fam pressure loadSet]
}

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

proc aie_edit_temperature {name} {
    aie_edit_attribute $name temperature Tmp
}

proc aie_parse_bcs_temperature {bcs} {
    set ret ""
    set got_it 0
    foreach bc $bcs {
	switch [lindex $bc 1] {
	    TEMP {
		foreach n {loadSet temp} v [lrange $bc 2 3] {
		    lappend ret $n $v
		}
		set got_it 1
	    }
	    LSDYNA_TID {
			lappend ret lsdyna_nsid [lindex $bc 2]
			set got_it 1
	    }
	}
    }
    if $got_it {
	return $ret
    } else {
	return ""
    }
}

foreach i { Point Curve Surf Body Subset Existing } {
    proc onCreate${i}TmpDEZLoad {} {
	global current_temperature_to_load
	global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
	global v3p_interp_select_interp_type
	global v3p_interp_data v3p_interp_ansys_select_command
	global v3p_interp_select_format v3p_interp_select_ansys_command
	global last_interp_var_list
	global icon_size1 env
	global beta_mode
	global v3_started did_startup_v3p did_setup_v3p_first
         global  solver_setup solver_setup_global
   
	variable ::AIEnv::GUIManagement::CATRootDir
	
	select_reset_parts_and_subsets
	
	foreach {n v} {
	    name "" loadSet 1 temp 0 scale_factor 1.0 lsdyna_nsid ""
	} {
	    if [info exists current_temperature_to_load($n)] {
		set $n $current_temperature_to_load($n)
	    } else {
		set $n $v
	    }
	}
	set loadSetList [aie_list_sets temperature]
	if {$loadSet == ""} { set loadSet [lindex $loadSetList 0] }
	set AllowActiveReload 1

         set temp_type 0

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

         if {$solve_format == "2"} {
            ::AIEnv::GUIManagement::ToggleGrey lsdynaPointTmpNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveTmpNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfTmpNodeSetId 0
            ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingTmpNodeSetId 0
         } else {
            ::AIEnv::GUIManagement::ToggleGrey lsdynaPointTmpNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaCurveTmpNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaSurfTmpNodeSetId 1
            ::AIEnv::GUIManagement::ToggleGrey lsdynaExistingTmpNodeSetId 1
         }

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

	set v3p_interp_select_interp_type {"Surfaces Only" "Volumes Only" "Surfaces and Volumes"}
	if { ![info exists v3p_interp_data(type)] } {
	   set v3p_interp_data(type) "Surfaces and Volumes"
	}

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

    }
    
    proc onCreate${i}TmpDEZApply {} "\
	global  solver_setup solver_setup_global;\
	if \{\$solver_setup != \"-1\"\} \{ \
		set solve_format \$solver_setup \
	\} else \{ \
		set solve_format \$solver_setup_global \
	\} ;\
	if \{\$solve_format == \"2\"\} \{ \
		if \{\$lsdyna_nsid == \"\" || \$lsdyna_nsid <= 0\} \{ \
			user_error \"Please enter a valid Node Set ID\" ;\
			set objList \{\} ;\
			return \
		\} \
	\} ;\
        if !\[info exists objList\] \{ set objList \{\} \} ;\
	if \{\$temp == \"\"\} \{set temp 0\};\
	if \{\$scale_factor == \"\"\} \{set scale_factor 1\};\
	foreach {n v} \[applyTmp \$temp_type \$temp \$scale_factor [string tolower $i] \$objList \$name \$loadSet \$lsdyna_nsid\] \{ set \$n \$v \} ;\
	set objList \{\} \
    "
}

proc onCreateTemperatureDEZLoad {} {
   global current_temperature_to_load
   global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
   global v3p_interp_select_interp_type
   global v3p_interp_data v3p_interp_ansys_select_command
   global v3p_interp_select_format v3p_interp_select_ansys_command
   global last_interp_var_list
   global icon_size1 env
   global beta_mode
   global v3_started did_startup_v3p did_setup_v3p_first
   global  solver_setup solver_setup_global
   
   variable ::AIEnv::GUIManagement::CATRootDir
	
   select_reset_parts_and_subsets
	
   foreach {n v} {
      name "" loadSet "" temp 0 scale_factor 1.0 lsdyna_nsid ""
   } {
      if [info exists current_temperature_to_load($n)] {
         set $n $current_temperature_to_load($n)
      } else {
         set $n $v
      }
   }
   set loadSetList [aie_list_sets temperature]
   set nlength [llength $loadSetList]
   if {$loadSet == "" && $nlength > 0} { 
       set loadSet [expr ([lindex [lsort $loadSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set loadSet 1
   }
   set AllowActiveReload 1

   set temp_type 0

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

   if {$solve_format == "2"} {
      ::AIEnv::GUIManagement::ToggleGrey lsdynacreateTemperatureNodeSetId 0
   } else {
      ::AIEnv::GUIManagement::ToggleGrey lsdynacreateTemperatureNodeSetId 1
   }

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

   set v3p_interp_select_interp_type {"Surfaces Only" "Volumes Only" "Surfaces and Volumes"}
   if { ![info exists v3p_interp_data(type)] } {
      set v3p_interp_data(type) "Surfaces and Volumes"
   }

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

}
    
proc onCreateTemperatureDEZApply {} {
   global  solver_setup solver_setup_global 
  global bc_name_list

  set def_name 0

  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 bc_name_list] && [lsearch $bc_name_list $name] >= 0} {
            user_error "The Name entered is already used.\nPlease enter another Name."
            set objList {}
            return
      }
  } else {
     set def_name 1
  }

   switch $createTemperatureVal { 
      0 { set entity "point"}
      1 { set entity "curve"}
      2 { set entity "surf"}
      3 { set entity "body"}
      4 { set entity "subset"}
      5 { set entity "part"}
      default {set entity ""}
   }

   if {$solver_setup != "-1"} {
      set solve_format $solver_setup
   } else {
      set solve_format $solver_setup_global
   }
   if {$solve_format == "2"} {
      if {$lsdyna_nsid == "" || $lsdyna_nsid <= 0} {
         user_error "Please enter a valid Node Set ID"
         set objList {}
         return
      }
   }
   if ![info exists objList] { set objList {} }
   if {$temp == ""} {set temp 0.0}
   if {$scale_factor == ""} {set scale_factor 1.0}
   foreach {n v} [applyTmp $temp_type $temp $scale_factor $entity $objList $name $loadSet $lsdyna_nsid] { 
      set $n $v 
   }
   set objList {}
   if {$def_name} {set name ""}
}

proc applyTmp {temp_type temp scale_factor type objects name loadset lsdyna_nsid} {
    global v3p_interp_data_input v3p_interp_data_output v3p_interp_data_conn
    global v3p_interp_data 
    global v3_started did_startup_v3p did_setup_v3p_first

    set temp_ok 1
    if {$temp_type == 0} {
#       set bcs [list [list 1 TEMP $loadset $temp] [list 1 LSDYNA_TID $lsdyna_nsid]]
		set tmp [list 1 TEMP $loadset $temp]
		if {$lsdyna_nsid != "" && $lsdyna_nsid > 0} {
		   set bcs [list $tmp [list 1 LSDYNA_TID $lsdyna_nsid]]
		} else {
		   set bcs [list $tmp]
                   }
    }

    if {$temp_type == 1} {
       set bcs [list [list 1 TEMP $loadset $temp]]
       ic_uns_write_node_list $objects node_list $scale_factor 
       if { $v3p_interp_data(type) == "Surfaces Only." } {
          set it Shells
       } elseif { $v3p_interp_data(type) == "Volumes Only." } {
         set it Volumes
       } else {
         set it Mixed
       }
       set interp_format "-format ASCII -command NODE -label TEMP -factor $v3p_interp_data(conv_factor)"
       if { ![info exists v3p_interp_data(selected_mats)] || $v3p_interp_data(selected_mats) == "" } {
         set cmd "interpolate_file_to_file node_list temp_list $v3p_interp_data(selected_vars) \"\" $it $interp_format"
       } else {
         set cmd "interpolate_file_to_file node_list temp_list $v3p_interp_data(selected_vars) $v3p_interp_data(selected_mats) $it $interp_format"
       }
       if {[info exist did_startup_v3p] && [info exist did_setup_v3p_first]} {
          mess "Starting Interpolation:\n"
          V3_client_cmd $cmd
          mess "Interpolation Done\n"
      } else {
         set temp_ok 0
	mess "Note: Interpolation can only occur after Post Results are loaded.\n" red
      }
    }

    if {$temp_type == 1 && $temp_ok} {
       set param_name "Nastran:TEMP:T"
       set on_nodes 1
       set colnum 1
       ic_uns_import_bcfield $param_name temp_list $on_nodes $colnum $temp 
    }

    if {$temp_ok} {
	aie_apply_attribute temperature $bcs TEMP $name $type $objects \
	    [select_get_parts_and_subsets]
    }

}


proc aie_apply_icons_temperature {fam obtype vis lvis} {
    set name ""
    set bcs [ic_boco_get $fam]
    if {$bcs == ""} { return 0 }
    set parsed [aie_parse_bcs_temperature $bcs]
    if {$parsed == ""} { return 0 }
    foreach {n v} $parsed { set $n $v }
    ic_boco_apply_generic_icon_to_group $fam temperature [list $temp] $vis
    return 1
}

proc aie_get_temperature_set {fam} {
    #set fam [ic_boco_groups_with_bctype [list TEMPERATURE_NAME $name]]
    return [aie_get_bc_argument $fam temperature loadSet]
}

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

proc onSetGravityDEZLoad {} {
    global lcs_data
    
    foreach {n v} {on 1 x 0.0 y 0.0 z 0.0 cid 0 sid "" scale 1.0 bds -1} { set $n $v }
    set ch [lindex [ic_param_list_children :Gravity_Load] 0]
    if [info exists vals] {unset vals}
    if {$ch != ""} { array set vals [ic_param_get_values :Gravity_Load] }
    
    foreach {n1 n2} {
	Create on
	Acceleration_Vector_Component_1 x
	Acceleration_Vector_Component_2 y
	Acceleration_Vector_Component_3 z
	CID cid
	SID sid
	Acceleration_Vector_Scale scale
    } {
	if [info exists vals($n1)] { set $n2 $vals($n1) }
    }
    set LCSList [lsort [array names lcs_data]]
    set LCS [aie_get_lcs_name $cid]
#VG<
	trace variable LCS "w" "aie_on_change_LCS [namespace current]\
	 {x y z} {X Y Z}"
	aie_on_change_LCS [namespace current] {x y z} {X Y Z}
#VG>
    set loadSetList [aie_list_sets load]
   set nlength [llength $loadSetList]
   if {$sid == "" && $nlength > 0} { 
       set sid [expr ([lindex [lsort $loadSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set sid 1
   }
}

proc onSetGravityDEZApply {} {
    global lcs_data
    
   foreach n {x y z} {
      if {[set $n] == ""} {
         set $n 0.0
      }
   }
   if {$scale == ""} {set scale 1.0}
    if {$LCS != ""} {
	set cid [lindex $lcs_data($LCS) 0]
    } else {
	set cid 0
    }
    set pvals [list Bulk_Data_Section -1]
    foreach {n1 n2} {
	Create on
	Acceleration_Vector_Component_1 x
	Acceleration_Vector_Component_2 y
	Acceleration_Vector_Component_3 z
	CID cid
	SID sid
	Acceleration_Vector_Scale scale
    } {
	lappend pvals $n1 [set $n2]
    }

    set x1 [expr {1.0*$x}]
    set x2 [expr {1.0*$y}]
    set x3 [expr {1.0*$z}]

    set ch [lindex [ic_param_list_children :Gravity_Load] 0]
    ic_param_delete_node :Gravity_Load
    ic_param_set_modified
    if {$x1 != 0.0 || $x2 != 0.0 || $x3 != 0.0} {
    	if {$ch == ""} { set ch :Gravity_Load/Gravity_Set1 }
    	ic_param_set_values $ch $pvals
    }
}

proc onCreateDynamicTimeDEZLoad {} {
    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

    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
         delay_id ""
         dphase_id ""
         excite_type ""
         table1_id ""
         t1 ""
         t2 ""
         freq ""
         phase ""
         exp_coef ""
         g_coef ""
         table2_id ""
         s_factor ""
         loadSet ""
         dynamic_time_excite 0
    } {
	if [info exists current_params_to_load($n)] {
             set new_param 0
             switch -- $n { 
                 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)]}
                 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}

    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(TLOAD1_$i)] || [info exists used(TLOAD2_$i)] || [info exists used($i)]} {incr i} { }
	set sid $i
    }

    set loadSetList [aie_list_sets load]
   set nlength [llength $loadSetList]
   if {$loadSet == "" && $nlength > 0} { 
       set loadSet [expr ([lindex [lsort $loadSetList] [expr ($nlength - 1)]] + 1)]
   } else {
       set loadSet 1
   }
    set AllowActiveReload 1
}

proc onCreateDynamicTimeDEZApply {} {
   global  env solver_setup solver_type
   global current_layers_to_load
   global params_data params_type
   global material_tbl_data material_tbl_type

   set solver_param 6

    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 {$dynamic_excite_type == 0} {
            set paramname TLOAD1_$sid
            aie_set_params_type $paramname [list $solver_param]
            aie_set_params_data $paramname [list $sid $dynamic_excite_type $loadSet $delay_id $excite_type $table1_id]
       }
        if {$dynamic_excite_type == 1} {
            set paramname TLOAD2_$sid
            aie_set_params_type $paramname [list $solver_param]
            aie_set_params_data $paramname [list $sid $dynamic_excite_type $loadSet $delay_id $excite_type $t1 $t2 \
                                                                          $freq $phase $exp_coef $g_coef]
       }

}































