

proc onv3CreateStrmSfEXGLoad {} {
   global surf_info

   set rr ""
   foreach sf [V3_surface_get selected count] {
      lappend rr $surf_info($sf,title) 
   }
   variable str_data:sfname
   set str_data:sfname $rr
}


proc onv3CreateStrmDEZLoad {} {
   global str_data
   foreach n {2d_how dir 2d_npts noded surf_ex} {
      v3CreateStrmDEZ set str_data:$n $str_data($n)
   }
}

proc onv3CreateStrmDEZApply {} {
   global str_data dset_info

   foreach n {2d_how dir 2d_npts noded surf_ex} {
      set str_data($n) [v3CreateStrmDEZ get str_data:$n]
   }
   set str_data(surfnpts) $str_data(2d_npts)
   update idletasks

   set how [v3CreateStrmDEZ get v3CreateStrmVal]

   switch -- $how {
      0 - 2 {
         global dset_info
         set w [aie_get_v3p_gwin]
         set str_data(2dpickwin) $w
         set theNode $dset_info($w,v3dset)
         set theTree $::AIEnv::GUIManagement::Tree::aieTree
         aie_set_v3strmgrp_v3dset $theTree $theNode
      }
   }

   switch -- $how {
      0 { aie_create_stream 2d v3CreateStrmDEZ}
      1 { aie_create_stream surf v3CreateStrmDEZ }
      2 { aie_create_stream load v3CreateStrmDEZ }
   }
}

proc aie_create_stream {what dez_cmd args} {
   global str_data

   switch -- $what {
      load {         
         set file [$dez_cmd get v3CreateStrmLdFileVar]
         if ![file readable $file] {
            set file [tk_getOpenFile -filetypes {{{All Files} *}} \
                     -title "Select File for Streamline  Seed" \
                     -parent . -initialdir [pwd] ]
         }
         if {$file == ""} {return ""}
         if ![file readable $file] {
            mess "Failed to read $file\n" read
            return ""
         }
         set fp [open $file r]
         if {$fp != ""} { V3_stream_seed reset }

         set n 0
         while 1 {
            if [eof $fp] {break}
            set rr [gets $fp]
            set x [lindex $rr 0]
            set y [lindex $rr 1]
            set z [lindex $rr 2]
             
            if {[string is double -strict $x] && \
                [string is double -strict $y] && \
                [string is double -strict $z]} {
               V3_stream_seed add_seed $x $y $z 0
               incr n
            }
         }
         close $fp

         if {$n > 0} { V3_stream_group create -intdir $str_data(dir) }
      }
      2d {
         update_defstrs_form 2d
      }
      surf {
         global surf_info
         set sf [lindex $surf_info(selected) 0]
         set theNode $surf_info($sf,node)
         set theTree $::AIEnv::GUIManagement::Tree::aieTree
         aie_set_v3strmgrp_v3dset $theTree $theNode

         update_defstrs_form surf start
      }
   }
}


proc onv3CreateStrm2dDEZApply {} {
   global str_data dset_info
   foreach n {2d_how dir 2d_npts} {
      variable str_data:$n; set str_data($n) [set str_data:$n]
   }
   update idletasks

   set v3node $str_data(v3dset)
   set str_data(2dpickwin) $dset_info($v3node,gwin)
   aie_create_stream 2d v3CreateStrm2dDEZ
}


proc onv3CreateStrmsfDEZApply {} {
   global str_data
   foreach n {noded surf_ex dir surfnpts} {
      variable str_data:$n; set str_data($n) [set str_data:$n]
   }
   update idletasks

   aie_create_stream surf v3CreateStrmsfDEZ
}

proc onv3CreateStrm2dDEZLoad {} {
   global str_data
   foreach n {2d_how dir 2d_npts} {
      variable str_data:$n; set str_data:$n $str_data($n)
   }
}

proc onv3CreateStrmsfDEZLoad {} {
   global str_data surf_info
   foreach n {noded surf_ex dir surfnpts} {
      variable str_data:$n; set str_data:$n $str_data($n)
   }

   set rr ""
   foreach sf [V3_surface_get selected count] {
      lappend rr $surf_info($sf,title)
   }
   variable str_data:sfname
   set str_data:sfname $rr
}





proc onv3StrmPropertiesDEZLoad {} {
   global str_data
   foreach n {ribbon_size max_sizing start_angle \
              animate_spray div_sizing bubble_size tail_len} {
      variable str_data:$n; set str_data:$n $str_data($n)
   }
}

