Index: trunk/gui/scripts/nanovisviewer.tcl
===================================================================
--- trunk/gui/scripts/nanovisviewer.tcl	(revision 971)
+++ trunk/gui/scripts/nanovisviewer.tcl	(revision 972)
@@ -57,10 +57,4 @@
     public method delete {args}
     public method scale {args}
-    public method GetDataObj { ivol } {
-	if { ![info exists _id2obj($ivol)] } {
-	    return ""
-	}
-	return $_id2obj($ivol)
-    }
     public method GetLimits { {ivol ""} } {
 	if { $ivol != "" } {
@@ -84,5 +78,5 @@
     protected method _send {string}
     protected method _send_dataobjs {}
-    protected method _send_transfuncs {}
+    protected method _send_transfunc {}
 
     protected method _ReceiveImage {option size}
@@ -104,5 +98,5 @@
 
     # The following methods are only used by this class. 
-    private method _SetTransfuncData { ivol }
+    private method _DefineTransferFunction { ivol }
     private method _AddIsoMarker { ivol x y }
     private method _InitIsoMarkers { ivol }
@@ -130,5 +124,7 @@
         
     private variable _isomarkers    ;# array of isosurface level values 0..1
+    private variable _styles
     private common   _settings
+    private variable _currentVolId ""
 }
 
@@ -151,7 +147,7 @@
         "[itcl::code $this _send_dataobjs]; list"
     # Send transfer functions event
-    $_dispatcher register !send_transfuncs
-    $_dispatcher dispatch $this !send_transfuncs \
-        "[itcl::code $this _send_transfuncs]; list"
+    $_dispatcher register !send_transfunc
+    $_dispatcher dispatch $this !send_transfunc \
+        "[itcl::code $this _send_transfunc]; list"
     # Rebuild event
     $_dispatcher register !rebuild
@@ -493,5 +489,5 @@
     $_dispatcher cancel !rebuild
     $_dispatcher cancel !send_dataobjs
-    $_dispatcher cancel !send_transfuncs
+    $_dispatcher cancel !send_transfunc
     image delete $_image(plot)
     image delete $_image(legend)
@@ -765,6 +761,4 @@
             # send the data as one huge base64-encoded mess -- yuck!
             set data [$dataobj values $comp]
-
-            # tell the engine to expect some data
             set nbytes [string length $data]
             if { ![SendBytes "volume data follows $nbytes"] } {
@@ -774,10 +768,11 @@
                 return
             }
-            set volId $_obj2id(count)
+            set ivol $_obj2id(count)
             incr _obj2id(count)
 
-            set _id2obj($volId) [list $dataobj $comp]
-            set _obj2id($dataobj-$comp) $volId
-            set _receiveids($volId) 1
+            set _id2obj($ivol) [list $dataobj $comp]
+            set _obj2id($dataobj-$comp) $ivol
+            set _receiveids($ivol) 1
+
         }
     }
@@ -819,43 +814,11 @@
 
 # ----------------------------------------------------------------------
