Salome HOME
77c7377f43c5e3147b9b67aeee2abdc81cefa509
[modules/geom.git] / src / XAO / XAO_Xao.cxx
1 // Copyright (C) 2013-2022  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 int Xao::countGroups() const
73 {
74     return m_groups.size();
75 }
76
77 Group* Xao::getGroup(int index)
78
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(XAO::Dimension dim, const std::string& name)
93
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 int Xao::countFields() const
119 {
120     return m_fields.size();
121 }
122
123 XAO::Type Xao::getFieldType(int index)
124
125 {
126     return getField(index)->getType();
127 }
128
129 Field* Xao::getField(int index)
130
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(int index)
145
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(int index)
154
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(int index)
163
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(int index)
172
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(XAO::Type type, XAO::Dimension dim, int nbComponents, const std::string& name)
181
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(XAO::Dimension dim, int nbComponents, const std::string& name)
191
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(XAO::Dimension dim, int nbComponents, const std::string& name)
200
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(XAO::Dimension dim, int nbComponents, const std::string& name)
209
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(XAO::Dimension dim, int nbComponents, const std::string& name)
218
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 bool Xao::exportXAO(const std::string& fileName, const std::string& shapeFileName)
245 {
246     return XaoExporter::saveToFile(this, fileName, shapeFileName);
247 }
248
249 const std::string Xao::getXML()
250 {
251     return XaoExporter::saveToXml(this);
252 }
253
254 bool Xao::importXAO(const std::string& fileName)
255 {
256     return XaoExporter::readFromFile(fileName, this);
257 }
258
259 bool Xao::setXML(const std::string& xml)
260 {
261     return XaoExporter::setXML(xml, this);
262 }
263
264 void Xao::checkGeometry() const
265 {
266     if (m_geometry == NULL)
267         throw XAO_Exception("Geometry is null");
268 }
269
270 void Xao::checkGroupIndex(int index) const
271 {
272     if (index >= 0 && index < countGroups())
273         return;
274
275     throw XAO_Exception(MsgBuilder() << "Group index is out of range [0, "
276                                      << countGroups()-1 << "]: " << index);
277 }
278
279 void Xao::checkFieldIndex(int index) const
280 {
281     if (index >= 0 && index < countFields())
282         return;
283
284     throw XAO_Exception(MsgBuilder() << "Field index is out of range [0, "
285                                      << countFields()-1 << "]: " << index);
286 }
287
288 void Xao::checkGroupDimension(XAO::Dimension dim) const
289 {
290     if (dim == XAO::WHOLE)
291         throw XAO_Exception(MsgBuilder() << "Invalid dimension for group: " << dim);
292 }