1
/**********************************************************************
2
Copyright (C) 2003 by Michael Banck <mbanck@gmx.net>
3
Some portions Copyright (C) 2004 by Chris Morley
5
This file is part of the Open Babel project.
6
For more information, see <http://openbabel.sourceforge.net/>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation version 2 of the License.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
***********************************************************************/
19
#include "obconversion.h"
26
class CHTFormat : public OBFormat
29
//Register this format type ID
32
OBConversion::RegisterFormat("cht",this);
35
virtual const char* Description() //required
42
virtual const char* SpecificationURL()
43
{return "http://ruby.chemie.uni-freiburg.de/~martin/chemtool/chemtool.html";}; //optional
45
//Flags() can return be any the following combined by | or be omitted if none apply
46
// NOTREADABLE READONEONLY NOTWRITABLE WRITEONEONLY
47
virtual unsigned int Flags()
52
////////////////////////////////////////////////////
53
/// The "API" interface functions
54
virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
56
////////////////////////////////////////////////////
57
/// The "Convert" interface functions
58
virtual bool WriteChemObject(OBConversion* pConv)
60
//Retrieve the target OBMol
61
OBBase* pOb = pConv->GetChemObject();
62
OBMol* pmol = dynamic_cast<OBMol*> (pOb);
65
ret=WriteMolecule(pmol,pConv);
67
std::string auditMsg = "OpenBabel::Write molecule ";
68
std::string description(Description());
69
auditMsg += description.substr( 0, description.find('\n') );
70
obErrorLog.ThrowError(__FUNCTION__,
79
//Make an instance of the format class
80
CHTFormat theCHTFormat;
82
/////////////////////////////////////////////////////////////////
84
bool CHTFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
86
OBMol* pmol = dynamic_cast<OBMol*>(pOb);
90
//Define some references so we can use the old parameter names
91
ostream &ofs = *pConv->GetOutStream();
94
char buffer[BUFF_SIZE];
95
int w, h, x, y; // to calculate the geometry
96
int bondtype; // type of bond
97
int conv_factor = 50; // please adjust
98
int natoms = 0; // number of additional (non-carbon) atoms
99
OBAtom *atom, *atom1, *atom2;
102
ofs << "Chemtool Version 1.4" << endl;
107
vector<OBNodeBase*>::iterator i;
108
for (atom = mol.BeginAtom(i); atom; atom = mol.NextAtom(i))
110
x = (int)(atom->GetX()) * conv_factor;
111
y = (int)(atom->GetY()) * conv_factor;
116
if (atom->GetAtomicNum() != 6)
119
ofs << "geometry " << w * 1.1 << " " << h * 1.1 << endl;
122
ofs << "bonds "<< mol.NumBonds() << endl;
123
vector<OBEdgeBase*>::iterator j;
124
for(bond = mol.BeginBond(j); bond; bond = mol.NextBond(j))
127
atom1 = bond->GetBeginAtom();
128
atom2 = bond->GetEndAtom();
129
if (bond->GetBO() == 2)
131
if (bond->GetBO() == 3)
133
// FIXME: use flag-info, too
134
sprintf(buffer, "%d\t%d\t%d\t%d\t%1d",
135
(int)floor(atom1->GetX() * conv_factor + 0.5),
136
(int)floor(atom1->GetY() * conv_factor + 0.5),
137
(int)floor(atom2->GetX() * conv_factor + 0.5),
138
(int)floor(atom2->GetY() * conv_factor + 0.5),
140
ofs << buffer << endl;
143
// start over, write additional atoms
144
ofs << "atoms " << natoms << endl;
145
for (atom = mol.BeginAtom(i); atom; atom = mol.NextAtom(i))
147
// Carbon does not need to be treated
148
if (atom->GetAtomicNum() != 6)
150
sprintf(buffer, "%d\t%d\t%s\t%d",
151
(int)floor(atom->GetX() * conv_factor + 0.5),
152
(int)floor(atom->GetY() * conv_factor + 0.5),
153
etab.GetSymbol(atom->GetAtomicNum()),
154
-1 // assume centered Text
156
ofs << buffer << endl;
160
// We don't have any splines to write
161
ofs << "splines 0" << endl;
166
} //namespace OpenBabel