ImportToExport

VTKEx/Cxx/IO/ImportToExport

Description

This example imports one of vtk3DSImporter, vtkGLTFImporter, vtkOBJImporter or vtkVRMLImporter and exports the scene using one of vtkOBJExporter

The parameters are the input file(s) and an export file name with extension.

Question

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

Code

ImportToExport.cxx

#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.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 <cctype>    // For to_lower
#include <iterator>  // For prev
#include <set>       // For valid extensions
#include <sstream>   // For stringstream
#include <string>    // For find_last_of()

int main(int argc, char* argv[])
{
  auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  auto renderer = vtkSmartPointer<vtkRenderer>::New();

  if (argc < 3)
  {
    std::cerr << "Expects input file name(s) and an output filename."
              << std::endl;
    return EXIT_FAILURE;
  }

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

  std::set<std::string> inputExtensions{{
      "3ds",
      "glb",
      "gltf",
      "obj",
      "wrl",
  }};
  std::set<std::string> outputExtensions{{
      "glb",
      "gltf",
      "obj",
      "wrl",
      "x3d",
  }};

  // 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 (inputExtensions.find(extension) == inputExtensions.end())
  {
    std::cout << "Invalid input extension.\nValid extensions are: "
              << std::endl;
    for (auto it = inputExtensions.begin(); it != inputExtensions.end(); ++it)
    {
      if (it != std::prev(inputExtensions.end()))
      {
        std::cout << *it << ", ";
      }
      else
      {
        std::cout << *it << std::endl;
      }
    }
    return EXIT_FAILURE;
  }

  if (extension == "wrl")
  {
    auto importer = vtkSmartPointer<vtkVRMLImporter>::New();
    importer->SetFileName(argv[1]);
    importer->SetRenderWindow(renderWindow);
    renderWindow = importer->GetRenderWindow();
    renderer = importer->GetRenderer();
    importer->Read();
    outputFileArgOffset = 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();
    outputFileArgOffset = 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();
    outputFileArgOffset = 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();
    outputFileArgOffset = 4;
  }

  std::string outputFileName = argv[outputFileArgOffset];
  std::string outputExtension{""};
  // Split the file path and extension
  if (outputFileName.find_last_of(".") != std::string::npos)
  {
    outputExtension =
        outputFileName.substr(outputFileName.find_last_of(".") + 1);
    auto pos = outputFileName.rfind(".", outputFileName.length());
    if (pos != std::string::npos)
    {
      outputFileName = outputFileName.substr(0, pos);
    }
  }
  std::transform(outputExtension.begin(), outputExtension.end(),
                 outputExtension.begin(), ::tolower);

  if (outputExtensions.find(outputExtension) == outputExtensions.end())
  {
    std::cout << "Invalid output extension.\nValid extensions are: "
              << std::endl;
    for (auto it = outputExtensions.begin(); it != outputExtensions.end(); ++it)
    {
      if (it != std::prev(outputExtensions.end()))
      {
        std::cout << *it << ", ";
      }
      else
      {
        std::cout << *it << std::endl;
      }
    }
    return EXIT_FAILURE;
  }

  if (outputExtension == "obj")
  {
    std::string exportFileName;
    exportFileName = outputFileName + "." + outputExtension;
    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(outputFileName.c_str());
    std::cout << "Writing " << exportFileName << std::endl;
    exporter->Write();
  }
  else if (outputExtension == "wrl")
  {
    std::string exportFileName;
    exportFileName = outputFileName + "." + outputExtension;
    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 = outputFileName + "." + "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 = outputFileName + "." + outputExtension;
    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 
  vtkvtkCommonCore
  vtkvtkIOExport
  vtkvtkIOExportOpenGL2
  vtkvtkIOExportPDF
  vtkvtkIOImport
  vtkvtkInteractionStyle
  vtkvtkRenderingContextOpenGL2
  vtkvtkRenderingCore
  vtkvtkRenderingFreeType
  vtkvtkRenderingGL2PSOpenGL2
  vtkvtkRenderingOpenGL2 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.