DumpXMLFile
Repository source: DumpXMLFile
Description¶
This example reports the cell, cell data and point data contained within a VTK XML or legacy file.
Note
This original source code for this example is here.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
DumpXMLFile.cxx
//
// DumpXMLFile - report on the contents of an XML or legacy vtk file
//  Usage: DumpXMLFile XMLFile1 XMLFile2 ...
//         where
//         XMLFile is a vtk XML file of type .vtu, .vtp, .vts, .vtr,
//         .vti, .vto
//
#include <vtkCellData.h>
#include <vtkCellTypes.h>
#include <vtkDataSet.h>
#include <vtkDataSetReader.h>
#include <vtkFieldData.h>
#include <vtkImageData.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkRectilinearGrid.h>
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLCompositeDataReader.h>
#include <vtkXMLImageDataReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLReader.h>
#include <vtkXMLRectilinearGridReader.h>
#include <vtkXMLStructuredGridReader.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtksys/SystemTools.hxx>
#include <map>
namespace {
template <class TReader> vtkDataSet* ReadAnXMLFile(const char* fileName)
{
  vtkNew<TReader> reader;
  reader->SetFileName(fileName);
  reader->Update();
  reader->GetOutput()->Register(reader);
  return dynamic_cast<vtkDataSet*>(reader->GetOutput());
}
} // namespace
int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cerr << "Usage: " << argv[0] << " XMLFile1 XMLFile2 ..." << std::endl;
    return EXIT_FAILURE;
  }
  // Process each file on the command line.
  int f = 1;
  while (f < argc)
  {
    vtkSmartPointer<vtkDataSet> dataSet;
    std::string extension =
        vtksys::SystemTools::GetFilenameLastExtension(argv[f]);
    // Dispatch based on the file extension.
    if (extension == ".vtu")
    {
      dataSet = ReadAnXMLFile<vtkXMLUnstructuredGridReader>(argv[f]);
    }
    else if (extension == ".vtp")
    {
      dataSet = ReadAnXMLFile<vtkXMLPolyDataReader>(argv[f]);
    }
    else if (extension == ".vts")
    {
      dataSet = ReadAnXMLFile<vtkXMLStructuredGridReader>(argv[f]);
    }
    else if (extension == ".vtr")
    {
      dataSet = ReadAnXMLFile<vtkXMLRectilinearGridReader>(argv[f]);
    }
    else if (extension == ".vti")
    {
      dataSet = ReadAnXMLFile<vtkXMLImageDataReader>(argv[f]);
    }
    else if (extension == ".vtk")
    {
      dataSet = ReadAnXMLFile<vtkDataSetReader>(argv[f]);
    }
    else
    {
      std::cerr << argv[0] << " Unknown extension: " << extension << std::endl;
      return EXIT_FAILURE;
    }
    int numberOfCells = dataSet->GetNumberOfCells();
    int numberOfPoints = dataSet->GetNumberOfPoints();
    // Generate a report.
    std::cout << "------------------------" << std::endl;
    std::cout << argv[f] << std::endl
              << " contains a " << std::endl
              << dataSet->GetClassName() << " that has " << numberOfCells
              << " cells" << " and " << numberOfPoints << " points."
              << std::endl;
    typedef std::map<int, int> CellContainer;
    CellContainer cellMap;
    for (int i = 0; i < numberOfCells; i++)
    {
      cellMap[dataSet->GetCellType(i)]++;
    }
    CellContainer::const_iterator it = cellMap.begin();
    while (it != cellMap.end())
    {
      std::cout << "\tCell type "
                << vtkCellTypeUtilities::GetClassNameFromTypeId(it->first)
                << " occurs " << it->second << " times." << std::endl;
      ++it;
    }
    // Now check for point data.
    vtkPointData* pd = dataSet->GetPointData();
    if (pd)
    {
      std::cout << " contains point data with " << pd->GetNumberOfArrays()
                << " arrays." << std::endl;
      for (int i = 0; i < pd->GetNumberOfArrays(); i++)
      {
        std::cout << "\tArray " << i << " is named "
                  << (pd->GetArrayName(i) ? pd->GetArrayName(i) : "NULL")
                  << " has " << pd->GetArray(i)->GetNumberOfTuples()
                  << " tuples" << " with "
                  << pd->GetArray(i)->GetNumberOfComponents() << " components"
                  << " of type " << pd->GetArray(i)->GetClassName()
                  << std::endl;
      }
    }
    // Now check for cell data.
    vtkCellData* cd = dataSet->GetCellData();
    if (cd)
    {
      std::cout << " contains cell data with " << cd->GetNumberOfArrays()
                << " arrays." << std::endl;
      for (int i = 0; i < cd->GetNumberOfArrays(); i++)
      {
        std::cout << "\tArray " << i << " is named "
                  << (cd->GetArrayName(i) ? cd->GetArrayName(i) : "NULL")
                  << std::endl;
      }
    }
    // Now check for field data.
    if (dataSet->GetFieldData())
    {
      std::cout << " contains field data with "
                << dataSet->GetFieldData()->GetNumberOfArrays() << " arrays."
                << std::endl;
      for (int i = 0; i < dataSet->GetFieldData()->GetNumberOfArrays(); i++)
      {
        std::cout
            << "\tArray " << i << " is named "
            << dataSet->GetFieldData()->GetArray(i)->GetName() << " has "
            << dataSet->GetFieldData()->GetArray(i)->GetNumberOfTuples()
            << " tuples" << " with "
            << dataSet->GetFieldData()->GetArray(i)->GetNumberOfComponents()
            << " components" << " of type "
            << dataSet->GetFieldData()->GetArray(i)->GetClassName()
            << std::endl;
      }
    }
    f++;
  }
  return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(DumpXMLFile)
find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOLegacy
  IOXML
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "DumpXMLFile: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(DumpXMLFile MACOSX_BUNDLE DumpXMLFile.cxx )
  target_link_libraries(DumpXMLFile PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS DumpXMLFile
  MODULES ${VTK_LIBRARIES}
)
Download and Build DumpXMLFile¶
Click here to download DumpXMLFile and its CMakeLists.txt file. Once the tarball DumpXMLFile.tar has been downloaded and extracted,
cd DumpXMLFile/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./DumpXMLFile
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.