proc aie_edit_v3strmgrp {name} {
   global str_data dset_info

   if ![info exist str_data($name,node)] {return ""}

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
   aie_set_v3strmgrp_v3dset $top_tree $str_data($name,node)

   StrmTree::selecttree 1 $top_tree $str_data($name,node)

   v3StrmPropertiesDEZ show
}

proc aie_set_v3strmgrp_v3dset {theTree theNode} {
   global str_data dset_info

   while {[set p [$theTree parent $theNode]] != "root"} {set theNode $p}
   if {$theNode != $str_data(v3dset)} {
      set str_data(v3dset) $theNode
      V3_stream_group client_id $dset_info($theNode,client_id)
   }

   return $theNode
}

proc aie_toggle_v3strmgrp {theTree theNode} {
   aie_set_v3strmgrp_v3dset $theTree $theNode
   StrmTree::toggle $theTree $theNode
}

proc aie_toggle_v3strmgrp_root {theTree theNode} {
   aie_set_v3strmgrp_v3dset $theTree $theNode
   StrmTree::toggle $theTree $theNode
}


proc aie_v3strmgrp_root_menu {mm theTree theNode} {
   set dset [aie_set_v3strmgrp_v3dset $theTree $theNode]

   global str_data
   set str_data(2dpickwin) [lindex [$theTree itemcget $dset -data] 1]
   set str_data(win) $str_data(2dpickwin)

   $mm add command -label "Set seed from 2d annoations" \
           -command "aie_v3strmgrp_tree start_2d $theNode {}"
   $mm add command -label "Set seed from the surface" -command \
           "aie_v3strmgrp_tree start_surf $theNode {}"
 # mm add command -label "Set seed from 3D points" ...
   $mm add command -label "Load seed from a file" -command \
           "aie_v3strmgrp_tree load_seed $theNode {}"
   $mm add separator
   $mm add command -label "Load streams from a file" -command \
           "aie_v3strmgrp_tree load $theNode {}"
   $mm add separator
   $mm add command -label "Pick and Delete" -command \
           "aie_v3strmgrp_tree pick $theNode {}"
}


proc aie_v3strmgrp_menu {mm theTree theNode id} {

   set dset [aie_set_v3strmgrp_v3dset $theTree $theNode]

   StrmTree::plotmenu $mm $theTree $theNode $dset

   $mm add separator
   $mm add command -label "Rename" -command \
               "aie_v3strmgrp_tree rename $theNode $id"
   $mm add command -label "Remove permanently" -command \
               "aie_v3strmgrp_tree remove $theNode $id"
   $mm add command -label "Add additional from file" -command \
               "aie_v3strmgrp_tree load_upon $theNode $id"
   $mm add command -label "Save" -command \
               "aie_v3strmgrp_tree save $theNode $id"
}

proc aie_v3strmgrp_tree {what node id args} {
   global str_data

   set top_tree [set ::AIEnv::GUIManagement::Tree::aieTree]
 
   switch -- $what {
      delete {
         V3_stream_group set_groupname [$top_tree itemcget $node -text]
         V3_stream_group delete
         $top_tree delete $node
         ...... not in use
      }
      undelete {
         V3_stream_group set_groupname [$top_tree itemcget $node -text]
         V3_stream_group undelete
         ...... not in use
      }
      remove {
         set name [$top_tree itemcget $node -text]
         if {$str_data(wf,confirm_remove)} {
            if {[confirm_diag "Remove $name"] != 0} { return }
         }

         V3_stream_group set_groupname $name
         V3_stream_group remove
         $top_tree delete $node
      }
      clean_all {
         if [confirm_diag "Clean All Streams"] {return}

         V3_stream_group clean
         foreach n [$top_tree nodes $node] {
            $top_tree delete $n
         }
      }
      rename {
         set oldname [$top_tree itemcget $node -text]
         set newname [question_box .text "Change Group's Name" \
                        "Rename group of streams" $oldname]
         if {$newname != ""} {
            V3_stream_group rename $oldname $newname
            $top_tree itemconfigure $node -text $newname
         }
      }
      save {
         set file [file_selection  -new_ok 1 -message \
                         "Select a file to save current group"]
         if {$file == ""} {return}

         if ![catch {set fp [open $file w]} err_mag] {
            close $fp
            V3_stream_group set_groupname [$top_tree itemcget $node -text]
            V3_stream_group save $file
         } else {
            mess "$err_mag\n" red
         }
      }
      load_upon {
          set filein [file_selection  -message \
                         "Select data file for streamlines"]
          if {$filein == ""} {return}

          V3_stream_group set_groupname [$top_tree itemcget $node -text]
          V3_stream_group load_upon $filein 
      }
      load {
          set filein [file_selection  -message \
                         "Select data file for streamlines"]
          if {$filein == ""} {return}

          V3_stream_group load $filein
      }
      load_seed {
          v3CreateStrmDEZ show
          v3CreateStrmDEZ set v3CreateStrmVal 2
      }
      start_2d {
          v3CreateStrmDEZ show
          v3CreateStrmDEZ set v3CreateStrmVal 0
          #v3CreateStrm2dDEZ show
      }
      start_surf {
          v3CreateStrmDEZ show
          v3CreateStrmDEZ set v3CreateStrmVal 1
          #v3CreateStrmsfDEZ show
      }
      pick {
         v3_pick_obj $str_data(win) streamline
      }
   }
}

