1 // Copyright (C) 2013-2015 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
19 // Author : Nathalie Gore (OpenCascade), Frederic Pons (OpenCascade)
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"
33 #include <libxml/xmlstring.h>
37 const xmlChar* C_XAO_VERSION = (xmlChar*)"1.0";
42 m_version = (char*)C_XAO_VERSION;
46 Xao::Xao(const std::string& author, const std::string& version)
55 if (m_geometry != NULL)
61 for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
66 for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it)
72 const int Xao::countGroups() const
74 return m_groups.size();
77 Group* Xao::getGroup(const int& index)
80 checkGroupIndex(index);
83 for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it, ++i)
92 Group* Xao::addGroup(const XAO::Dimension& dim, const std::string& name)
96 checkGroupDimension(dim);
98 Group* group = new Group(dim, m_geometry->countElements(dim), name);
99 m_groups.push_back(group);
103 bool Xao::removeGroup(Group* group)
105 int nb = countGroups();
106 m_groups.remove(group);
108 bool res = (nb-1 == countGroups());
118 const int Xao::countFields() const
120 return m_fields.size();
123 const XAO::Type Xao::getFieldType(const int& index)
124 throw (XAO_Exception)
126 return getField(index)->getType();
129 Field* Xao::getField(const int& index)
130 throw (XAO_Exception)
132 checkFieldIndex(index);
135 for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it, ++i)
141 throw XAO_Exception("Field not found.");
144 BooleanField* Xao::getBooleanField(const int& index)
145 throw (XAO_Exception)
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;
153 DoubleField* Xao::getDoubleField(const int& index)
154 throw (XAO_Exception)
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;
162 IntegerField* Xao::getIntegerField(const int& index)
163 throw (XAO_Exception)
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;
171 StringField* Xao::getStringField(const int& index)
172 throw (XAO_Exception)
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;
180 Field* Xao::addField(const XAO::Type& type, const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
181 throw (XAO_Exception)
184 int nbElts = m_geometry->countElements(dim);
185 Field* field = Field::createField(type, dim, nbElts, nbComponents, name);
186 m_fields.push_back(field);
190 IntegerField* Xao::addIntegerField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
191 throw (XAO_Exception)
194 int nbElts = m_geometry->countElements(dim);
195 IntegerField* field = new IntegerField(dim, nbElts, nbComponents, name);
196 m_fields.push_back(field);
199 BooleanField* Xao::addBooleanField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
200 throw (XAO_Exception)
203 int nbElts = m_geometry->countElements(dim);
204 BooleanField* field = new BooleanField(dim, nbElts, nbComponents, name);
205 m_fields.push_back(field);
208 DoubleField* Xao::addDoubleField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
209 throw (XAO_Exception)
212 int nbElts = m_geometry->countElements(dim);
213 DoubleField* field = new DoubleField(dim, nbElts, nbComponents, name);
214 m_fields.push_back(field);
217 StringField* Xao::addStringField(const XAO::Dimension& dim, const int& nbComponents, const std::string& name)
218 throw (XAO_Exception)
221 int nbElts = m_geometry->countElements(dim);
222 StringField* field = new StringField(dim, nbElts, nbComponents, name);
223 m_fields.push_back(field);
229 bool Xao::removeField(Field* field)
231 int nb = countFields();
232 m_fields.remove(field);
234 bool res = (nb-1 == countFields());
244 const bool Xao::exportXAO(const std::string& fileName)
246 return XaoExporter::saveToFile(this, fileName);
249 const std::string Xao::getXML()
251 return XaoExporter::saveToXml(this);
254 const bool Xao::importXAO(const std::string& fileName)
256 return XaoExporter::readFromFile(fileName, this);
259 const bool Xao::setXML(const std::string& xml)
261 return XaoExporter::setXML(xml, this);
264 void Xao::checkGeometry() const
267 if (m_geometry == NULL)
268 throw XAO_Exception("Geometry is null");
271 void Xao::checkGroupIndex(const int& index) const
274 if (index >= 0 && index < countGroups())
277 throw XAO_Exception(MsgBuilder() << "Group index is out of range [0, "
278 << countGroups()-1 << "]: " << index);
281 void Xao::checkFieldIndex(const int& index) const
284 if (index >= 0 && index < countFields())
287 throw XAO_Exception(MsgBuilder() << "Field index is out of range [0, "
288 << countFields()-1 << "]: " << index);
291 void Xao::checkGroupDimension(const XAO::Dimension& dim) const
294 if (dim == XAO::WHOLE)
295 throw XAO_Exception(MsgBuilder() << "Invalid dimension for group: " << dim);