1 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 import xml.dom.minidom
21 from ATOMGEN_Data import *
23 __doc_name__ = "Atomic"
24 __doc_tag__ = "document"
25 __mol_tag__ = "molecule"
27 __name_attr__ = "name"
32 def readXmlFile( fileName ):
34 Reads the atomic data from the XML file
37 doc = xml.dom.minidom.parse( fileName )
38 if str( doc.doctype.name ) != __doc_name__:
39 return # wrong file format
40 docnode = doc.getElementsByTagName( __doc_tag__ )
42 return # wrong file format
43 mols = docnode[0].getElementsByTagName( __mol_tag__ )
45 c_name = str( mol.getAttribute( __name_attr__ ) )
46 ac = Molecule( c_name )
47 atoms = mol.getElementsByTagName( __atom_tag__ )
49 a_name = str( atom.getAttribute( __name_attr__ ) )
50 x = float( atom.getAttribute( __x_attr__ ) )
51 y = float( atom.getAttribute( __y_attr__ ) )
52 z = float( atom.getAttribute( __z_attr__ ) )
53 ac.addAtom( Atom( a_name, x, y, z ) )
57 def writeXmlFile( filename, data ):
59 Writes the atomic data to the XML file
62 raise Exception( "Bad data" )
63 doc = xml.dom.minidom.Document()
64 doc.appendChild(xml.dom.minidom.DocumentType( __doc_name__ ) )
65 docnode = doc.appendChild( doc.createElement( __doc_tag__ ) )
67 molnode = doc.createElement( __mol_tag__ )
68 molnode.setAttribute( __name_attr__, mol.name )
69 for atom in mol.atoms:
70 atomnode = doc.createElement( __atom_tag__ )
71 atomnode.setAttribute( __name_attr__, atom.name )
72 atomnode.setAttribute( __x_attr__, str( atom.x ) )
73 atomnode.setAttribute( __y_attr__, str( atom.y ) )
74 atomnode.setAttribute( __z_attr__, str( atom.z ) )
75 molnode.appendChild( atomnode )
76 docnode.appendChild( molnode )
77 with open(filename, "w") as f:
78 f.write(doc.toprettyxml())