Salome HOME
Example of a python component from Salome tutorial
[samples/atomgen.git] / src / ATOMGEN / ATOMGEN_XmlParser.py
1 #  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21
22 import xml.dom.minidom
23 from ATOMGEN_Data import *
24
25 __doc_name__  = "Atomic"
26 __doc_tag__   = "document"
27 __mol_tag__   = "molecule"
28 __atom_tag__  = "atom"
29 __name_attr__ = "name"
30 __x_attr__    = "x"
31 __y_attr__    = "y"
32 __z_attr__    = "z"
33
34 def readXmlFile( fileName ):
35     """
36     Reads the atomic data from the XML file
37     """
38     data = []
39     doc = xml.dom.minidom.parse( fileName )
40     if str( doc.doctype.name ) != __doc_name__:
41         return # wrong file format
42     docnode = doc.getElementsByTagName( __doc_tag__ )
43     if not docnode:
44         return # wrong file format
45     mols = docnode[0].getElementsByTagName( __mol_tag__ )
46     for mol in mols:
47         c_name = str( mol.getAttribute( __name_attr__ ) )
48         ac =  Molecule( c_name )
49         atoms = mol.getElementsByTagName( __atom_tag__ )
50         for atom in atoms:
51             a_name = str( atom.getAttribute( __name_attr__ ) )
52             x = float( atom.getAttribute( __x_attr__ ) )
53             y = float( atom.getAttribute( __y_attr__ ) )
54             z = float( atom.getAttribute( __z_attr__ ) )
55             ac.addAtom( Atom( a_name, x, y, z ) )
56         data.append( ac )
57     return data
58
59 def writeXmlFile( filename, data ):
60     """
61     Writes the atomic data to the XML file
62     """
63     if not data:
64         raise Exception( "Bad data" )
65     doc = xml.dom.minidom.Document()
66     doc.appendChild(xml.dom.minidom.DocumentType( __doc_name__ ) )
67     docnode = doc.appendChild( doc.createElement( __doc_tag__ ) )
68     for mol in data:
69         molnode = doc.createElement( __mol_tag__ )
70         molnode.setAttribute( __name_attr__, mol.name )
71         for atom in mol.atoms:
72             atomnode = doc.createElement( __atom_tag__ )
73             atomnode.setAttribute( __name_attr__, atom.name )
74             atomnode.setAttribute( __x_attr__, str( atom.x ) )
75             atomnode.setAttribute( __y_attr__, str( atom.y ) )
76             atomnode.setAttribute( __z_attr__, str( atom.z ) )
77             molnode.appendChild( atomnode )
78         docnode.appendChild( molnode )
79     file = open( filename, "w" )
80     file.write( doc.toprettyxml() )
81     file.close()
82     pass