-# USAGE: _send_transfuncs
-# ----------------------------------------------------------------------
-itcl::body Rappture::NanovisViewer::_send_transfuncs {} {
-    blt::busy hold $itk_component(hull); update idletasks
-    foreach dataobj $_sendobjs2 {
-        foreach comp [$dataobj components] {
-            #
-            # Determine the transfer function needed for this volume
-            # and make sure that it's defined on the server.
-            #
-	    set ivol $_obj2id($dataobj-$comp)
-            if { ![info exists _isomarkers($ivol)] } {
-                _InitIsoMarkers $ivol
-            } else {
-                _HideIsoMarkers $ivol
-            }
-            if { ![_SetTransfuncData $ivol] } {
-                return
-            }
-        }
-    }
-    set _sendobjs2 ""
-    blt::busy release $itk_component(hull)
-
-    # activate the proper volume
-    set first [lindex [get] 0]
-    foreach key [array names _obj2id *-*] {
-        set state [string match $first-* $key]
-	set ivol $_obj2id($key)
-        _send "volume state $state $ivol"
-        if {[info exists _id2style($ivol)]} {
-            _send "volume shading transfunc $_id2style($ivol) $ivol"
-        }
-    }
-    set comp [lindex [$dataobj components] 0]
-    _ShowIsoMarkers $_obj2id($first-$comp)
-    # if there are any commands in the buffer, send them now that we're done
-    SendBytes $_outbuf
-    set _outbuf ""
+# USAGE: _send_transfunc
+# ----------------------------------------------------------------------
+itcl::body Rappture::NanovisViewer::_send_transfunc {} {
+    if { ![_DefineTransferFunction $_currentVolId] } {
+	return
+    }
+    _fixLegend
     $_dispatcher event -idle !legend
 }
@@ -924,8 +887,8 @@
     if { [isconnected] } {
         array set info $args
-        set volume $info(id)
-	set _limits($volume-min) $info(min)
-	set _limits($volume-max) $info(max)
-        foreach { dataobj comp } $_id2obj($volume) break
+        set ivol $info(id)
+	set _limits($ivol-min) $info(min)
+	set _limits($ivol-max) $info(max)
+        foreach { dataobj comp } $_id2obj($ivol) break
 
         if { ![info exists _limits($dataobj-vmin)] } {
@@ -944,7 +907,15 @@
         lappend _sendobjs2 $dataobj
         unset _receiveids($info(id))
-        if { [array size _receiveids] == 0 } {
-            $_dispatcher event -idle !send_transfuncs
-        }
+
+	if { ![info exists _isomarkers($ivol)] } {
+	    _InitIsoMarkers $ivol
+	} else {
+	    _HideIsoMarkers $ivol
+	}
+	# We can update the transfer function now that we know the data limits.
+	if { ![_DefineTransferFunction $ivol] } {
+	    return
+	}
+	_fixLegend
     }
 }
@@ -982,5 +953,5 @@
         set h [winfo height $itk_component(3dview)]
         _send "screen $w $h"
-        # nothing to send -- activate the proper volume
+        # nothing to send -- activate the proper ivol
         set first [lindex [get] 0]
         if {"" != $first} {
@@ -998,4 +969,17 @@
 
         _ShowIsoMarkers $ivol 
+
+	if { [info exists _settings($this-$ivol-opacity)] } {
+	    set inner [$itk_component(controls).panel component inner]
+	    set sval $_settings($this-$ivol-opacity)
+	    set sval [expr {round($sval * 100)}]
+	    $inner.scales.opacity set $sval
+	}
+	if { [info exists _settings($this-$ivol-thickness)] } {
+	    set inner [$itk_component(controls).panel component inner]
+	    set sval $_settings($this-$ivol-thickness)
+	    set sval [expr {round($sval * 10000.0)}]
+	    $inner.scales.thickness set $sval
+	}
         UpdateTransferFunction $ivol
 
@@ -1223,5 +1207,5 @@
 
             # show the current value in the readout
-#puts "readout: $axis = $newval"
+	    #puts "readout: $axis = $newval"
 
             set ids [_currentVolumeIds -cutplanes]
@@ -1313,12 +1297,15 @@
                 set dataobj [lindex [get] 0]
                 if {$dataobj != 0} {
-                    set val [$inner.scales.opacity get]
-                    set sval [expr { 0.01 * double($val) }]
+		    set val [$inner.scales.opacity get]
+		    set sval [expr { 0.01 * double($val) }]
 		    # FIXME: This will change when we can select the current 
 		    #        volume.
 		    set comp [lindex [$dataobj components] 0]
 		    set tag $dataobj-$comp
-                    set _settings($this-$tag-opacity) $sval
-                    UpdateTransferFunction $_obj2id($tag)
+		    if { [info exists _obj2id($tag)] } {
+			set ivol $_obj2id($tag)
+			set _settings($this-$ivol-opacity) $sval
+			UpdateTransferFunction $ivol
+		    }
                 }
             }
@@ -1336,6 +1323,9 @@
 		    set comp [lindex [$dataobj components] 0]
 		    set tag $dataobj-$comp
-                    set _settings($this-$tag-thickness) $sval
-                    UpdateTransferFunction $_obj2id($dataobj-$comp)
+		    if { [info exists _obj2id($tag)] } {
+			set ivol $_obj2id($tag)
+			set _settings($this-$ivol-thickness) $sval
+			UpdateTransferFunction $ivol
+		    }
                 }
             }
@@ -1379,12 +1369,12 @@
     set h [expr {[winfo height $itk_component(legend)]-20-$lineht}]
     set ivol ""
