Skip to content


# ReadCML

VTKEx/Cxx/IO/ReadCML

## Description

This example uses vtkCMLMoleculeReader to read a Chemistry Markup Language file. CML has been developed by Peter Murray-Rust and Henry Rzepa since 1995. It is the de facto XML for chemistry, accepted by publishers and with more than 1 million lines of Open Source code supporting it.

Info

This example uses the file porphyrin.cml. A description of the molecule Porphyrin is described here.

Other languages

See (Java)

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

ReadCML.cxx

    #include <vtkNew.h>
    #include <vtkCMLMoleculeReader.h>

    #include <vtkActor.h>
    #include <vtkProperty.h>
    #include <vtkCamera.h>
    #include <vtkMolecule.h>
    #include <vtkMoleculeMapper.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>

    #include <vtkNamedColors.h>

    int main(int argc, char *argv[])
    {
      std::string fname(argv[1]);
      vtkNew<vtkCMLMoleculeReader> cmlSource;

      cmlSource->SetFileName(fname.c_str());

      vtkNew<vtkMoleculeMapper> molmapper;
      molmapper->SetInputConnection(cmlSource->GetOutputPort());

      molmapper->UseBallAndStickSettings();

      vtkNew<vtkNamedColors> colors;

      vtkNew<vtkActor> actor;
      actor->SetMapper(molmapper);
      actor->GetProperty()->SetDiffuse(.7);
      actor->GetProperty()->SetSpecular(.5);
      actor->GetProperty()->SetSpecularPower(20.0);

      vtkNew<vtkRenderer> ren;
      vtkNew<vtkRenderWindow> renderWindow;
      renderWindow->AddRenderer(ren);
      vtkNew<vtkRenderWindowInteractor> iren;
      iren->SetRenderWindow(renderWindow);

      ren->AddActor(actor);

      ren->SetBackground(0.0,0.0,0.0);
      renderWindow->SetSize(640, 480);
      renderWindow->Render();
      ren->GetActiveCamera()->Zoom(2.0);
      ren->SetBackground(colors->GetColor3d("Silver").GetData());

      // Finally render the scene
      renderWindow->SetMultiSamples(0);
      renderWindow->GetInteractor()->Initialize();
      renderWindow->GetInteractor()->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ReadCML)

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

### Download and Build ReadCML

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

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

    ./ReadCML

WINDOWS USERS

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