vtkXMLDataSetWriter.cxx 5.86 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkXMLDataSetWriter.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8 9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkXMLDataSetWriter.h"

17
#include "vtkCallbackCommand.h"
18
#include "vtkDataSet.h"
19
#include "vtkHyperOctree.h"
20
#include "vtkImageData.h"
21
#include "vtkInformation.h"
22 23 24 25 26
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
#include "vtkStructuredGrid.h"
#include "vtkUnstructuredGrid.h"
27
#include "vtkXMLHyperOctreeWriter.h"
28 29 30 31 32
#include "vtkXMLImageDataWriter.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkXMLRectilinearGridWriter.h"
#include "vtkXMLStructuredGridWriter.h"
#include "vtkXMLUnstructuredGridWriter.h"
33
#include "vtkAlgorithmOutput.h"
34 35 36 37 38

vtkStandardNewMacro(vtkXMLDataSetWriter);

//----------------------------------------------------------------------------
vtkXMLDataSetWriter::vtkXMLDataSetWriter()
39
{
40 41 42 43
  // Setup a callback for the internal writer to report progress.
  this->ProgressObserver = vtkCallbackCommand::New();
  this->ProgressObserver->SetCallback(&vtkXMLDataSetWriter::ProgressCallbackFunction);
  this->ProgressObserver->SetClientData(this);
44 45 46 47 48
}

//----------------------------------------------------------------------------
vtkXMLDataSetWriter::~vtkXMLDataSetWriter()
{
49
  this->ProgressObserver->Delete();
50 51 52 53 54 55 56 57 58 59 60
}

//----------------------------------------------------------------------------
void vtkXMLDataSetWriter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
vtkDataSet* vtkXMLDataSetWriter::GetInput()
{
61
  return static_cast<vtkDataSet*>(this->Superclass::GetInput());
62 63 64
}

//----------------------------------------------------------------------------
65
int vtkXMLDataSetWriter::WriteInternal()
66
{
67
  vtkAlgorithmOutput* input = this->GetInputConnection(0, 0);
68
  vtkXMLWriter* writer = 0;
69

70
  // Create a writer based on the data set type.
71
  switch (this->GetInput()->GetDataObjectType())
72
    {
73
    case VTK_UNIFORM_GRID:
74 75 76 77
    case VTK_IMAGE_DATA:
    case VTK_STRUCTURED_POINTS:
      {
      vtkXMLImageDataWriter* w = vtkXMLImageDataWriter::New();
78
      w->SetInputConnection(input);
79 80 81 82 83
      writer = w;
      } break;
    case VTK_STRUCTURED_GRID:
      {
      vtkXMLStructuredGridWriter* w = vtkXMLStructuredGridWriter::New();
84
      w->SetInputConnection(input);
85 86 87 88 89
      writer = w;
      } break;
    case VTK_RECTILINEAR_GRID:
      {
      vtkXMLRectilinearGridWriter* w = vtkXMLRectilinearGridWriter::New();
90
      w->SetInputConnection(input);
91 92 93 94 95
      writer = w;
      } break;
    case VTK_UNSTRUCTURED_GRID:
      {
      vtkXMLUnstructuredGridWriter* w = vtkXMLUnstructuredGridWriter::New();
96
      w->SetInputConnection(input);
97 98 99 100 101
      writer = w;
      } break;
    case VTK_POLY_DATA:
      {
      vtkXMLPolyDataWriter* w = vtkXMLPolyDataWriter::New();
102
      w->SetInputConnection(input);
103 104
      writer = w;
      } break;
105 106 107
    case VTK_HYPER_OCTREE:
      {
      vtkXMLHyperOctreeWriter* w = vtkXMLHyperOctreeWriter::New();
108
      w->SetInputConnection(input);
109 110
      writer = w;
      } break;
111
    }
112

113
  // Make sure we got a valid writer for the data set.
114
  if (!writer)
115 116
    {
    vtkErrorMacro("Cannot write dataset type: "
117 118
                  << this->GetInput()->GetDataObjectType() << " which is a "
                  << this->GetInput()->GetClassName());
119 120
    return 0;
    }
121

122 123 124 125 126 127 128 129
  // Copy the settings to the writer.
  writer->SetDebug(this->GetDebug());
  writer->SetFileName(this->GetFileName());
  writer->SetByteOrder(this->GetByteOrder());
  writer->SetCompressor(this->GetCompressor());
  writer->SetBlockSize(this->GetBlockSize());
  writer->SetDataMode(this->GetDataMode());
  writer->SetEncodeAppendedData(this->GetEncodeAppendedData());
130 131
  writer->SetHeaderType(this->GetHeaderType());
  writer->SetIdType(this->GetIdType());
132
  writer->AddObserver(vtkCommand::ProgressEvent, this->ProgressObserver);
133

134 135
  // Try to write.
  int result = writer->Write();
136

137
  // Cleanup.
138
  writer->RemoveObserver(this->ProgressObserver);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  writer->Delete();
  return result;
}

//----------------------------------------------------------------------------
const char* vtkXMLDataSetWriter::GetDataSetName()
{
  return "DataSet";
}

//----------------------------------------------------------------------------
const char* vtkXMLDataSetWriter::GetDefaultFileExtension()
{
  return "vtk";
}
154 155 156 157 158 159

//----------------------------------------------------------------------------
void vtkXMLDataSetWriter::ProgressCallbackFunction(vtkObject* caller,
                                                   unsigned long,
                                                   void* clientdata, void*)
{
160
  vtkAlgorithm* w = vtkAlgorithm::SafeDownCast(caller);
161
  if (w)
162 163 164 165 166 167
    {
    reinterpret_cast<vtkXMLDataSetWriter*>(clientdata)->ProgressCallback(w);
    }
}

//----------------------------------------------------------------------------
168
void vtkXMLDataSetWriter::ProgressCallback(vtkAlgorithm* w)
169
{
170
  float width = this->ProgressRange[1] - this->ProgressRange[0];
171 172 173
  float internalProgress = w->GetProgress();
  float progress = this->ProgressRange[0] + internalProgress*width;
  this->UpdateProgressDiscrete(progress);
174
  if (this->AbortExecute)
175 176 177 178
    {
    w->SetAbortExecute(1);
    }
}
179

180
//----------------------------------------------------------------------------
181 182 183 184 185 186
int vtkXMLDataSetWriter::FillInputPortInformation(
  int vtkNotUsed(port), vtkInformation* info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  return 1;
}