
proc aie_refresh_screen {} {
    aie_deleteShellDiagnostics 2
    aie_update_visible 0
    aie_unblank_geometry entity
    select_unset_bindings 1
    tdv_pop_key_command n
    aie_unset_all_selection_modes
    aie_reset_geo_selected
    banner ""
}

proc aie_unset_all_selection_modes {} {
    global hexa_select aie_in_select_geom 
    global aie_in_select_geom_or_mesh aie_in_select_part
    global aie_in_select_mesh aie_in_select_str
    
    if [info exists ::hexa_select] { 
        catch { unset ::hexa_select }
    }
    if [info exists aie_in_select_geom] {
        catch { unset aie_in_select_geom }
    }
    if [info exists aie_in_select_geom_or_mesh] {
        catch { unset aie_in_select_geom_or_mesh }
    }
    if [info exists aie_in_select_part] {
        catch { aie_in_select_part }
    }
    if [info exists aie_in_select_mesh] {
        catch { unset aie_in_select_mesh }
    }
    if [info exists aie_in_select_str] {
        catch { unset aie_in_select_str }
    }
}

proc onreplicateDEZLoad {} {
   aie_symmetry_view_fm win
}

proc onreplicateDEZApply {} {
   global symm_data

   aie_symmetry_view_fm do_rep
   tdv_update_symmetry $symm_data(win)
}

proc aie_symmetry_view_fm {what args} {
   global tdv_mirrors tdv_replica tdv_xsym_var tdv_ysym_var tdv_zsym_var
   global symm_data tdv_current_viewer

   if {$what == "set_win"} {
      set w [lindex $args 0]
      if {$w == ""} {set w $tdv_current_viewer}
      if ![winfo exist $w] {return ""}

      set symm_data(win) $w
      replicateDEZ show
      return $w
   }

   if {![info exist symm_data(win)] || ![winfo exist $symm_data(win)]} {
      set symm_data(win) $tdv_current_viewer
   }

   set w $symm_data(win)

   switch -- $what {
       win {
         set symm_data(rep_type) $tdv_replica($w,type)

         set symm_data(angle) $tdv_replica($w,angle)
         set symm_data(nrep) [expr $tdv_replica($w,nrep) -1]
         foreach n {displacement center axis} {set symm_data($n) ""}
         foreach c {x y z} {
            set symm_data(${c}m) [set tdv_${c}sym_var($w)]
            lappend symm_data(displacement) $tdv_replica($w,d${c})
            lappend symm_data(center)       $tdv_replica($w,${c}0)
            lappend symm_data(axis)         $tdv_replica($w,a${c})
         }
         set symm_data(do_rep)  $tdv_replica($w)
         set symm_data(mir_mod) ""
         set symm_data(mir_del) ""

         set ty $symm_data(rep_type)
         if {$symm_data(do_rep) != $symm_data(${ty}_rep)} {
            set symm_data(${ty}_rep) $symm_data(do_rep)
         }
         set ot [lindex {shift rot} [lsearch {rot shift} $ty]]
         if {$symm_data(${ot}_rep) == 1} { set symm_data(${ot}_rep) 0 }  
         foreach T {Shift Rot} { [replicateDEZ path replicate${T}] actframe }
      }
      rot_rep - shift_rep {
         set ty [string range $what 0 end-4]
         if {$symm_data(${ty}_rep)} {
            set ot [lindex {shift rot} [lsearch {rot shift} $ty]]
            if {$symm_data(${ot}_rep)} {
               set symm_data(${ot}_rep) 0
               [replicateDEZ path replicate[string toupper $ot 0 0]] actframe
            }
            set symm_data(rep_type) $ty
         }
         set symm_data(do_rep) $symm_data(${ty}_rep)
      }
      do_rep {
         set tdv_replica($w) $symm_data(do_rep)
         set tdv_replica($w,nrep) [expr $symm_data(nrep) +1]
         set tdv_replica($w,type) $symm_data(rep_type)
         if {$symm_data(rep_type) == "rot"} {
            if {[scan $symm_data(center) "%g %g %g" x0 y0 z0] == 3} {
               foreach n {x0 y0 z0} {set tdv_replica($w,$n) [set $n]}
            }
            if {[scan $symm_data(axis) "%g %g %g" ax ay az] == 3} {
               foreach n {ax ay az} {set tdv_replica($w,$n) [set $n]}
            }
            set tdv_replica($w,angle) $symm_data(angle)
         } else {
            if {[scan $symm_data(displacement) "%g %g %g" dx dy dz] == 3} {
               foreach n {dx dy dz} {set tdv_replica($w,$n) [set $n]}
            }
         }
      }
      xm - ym - zm {
         set c [string index $what 0]
         set i [lsearch "x y z" $c]
         set t [string toupper $c]
         if {$symm_data($what) > 2} {
            if ![info exist tdv_mirrors($w,$c,D)] {
               set rr [list 0 0 0]
               set rr [lreplace $rr $i $i 1]
               foreach {k n} {0 A 1 B 2 C} {set $n [lindex $rr $k]}
               set D [lindex [lindex [$w bbox interesting] 0] 0]
            } else {
               foreach n {A B C D} { set $n $tdv_mirrors($w,$c,$n) }
            }
            set rr [aie_dlg_get_plane_coef .mir_pl "Modify specific $t mirror" \
                   "Set planar coefficients for the $t mirror" \
                   [list $A $B $C $D]]
            foreach {k n} {0 A 1 B 2 C 3 D} { set $n [lindex $rr $k] }
            if {($A == "") || ($B == "") || ($C == "") || ($D == "")} {return}

            foreach n {A B C D} { set tdv_mirrors($w,$c,$n) [set $n] }
         } elseif {$symm_data($what) > 0} {
            set rr [list 0 0 0]
            set rr [lreplace $rr $i $i 1]
            foreach {k n} {0 A 1 B 2 C} {
               set tdv_mirrors($w,$c,$n) [lindex $rr $k]
            }
            set j [expr $symm_data($what) -1]
            set tdv_mirrors($w,$c,D) \
                  [lindex [lindex [$w bbox interesting] $j] $i]
         }
         set tdv_${c}sym_var($w) $symm_data($what)

         replicateDEZ setdirty
      }
   }
}

