

proc onv3NastranVarsDEZLoad {} {
   global nastran_vars

   aie_update_nastran_vars setup
   aie_update_nastran_vars current

   update idletasks
}

proc onV3NastranVarsMdModify {args} {
   set md [lindex $args 1]
   set j [lsearch $::nastran_vars(sv_modes) $md]
   if {$j >= 0} {
      set ::nastran_vars(mode_s) \
             [lindex $::nastran_vars(sv_modes) [expr $j + 1]]

      aie_update_nastran_vars mode
   }
}

proc onV3NastranVarsLdModify {args} {
   set ld [lindex $args 1]
   set j [lsearch $::nastran_vars(sv_loads) $ld]
   if {$j >= 0} {
      set ::nastran_vars(load_s) \
             [lindex $::nastran_vars(sv_loads) [expr $j + 1]]

      aie_update_nastran_vars load
   }
}

proc aie_update_nastran_vars {what args} {
   global nastran_vars

   switch -- $what {
      setup {
         if ![info exist nastran_vars(setupped)] {
            set nastran_vars(sv_modes) [V3_variables_get nastran_modes s]
            if {$nastran_vars(sv_modes) == ""} {
               ::AIEnv::GUIManagement::ToggleGrey v3NastranVarsMd 1
            } else {
               ::AIEnv::GUIManagement::ToggleGrey v3NastranVarsMd 0
            }
            set nastran_vars(vv_modes) [V3_variables_get nastran_modes v]

            set rr ""
            foreach {t v} $nastran_vars(sv_modes) { lappend rr $t }
            set nastran_vars(svModes) $rr

            set nastran_vars(sv_loads) [V3_variables_get nastran_loads s]
            if {$nastran_vars(sv_loads) == ""} {
               ::AIEnv::GUIManagement::ToggleGrey v3NastranVarsLd 1
            } else {
               ::AIEnv::GUIManagement::ToggleGrey v3NastranVarsLd 0
            }
            set nastran_vars(vv_loads) [V3_variables_get nastran_loads v]

            set rr ""
            foreach {t v} $nastran_vars(sv_loads) { lappend rr $t }
            set nastran_vars(svLoads) $rr

            foreach c {s v} {
               set sides [V3_variables_get nastran_sides $c]
               if {[V3_variables_get nastran_onesided $c] != ""} {
                  set sides [linsert $sides 0 Displacement onesided]
               }
               set nastran_vars(${c}v_classes) $sides
            }

            global v3p_mode_title
            if {[info exist v3p_mode_title] && ($v3p_mode_title != "")} {
               if ![catch {set pp [v3NastranVarsDEZ path v3NastranVarsMd]}] {
                  set pp [lindex $pp 0]
                  if {[winfo exist $pp] && ([winfo class $pp] == "Label")} {
                     $pp configure -text $v3p_mode_title
                  }
               } 
            }

            set nastran_vars(setupped) 1
         }
      }
      current {
         aie_update_nastran_vars setup

         if {$nastran_vars(sv_modes) != ""} {
            set nastran_vars(mode_s) [V3_variables_get nastran_current mode_s]
            set nastran_vars(mode_v) [V3_variables_get nastran_current mode_v]

            set j [lsearch $nastran_vars(sv_modes) $nastran_vars(mode_s)]
            if {$j > 0} {
               set nastran_vars(modeS) \
                       [lindex $nastran_vars(sv_modes) [expr $j - 1]]
            }
         } else {
            set nastran_vars(modeS) ""
         }
         if {$nastran_vars(sv_loads) != ""} {
            set nastran_vars(load_s) [V3_variables_get nastran_current load_s]
            set nastran_vars(load_v) [V3_variables_get nastran_current load_v]

            set j [lsearch $nastran_vars(sv_loads) $nastran_vars(load_s)]
            if {$j > 0} {
               set nastran_vars(loadS) \
                       [lindex $nastran_vars(sv_loads) [expr $j - 1]]
            }
         } else {
            set nastran_vars(loadS) ""
         }

         set nastran_vars(side_s) [V3_variables_get nastran_current side_s]
         set K onesided
         switch -- $nastran_vars(side_s) {
            Z1 { set K Tsided }
            Z2 { set K Bsided }
            S  { set K Ssided }
            E  { set K Esided }
         }
         set nastran_vars(side_K) $K

         set nastran_vars(sv_bname) [V3_variables_get nastran_current sv_bname]
         set svlist [V3_variables_get nastran_$K s]
         v3NastranVarsDEZ set v3pNastranSVarList $svlist
         v3NastranVarsDEZ set v3pNastranSVarCur $nastran_vars(sv_bname)

         set nastran_vars(sv_min) [V3_variables_get nastran_current sv_min]
         set nastran_vars(sv_max) [V3_variables_get nastran_current sv_max]

         set vvlist [V3_variables_get nastran_$K v]
         v3NastranVarsDEZ set v3pNastranVVarList $vvlist
         if {$vvlist != ""} {
            set nastran_vars(side_v) [V3_variables_get nastran_current side_v]
            set nastran_vars(vv_bname) [V3_variables_get nastran_current vv_bname]
            v3NastranVarsDEZ set v3pNastranVVarCur  $nastran_vars(vv_bname)
            ::AIEnv::GUIManagement::ToggleGrey v3NastranVVarFm 0
         } else {
            v3NastranVarsDEZ set v3pNastranVVarCur  ""
            ::AIEnv::GUIManagement::ToggleGrey v3NastranVVarFm 1
         }

         set nastran_vars(vmax) [V3_variables_get nastran_current vmax]
      }
      load {
         V3_nastran_variable select_load v $nastran_vars(load_s)
         V3_nastran_variable select_load s $nastran_vars(load_s)

         aie_update_nastran_vars current
      }
      mode {
         V3_nastran_variable select_mode v $nastran_vars(mode_s)
         V3_nastran_variable select_mode s $nastran_vars(mode_s)

         aie_update_nastran_vars current
      }
      side {
         set K onesided
         switch -- $nastran_vars(side_s) {
            Z1 { set K Tsided }
            Z2 { set K Bsided }
            S  { set K Ssided }
            E  { set K Esided }
         }
         set nastran_vars(side_K) $K

         set sslist [V3_variables_get nastran_$K s]
         set js [lsearch $sslist $nastran_vars(sv_bname)]
         
         v3NastranVarsDEZ set v3pNastranSVarList $sslist

         set vvlist [V3_variables_get nastran_$K v]
         if {$vvlist != ""} {
            v3NastranVarsDEZ set v3pNastranVVarList $vvlist
            V3_nastran_variable select_side v $nastran_vars(side_s)
            ::AIEnv::GUIManagement::ToggleGrey v3NastranVVarFm 0
         } else {
            ::AIEnv::GUIManagement::ToggleGrey v3NastranVVarFm 1
         } 
         V3_nastran_variable select_side s $nastran_vars(side_s)

         aie_update_nastran_vars current
      }
   }
}

proc onV3NastranSVarModify {args} {
   global nastran_vars
   set K $nastran_vars(side_K)
 
   V3_nastran_variable select_$K s \
             [v3NastranVarsDEZ get v3pNastranSVarCur] [lindex $args 1]
   update idletasks

   set nastran_vars(sv_min) [V3_variables_get nastran_current sv_min]
   set nastran_vars(sv_max) [V3_variables_get nastran_current sv_max]
}

proc onV3NastranVVarModify {args} {
   global nastran_vars
   set K $nastran_vars(side_K)

   V3_nastran_variable select_$K v  \
                 [v3NastranVarsDEZ get v3pNastranVVarCur] [lindex $args 1]

   set nastran_vars(vmax) [V3_variables_get nastran_current vmax]
}
