ImportToExport

VTKExamples/Cxx/IO/ImportToExport

Description

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

ImportToExport.cxx

#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>

// Importers
#include <vtk3DSImporter.h>
#include <vtkGLTFImporter.h>
#include <vtkOBJImporter.h>
#include <vtkVRMLImporter.h>

// Exporters
#include <vtkGLTFExporter.h>
#include <vtkIVExporter.h>
#include <vtkOBJExporter.h>
#include <vtkOOGLExporter.h>
#include <vtkRIBExporter.h>
#include <vtkSVGExporter.h>
#include <vtkVRMLExporter.h>
#include <vtkX3DExporter.h>

#include <algorithm> // For transform()
#include <string> // For find_last_of()
#include <cctype> // For to_lower
#include <sstream> // For stringstream
int main(int argc, char *argv[])
{
  auto renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  auto renderer =
    vtkSmartPointer<vtkRenderer>::New();

  std::string fileName = argv[1];
  std::string extension = "";
  int filePrefixArgOffset = 0; // depends on importer

  // Make the extension lowercase
  std::transform(extension.begin(), extension.end(), extension.begin(),
                 ::tolower);
  if (fileName.find_last_of(".") != std::string::npos)
  {
    extension = fileName.substr(fileName.find_last_of(".") + 1);
  }
  if (extension == "wrl")
  {
    auto importer =
      vtkSmartPointer<vtkVRMLImporter>::New();
    importer->SetFileName(argv[1]);
    importer->SetRenderWindow(renderWindow);
    renderWindow = importer->GetRenderWindow();
    renderer = importer->GetRenderer();
    importer->Read();
    filePrefixArgOffset = 2;
  }
  else if (extension == "3ds")
  {
    auto importer =
      vtkSmartPointer<vtk3DSImporter>::New();
    importer->SetFileName(argv[1]);
    importer->SetRenderWindow(renderWindow);
    importer->ComputeNormalsOn();
    renderWindow = importer->GetRenderWindow();
    renderer = importer->GetRenderer();
    importer->Read();
    filePrefixArgOffset = 2;
  }
  else if (extension == "gltf" || extension == "glb")
  {
    auto importer =
      vtkSmartPointer<vtkGLTFImporter>::New();
    importer->SetFileName(argv[1]);
    importer->SetRenderWindow(renderWindow);
    renderWindow = importer->GetRenderWindow();
    renderer = importer->GetRenderer();
    importer->Read();
    filePrefixArgOffset = 2;
  }
  else if (extension == "obj")
  {
    auto importer =
      vtkSmartPointer<vtkOBJImporter>::New();
    importer->SetFileName(argv[1]);
    importer->SetFileNameMTL(argv[2]);
    importer->SetTexturePath(argv[3]);
    importer->SetRenderWindow(renderWindow);
    renderWindow = importer->GetRenderWindow();
    renderer = importer->GetRenderer();
    importer->Read();
    filePrefixArgOffset = 4;
  }
  std::string outputExtension = argv[filePrefixArgOffset + 1];

  if (outputExtension == "obj")
  {
    auto exporter =
      vtkSmartPointer<vtkOBJExporter>::New();
    std::stringstream comment;
    comment << "Converted by ImportExport from " << fileName;
    exporter->SetOBJFileComment(comment.str().c_str());
    exporter->SetMTLFileComment(comment.str().c_str());
    exporter->SetActiveRenderer(renderer);
    exporter->SetRenderWindow(renderWindow);
    exporter->SetFilePrefix(argv[filePrefixArgOffset]);
    exporter->Write();
  }
  else if (outputExtension == "wrl")
  {
    std::string exportFileName;
    exportFileName = std::string(argv[filePrefixArgOffset]) + "." + "wrl";
    auto exporter =
      vtkSmartPointer<vtkVRMLExporter>::New();
    exporter->SetFileName(exportFileName.c_str());
    exporter->SetActiveRenderer(renderer);
    exporter->SetRenderWindow(renderWindow);
    std::cout << "Writing " << exportFileName << std::endl;
    exporter->Write();
  }
  else if (outputExtension == "gltf" || outputExtension == "glb")
  {
    std::string exportFileName;
    exportFileName = std::string(argv[filePrefixArgOffset]) + "." + "gltf";
    auto exporter =
      vtkSmartPointer<vtkGLTFExporter>::New();
    exporter->SetFileName(exportFileName.c_str());
    exporter->SetActiveRenderer(renderer);
    exporter->SetRenderWindow(renderWindow);
    std::cout << "Writing " << exportFileName << std::endl;
    exporter->Write();
  }
  else if (outputExtension == "x3d")
  {
    std::string exportFileName;
    exportFileName = std::string(argv[filePrefixArgOffset]) + "." + "x3d";
    auto exporter =
      vtkSmartPointer<vtkX3DExporter>::New();
    exporter->SetFileName(exportFileName.c_str());
    exporter->SetActiveRenderer(renderer);
    exporter->SetRenderWindow(renderWindow);
    std::cout << "Writing " << exportFileName << std::endl;
    exporter->Write();
  }
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImportToExport)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkIOExport
  vtkIOExportOpenGL2
  vtkIOExportPDF
  vtkIOImport
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping ImportToExport: ${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(ImportToExport MACOSX_BUNDLE ImportToExport.cxx )
  target_link_libraries(ImportToExport PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImportToExport MACOSX_BUNDLE ImportToExport.cxx )
  target_link_libraries(ImportToExport PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImportToExport
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build ImportToExport

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

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

./ImportToExport

WINDOWS USERS

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