# $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

# Create a non-trivial vtkPolyData.
set kw(vtkTemp243) [$kw(vtkTemp1) CreatePVSource SphereSource]
set kw(vtkTemp259) [$kw(vtkTemp243) GetPVWidget {Theta Resolution}]
$kw(vtkTemp259) SetValue 36
set kw(vtkTemp268) [$kw(vtkTemp243) GetPVWidget {Phi Resolution}]
$kw(vtkTemp268) SetValue 36
$kw(vtkTemp243) AcceptCallback
update

# Give it some scalar data.
set kw(vtkTemp295) [$kw(vtkTemp1) CreatePVSource ElevationFilter]
set kw(vtkTemp999) [$kw(vtkTemp295) GetPVWidget {Line}]
$kw(vtkTemp999) SetPoint1 0 0 0
$kw(vtkTemp999) SetPoint2 0 0 1
$kw(vtkTemp295) 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 "vtp"
if {$nprocs > 1} { set ext "pvtp" }

# Write the vtkPolyData.
$kw(vtkTemp1) WriteVTKFile "testPolyData.$ext"

# Delete the old pipeline.
$kw(vtkTemp295) DeleteCallback
$kw(vtkTemp243) DeleteCallback

# Read the vtkPolyData from the temporary file.
$kw(vtkTemp1) Open "testPolyData.$ext"
set kw(vtkTemp337) [$kw(vtkTemp1) GetCurrentPVSource]
$kw(vtkTemp337) AcceptCallback
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 "testPolyData.$ext" }

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