Skip to content

ReadCML

vtk-examples/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 question about this example, please use the VTK Discourse Forum

Code

ReadCML.cxx

#include <vtkActor.h>
#include <vtkCMLMoleculeReader.h>
#include <vtkCamera.h>
#include <vtkMoleculeMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cerr << "Usage: " << argv[0] << " Filename(.cml) e.g. porphyrin.cml"
              << std::endl;
    return EXIT_FAILURE;
  }
  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(0.7);
  actor->GetProperty()->SetSpecular(0.5);
  actor->GetProperty()->SetSpecularPower(20.0);

  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(ren);
  renderWindow->SetWindowName("ReadCML");

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renderWindow);

  ren->AddActor(actor);

  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.12 FATAL_ERROR)

project(ReadCML)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  DomainsChemistry
  DomainsChemistryOpenGL2
  IOChemistry
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ReadCML: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
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}
)

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.