Skip to content


# XGMLReader

VTKEx/Cxx/InfoVis/XGMLReader

### Description

This example reads and displays the graph from a gml file. You can test this with VTKData/Data/Infovis/fsm.gml.

Other languages

See (CSharp)

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

XGMLReader.cxx

    #include <vtkPolyData.h>
    #include <vtkXGMLReader.h>
    #include <vtkSmartPointer.h>
    #include <vtkUndirectedGraph.h>
    #include <vtkGraphLayoutStrategy.h>
    #include <vtkGraphLayoutView.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkSimple2DLayoutStrategy.h>

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

      std::string inputFilename = argv[1];

      vtkSmartPointer<vtkXGMLReader> reader =
        vtkSmartPointer<vtkXGMLReader>::New();
      reader->SetFileName(inputFilename.c_str());
      reader->Update();

      vtkUndirectedGraph* g = reader->GetOutput();

      vtkSmartPointer<vtkGraphLayoutView> graphLayoutView =
        vtkSmartPointer<vtkGraphLayoutView>::New();
      graphLayoutView->AddRepresentationFromInput(g);
      graphLayoutView->SetLayoutStrategy("Simple 2D");
      graphLayoutView->ResetCamera();
      graphLayoutView->Render();

      graphLayoutView->GetInteractor()->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(XGMLReader)

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

### Download and Build XGMLReader

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

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

    ./XGMLReader

WINDOWS USERS

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