# $Application should exit when this script is finished.
$Application ExitOnReturnOn

set kw(vtkTemp1) [$Application GetMainWindow]
set pv(vtkTemp43) [$kw(vtkTemp1) GetMainView]

if {[info exists argc]} { 
    set argcm1 [expr $argc - 1]
    for {set i 0} {$i < $argcm1} {incr i} {
        if {[lindex $argv $i] == "-D" && $i < $argcm1} {
		set DataDir [lindex $argv [expr $i + 1]]
        }
        if {[lindex $argv $i] == "-UC" && $i < $argcm1} {
            set rmui [$pv(vtkTemp43) GetRenderModuleUI]
            catch {$rmui SetCompositeThreshold 0}
        }
    }
} 

$pv(vtkTemp43) SetBackgroundColor 0 0 0

$kw(vtkTemp1) SetUseMessageDialog 0

# Open a structured grid data set.
$kw(vtkTemp1) OpenCustom "P3DReader" "$DataDir/Data/combxyz.bin"
set kw(vtkTemp292) [$kw(vtkTemp1) GetCurrentPVSource]
set kw(vtkTemp305) [$kw(vtkTemp292) GetPVWidget {QFileName}]
$kw(vtkTemp305) SetValue "$DataDir/Data/combq.bin"
$kw(vtkTemp292) AcceptCallback

# Choose the file name/type based on whether we are in parallel mode.
set kw(vtkTemp1000) [$Application GetController]
set nprocs [$kw(vtkTemp1000) GetNumberOfProcesses]

set ext "vts"
if {$nprocs > 1} { set ext "pvts" }

# Write the vtkStructuredGrid.
$kw(vtkTemp1) WriteVTKFile "testStructuredGrid.$ext"

# Delete the old pipeline.
$kw(vtkTemp292) DeleteCallback

# Read the vtkStructuredGrid from the temporary file.
$kw(vtkTemp1) Open "testStructuredGrid.$ext"
set kw(vtkTemp306) [$kw(vtkTemp1) GetCurrentPVSource]
$kw(vtkTemp306) AcceptCallback

# Contour it to produce some actual data for display.
set kw(vtkTemp336) [$kw(vtkTemp1) CreatePVSource Contour]
set kw(vtkTemp342) [$kw(vtkTemp336) GetPVWidget {Scalars}]
$kw(vtkTemp342) SetValue {Density}
set kw(vtkTemp345) [$kw(vtkTemp336) GetPVWidget {Contour Values}]
$kw(vtkTemp345) RemoveAllValues
$kw(vtkTemp345) AddValue 0.4
$kw(vtkTemp345) AddValue 0.5
$kw(vtkTemp345) AddValue 0.6
$kw(vtkTemp345) AddValue 0.7
$kw(vtkTemp336) AcceptCallback

# Fix the size of the image.
set RenWin1 [[[$Application GetMainWindow] GetMainView] GetRenderWindow]
$RenWin1 SetSize 300 300
$RenWin1 Render
update

for {set i  1} {$i < [expr $argc - 1]} {incr i} {
    if {[lindex $argv $i] == "-C"} {
	source [lindex $argv [expr $i + 1]]
	$Application SetExitStatus [CompareImage [[$Application GetMainWindow] GetMainView]]
    }
}

# Delete the temporary file.
catch { file delete -force "testStructuredGrid.$ext" }

if {$nprocs > 1} {
  # Delete the temporary file's pieces.
  for {set i 0} { $i < $nprocs } { incr i } {
  catch { file delete -force "testStructuredGrid$i.vts" }
  }
}
