# Cutplane interface callbacks
# jari.spaete@ansys.com

proc aie_update_cutplane_wheelzoom {w in {X ""} {Y ""}} {
   global tdv_dynamics dynamic_info cut_info
   
   if [info exists cut_info(mouse_wheel)] {
       if {$cut_info(mouse_wheel) == 0} return
   }
   if {[string compare $X ""] && [string compare $Y ""] \
              && [string compare [winfo containing $X $Y] $w]} return

   if {($X != "") && ($Y != "")} {
      if {$in < -120} {set in -120} \
      elseif {$in > 120} {set in 120}
      global tdv_mouse_bindings
      if [info exists tdv_mouse_bindings(wheel)] {
         if ![string is double -strict $tdv_mouse_bindings(wheel)] {
            set tdv_mouse_bindings(wheel) 5
         }
         set in [expr {$tdv_mouse_bindings(wheel) * 0.2 * $in}]
      }
   }

   set incr 20
    set loops [ceil [expr {abs([expr {$in / $incr}])}]]
   if {$loops <= 0} return

   set inc [expr {$in / $loops}]
   set tdv_dynamics($w) 1
   for {set i 0} {$i < $loops} {incr i} {
      if ![info exist tdv_dynamics($w)] {return ""}
      set val [expr $dynamic_info(an_val) + $inc/2000.0]
      if {$val > 1.0} {set val [expr $val - 1.0]}
      if {$val < 0.0} {set val [expr $val + 1.0]}
      set dynamic_info(an_val) $val
      update idletasks

      if {$loops <= ($i+1)} {
         aie_update_cutplane_scan -1
      } elseif {$i == 0} {
         aie_update_cutplane_scan 1
      } else {
         aie_update_cutplane_scan 0
      }
   }
   if [info exist tdv_dynamics($w)] {unset tdv_dynamics($w)}
}


proc aie_update_cutplane_scan {i args} {
   global dynamic_info cut_info
   global tdv_default tdv_current_viewer

   set an_val $dynamic_info(an_val)
   if ![string is double -strict $an_val] {set an_val 0.5}
   if {$an_val < 0.000001} { set an_val 0.000001 } \
   elseif {$an_val > 0.999999} { set an_val 0.999999 }

   if ![info exist cut_info(cur)] { return "" }
   $cut_info(cur) reset_scan $an_val

   set cur $cut_info(cur)
   aie_cutplane_update coeffs [$cur get_coeffs] ptnorm [$cur get_ptnorm] corners [$cur get_corner]
   $cur configure -scan_state [expr ($i >= 0) ? 1 : 0]

   if {[info exist cut_info($cur,win)] && [winfo exist $cut_info($cur,win)]} {
      set w $cut_info($cur,win)
   } elseif {[winfo exist $tdv_current_viewer]} {
      set w $tdv_current_viewer 
   }
   
   if {![info exist cut_info(speed)] && ($i == 1)} {
       if ![info exists cut_info(hexa)] {
            set tm 0
            foreach ss [ic_uns_map_list 1] {
                incr tm [ic_uns_subset_get_cutspeed $ss]
            }
            set cut_info(speed) $tm
        }
        set cut_info(display_save) [$cur get -display_type]
   }

   if {$cut_info($cur,cmd) != ""} {
      if {$i < 0} {
         eval $cut_info($cur,cmd) $cur
         if [info exist cut_info(speed)] { unset cut_info(speed) }
         if [info exist cut_info(display_save)] {
            $cur configure -display_type $cut_info(display_save)
            unset cut_info(display_save)
         }
         return
      } elseif {[info exist cut_info(speed)] && ($cut_info(speed) < 200)} {
         eval $cut_info($cur,cmd) $cur
         return
      }
   }

   if {$w != ""} {
      if {!$cut_info(display)} {
         $cur configure -display_type 7
      }
   }
}