-
-    # The changes when we can select the current volume.
-    set dataobj [lindex [get] 0]
-    if {"" != $dataobj} {
-        set comp [lindex [$dataobj components] 0]
-        if {[info exists _obj2id($dataobj-$comp)]} {
-            set ivol $_obj2id($dataobj-$comp)
-        }
+    if { $ivol == "" } {
+	set dataobj [lindex [get] 0]
+	if {"" != $dataobj} {
+	    set comp [lindex [$dataobj components] 0]
+	    if {[info exists _obj2id($dataobj-$comp)]} {
+		set ivol $_obj2id($dataobj-$comp)
+	    }
+	}
     }
     if {$w > 0 && $h > 0 && "" != $ivol} {
@@ -1396,5 +1386,5 @@
 
 # ----------------------------------------------------------------------
-# USAGE: _SetTransfuncData <ivol>
+# USAGE: _DefineTransferFunction <ivol>
 #
 # Used internally to compute the colormap and alpha map used to define
@@ -1402,5 +1392,5 @@
 # Returns: name {v r g b ...} {v w ...}
 # ----------------------------------------------------------------------
-itcl::body Rappture::NanovisViewer::_SetTransfuncData { ivol } {
+itcl::body Rappture::NanovisViewer::_DefineTransferFunction { ivol } {
     array set style {
         -color rainbow
@@ -1411,5 +1401,4 @@
     array set style [lindex [$dataobj components -style $comp] 0]
     set sname "$style(-color):$style(-levels):$style(-opacity)"
-
     if {$style(-color) == "rainbow"} {
         set style(-color) "white:yellow:green:cyan:blue:magenta"
@@ -1438,8 +1427,9 @@
     set isovalues [lsort -real $isovalues]
 
-    set delta 0.01
-    if { [info exists _settings($tag-thickness)]} {
-        set delta $_settings($tag-thickness)
-    }
+    if { ![info exists _settings($tag-thickness)]} {
+	set _settings($tag-thickness) 0.05
+    }
+    set delta $_settings($tag-thickness)
+
     set first [lindex $isovalues 0]
     set last [lindex $isovalues end]
@@ -1475,5 +1465,10 @@
     }
     set _id2style($ivol) $sname
-    return [_send transfunc define $sname $cmap $wmap]
+    set _styles($sname) 1
+    set cmds [subst {
+	transfunc define $sname { $cmap } { $wmap }
+	volume shading transfunc $sname $ivol
+    }]
+    return [SendBytes $cmds]
 }
 
@@ -1530,5 +1525,5 @@
     foreach dataobj [array names _all_data_objs] {
         foreach comp [$dataobj components] {
-	    _HideIsoMarkers _$obj2id($dataobj-$comp)
+	    _HideIsoMarkers $_obj2id($dataobj-$comp)
 	}
     }
@@ -1549,9 +1544,17 @@
     set c $itk_component(legend)
     regsub -all "," $levels " " levels
-    for {set i 1} { $i <= $levels } {incr i} {
-	set x [expr {double($i)/($levels+1)}]
-	set m [IsoMarker \#auto $c $this $ivol]
-	$m SetRelativeValue $x
-	lappend _isomarkers($ivol) $m 
+    if {[string is int $levels]} {
+	for {set i 1} { $i <= $levels } {incr i} {
+	    set x [expr {double($i)/($levels+1)}]
+	    set m [IsoMarker \#auto $c $this $ivol]
+	    $m SetRelativeValue $x
+	    lappend _isomarkers($ivol) $m 
+	}
+    } else {
+        foreach x $levels {
+	    set m [IsoMarker \#auto $c $this $ivol]
+	    $m SetRelativeValue $x
+	    lappend _isomarkers($ivol) $m 
+        }
     }
 }
@@ -1593,7 +1596,7 @@
         -levels 6
     }
+    set _isomarkers($ivol) ""
     foreach {dataobj comp} $_id2obj($ivol) break
     array set style [lindex [$dataobj components -style $comp] 0]
-
     if { [info exists style(-markers)] } {
 	_ParseMarkersOption $ivol $style(-markers)
@@ -1617,8 +1620,6 @@
         return
     }
-    # Compute a transfer function for the current data set.
-    _SetTransfuncData $ivol
-    _send [list volume shading transfunc $_id2style($ivol) $ivol]
-    _fixLegend
+    set _currentVolId $ivol
+    $_dispatcher event -idle !send_transfunc
 }
 
@@ -1640,5 +1641,5 @@
         set list {}
         set marker [namespace tail $marker]
-        foreach m $_isomarkers($tag) {
+        foreach m $_isomarkers($ivol) {
             set sx [$m GetScreenPosition]
             if { $m != $marker } {
