Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / XAO / XAO_Xao.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Nathalie Gore (OpenCascade), Frederic Pons (OpenCascade)
20
21 #include <iostream>
22 #include "XAO_XaoUtils.hxx"
23 #include "XAO_Xao.hxx"
24 #include "XAO_Geometry.hxx"
25 #include "XAO_Group.hxx"
26 #include "XAO_Field.hxx"
27 #include "XAO_IntegerField.hxx"
28 #include "XAO_BooleanField.hxx"
29 #include "XAO_DoubleField.hxx"
30 #include "XAO_StringField.hxx"
31 #include "XAO_XaoExporter.hxx"
32
33 #include <libxml/xmlstring.h>
34
35 using namespace XAO;
36
37 const xmlChar* C_XAO_VERSION = (xmlChar*)"1.0";
38
39 Xao::Xao()
40 {
41     m_author = "";
42     m_version = (char*)C_XAO_VERSION;
43     m_geometry = NULL;
44 }
45
46 Xao::Xao(const std::string& author, const std::string& version)
47 {
48     m_author = author;
49     m_version = version;
50     m_geometry = NULL;
51 }
52
53 Xao::~Xao()
54 {
55     if (m_geometry != NULL)
56     {
57         delete m_geometry;
58         m_geometry = NULL;
59     }
60
61     for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
62     {
63         delete (*it);
64     }
65
66     for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it)
67     {
68         delete (*it);
69     }
70 }
71
72 const int Xao::countGroups() const
73 {
74     return m_groups.size();
75 }
76
77 Group* Xao::getGroup(const int& index)
78 throw (XAO_Exception)
79 {
80     checkGroupIndex(index);
81
82     int i = 0;
83     for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it, ++i)
84     {
85         if (i == index)
86             return (*it);
87     }
88
89     return NULL;
90 }
91
92 Group* Xao::addGroup(const XAO::Dimension& dim, const std::string& name)
93 throw (XAO_Exception)
94 {
95     checkGeometry();
96     checkGroupDimension(dim);
97
98     Group* group = new Group(dim, m_geometry->countElements(dim), name);
99     m_groups.push_back(group);
100     return group;
101 }
102
103 bool Xao::removeGroup(Group* group)
104 {
105     int nb = countGroups();
106     m_groups.remove(group);
107
108     bool res = (nb-1 == countGroups());
109     if (res)
110     {
111         delete group;
112         group = NULL;
113     }
114
115     return res;
116 }
117
118 const int Xao::countFields() const
119 {
120     return m_fields.size();
121 }
122
123 const XAO::Type Xao::getFieldType(const int& index)
124 throw (XAO_Exception)
125 {
126     return getField(index)->getType();
127 }
128
129 Field* Xao::getField(const int& index)
130 throw (XAO_Exception)
131 {
132     checkFieldIndex(index);
133
134     int i = 0;
135     for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it, ++i)
136     {
137         if (i == index)
138             return (*it);
139     }
140
141     throw XAO_Exception("Field not found.");
142 }
143
144 BooleanField* Xao::getBooleanField(const int& index)
145 throw (XAO_Exception)
146 {
147     Field* field = getField(index);
148     if (field->getType() != XAO::BOOLEAN)
149         throw XAO_Exception(MsgBuilder() << "Field " << index << " is not a boolean field.");
150     return (BooleanField*)field;
151 }
152
153 DoubleField* Xao::getDoubleField(const int& index)
154 throw (XAO_Exception)
155 {
156     Field* field = getField(index);
157     if (field->getType() != XAO::DOUBLE)
158         throw XAO_Exception(MsgBuilder() << "Field " << index << " is not a double field.");
159     return (DoubleField*)field;
160 }
161
162 IntegerField* Xao::getIntegerField(const int& index)
163 throw (XAO_Exception)
164 {
165     Field* field = getField(index);
166     if (field->getType() != XAO::INTEGER)
167         throw XAO_Exception(MsgBuilder() << "Field " << index << " is not an integer field.");
168     return (IntegerField*)field;
169 }
170
171 StringField* Xao::getStringField(const int& index)
172 throw (XAO_Exception)
173 {
174     Field* field = getField(index);
175     if (field->getType() != XAO::STRING)
176         throw XAO_Exception(MsgBuilder() << "Field " << index << " is not a string field.");
177     return (StringField*)field;
178 }
179
180 Field* Xao::addField(const XAO::Type& type, const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
181 throw (XAO_Exception)
182 {
183     checkGeometry();
184     int nbElts = m_geometry->countElements(dim);
185     Field* field = Field::createField(type, dim, nbElts, nbComponents, name);
186     m_fields.push_back(field);
187     return field;
188 }
189
190 IntegerField* Xao::addIntegerField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
191 throw (XAO_Exception)
192 {
193     checkGeometry();
194     int nbElts = m_geometry->countElements(dim);
195     IntegerField* field = new IntegerField(dim, nbElts, nbComponents, name);
196     m_fields.push_back(field);
197     return field;
198 }
199 BooleanField* Xao::addBooleanField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
200 throw (XAO_Exception)
201 {
202     checkGeometry();
203     int nbElts = m_geometry->countElements(dim);
204     BooleanField* field = new BooleanField(dim, nbElts, nbComponents, name);
205     m_fields.push_back(field);
206     return field;
207 }
208 DoubleField* Xao::addDoubleField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
209 throw (XAO_Exception)
210 {
211     checkGeometry();
212     int nbElts = m_geometry->countElements(dim);
213     DoubleField* field = new DoubleField(dim, nbElts, nbComponents, name);
214     m_fields.push_back(field);
215     return field;
216 }
217 StringField* Xao::addStringField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
218 throw (XAO_Exception)
219 {
220     checkGeometry();
221     int nbElts = m_geometry->countElements(dim);
222     StringField* field = new StringField(dim, nbElts, nbComponents, name);
223     m_fields.push_back(field);
224     return field;
225 }
226
227
228
229 bool Xao::removeField(Field* field)
230 {
231     int nb = countFields();
232     m_fields.remove(field);
233
234     bool res = (nb-1 == countFields());
235     if (res)
236     {
237         delete field;
238         field = NULL;
239     }
240
241     return res;
242 }
243
244 const bool Xao::exportXAO(const std::string& fileName)
245 {
246     return XaoExporter::saveToFile(this, fileName);
247 }
248
249 const std::string Xao::getXML()
250 {
251     return XaoExporter::saveToXml(this);
252 }
253
254 const bool Xao::importXAO(const std::string& fileName)
255 {
256     return XaoExporter::readFromFile(fileName, this);
257 }
258
259 const bool Xao::setXML(const std::string& xml)
260 {
261     return XaoExporter::setXML(xml, this);
262 }
263
264 void Xao::checkGeometry() const
265 throw(XAO_Exception)
266 {
267     if (m_geometry == NULL)
268         throw XAO_Exception("Geometry is null");
269 }
270
271 void Xao::checkGroupIndex(const int& index) const
272 throw(XAO_Exception)
273 {
274     if (index >= 0 && index < countGroups())
275         return;
276
277     throw XAO_Exception(MsgBuilder() << "Group index is out of range [0, "
278                                      << countGroups()-1 << "]: " << index);
279 }
280
281 void Xao::checkFieldIndex(const int& index) const
282 throw(XAO_Exception)
283 {
284     if (index >= 0 && index < countFields())
285         return;
286
287     throw XAO_Exception(MsgBuilder() << "Field index is out of range [0, "
288                                      << countFields()-1 << "]: " << index);
289 }
290
291 void Xao::checkGroupDimension(const XAO::Dimension& dim) const
292 throw(XAO_Exception)
293 {
294     if (dim == XAO::WHOLE)
295         throw XAO_Exception(MsgBuilder() << "Invalid dimension for group: " << dim);
296 }