proc aie_cutplane_get {what args} {
   global cut_info

   if ![info exist cut_info(cur)] { return "" }

   switch -- $what {
      coeffs - ptnorm - corner - scan {
         return [$cut_info(cur) get_$what]
      }
      plane {
         return $cut_info(cur)
      }
   }
}

proc aie_cutplane_update {args} {
   global cut_info
   foreach {s r} $args {
      switch $s {
         coeffs {
            set cut_info(a) [lindex $r 0]
            set cut_info(b) [lindex $r 1]
            set cut_info(c) [lindex $r 2]
            set cut_info(d) [lindex $r 3]
         }
         ptnorm {
            set cut_info(px) [lindex $r 0]
            set cut_info(py) [lindex $r 1]
            set cut_info(pz) [lindex $r 2]
            set cut_info(xyz) [lrange $r 0 2]
            set cut_info(nx) [lindex $r 3]
            set cut_info(ny) [lindex $r 4]
            set cut_info(nz) [lindex $r 5]
         }
         corners {
            set cut_info(pt1) [lindex $r 0]
            set cut_info(pt2) [lindex $r 1]
            set cut_info(pt3) [lindex $r 2]
            if {[lindex $r 3] != ""} {set cut_info(pt4) [lindex $r 3]}
         }
         scan {set cut_info(an_val) $r}
      }
   }
}

