#
# This script will set the voxel dimensions for a group of volumes on a page.
# Running this script will bring up a widget asking the user to specify
# th edimensions in x, y and z and then set the image properties to the 
# dimensions specified




#
# Path for the MEDx  Widgets
#
set PXHOME $env(PXHOME)
set auto_path [linsert $auto_path 0 $PXHOME/tcl $PXHOME/ui/tix]
 
#
# Procedure:    SetVoxelDimensions
#
# Usage:        SetVoxelDimensions  widget
#
# Summary:      Creates a dialog box for setting voxel dimensions.
#

proc VoxelDim { w } {
    global  VoxelDim 
 
    if [winfo exists $w] {
        wm deiconify $w
        raise $w
        return
    }
 
    toplevel $w
    wm title $w "Voxel Dimensions"
    wm iconname $w "Voxel Dimension"

 
    frame $w.f

   set VoxelDim($w,x) 1 
    tixControl $w.x -label "X Dimension (mm)" \
        -variable VoxelDim($w,x) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 23
            label.anchor w 
        }

    set VoxelDim($w,y) 1 
    tixControl $w.y -label "Y Dimension (mm)" \
        -variable VoxelDim($w,y) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 23
            label.anchor w
        }
    
    
    set VoxelDim($w,z) 1 
    tixControl $w.z -label "Z Dimension (mm)" \
        -variable VoxelDim($w,z) -min 0 -step 0.5 \
        -selectmode immediate \
        -options {
            entry.width 8
            label.width 23
            label.anchor w
        }

    pack $w.x $w.y $w.z -in $w.f -side top -anchor w \
        -padx 3 -pady 10
    

   frame $w.btns
   frame $w.btns.b -relief raised -borderwidth 1

        
 
    button $w.ok -text "OK" -width 5 \
        -command "VoxelDim:apply $w destroy" 
     bind $w.ok <Return> {
         [winfo toplevel %W].ok invoke}
    
    button $w.cancel    -command "VoxelDim:destroy $w" \
        -text "Cancel" -width 5
    bind $w.cancel <Return> {
       [winfo toplevel %W].cancel invoke}


    pack $w.btns.b -side bottom -fill x -padx 3 -pady 5
    pack $w.ok $w.cancel -in $w.btns.b \
        -side left -expand yes -padx 3 -pady 10 -fill y
 
    pack $w.f $w.btns -expand yes -fill both
 
}

#
#
# Usage:        VoxelDim:apply w destroy
#
# Summary:      Applies Specified Voxel Dimensions to group 
#

proc VoxelDim:apply { w dialog } {
    global VoxelDim

    set x $VoxelDim($w,x)
    set y $VoxelDim($w,y)
    set z $VoxelDim($w,z)

#
#Getting the individual pages from the group and setting
#the voxel dimensions
#
    MxGetCurrentGroup group
    MxGetPagesFromGroup $group page
    set no_of_pages [llength $page]

    for {set i 0} {$i < $no_of_pages} {incr i} {
	set I [lindex $page $i]
	MxGetImageProperties $I imageProperties
	eval set properties \{\{XScale $x\} \{YScale $y\} \{ZScale $z\}\}
	MxSetImageProperties $I properties
}
if {$dialog == "destroy"} {
 destroy $w
}
  

}
 

# Procedure:    VoxelDim:destroy
#
# Usage:        VoxelDim:destroy w
#
# Summary:      Destroys VoxelDim dialog box
#
proc VoxelDim:destroy { w } {
 
        destroy $w
}


VoxelDim .file
tkwait window .file







