# VRMLImporter

VTKExamples/Cxx/IO/VRMLImporter

### Description

This example loads a wrl file and displays it on the screen. An example file is here.

Seealso

VRMLImporterDemo shows how to add additional pipeline elements to imported polydata.

Warning

The vtkVRMLImporter only imports VRML 2.0 files.

### Code

VRMLImporter.cxx

    #include <vtkSmartPointer.h>
    #include <vtkVRMLImporter.h>

    #include <vtkActorCollection.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>

    #include <vtkNamedColors.h>

    int main ( int argc, char *argv[])
    {
      if(argc < 2)
      {
        std::cout << "Required arguments: Filename" << std::endl;
        return EXIT_FAILURE;
      }

      std::string filename = argv[1];
      std::cout << "Reading " << filename << std::endl;

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      // VRML Import
      vtkSmartPointer<vtkVRMLImporter> importer =
        vtkSmartPointer<vtkVRMLImporter>::New();
      importer->SetFileName ( filename.c_str() );
      importer->SetRenderWindow(renderWindow);
      importer->Update();

      vtkSmartPointer<vtkActorCollection> actors =
        vtkSmartPointer<vtkActorCollection>::New();
      actors = renderer->GetActors();
      std::cout << "There are " << actors->GetNumberOfItems() << " actors" << std::endl;

      renderer->SetBackground(colors->GetColor3d("Burlywood").GetData());
      renderWindow->SetSize(512, 512);
      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(VRMLImporter)

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

### Download and Build VRMLImporter

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

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

    ./VRMLImporter

WINDOWS USERS

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