proc aie_cutplane_create {args} {
   global cut_info

   set cut0 [pgeom_cut -interesting 0]
   if ![info exist cut_info(cutlist)] { set cut_info(cutlist) "" }
   lappend cut_info(cutlist) $cut0
   set cut_info(cur) $cut0
   set init_type ""
   set display_type 0; set display_col #ff22bb; set display_transp 0.5
   set bmin ""; set bmax ""

   set nargs [llength $args]
   for {set i 0} {$i < $nargs} {incr i} {
      switch -- [lindex $args $i] {
         -mbmin {
            incr i; set bmin [lindex $args $i]
            if {![string is double -strict [lindex $bmin 0]] || \
                ![string is double -strict [lindex $bmin 1]] || \
                ![string is double -strict [lindex $bmin 2]]} {
               incr i -1; set bmin ""
            }
         }
         -mbmax {
            incr i; set bmax [lindex $args $i]
            if {![string is double -strict [lindex $bmax 0]] || \
                ![string is double -strict [lindex $bmax 1]] || \
                ![string is double -strict [lindex $bmax 2]]} {
               incr i -1; set bmax ""
            }
         }
         -win { incr i; set cut_info($cut0,win) [lindex $args $i] }
         -cmd { incr i; set cut_info($cut0,cmd) [lindex $args $i] }
         -init_type { incr i; set init_type [lindex $args $i] }
         -display_type {
            incr i; set display_type   [lindex $args $i]
            if ![string is integer -strict $display_type] {
               set display_type 7
            }
         }
         -display_col {
            incr i; set display_col [lindex $args $i]
            if {$display_col == ""} { set display_col #ff22bb }
         } 
         -display_transp {
            incr i; set display_transp [lindex $args $i]
         }
         default {
         }
      }
   }
   
   if ![info exists cut_info($cut0,cmd)] {
	   set cut_info($cut0,cmd) aie_cutplane_update_mesh
   }

   set cut_info(active) 1
       
   $cut0 configure -display_type $display_type -color $display_col
   set cut_info(display_color) $display_col
   set cut_info(display) [expr $display_type ? 1 : 0]
   set cut_info(display_n) [expr $display_type & 0x4]
   set cut_info(display_bd) [expr $display_type & 0x1]
   set cut_info(display_tsh) [expr $display_type & 0x2]

   if ![string is double -strict $display_transp] {set display_transp 0.5}
   $cut0 set_material $display_transp
   set cut_info(display_transp) $display_transp
   if {($display_transp < 0.999) && ($cut_info(display_tsh) > 0)} {
      set cut_info(display_tsh) $display_transp
   }

   if {![info exist cut_info($cut0,win)] || \
       ![winfo exist $cut_info($cut0,win)]} {
      global tdv_default
      set cut_info($cut0,win) $tdv_default
   }
   $cut_info($cut0,win) add_image $cut0

   if {![info exist cut_info($cut0,cmd)]} {
      set cut_info($cut0,cmd) ""
   }

   if {($bmax == "") || ($bmin == "")} {
    set rr [$cut_info($cut0,win) bbox interesting]
       if {$rr == ""} {
	    # Put something in so that the fields won't be empty
	   set rr {{0 0 0} {1 1 1}}
       }
      set bmin [lindex $rr 0]
      set bmax [lindex $rr 1]
      set n 0
      for {set k 0} {$k < 3} {incr k} {
         if {[lindex $bmin $k] < [lindex $bmax $k]} {incr n}
      }
      if {$n < 2} { set bmin ""; set bmax "" } 
   }
   if {($bmax != "") && ($bmin != "")} {
      $cut0 configure -mbmin $bmin -mbmax $bmax
   }

   if {$init_type == ""} { set init_type 2 }
   if {[string is integer $init_type] && \
       ($init_type >=0) && ($init_type < 3)} {

      $cut0 set_default $init_type

      aie_cutplane_update coeffs [$cut0 get_coeffs] \
                          ptnorm [$cut0 get_ptnorm] \
                          corners [$cut0 get_corner] \
                          scan [$cut0 get_scan]

      if {$cut_info($cut0,cmd) != ""} {
         eval $cut_info($cut0,cmd) $cut0
      }
   }
   return $cut0
}

proc aie_cutplane_clear {uninit} {
    global cut_info
    
    if {[info exist cut_info(cur)] && ([info command $cut_info(cur)] != "") && $cut_info(active)} {
        aie_cutplane_set active 0
    }
    if $uninit {
        if [info exists cut_info(whole)] {
	        set whole $cut_info(whole)
	    } else {
	        set whole 1
	    }
	    catch { unset cut_info }
	    set cut_info(active) 0
        set cut_info(whole) $whole
    }
}

proc aie_cutplane_reset {} {
    global cut_info dynamic_info

    cutPlaneDEZ apply
    aie_cutplane_create
    set ::dynamic_info(an_val) [aie_cutplane_get scan]
	aie_update_cutplane_scan 1 ::dynamic_info(an_val)
	aie_update_cutplane_scan -1 ::dynamic_info(an_val)
}

proc aie_cutplane_set {what args} {
   global cut_info

   if {![info exist cut_info(cur)] || ([info command $cut_info(cur)] == "")} {
      aie_cutplane_create
   }
   set cut0 $cut_info(cur)

   set did 0
   switch -- $what {
      active {
         set on [lindex $args 0]
         if {$on == ""} { set on $cut_info(active) }
         if {$on} {
            if [info exist cut_info(display_saved)] {
               set cut_info(display) $cut_info(display_saved)
               unset cut_info(display_saved)
            }      
            if {$cut_info(display)} { aie_cutplane_set display }
         } else {
            set cut_info(display_saved) $cut_info(display)
            if {$cut_info(display)} {
               set cut_info(display) 0
               aie_cutplane_set display
            } 
         }
         set cut_info(active) $on
         set did 1
      }
      display_type {
         set display_type   [lindex $args 0]
         if ![string is integer -strict $display_type] {return ""}
         #set cut_info(display_type) $display_type
         $cut_info(cur)  configure -display_type $display_type
      }
      display_color {
         set display_col  [lindex $args 0]
         if {$display_col == ""} {set display_col $cut_info(display_color)}
         $cut_info(cur)  configure -color $display_col
      }
      display_transp {
         set display_transp [lindex $args 0]
         if {$display_transp == ""} {
            set display_transp $cut_info(display_transp)
         }
         if ![string is double -strict $display_transp] { return "" }
         $cut_info(cur) set_material $display_transp
      }
      displays {
         if {!$cut_info(display)} {
            $cut0 configure -display_type 0
            return ""
         }   
         set tsh $cut_info(display_tsh); set ty 0
         if {$cut_info(display_bd)} {set ty [expr $ty | 0x1]}
         if {$tsh != 0} {
            set ty [expr $ty | 0x2]
            $cut0 set_material $tsh
         }
         if {$cut_info(display_n)}  {set ty [expr $ty | 0x4]}
         $cut0 configure -display_type $ty
      }
      display {
         if {$cut_info(display)} {
            $cut_info(cur)  configure -display_type 7
         } else {
            $cut_info(cur)  configure -display_type 0
         }
      }
      win {
         if [winfo exist [lindex $args 0]] {
            set cut_info($cut0,win) [lindex $args 0]
            $cut_info($cut0,win) add_image $cut0
         }
      }
      cmd {
         set cmd [lindex $args 0]
         if {[info command [lindex $cmd 0]] != ""} {
            set cut_info($cut0,cmd) $cmd
            eval $cut_info($cut0,cmd) $cut0
         }
      }
      mbox {
         set bmax [lindex $args 1]
         set bmin [lindex $bmax 0]
         if {[string is double -strict [lindex $bmin 0]] && \
             [string is double -strict [lindex $bmin 1]] && \
             [string is double -strict [lindex $bmin 2]] && \
             [string is double -strict [lindex $bmax 0]] && \
             [string is double -strict [lindex $bmax 1]] && \
             [string is double -strict [lindex $bmax 2]]} {
            $cut0 configure -mbmin $bmin -mbmax $bmax
            set did 2
         }
      }
      preset {
         set k [lindex $args 0]
         if ![string is integer  -strict $k] {return ""}
         if {($k < 0) || ($k > 2)} {return ""}
         $cut_info(cur) set_default $k
         set did 1
      }
      ptnorm {
         foreach i {0 1 2 3 4 5} c {px py pz nx ny nz} {
            if ![string is double -strict [lindex $args $i]] {return ""}
            set $c [lindex $args $i]
         }
         $cut_info(cur) set_ptnorm $px $py $pz $nx $ny $nz
         set did 1
      }
      coeffs {
         foreach i {0 1 2 3} j {a b c d} {
            if ![string is double -strict [lindex $args $i]] {return ""}
            set $j [lindex $args $i]
         }
         $cut_info(cur) set_coeffs $a $b $c $d
         set did 1
      }
      corner - corners {
         foreach i {0 1 2 3 4 5 6 7 8} c {x1 y1 z1 x2 y2 z2 x3 y3 z3} {
            if ![string is double -strict [lindex $args $i]] {return ""}
            set $c [lindex $args $i]
         }
         $cut_info(cur) set_corner $x1 $y1 $z1 $x2 $y2 $z2 $x3 $y3 $z3
         set did 1
      }
      scan {
         if ![string is double -strict [lindex $args 0]] {return ""}
         $cut_info(cur) reset_scan [lindex $args 0]
         set did 1
      }
      pick - move {
         set w $cut_info($cut0,win)
         if {[anno_setup_mouse_binding $w] == 0} {return ""}
 
         set display_type [$cut_info(cur) get -display_type]
         $cut_info(cur) configure -display_type 7
         $cut_info(cur) set_material 1.0

         banner "Rotate cut plane with left button down, \
                 Move with middle button, \
                 Apply with right button clicking.\n" $w

         global button_events
         catch {$w configure -cursor {hand2 red white}}
         set button_events($w) ""

         set p_dist 280
         set objs [list $cut_info(cur)]
         while 1 {
            eval tkwait variable button_events($w)
            set val [set button_events($w)]
            set ptr_status [lindex $val 0]
            if {$ptr_status == 3} { break }
            if {$ptr_status <= 0} {continue}

            set x [lindex $val 1]; set y [lindex $val 2]
            set picked [$w pick $x $y "" pgeom $p_dist]
            if {$picked == ""} {continue}

            set picked_obj [lindex $picked 0]
            set picked_dist [lindex $picked 1]

            if {(($objs != "") && ([lsearch $objs $picked_obj] < 0)) \
                     || ([info command $picked_obj] == "")} { continue }

            catch {$w configure -cursor {fleur red white}}
            set last_pick $picked_obj

            while 1 {
               eval tkwait variable button_events($w)
               set val [set button_events($w)]
               if {[lindex $val 0] != 0} { break }

               set x0 $x; set y0 $y
               set x [lindex $val 1]; set y [lindex $val 2]
               if {$ptr_status == 1} {
                  $picked_obj rotate $picked_dist $x0 $y0 $x $y
               } else {
                  $picked_obj move [expr $x - $x0] [expr $y - $y0]
               }
               $picked_obj signal_change 1
            }
            catch {$w configure -cursor {hand2 red white}}
         }
         anno_reset_mouse_binding $w

         set did 1
         $cut_info(cur) configure -display_type $display_type
         $cut_info(cur) set_material $cut_info(display_transp)
         eval $cut0 set_coeffs [$cut0 get_coeffs]
      }
      3pts {
         set w $cut_info($cut0,win)
         if {[anno_setup_mouse_binding $w] == 0} {return ""}
         set display_type [$cut_info(cur) get -display_type]
         $cut_info(cur) configure -display_type 0
         global button_events tdv_default
         
      # using regular location picker         
         banner "Click a button to set the location." $w
         set xyz3 [join [va_create_xyz auto 3 0]]
         banner "" $w
         
         if {[llength $xyz3] == 9} { 
			eval $cut0 set_corner $xyz3
			$cut_info(cur) configure -display_type $display_type
			eval $cut0 set_coeffs [$cut0 get_coeffs]
            set did 1
         }
      }
   }

   if {$did} {
      if {($cut_info($cut0,cmd) != "") && ($did == 1)} {
         eval $cut_info($cut0,cmd) $cut0
      } else {
         if [info exist cut_info($cut0,win)] {
            $cut_info($cut0,win) redraw
         }
      }
      aie_cutplane_update coeffs [$cut0 get_coeffs] ptnorm [$cut0 get_ptnorm] corners [$cut0 get_corner] scan [$cut0 get_scan]
   }
   return $did
}


    
proc oncutPlaneDEZApply {} {
    global dynamic_info cut_info
    
    set which [::AIEnv::GUIManagement::ExclusiveGroup::GetCurrentGroupName cutPlaneM]
    set what ""; set abcd ""
    switch -- $which {
        cutPlaneCf {
	        set abcd [list a b c d]
	        set what coeffs
	    }
	    cutPlanePn {
	        foreach v $cut_info(xyz) s {px py pz} { set cut_info($s) $v }
	        set abcd [list px py pz nx ny nz]
	        set what ptnorm
	        set backup $cut_info(xyz)
	    } 
	    cutPlaneCp {
	        set abcd [list pt1 pt2 pt3]
	        set what corner
	        foreach p $abcd {
		        if {[llength $cut_info($p)] == 1} {
		            set cut_info($p) [lindex $cut_info($p) 0]
		        }
	        }
	    }
    }
    if {$abcd != ""} {
	    set rr [aie_cutplane_get $what]
	    if {$rr == ""} { return "" }
	    set did 0
	    foreach v $rr s $abcd {
	        if {$s == ""} continue
	        foreach k $cut_info($s) {
		        if ![string is double -strict $k] {mess "Error $s: value $k\n" red; return ""}
	        }
	        if {$v != $cut_info($s)} {incr did}
	    }
	    if {$did} {
	        set pp ""
	        foreach s $abcd {eval lappend pp $cut_info($s)}
	        eval aie_cutplane_set $what $pp
    	    switch -- $which {
	            cutPlanePn {
	                #set cut_info(xyz) $backup
                    set ::dynamic_info(an_val) [aie_cutplane_get scan]
	                #aie_update_cutplane_scan 1 ::dynamic_info(an_val)
	                #aie_update_cutplane_scan -1 ::dynamic_info(an_val)
	            }
	        } 
	    }
    }
    
    cutPlaneDEZ setdirty
}

proc oncutPlaneDEZLoad {} {
    global cut_info dynamic_info tdv_default
    variable cutPlaneMethod

    if [info exists cut_info(hexa)] {
        unset cut_info(hexa)
        aie_hex_cut_plane_clear 1
    }

    if {![info exist cut_info(cur)] || ![info exist cut_info(unstr_mesh)]} {
       # need to initialize a few variables
       set dynamic_info(an_val) 0.5
       set cut_info(whole) 1
       set cut_info(unstr_mesh) 1
       aie_cutplane_create
    } else {
        aie_cutplane_set active
    }

    if {![info exist cutPlaneMethod] || ($cutPlaneMethod == "")} { set cutPlaneMethod 1 }

    set cut_info(display_color) [$cut_info(cur) get -color]
    set display_type [$cut_info(cur) get -display_type]

    set cut_info(display) [expr $display_type ? 1 : 0]
    set cut_info(display_n) [expr $display_type & 0x4]
    set cut_info(display_bd) [expr $display_type & 0x1]
    set cut_info(display_tsh) [expr $display_type & 0x2]

    if {($cut_info(display_transp) < 0.999) && ($cut_info(display_tsh) > 0)} {
        set cut_info(display_tsh) 0.5
    }

    set w $tdv_default
    foreach {i v} {<Button-4> 50 <Button-5> -50} {
        set cut_info(binding,$w,$i) [list bind $w $i [eval bind $w $i]]
        bind $w $i "aie_update_cutplane_wheelzoom $w $v"
    }
    if ![info exists cut_info(mouse_wheel)] {
       set cut_info(mouse_wheel) 1
    }
    foreach m [list . $w] {
        set cut_info(binding,$m,<MouseWheel>) [list bind $m <MouseWheel> [eval bind $m <MouseWheel>]]
        bind $m <MouseWheel> "aie_update_cutplane_wheelzoom $w %D %X %Y"
    }
}

proc oncutPlaneDEZAfterLoad {} {
    global cut_info 

    if ![ic_uns_is_loaded] {
        cutPlaneDEZ unload
        mess "Error: No existing mesh available to use.  Please load the mesh first.\n" red
        return
    }
    
    if [info exists cut_info(active)] {
        if {[trace vinfo cut_info(active)] != ""} {
            trace vdelete cut_info(active) w aie_cutplane_trace_show
        }   
        trace variable cut_info(active) w aie_cutplane_trace_show
        set cut_info(active) $cut_info(active)
    }
}

proc oncutPlaneDEZCancel {} {
   global tdv_default cut_info
   
   if [info exists cut_info(active)] {
        set cut_info(active) 0
        aie_cutplane_update_mesh
        if {[trace vinfo cut_info(active)] != ""} {
            trace vdelete cut_info(active) w aie_cutplane_trace_show
        }
    }
    foreach {n v} [array get cut_info binding,*] {
        eval $v
        unset cut_info($n)
    }
}

proc oncutPlaneDEZUnLoad {} {
}

proc oncutPlanePnPtFinish {} {
    if {$::cut_info(xyz) == ""} return
    if {[llength $::cut_info(xyz)] == 1} {
        if {[ic_geo_check_objects_exist point $::cut_info(xyz)] != ""} {
            set ::cut_info(xyz) [ic_coords_into_local [ic_geo_get_point_location $::cut_info(xyz)]]
        }
    }
}

proc oncutPlaneCp1Finish {} {
    if {$::cut_info(pt1) == ""} return
    if {[llength $::cut_info(pt1)] == 1} {
        if {[ic_geo_check_objects_exist point $::cut_info(pt1)] != ""} {
            set ::cut_info(pt1) [ic_coords_into_local [ic_geo_get_point_location $::cut_info(pt1)]]
        }
    }
}

proc oncutPlaneCp2Finish {} {
    if {$::cut_info(pt2) == ""} return
    if {[llength $::cut_info(pt2)] == 1} {
        if {[ic_geo_check_objects_exist point $::cut_info(pt2)] != ""} {
            set ::cut_info(pt2) [ic_coords_into_local [ic_geo_get_point_location $::cut_info(pt2)]]
        }
    }
}

proc oncutPlaneCp3Finish {} {
    if {$::cut_info(pt3) == ""} return
    if {[llength $::cut_info(pt3)] == 1} {
        if {[ic_geo_check_objects_exist point $::cut_info(pt3)] != ""} {
            set ::cut_info(pt3) [ic_coords_into_local [ic_geo_get_point_location $::cut_info(pt3)]]
        }
    }
}

proc aie_cutplane_trace_show {{name ""} {ele ""} {op ""}} {
    set wn [cutPlaneDEZ path cutPlaneOn]
    if [winfo exist $wn] { CheckFrame::actframe $wn }
}

proc oncutPlaneITLoad {} {
   aie_cutplane_set pick 
   cutPlaneDEZ set cutPlaneMethod 1
}

proc oncutPlane3PLoad {} {
   aie_cutplane_set 3pts
   cutPlaneDEZ set cutPlaneMethod 1
}

proc oncutPlaneSYLoad {} {
   aie_cutplane_set preset 1
   cutPlaneDEZ set cutPlaneMethod 1
}

proc oncutPlaneSZLoad {} {
   aie_cutplane_set preset 2
   cutPlaneDEZ set cutPlaneMethod 1
}

proc oncutPlaneSXLoad {} {
   aie_cutplane_set preset 0
   cutPlaneDEZ set cutPlaneMethod 1
}

proc aie_cutplane_update_mesh {{cur ""}} {
    global cut_info cut_saved_fam_visible cut_saved_type_visible

    if ![ic_uns_is_loaded] return
    if {$cur == "cur"} { set cur $cut_info(cur) }
    set ta [tdv_active 0]
    if {($cur == "") || ![info exists cut_info] || !$cut_info(active)} {
	    # First reset the type and family visibility
	    if [info exists cut_saved_fam_visible] {
	        aie_reset_type_visible [array get cut_saved_type_visible]
	        aie_reset_fam_visible [array get cut_saved_fam_visible]
	    }
	    foreach ss [ic_uns_map_list 0] {
	        ::AIEnv::GUIManagement::Tree::RestoreVisibility
	        ic_uns_subset_cut $ss set 0
	    }
	    if [info exists cut_info(cur)] {
	        $cut_info(cur) configure -display_type 0
	    }
	    aie_refresh_parts
    } else {
	    set ptnorm [$cur get_ptnorm]
        set in_scan [$cur get -scan_state]
	    foreach ss [ic_uns_map_list 1] {
	        if {$ss == ""} continue
	        ic_uns_subset_cut $ss set $cut_info(active) $cut_info(whole) [lrange $ptnorm 0 2] [lrange $ptnorm 3 5] $in_scan
	    }
	    ::AIEnv::GUIManagement::Tree::SaveVisibility
    }
    tdv_active $ta
}

proc aie_cutplane_mesh_subset {} {
    global cut_info
    
    # First make a new cut plane that has everything in it
    set nsname [ic_uns_subset_unused_name Cutplane]
    set ns [ic_uns_subset_create $nsname]
    
    foreach sub [ic_uns_subset_list_visible] {
        ic_uns_subset_add_from $ns $sub
    }
    set ptnorm [$cut_info(cur) get_ptnorm]
    ic_uns_subset_cut $ns set $cut_info(active) $cut_info(whole) \
	[lrange $ptnorm 0 2] [lrange $ptnorm 3 5]
    ic_uns_subset_cut $ns restrict
    set nel [ic_uns_subset_count_elements $ns]
    mess "$nel elements in the cut subset\n"
    
    if $nel {
        ::AIEnv::GUIManagement::Tree::AddGroup mesh_subset $nsname 1
        ::AIEnv::GUIManagement::Tree::ToggleGroupVisibility mesh_subset $nsname same
    }
}