proc aie_dlg_get_plane_coef {w title text defaults {geo +400+400}} {
   global dlg_plane

   catch {top_destroy $w 1}
   if {$defaults != ""} {
      foreach {i c} {0 A 1 B 2 C 3 D} {
         set dlg_plane($c) [lindex $defaults $i]
      }
   } else {
      foreach c {A B C D} {set dlg_plane($c) 0}
      set dlg_plane(C) 1.0
   }

   top_window $w 1 $title
   label $w.l -text $text

   foreach {s c} {a A b B c C d D} {
      frame $w.$s
      label $w.$s.l  -text " $c : " -width 5 -padx 6
      entry $w.$s.e  -width 16 -relief sunken -bd 2 -textvariable dlg_plane($c)
      pack $w.$s.l -side left
      pack $w.$s.e -side right -anchor center -expand 1 -fill both -padx 2
   }

   button $w.done -text Done -width 5 -command "top_destroy $w 1"
   button $w.cancel -text Cancel -width 5 \
                 -command "set dlg_plane(A) {}; top_destroy $w 1"

   pack $w.l $w.a $w.b $w.c $w.d  -side top -fill both
   pack $w.done $w.cancel -side left -fill both -expand 1 -padx 10 -pady 10

   top_geometry $w $geo
   focus $w
   tkwait window $w

   return [list $dlg_plane(A) $dlg_plane(B) $dlg_plane(C) $dlg_plane(D)]
}

proc aie_set_grad_bg {t_or_b} {
    global tdv_current_viewer

    set tcol [$tdv_current_viewer get -background]
    set bcol [$tdv_current_viewer get -bmbgcolor]
    switch $t_or_b {
	top {
	    set tcol [edit_one_color \
			  "Please select new color for background at top" $tcol 0]
	    if {$tcol == "*cancel*"} {return ""}
	}
	bottom {
	    set bcol [edit_one_color \
			  "Please select new color for background at bottom" $bcol 0]
	    if {$bcol == "*cancel*"} {return ""}
	}
	both {
	    set tcol [edit_one_color \
			  "Please select new color for background" $tcol 0]
	    if {$tcol == "*cancel*"} {return ""}
	    set bcol $tcol
	}
    }
    
    tdv_background $tdv_current_viewer $tcol $bcol
    $tdv_current_viewer redraw
}

proc aie_set_background {{t_or_b top}} {
    global tdv_default tdv_current_viewer

    #set_background
    aie_set_grad_bg $t_or_b
    if {$tdv_current_viewer != $tdv_default} {return}

    ic_uns_families_changed
    if [ic_hex_blocking_loaded] {
	hex_update_display
    }
    aie_set_options
}
