File: svgdepict.cpp

package info (click to toggle)
openbabel 3.1.1%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 259,620 kB
  • sloc: cpp: 361,957; python: 11,640; ansic: 6,470; perl: 6,010; pascal: 793; php: 529; sh: 226; xml: 97; ruby: 64; makefile: 45; java: 23
file content (62 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <openbabel/babelconfig.h>
#include <openbabel/obconversion.h>
#include <openbabel/mol.h>
 
#include <openbabel/depict/svgpainter.h>
#include <openbabel/depict/depict.h>
 
#include <iostream>
using namespace std;
 
using namespace OpenBabel;
 
int main(int argc, char **argv)
{
  if (argc < 2) {
    std::cerr << "Usage: " << argv[0] << " filename\n"
      "Writes an SVG file containing a 2D depiction for each molecule." << std::endl;
    return -1;
  }
 
  // read a molecule
  OBConversion conv;
  OBFormat *format = conv.FormatFromExt(argv[1]);
  conv.SetInFormat(format);
 
  std::string filename(argv[1]);
  std::ifstream ifs;
  ifs.open(filename.c_str());
  if (!ifs) {
    std::cerr << "Could not open " << filename << std::endl;
    return -1;
  }
 
  string::size_type pos = filename.find_last_of(".");
  if (pos != string::npos)
    filename = filename.substr(0, pos);

  unsigned int count = 1;
  OBMol mol;
  while (conv.Read(&mol, &ifs)) {
    if(!mol.Has2D(true)) {
      cerr << "Molecule " << count << ", " << mol.GetTitle()
           << ", does not have 2D coordinates" << endl;
      continue;
    }
    std::stringstream ss;
    ss << filename << count << ".svg";
    ofstream ofs(ss.str().c_str());
    if(ofs)
    {
      SVGPainter painter(ofs);     
      OBDepict depictor(&painter);
      depictor.DrawMolecule(&mol);   // <svg...> written here
      //depictor.AddAtomLabels(OBDepict::AtomSymmetryClass);
    }                                // </svg> written here 
    //Note that the svg is not complete until the destructor of the SVGPainter has been called
    ofs.close();
    count++;
  }
 
  return 0;
}