# ReadExodusData

VTKExamples/Cxx/IO/ReadExodusData

### Description

The exmaple uses vtkExodusIIReader to read an ExodusII file. The nodal variable to read is the second argument. The nodal variable is displayed with a color map.

Other Languages

See (Python)

### Code

ReadExodusData.cxx

    #include <vtkNew.h>
    #include <vtkNamedColors.h>
    #include <vtkExodusIIReader.h>
    #include <vtkCompositeDataGeometryFilter.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkCompositeDataGeometryFilter.h>

    int main (int argc, char *argv[])
    {
      vtkNew<vtkNamedColors>  colors;

      if (argc < 3)
      {
        std::cout << "Ysage: " << argv[0] << " exodus_file.e nodal_variable";
        return EXIT_FAILURE;
      }

      // Read Exodus Data
      vtkNew<vtkExodusIIReader> reader;
      reader->SetFileName(argv[1]);
      reader->UpdateInformation();
      reader->SetTimeStep(10);
      reader->SetAllArrayStatus(vtkExodusIIReader::NODAL, 1);  // enables all NODAL variables

      // Create Geometry
      vtkNew<vtkCompositeDataGeometryFilter> geometry;
      geometry->SetInputConnection(0, reader->GetOutputPort(0));

      // Mapper
      vtkNew<vtkPolyDataMapper> mapper;
      mapper->SetInputConnection(geometry->GetOutputPort());
      mapper->SelectColorArray(argv[2]);
      mapper->SetScalarModeToUsePointFieldData();
      mapper->InterpolateScalarsBeforeMappingOn();

      // Actor
      vtkNew<vtkActor> actor;
      actor->SetMapper(mapper);

      // Renderer
      vtkNew<vtkRenderer> renderer;
      renderer->AddViewProp(actor);
      renderer->SetBackground(colors->GetColor3d("DimGray").GetData());

      renderer->GetActiveCamera()->SetPosition(9.0, 9.0, 7.0);
      renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
      renderer->GetActiveCamera()->SetViewUp(0.2, -0.7, 0.7);
      renderer->GetActiveCamera()->SetDistance(14.5);

      // Window and Interactor
      vtkNew<vtkRenderWindow>  window;
      window->AddRenderer(renderer);
      window->SetSize(600, 600);

      vtkNew<vtkRenderWindowInteractor> interactor;
      interactor->SetRenderWindow(window);
      interactor->Initialize();

      // Show the result
      window->Render();
      interactor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ReadExodusData)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkFiltersGeometry
      vtkIOExodus
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ReadExodusData: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(ReadExodusData MACOSX_BUNDLE ReadExodusData.cxx )
      target_link_libraries(ReadExodusData PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ReadExodusData MACOSX_BUNDLE ReadExodusData.cxx )
      target_link_libraries(ReadExodusData PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ReadExodusData
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ReadExodusData

Click here to download ReadExodusData and its CMakeLists.txt file. Once the tarball ReadExodusData.tar has been downloaded and extracted,

    cd ReadExodusData/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:

    ./ReadExodusData

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.