proc aie_modify_streams {what args} {
   global str_data
   foreach n {start_angle animate_spray tail_len} {
      set val [v3StrmPropertiesDEZ get str_data:$n]
      if {$str_data($n) != $val} {
         set str_data($n) $val
         update idletasks; update_disstrs_form $n
      }
   }
   set val [v3StrmPropertiesDEZ get str_data:bubble_size]
   if {$str_data(bubble_size) != $val} {
      set str_data(bubble_size) $val
      update idletasks; update_disstrs_form asize
   }   
   set did 0
   foreach n {ribbon_size max_sizing} {
      set val [v3StrmPropertiesDEZ get str_data:$n]
      if {$str_data($n) != $val} { incr did; set str_data($n) $val }
   }
   if {$did} {update idletasks; update_disstrs_form size}

   if {$what != ""} {update_disstrs_form $what}
}

proc onv3StrmPropertiesDEZApply {} {
   aie_modify_streams ""
}



proc aie_setup_animate {what args} {
   global animate_data linerot_data modal_view

   if {[info command v3AnimateDEZ] == ""} {return ""}

   switch -- $what {
      win {
      }
      get_win {
         if {![info exist animate_data(win)] || \
             ![winfo exist $animate_data(win)]} {
            global v3_viewer
            set animate_data(win) $v3_viewer
         }
         return $animate_data(win)
      }
      auto_mv {
         set m [v3AnimateDEZ get animate_data:automv]
         global hardcopy_auto_generate
         
         if ![info exist hardcopy_auto_generate] {
            if {$m} {hardcopy_auto_cmd}
         } else {
            if {!$m} {hardcopy_auto_cmd}
         }
         return $m
      }
      encode_mpg {
         mpegDEZ load
      }
      encode_gifa {
         gifaDEZ load
      }
      streams {
         set k [v3AnimateDEZ get animate_data:streams]
       # ::AIEnv::GUIManagement::ToggleGrey v3AnimaStreamPm [expr !$k]
         set animate_data(streams) $k
         if {!$k} { V3_modify_strm_all -animate_on 0 }
         return $k 
      }
      dynamic {
         set k [v3AnimateDEZ get animate_data:dynamic]
       # ::AIEnv::GUIManagement::ToggleGrey v3AnimaDynamicPm [expr !$k]
         set animate_data(dynamic) $k
       # if {!$k} { update_dynamic_type none }
         return $k
      }
      transient {
         set k [v3AnimateDEZ get animate_data:transient]
         set animate_data(transient) $k
         return $k
      }
      views {
         global vtan_data
         set vtan_data(win) [aie_setup_animate get_win]

         set k [v3AnimateDEZ get animate_data:views]
       # ::AIEnv::GUIManagement::ToggleGrey v3AnimaViewPm [expr !$k]
         set animate_data(views) $k
         if {$k} {
            update_vtan_form win
            set animate_data(linerot) 0; v3AnimateDEZ set animate_data:linerot 0
         }
         if {[info exist vtan_data(viewlist)] && \
              ([info command $vtan_data(viewlist)] != "")} {
            while {$vtan_data(nviews) > 0} {
               incr vtan_data(nviews) -1
               $vtan_data(viewlist) del_transf $vtan_data(nviews)
            }
         }
         return $k
      }
      app_view {
         global vtan_data
         if {[info exist vtan_data(viewlist)] && \
              ([info command $vtan_data(viewlist)] != "")} {
            $vtan_data(viewlist) app_current 
            incr vtan_data(nviews)
         }
      }
      linerot {
         set linerot_data(win) [aie_setup_animate get_win]

         set k [v3AnimateDEZ get animate_data:linerot]
       # ::AIEnv::GUIManagement::ToggleGrey v3AnimaRotPm [expr !$k]
         set animate_data(linerot) $k
         if {$k} { set animate_data(views) 0; v3AnimateDEZ set animate_data:views 0 }
         return $k
      }
      deform - modal {
         set modal_view(win) [aie_setup_animate get_win]

         set k [v3AnimateDEZ get animate_data:$what]
       # set name v3Anima[string toupper $what 0 0]Pm 
       # ::AIEnv::GUIManagement::ToggleGrey $name [expr !$k]
         set animate_data(modal) $k
         if {$k} {
            V3_animate_modal on
            update_modal_view win

            V3_animate_modal set_surf

            if {$modal_view(m_or_d) == "d"} {
               v3AnimateDEZ set modal_view:deform_amp $modal_view(df_amp)
            } else {
               v3AnimateDEZ set modal_view:modal_amp $modal_view(df_amp)
            }
         } else {
            V3_animate_modal off
         }
         set modal_view(deforming) $k

         update_modal_view deforming

         return $k
      }
   }

   if {$animate_data(streams)} {
      global str_data
      foreach {t str} [V3_stream_group_get list] {
         V3_stream_group set_group $str
         if {!$str_data(animate_on)} {continue}
         foreach {n s} {animate_spray animate_spray \
                        tail_len tail_len bubble_size asize} {
            set val [v3AnimateDEZ get str_data:$n]
            if {$val != $str_data($n)} {
               set str_data($n) $val; update_disstrs_form $s
            }
         }
      }
   }

   if {$animate_data(linerot)} {
      foreach s {total_degree axis center} {
         set val [v3AnimateDEZ get linerot_data:$s]
         if {$val != $linerot_data($s)} { set linerot_data($s) $val }
      }
   }

   if {$animate_data(modal)} {
      if {$modal_view(m_or_d) != "d"} {
         foreach {vn n p} {modal_view:nsteps nsteps nsteps v3pModeCur freq set_freq} {
            set val [v3AnimateDEZ get $vn]
            if {$val != $modal_view($n)} {set modal_view($n) $val; update_modal_view $p}
         }
      }
   }
  
   switch -- $what {
      deform_ud - modal_ud - deform_amp - modal_amp - deform_back - deform_svar {
         set t [lindex  $args 0]; if {$t == ""} {set t $what}
         set val [v3AnimateDEZ get modal_view:$what]
         if {$val != $modal_view($t)} {
            set modal_view($t) $val
            update_modal_view $t
         }
      }
   }
}

