# $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 an unstructured grid data set.
$kw(vtkTemp1) Open "$DataDir/Data/vox8_bin.vtk"

# 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 "vtu"
if {$nprocs > 1} { set ext "pvtu" }

# Write the vtkUnstructuredGrid.
$kw(vtkTemp1) WriteVTKFile "testUnstructuredGrid.$ext"

# Delete the old pipeline.
set kw(vtkTemp257) [$kw(vtkTemp1) GetCurrentPVSource]
$kw(vtkTemp257) DeleteCallback

# Read the vtkUnstructuredGrid from the temporary file.
$kw(vtkTemp1) Open "testUnstructuredGrid.$ext"
set kw(vtkTemp291) [$kw(vtkTemp1) GetCurrentPVSource]
$kw(vtkTemp291) AcceptCallback

set kw(vtkTemp841) [$kw(vtkTemp1) CreatePVSource DataSetTriangleFilter]
$kw(vtkTemp841) AcceptCallback
[$kw(vtkTemp841) GetPVOutput] DrawSurface

update

# 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 "testUnstructuredGrid.$ext" }

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