1 // Copyright (C) 2013-2024 CEA, EDF
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 int Xao::countGroups() const
74 return m_groups.size();
77 Group* Xao::getGroup(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(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 int Xao::countFields() const
120 return m_fields.size();
123 XAO::Type Xao::getFieldType(int index)
126 return getField(index)->getType();
129 Field* Xao::getField(int index)
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(int index)
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(int index)
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(int index)
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(int index)
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(XAO::Type type, XAO::Dimension dim, int nbComponents, const std::string& name)
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(XAO::Dimension dim, int nbComponents, const std::string& name)
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(XAO::Dimension dim, int nbComponents, const std::string& name)
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(XAO::Dimension dim, int nbComponents, const std::string& name)
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(XAO::Dimension dim, int nbComponents, const std::string& name)
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 bool Xao::exportXAO(const std::string& fileName, const std::string& shapeFileName)
246 return XaoExporter::saveToFile(this, fileName, shapeFileName);
249 const std::string Xao::getXML()
251 return XaoExporter::saveToXml(this);
254 bool Xao::importXAO(const std::string& fileName)
256 return XaoExporter::readFromFile(fileName, this);
259 bool Xao::setXML(const std::string& xml)
261 return XaoExporter::setXML(xml, this);
264 void Xao::checkGeometry() const
266 if (m_geometry == NULL)
267 throw XAO_Exception("Geometry is null");
270 void Xao::checkGroupIndex(int index) const
272 if (index >= 0 && index < countGroups())
275 throw XAO_Exception(MsgBuilder() << "Group index is out of range [0, "
276 << countGroups()-1 << "]: " << index);
279 void Xao::checkFieldIndex(int index) const
281 if (index >= 0 && index < countFields())
284 throw XAO_Exception(MsgBuilder() << "Field index is out of range [0, "
285 << countFields()-1 << "]: " << index);
288 void Xao::checkGroupDimension(XAO::Dimension dim) const
290 if (dim == XAO::WHOLE)
291 throw XAO_Exception(MsgBuilder() << "Invalid dimension for group: " << dim);