~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to src/formats/chemtoolformat.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
Copyright (C) 2003 by Michael Banck <mbanck@gmx.net>
 
3
Some portions Copyright (C) 2004 by Chris Morley
 
4
 
 
5
This file is part of the Open Babel project.
 
6
For more information, see <http://openbabel.sourceforge.net/>
 
7
 
 
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.
 
11
 
 
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
***********************************************************************/
 
17
 
 
18
#include "mol.h"
 
19
#include "obconversion.h"
 
20
#include <stdlib.h>
 
21
 
 
22
using namespace std;
 
23
namespace OpenBabel
 
24
{
 
25
 
 
26
class CHTFormat : public OBFormat
 
27
{
 
28
public:
 
29
    //Register this format type ID
 
30
    CHTFormat()
 
31
    {
 
32
        OBConversion::RegisterFormat("cht",this);
 
33
    }
 
34
 
 
35
    virtual const char* Description() //required
 
36
    {
 
37
        return
 
38
            "Chemtool format\n \
 
39
            No comments yet\n";
 
40
    };
 
41
 
 
42
  virtual const char* SpecificationURL()
 
43
  {return "http://ruby.chemie.uni-freiburg.de/~martin/chemtool/chemtool.html";}; //optional
 
44
 
 
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()
 
48
    {
 
49
        return NOTREADABLE;
 
50
    };
 
51
 
 
52
    ////////////////////////////////////////////////////
 
53
    /// The "API" interface functions
 
54
    virtual bool WriteMolecule(OBBase* pOb, OBConversion* pConv);
 
55
 
 
56
    ////////////////////////////////////////////////////
 
57
    /// The "Convert" interface functions
 
58
    virtual bool WriteChemObject(OBConversion* pConv)
 
59
    {
 
60
        //Retrieve the target OBMol
 
61
        OBBase* pOb = pConv->GetChemObject();
 
62
        OBMol* pmol = dynamic_cast<OBMol*> (pOb);
 
63
        bool ret=false;
 
64
        if(pmol)
 
65
            ret=WriteMolecule(pmol,pConv);
 
66
 
 
67
        std::string auditMsg = "OpenBabel::Write molecule ";
 
68
        std::string description(Description());
 
69
        auditMsg += description.substr( 0, description.find('\n') );
 
70
        obErrorLog.ThrowError(__FUNCTION__,
 
71
                              auditMsg,
 
72
                              obAuditMsg);
 
73
 
 
74
        delete pOb;
 
75
        return ret;
 
76
    };
 
77
};
 
78
 
 
79
//Make an instance of the format class
 
80
CHTFormat theCHTFormat;
 
81
 
 
82
/////////////////////////////////////////////////////////////////
 
83
 
 
84
bool CHTFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv)
 
85
{
 
86
    OBMol* pmol = dynamic_cast<OBMol*>(pOb);
 
87
    if(pmol==NULL)
 
88
        return false;
 
89
 
 
90
    //Define some references so we can use the old parameter names
 
91
    ostream &ofs = *pConv->GetOutStream();
 
92
    OBMol &mol = *pmol;
 
93
 
 
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;
 
100
    OBBond *bond;
 
101
 
 
102
    ofs << "Chemtool Version 1.4" << endl;
 
103
 
 
104
    // get the geometry
 
105
    w = 0;
 
106
    h = 0;
 
107
    vector<OBNodeBase*>::iterator i;
 
108
    for (atom = mol.BeginAtom(i); atom; atom = mol.NextAtom(i))
 
109
    {
 
110
        x = (int)(atom->GetX()) * conv_factor;
 
111
        y = (int)(atom->GetY()) * conv_factor;
 
112
        if (x > w)
 
113
            w = x;
 
114
        if (y > h)
 
115
            h = y;
 
116
        if (atom->GetAtomicNum() != 6)
 
117
            natoms++;
 
118
    }
 
119
    ofs << "geometry " << w * 1.1 << " " << h * 1.1 << endl;
 
120
 
 
121
    // write out bonds
 
122
    ofs << "bonds "<< mol.NumBonds() << endl;
 
123
    vector<OBEdgeBase*>::iterator j;
 
124
    for(bond = mol.BeginBond(j); bond; bond = mol.NextBond(j))
 
125
    {
 
126
        bondtype = 0;
 
127
        atom1 = bond->GetBeginAtom();
 
128
        atom2 = bond->GetEndAtom();
 
129
        if (bond->GetBO() == 2)
 
130
            bondtype = 1;
 
131
        if (bond->GetBO() == 3)
 
132
            bondtype = 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),
 
139
                bondtype);
 
140
        ofs << buffer << endl;
 
141
    }
 
142
 
 
143
    // start over, write additional atoms
 
144
    ofs << "atoms " << natoms << endl;
 
145
    for (atom = mol.BeginAtom(i); atom; atom = mol.NextAtom(i))
 
146
    {
 
147
        // Carbon does not need to be treated
 
148
        if (atom->GetAtomicNum() != 6)
 
149
        {
 
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
 
155
                   );
 
156
            ofs << buffer << endl;
 
157
        }
 
158
    }
 
159
 
 
160
    // We don't have any splines to write
 
161
    ofs << "splines 0" << endl;
 
162
 
 
163
    return true;
 
164
}
 
165
 
 
166
} //namespace OpenBabel