proc aie_update_animate {what args} {
   global animate_data

   if {[info command v3AnimateDEZ] == ""} {return ""}

   if {![info exist animate_data(win)] || \
       ![winfo exist $animate_data(win)]} {
      global v3_viewer
      set animate_data(win) $v3_viewer
   }

   aie_setup_animate ""
 
   set val [v3AnimateDEZ get animate_data:speed]
   if {![string is integer -strict $val] || ($val < 1)} {
      mess "Animation's speed should be a positive integer (in msec)\n"
   } elseif {$val != $animate_data(speed)} {
      set animate_data(speed) $val
   }

   set val [v3AnimateDEZ get animate_data:nsteps]
   if {$val != $animate_data(nsteps)} {
      set animate_data(nsteps) $val
     #if [info exist animate_data(dt)] {unset animate_data(dt)}
      animate_all_setup
   }

   set val [v3AnimateDEZ get v3AnimaNcycles]
   if {$val == "looping"} {
      set animate_data(looping) 1
   } else {
      set animate_data(looping) 0
      if {[string is integer -strict $val] && ($val != $animate_data(ncycles))} {
         set animate_data(ncycles) $val
         set rr [v3AnimateDEZ get v3AnimaCycles]
         if {[lsearch $rr $val] < 0} {
            v3AnimateDEZ set v3AnimaCycles [lappend rr $val]
         }
      }
   }
 
   switch -- $what {
      instant {
         set val [v3AnimateDEZ get animate_data:instant]
         if {$val != $animate_data(instant)} {
            set animate_data(instant) $val
            animate_all_instant 
         }

         if {$animate_data(transient)} { animate_transient instant $val }
      }
      animate {
         ::AIEnv::GUIManagement::anyToggleGreyOn \
                   [lindex [v3AnimateDEZ path v3Animator] 1]
         
         if {!$animate_data(on)} { animate_all_cmd }

         ::AIEnv::GUIManagement::anyToggleGreyOff \
                   [lindex [v3AnimateDEZ path v3Animator] 1]
      }
      stop {
         if {$animate_data(on)} {set animate_data(on) 0}

         if {$animate_data(transient)} { animate_transient stop }
      }
      incr {
         incr_1_all_cmd 1
         v3AnimateDEZ set animate_data:instant $animate_data(instant)

         if {$animate_data(transient)} { animate_transient incr }
      }
      back {
         incr_1_all_cmd -1
         v3AnimateDEZ set animate_data:instant $animate_data(instant)

         if {$animate_data(transient)} { animate_transient back }
      }
   }
}


proc onv3AnimateDEZLoad {} {
   global animate_data
   foreach s {nsteps speed} {
      variable animate_data:$s; set animate_data:$s $animate_data($s)
   }   

   variable v3AnimaCycles
   if {![info exist v3AnimaCycles] || ($v3AnimaCycles == "")} {
      set v3AnimaCycles [list looping 1 5 10]
   }

   variable v3AnimaNcycles
   if {$animate_data(looping)} {
      set v3AnimaNcycles looping
   } else {
      set v3AnimaNcycles $animate_data(ncycles)
   }

   set animate_data(incr_after_cmd) "v3AnimateDEZ set animate_data:instant"

 # -------- for streams
   set str_fm [v3AnimateDEZ path v3AnimaStream]
   if [winfo exist $str_fm] {
      global str_data
      foreach s {animate_spray tail_len bubble_size} {
         variable str_data:$s
         if {![info exist str_data:$s] || [set str_data:$s] == ""} {
            set str_data:$s $str_data($s)
         }
      }
      $str_fm actframe
   }

  # ------- for views
   variable animate_data:views
   set animate_data:views $animate_data(views)
   [v3AnimateDEZ path v3AnimaView] actframe

  # ------- for linerot

   global linerot_data
   foreach s {total_degree axis center} {
      variable linerot_data:$s; set linerot_data:$s $linerot_data($s)
   }
   variable animate_data:linerot
   set animate_data:linerot $animate_data(linerot)
 # ::AIEnv::GUIManagement::ToggleGrey v3AnimaRotPm [expr !$animate_data(linerot)]
   [v3AnimateDEZ path v3AnimaRot] actframe

  # ------- for transient

   global transient_info

   variable animate_data:transient
   set animate_data:transient $animate_data(transient)

   if [info exist transient_info(i_or_p)] {
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaTransient 0
   } else {
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaTransient 1
   }

  # ------- for deform or modal

   global modal_view
   foreach s {deform_svar deform_back nsteps} {
      variable modal_view:$s; set modal_view:$s $modal_view($s)
   }
   foreach s {mod_ambar df_ambar} {variable modal_view:$s; set modal_view:$s 0}
   foreach s {modal deform} {
      variable modal_view:${s}_amp; set modal_view:${s}_amp $modal_view(df_amp)
      variable modal_view:${s}_ud; set modal_view:${s}_ud $modal_view(with_undeformed)
   }

   set md_fm [v3AnimateDEZ path v3AnimaDeform]
   if ![winfo exist $md_fm] {return ""}

   set auto_on 0
   global custom_format
   if [info exist custom_format] {
      switch -- $custom_format {
         nastran { set auto_on 1 }
         ansys   { set auto_on 1 }
      }
   }

   if {$modal_view(m_or_d) == "d"} {
      if {$auto_on} { v3AnimateDEZ set animate_data:deform 1 }
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaModal 1
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaDeform 0
      [v3AnimateDEZ path v3AnimaDeform] actframe

      if {$auto_on} { aie_setup_animate deform }
   } else {
      if {$auto_on} { v3AnimateDEZ set animate_data:modal 1 }
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaModal 0
      update idletasks

      [v3AnimateDEZ path v3AnimaModal] actframe
      ::AIEnv::GUIManagement::ToggleGrey v3AnimaDeform 1

      variable v3pModesList; set v3pModesList [update_modal_view list_freq]
      variable v3pModeCur; set v3pModeCur $modal_view(freq)

      if {$auto_on} { aie_setup_animate modal }
   }
}


proc onv3AnimateDEZApply {} {
   aie_update_animate ""
}

proc onv3AnimateDEZCancel {} {
}
