rework on field

This commit is contained in:
fps 2013-08-27 11:27:42 +00:00
parent 243d3de586
commit 1c73f7846c
26 changed files with 1862 additions and 565 deletions

60
src/XAO/BooleanField.cxx Normal file
View File

@ -0,0 +1,60 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "BooleanField.hxx"
#include "BooleanStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
BooleanField::BooleanField(const XAO::Dimension dimension, const int nbComponents)
{
m_dimension = dimension;
m_nbComponents = nbComponents;
}
BooleanField::BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents)
{
m_dimension = dimension;
m_nbComponents = nbComponents;
m_name = name;
}
BooleanStep* BooleanField::addStep(const int step)
{
BooleanStep* bstep = new BooleanStep(step, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
BooleanStep* BooleanField::addStep(const int step, const int stamp)
{
BooleanStep* bstep = new BooleanStep(step, stamp, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
BooleanStep* BooleanField::getStep(const int index)
{
if (index < m_steps.size())
return m_steps[index];
throw SALOME_Exception("IndexOutOfRange");
}

51
src/XAO/BooleanField.hxx Normal file
View File

@ -0,0 +1,51 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_BOOLEANFIELD_HXX__
#define __XAO_BOOLEANFIELD_HXX__
#include <iostream>
#include <string>
#include <vector>
#include "Xao.hxx"
#include "Field.hxx"
#include "BooleanStep.hxx"
namespace XAO
{
class BooleanField : public Field
{
public:
BooleanField(const XAO::Dimension dimension, const int nbComponents);
BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents);
virtual const XAO::Type getType() { return XAO::BOOLEAN; }
BooleanStep* addStep(const int step);
BooleanStep* addStep(const int step, const int stamp);
BooleanStep* getStep(const int index);
private:
std::vector<BooleanStep*> m_steps;
};
}
#endif /* __XAO_BOOLEANFIELD_HXX__ */

144
src/XAO/BooleanStep.cxx Normal file
View File

@ -0,0 +1,144 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "BooleanStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
BooleanStep::BooleanStep(const int& nbElements, const int& nbComponents)
{
Init(0, 0, nbElements, nbComponents);
}
BooleanStep::BooleanStep(const int& step, const int& nbElements, const int& nbComponents)
{
Init(step, 0, nbElements, nbComponents);
}
BooleanStep::BooleanStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
Init(step, stamp, nbElements, nbComponents);
}
void BooleanStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
m_nbElements = nbElements;
m_nbComponents = nbComponents;
m_step = step;
m_stamp = stamp;
m_values.reserve(m_nbElements);
for (int i = 0; i < m_nbElements; ++i)
{
for (int j = 0; j < m_nbComponents; ++j)
m_values[i][j] = false;
}
}
std::vector<bool> BooleanStep::getValues()
{
std::vector<bool> result;
result.reserve(m_nbElements * m_nbComponents);
std::vector< std::vector<bool> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<bool> eltValues = *it;
result.insert(result.end(), eltValues.begin(), eltValues.end());
}
return result;
}
std::vector<bool> BooleanStep::getElement(const int& element)
{
checkElement(element);
std::vector<bool> result(m_values[element]);
return result;
}
std::vector<bool> BooleanStep::getComponent(const int& component)
{
checkElement(component);
std::vector<bool> result;
result.reserve(m_nbElements);
std::vector< std::vector<bool> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<bool> eltValues = *it;
result.push_back(eltValues[component]);
}
return result;
}
const bool BooleanStep::getValue(const int& element, const int& component)
{
checkElement(element);
checkComponent(component);
return m_values[element][component];
}
void BooleanStep::setValues(const std::vector<bool>& values)
{
if (values.size() != m_nbComponents * m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
{
for (int j = 0; j < m_nbComponents; ++j)
{
m_values[i][j] = values[i * m_nbComponents + j];
}
}
}
void BooleanStep::setElements(const int& element, const std::vector<bool>& elements)
{
checkElement(element);
if (elements.size() != m_nbComponents)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbComponents; ++i)
m_values[element][i] = elements[i];
}
void BooleanStep::setComponents(const int& component, const std::vector<bool>& components)
{
checkElement(component);
if (components.size() != m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
m_values[i][component] = components[i];
}
void BooleanStep::setValue(const int& element, const int& component, const bool& value)
{
checkElement(element);
checkComponent(component);
m_values[element][component] = value;
}

84
src/XAO/BooleanStep.hxx Normal file
View File

@ -0,0 +1,84 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_BOOLEANSTEP_HXX__
#define __XAO_BOOLEANSTEP_HXX__
#include <map>
#include <vector>
#include "Xao.hxx"
#include "Step.hxx"
namespace XAO
{
class BooleanStep : public Step
{
public:
BooleanStep(const int& nbElements, const int& nbComponents);
BooleanStep(const int& step, const int& nbElements, const int& nbComponents);
BooleanStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::BOOLEAN; }
/**
* Gets all the values in a vector by elements and by components.
* @return a vector containing all the values.
*/
std::vector<bool> getValues();
/**
* Gets all the values for an element.
* @param element the index of the element to get.
* @return a vector containing all the values for the given element.
*/
std::vector<bool> getElement(const int& element);
/**
* Gets all the values for a component.
* @param component the index of the component to get.
* @return a vector containing all the values for the given component.
*/
std::vector<bool> getComponent(const int& component);
/**
* Gets a value for an element and a component.
* @param element the index of the element.
* @param component the index of the component.
* @return the value.
*/
const bool getValue(const int& element, const int& component);
void setValues(const std::vector<bool>& values);
void setElements(const int& element, const std::vector<bool>& elements);
void setComponents(const int& component, const std::vector<bool>& components);
void setValue(const int& element, const int& component, const bool& value);
private:
void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
private:
std::vector< std::vector<bool> > m_values;
};
}
#endif /* __XAO_BOOLEANSTEP_HXX__ */

49
src/XAO/DoubleField.hxx Normal file
View File

@ -0,0 +1,49 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_DOUBLEFIELD_HXX__
#define __XAO_DOUBLEFIELD_HXX__
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Xao.hxx"
#include "Field.hxx"
#include "DoubleStep.hxx"
namespace XAO
{
class DoubleField : public Field
{
public:
DoubleField(const XAO::Dimension dimension, const int nbComponents);
DoubleField(const std::string name, const XAO::Dimension dimension, const int nbComponents);
virtual const XAO::Type getType() { return XAO::DOUBLE; }
DoubleStep* addStep(const int step);
DoubleStep* addStep(const int step, const int stamp);
DoubleStep* getStep(const int index);
};
}
#endif /* __XAO_DOUBLEFIELD_HXX__ */

54
src/XAO/DoubleStep.hxx Normal file
View File

@ -0,0 +1,54 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_DOUBLESTEP_HXX__
#define __XAO_DOUBLESTEP_HXX__
#include <vector>
#include "Xao.hxx"
namespace XAO
{
class DoubleStep : public Step
{
public:
DoubleStep(const int nbElements, const int nbComponents);
DoubleStep(const int step, const int nbElements, const int nbComponents);
DoubleStep(const int step, const int stamp, const int nbElements, const int nbComponents);
virtual const XAO::Type getType() { return XAO::DOUBLE; }
std::vector<double> getValues();
std::vector<double> getElement(const int i);
std::vector<double> getComponent(const int j);
const double getValue(const int i, const int j);
void setValues(std::vector<double> values);
void setElements(const int i, std::vector<double> elements);
void setComponents(const int j, std::vector<double> components);
void setValue(const int i, const int j, const double value);
};
}
#endif /* __XAO_DOUBLESTEP_HXX__ */

View File

@ -16,34 +16,56 @@
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Nathalie Gore (OpenCascade)
// Author : Frederic Pons (OpenCascade)
#include <string>
#include <iostream>
#include "Xao.hxx"
#include "Field.hxx"
#include "BooleanField.hxx"
#include "IntegerField.hxx"
#include "DoubleField.hxx"
#include "StringField.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
template<typename T>
Field<T>::Field(const FieldDimension dim, const int nbComponents)
: m_name(""), m_dimension(dim)
// -------------------------------------------------------
Field* Field::createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents)
{
m_components.reserve(nbComponents);
return createField(type, "", dimension, nbComponents);
}
template<typename T>
Field<T>::Field(const char* name, const FieldDimension dim, const int nbComponents)
: m_name(name), m_dimension(dim)
Field* Field::createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents)
{
m_components.reserve(nbComponents);
if (type == XAO::BOOLEAN)
return new BooleanField(name, dimension, nbComponents);
if (type == XAO::INTEGER)
return new IntegerField(name, dimension, nbComponents);
if (type == XAO::DOUBLE)
return new DoubleField(name, dimension, nbComponents);
if (type == XAO::STRING)
return new StringField(name, dimension, nbComponents);
throw SALOME_Exception("Bad Type");
}
template<typename T>
void Field<T>::setComponentName(const int index, const char* name)
const std::string Field::getComponentName(const int index)
{
if (index < m_components.size())
return m_components[index];
// TODO: throw
return "";
}
void Field::setComponentName(const int index, const std::string name)
{
if (index < m_components.size())
{
m_components[index] = name;
}
// TODO: throw
}

View File

@ -16,113 +16,126 @@
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Nathalie Gore (OpenCascade)
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_FIELD_HXX__
#define __XAO_FIELD_HXX__
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Xao.hxx"
#include "Step.hxx"
namespace XAO
{
enum FieldType
/**
* @class Field
* A geometrical Field.
*/
class Field
{
FIELD_BOOLEAN = 0,
FIELD_INTEGER = 1,
FIELD_DOUBLE = 2,
FIELD_STRING = 3
};
protected:
Field();
enum FieldDimension
{
FIELD_VERTEX = 0,
FIELD_EDGE = 1,
FIELD_FACE = 2,
FIELD_SOLID = 3,
FIELD_WHOLE = -1
};
template <typename T>
class Step
{
public:
private:
int m_number;
int m_stamp;
std::map<int, T> m_values;
static Field* createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents);
static Field* createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents);
};
class IField
{
public:
virtual const char* getName() const = 0;
virtual void setName(const char* name) = 0;
virtual const FieldType getType() = 0;
virtual const FieldDimension getDimension() = 0;
virtual void setComponentName(const int index, const char* name) = 0;
virtual const int countComponents() = 0;
virtual const int getStepCount() = 0;
};
template <typename T>
class Field : IField
{
public:
Field(const FieldDimension dim, const int nbComponents);
Field(const char* name, const FieldDimension dim, const int nbComponents);
virtual ~Field();
const char* getName() const
/**
* Gets the Type of the field.
* @return the Type of the field.
*/
virtual const XAO::Type getType() = 0;
/**
* Gets the name of the Field.
* @return the name of the Field.
*/
const std::string getName()
{
return m_name.c_str();
return m_name;
}
void setName(const char* name)
/**
* Sets the name of the Field.
* @param name the name to set.
*/
void setName(std::string name)
{
m_name = name;
}
virtual const FieldType getType();
const FieldDimension getDimension()
/**
* Gets the Dimension of the Field.
* @return the Dimension of the Field.
*/
const XAO::Dimension getDimension()
{
return m_dimension;
}
void setComponentName(const int index, const char* name);
int countElements();
/**
* Gets the number of components.
* @return the number of components.
*/
const int countComponents()
{
return m_components.size();
}
const int getStepCount()
const int countValues();
/**
* Gets the number of the steps.
* @return the number of steps.
*/
const int countSteps()
{
return m_steps.size();
}
private:
/**
* Gets the name of a component.
* @param index the index of the component to get.
* @return the name of the component for the given index.
*/
const std::string getComponentName(const int index);
/**
* Sets the name of a component.
* @param componentIndex the index of the component to set.
* @param name the name to set.
*/
void setComponentName(const int componentIndex, const std::string name);
/**
* Remove a step.
* @param step the step to remove.
* @return
*/
bool removeStep(Step* step);
protected:
/** The name of the Field. */
std::string m_name;
FieldDimension m_dimension;
FieldType m_type;
/** The dimension of the Field. */
XAO::Dimension m_dimension;
/** The number of components. */
int m_nbComponents;
/** The components of the field. */
std::vector<std::string> m_components;
std::list< Step<T>* > m_steps;
};
/** The steps. */
std::map<int, Step* > m_steps;
class BooleanField : Field<bool>
{
public:
BooleanField(const FieldDimension dim, const int nbComponents);
BooleanField(const char* name, const FieldDimension dim, const int nbComponents);
virtual const FieldType getType()
{
return FIELD_BOOLEAN;
}
int m_nbElements;
};
}

View File

@ -42,6 +42,11 @@ GeometricElement::~GeometricElement()
{
}
bool GeometricElement::hasName()
{
return !m_name.empty();
}
GeometricElementList::GeometricElementList()
{
setSize(0);
@ -89,6 +94,16 @@ void GeometricElementList::setName(const int index, const char* name)
m_elements[index].setName(name);
}
bool GeometricElementList::hasName(const int index)
{
if (m_count == 0 || index > m_count)
{
throw SALOME_Exception("Problem with number of elements");
}
return m_elements[index].hasName();
}
const char* GeometricElementList::getReference(const int index)
{
if (m_count == 0 || index > m_count)

View File

@ -65,6 +65,12 @@ namespace XAO
m_name = name;
}
/**
* Checks if the element has a name.
* @return true if the element has a name, false otherwise.
*/
bool hasName();
/**
* Gets the reference of the element.
* \return the reference.
@ -143,6 +149,13 @@ namespace XAO
*/
void setName(const int index, const char* name);
/**
* Checks if an element has a name.
* @param index the index of the element.
* @return true if the element has a name, false otherwise.
*/
bool hasName(const int index);
/**
* Gets the reference of an element.
* \param index the index of the element.
@ -165,6 +178,14 @@ namespace XAO
*/
const int getIndexByReference(const char* reference);
/**
* Iterator on the element of the list.
*/
typedef std::map<int, GeometricElement>::iterator iterator;
iterator begin() { return m_elements.begin(); }
iterator end() { return m_elements.end(); }
private:
int m_count;
std::map<int, GeometricElement> m_elements;

View File

@ -123,32 +123,33 @@ void Geometry::initListIds(const Standard_Integer shapeType)
{
m_vertices.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++)
m_vertices.setReference(i, XaoUtils::intToString((*it)));
m_vertices.setReference(i, XaoUtils::intToString((*it)).c_str());
break;
}
case TopAbs_EDGE:
{
m_edges.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++)
m_edges.setReference(i, XaoUtils::intToString((*it)));
m_edges.setReference(i, XaoUtils::intToString((*it)).c_str());
break;
}
case TopAbs_FACE:
{
m_faces.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++)
m_faces.setReference(i, XaoUtils::intToString((*it)));
m_faces.setReference(i, XaoUtils::intToString((*it)).c_str());
break;
}
case TopAbs_SOLID:
{
m_solids.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++)
m_solids.setReference(i, XaoUtils::intToString((*it)));
m_solids.setReference(i, XaoUtils::intToString((*it)).c_str());
break;
}
}
}
const char* Geometry::getElementReference(const Dimension dim, const int index)
{
if (dim == VERTEX)
@ -179,3 +180,32 @@ const int Geometry::getElementIndexByReference(const Dimension dim, const char*
throw SALOME_Exception("Unknown dimension");
}
GeometricElementList::iterator Geometry::begin(const Dimension dim)
{
if (dim == VERTEX)
return m_vertices.begin();
if (dim == EDGE)
return m_edges.begin();
if (dim == FACE)
return m_faces.begin();
if (dim == SOLID)
return m_solids.begin();
std::cout << "begin: unknown dimension" << std::endl;
throw SALOME_Exception("Unknown dimension");
}
GeometricElementList::iterator Geometry::end(const Dimension dim)
{
if (dim == VERTEX)
return m_vertices.end();
if (dim == EDGE)
return m_edges.end();
if (dim == FACE)
return m_faces.end();
if (dim == SOLID)
return m_solids.end();
std::cout << "begin: unknown dimension" << std::endl;
throw SALOME_Exception("Unknown dimension");
}

View File

@ -89,6 +89,11 @@ namespace XAO
void setFaceName(const int index, const char* name) { m_faces.setName(index, name); }
void setSolidName(const int index, const char* name) { m_solids.setName(index, name); }
bool hasVertexName(const int index) { return m_vertices.hasName(index); }
bool hasEdgeName(const int index) { return m_edges.hasName(index); }
bool hasFaceName(const int index) { return m_faces.hasName(index); }
bool hasSolidName(const int index) { return m_solids.hasName(index); }
const char* getVertexReference(const int index) { return m_vertices.getReference(index); }
const char* getEdgeReference(const int index) { return m_edges.getReference(index); }
const char* getFaceReference(const int index) { return m_faces.getReference(index); }
@ -106,6 +111,9 @@ namespace XAO
const int getSolidIndexByReference(const char* reference) { return m_solids.getIndexByReference(reference); }
const int getElementIndexByReference(const Dimension dim, const char* reference);
GeometricElementList::iterator begin(const Dimension dim);
GeometricElementList::iterator end(const Dimension dim);
private:
void initListIds(const Standard_Integer shapeType);

View File

@ -29,43 +29,88 @@
namespace XAO
{
/**
* \class Group
* Class to represent a Geometrical Group.
*/
class Group
{
public:
/**
* Default constructor.
*/
Group();
~Group();
/**
* Destructor.
*/
virtual ~Group();
/**
* Sets the name of the group.
* \param name the name to set.
*/
void setName(const char* name)
{
m_name = name;
}
/**
* Gets the name of the group.
* \return the name of the group.
*/
const char *getName() const
{
return m_name.c_str();
}
/**
* Gets the dimension of the group.
* \return the dimension of the group.
*/
const Dimension getDimension();
/**
* Sets the dimension of the group.
* \param dim the dimension to set.
*/
void setDimension(const Dimension dim);
/**
* Gets the number of elements in the group.
* \return the number of elements.
*/
int getCount()
{
return m_elements.size();
}
/**
* Adds an element to the group.
* \param value the index of the element to add in the geometric element list (vertex, face...).
*/
void addElement(const int value)
{
m_elements.push_back(value);
}
/**
* Gets the reference of an element.
* \param index the index of the element.
* \return the reference of the element.
*/
const int getElement(const int index)
{
return m_elements[index];
}
private:
/** The name of the group. */
std::string m_name;
/** The dimension of the group. */
Dimension m_dimension;
/** The number of elements in the group. */
int m_count;
/** The elements of the group. */
std::vector<int> m_elements;
};
}

49
src/XAO/IntegerField.hxx Normal file
View File

@ -0,0 +1,49 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_INTEGERFIELD_HXX__
#define __XAO_INTEGERFIELD_HXX__
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Xao.hxx"
#include "Field.hxx"
#include "IntegerStep.hxx"
namespace XAO
{
class IntegerField : public Field
{
public:
IntegerField(const XAO::Dimension dimension, const int nbComponents);
IntegerField(const std::string name, const XAO::Dimension dimension, const int nbComponents);
virtual const XAO::Type getType() { return XAO::INTEGER; }
IntegerStep* addStep(const int step);
IntegerStep* addStep(const int step, const int stamp);
IntegerStep* getStep(const int index);
};
}
#endif /* __XAO_INTEGERFIELD_HXX__ */

54
src/XAO/IntegerStep.hxx Normal file
View File

@ -0,0 +1,54 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_INTEGERSTEP_HXX__
#define __XAO_INTEGERSTEP_HXX__
#include <vector>
#include "Xao.hxx"
namespace XAO
{
class IntegerStep : public Step
{
public:
IntegerStep(const int nbElements, const int nbComponents);
IntegerStep(const int step, const int nbElements, const int nbComponents);
IntegerStep(const int step, const int stamp, const int nbElements, const int nbComponents);
virtual const XAO::Type getType() { return XAO::INTEGER; }
std::vector<int> getValues();
std::vector<int> getElement(const int i);
std::vector<int> getComponent(const int j);
const int getValue(const int i, const int j);
void setValues(std::vector<int> values);
void setElements(const int i, std::vector<int> elements);
void setComponents(const int j, std::vector<int> components);
void setValue(const int i, const int j, const int value);
};
}
#endif /* __XAO_INTEGERSTEP_HXX__ */

View File

@ -32,7 +32,17 @@ salomeinclude_HEADERS = \
Xao.hxx \
Geometry.hxx \
Group.hxx \
Field.hxx
Field.hxx \
BooleanField.hxx \
IntegerField.hxx \
DoubleField.hxx \
StringField.hxx \
Step.hxx \
BooleanStep.hxx \
IntegerStep.hxx \
DoubleStep.hxx \
StringStep.hxx \
XaoExporter.hxx
lib_LTLIBRARIES = libXAO.la
dist_libXAO_la_SOURCES = \
@ -41,7 +51,11 @@ dist_libXAO_la_SOURCES = \
Xao.cxx \
Geometry.cxx \
Group.cxx \
Field.cxx
Field.cxx \
BooleanField.cxx \
Step.cxx \
BooleanStep.cxx \
XaoExporter.cxx
libXAO_la_CPPFLAGS = \

66
src/XAO/Step.cxx Normal file
View File

@ -0,0 +1,66 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "Xao.hxx"
#include "Step.hxx"
#include "BooleanStep.hxx"
#include "IntegerStep.hxx"
#include "DoubleStep.hxx"
#include "StringStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
Step* Step::createStep(const XAO::Type& type, const int& nbElements, const int &nbComponents)
{
return createStep(type, 0, 0, nbElements, nbComponents);
}
Step* Step::createStep(const XAO::Type& type, const int& step, const int& nbElements, const int& nbComponents)
{
return createStep(type, step, 0, nbElements, nbComponents);
}
Step* Step::createStep(const XAO::Type& type, const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
if (type == XAO::BOOLEAN)
return new BooleanStep(step, stamp, nbElements, nbComponents);
if (type == XAO::INTEGER)
return new IntegerStep(step, stamp, nbElements, nbComponents);
if (type == XAO::DOUBLE)
return new DoubleStep(step, stamp, nbElements, nbComponents);
if (type == XAO::STRING)
return new StringStep(step, stamp, nbElements, nbComponents);
throw SALOME_Exception("Unknown Type");
}
void Step::checkElement(const int& element)
{
if (element >= m_nbElements)
throw SALOME_Exception("IndexOutOfRange element"); // TODO
}
void Step::checkComponent(const int& component)
{
if (component >= m_nbComponents)
throw SALOME_Exception("IndexOutOfRange component"); // TODO
}

85
src/XAO/Step.hxx Normal file
View File

@ -0,0 +1,85 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_STEP_HXX__
#define __XAO_STEP_HXX__
#include "Xao.hxx"
namespace XAO
{
class Step
{
protected:
Step();
public:
static Step* createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents);
static Step* createStep(const XAO::Type& type, const int& step, const int& nbElements, const int& nbComponents);
static Step* createStep(const XAO::Type& type, const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() = 0;
/**
* Gets the step index.
* @return the index of the step.
*/
const int getStep() { return m_step; }
/**
* Sets the number of the step.
* @param step the index to set.
*/
void setStep(const int& step) { m_step = step; }
/**
* Gets the stamp of the index.
* @return the stamp of the index.
*/
const int getStamp() { return m_stamp; }
/**
* Sets the stamp of the index.
* @param stamp the stamp to set.
*/
void setStamp(const int& stamp) { m_stamp = stamp; }
const int countComponents() { return m_nbComponents; }
const int countElements() { return m_nbElements; }
virtual const int countValues();
protected:
void checkElement(const int& element);
void checkComponent(const int& component);
protected:
int m_step;
int m_stamp;
int m_nbComponents;
int m_nbElements;
};
}
#endif /* __XAO_STEP_HXX__ */

49
src/XAO/StringField.hxx Normal file
View File

@ -0,0 +1,49 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_STRINGFIELD_HXX__
#define __XAO_STRINGFIELD_HXX__
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "Xao.hxx"
#include "Field.hxx"
#include "StringStep.hxx"
namespace XAO
{
class StringField : public Field
{
public:
StringField(const XAO::Dimension dimension, const int nbComponents);
StringField(const std::string name, const XAO::Dimension dimension, const int nbComponents);
virtual const XAO::Type getType() { return XAO::STRING; }
StringStep* addStep(const int step);
StringStep* addStep(const int step, const int stamp);
StringStep* getStep(const int index);
};
}
#endif /* __XAO_STRINGFIELD_HXX__ */

55
src/XAO/StringStep.hxx Normal file
View File

@ -0,0 +1,55 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#ifndef __XAO_STRINGSTEP_HXX__
#define __XAO_STRINGSTEP_HXX__
#include <string>
#include <vector>
#include "Xao.hxx"
namespace XAO
{
class StringStep : public Step
{
public:
StringStep(const int nbElements, const int nbComponents);
StringStep(const int step, const int nbElements, const int nbComponents);
StringStep(const int step, const int stamp, const int nbElements, const int nbComponents);
virtual const XAO::Type getType() { return XAO::STRING; }
std::vector<std::string> getValues();
std::vector<std::string> getElement(const int i);
std::vector<std::string> getComponent(const int j);
const std::string getValue(const int i, const int j);
void setValues(std::vector<std::string> values);
void setElements(const int i, std::vector<std::string> elements);
void setComponents(const int j, std::vector<std::string> components);
void setValue(const int i, const int j, const std::string value);
};
}
#endif /* __XAO_STRINGSTEP_HXX__ */

View File

@ -25,52 +25,13 @@
#include "Geometry.hxx"
#include "Group.hxx"
#include "Field.hxx"
#include "XaoExporter.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
const xmlChar* C_XAO_VERSION = (xmlChar*)"1.0";
const xmlChar* C_TAG_XAO = (xmlChar*)"XAO";
const xmlChar* C_ATTR_XAO_AUTHOR = (xmlChar*)"author";
const xmlChar* C_ATTR_XAO_VERSION = (xmlChar*)"version";
const xmlChar* C_TAG_GEOMETRY = (xmlChar*)"geometry";
const xmlChar* C_ATTR_GEOMETRY_NAME = (xmlChar*)"name";
const xmlChar* C_TAG_SHAPE = (xmlChar*)"shape";
const xmlChar* C_ATTR_SHAPE_FORMAT = (xmlChar*)"format";
const xmlChar* C_TAG_TOPOLOGY = (xmlChar*)"topology";
const xmlChar* C_TAG_VERTICES = (xmlChar*)"vertices";
const xmlChar* C_TAG_VERTEX = (xmlChar*)"vertex";
const xmlChar* C_TAG_EDGES = (xmlChar*)"edges";
const xmlChar* C_TAG_EDGE = (xmlChar*)"edge";
const xmlChar* C_TAG_FACES = (xmlChar*)"faces";
const xmlChar* C_TAG_FACE = (xmlChar*)"face";
const xmlChar* C_TAG_SOLIDS = (xmlChar*)"solids";
const xmlChar* C_TAG_SOLID = (xmlChar*)"solid";
const xmlChar* C_ATTR_COUNT = (xmlChar*)"count";
const xmlChar* C_ATTR_ELT_INDEX = (xmlChar*)"index";
const xmlChar* C_ATTR_ELT_NAME = (xmlChar*)"name";
const xmlChar* C_ATTR_ELT_REFERENCE = (xmlChar*)"reference";
const xmlChar* C_TAG_GROUPS = (xmlChar*)"groups";
const xmlChar* C_TAG_GROUP = (xmlChar*)"group";
const xmlChar* C_ATTR_GROUP_NAME = (xmlChar*)"name";
const xmlChar* C_ATTR_GROUP_DIM = (xmlChar*)"dimension";
const xmlChar* C_TAG_ELEMENT = (xmlChar*)"element";
const xmlChar* C_ATTR_ELEMENT_INDEX = (xmlChar*)"index";
const xmlChar* C_TAG_FIELDS = (xmlChar*)"fields";
const xmlChar* C_TAG_FIELD = (xmlChar*)"field";
const xmlChar* C_TAG_COMPONENTS = (xmlChar*)"components";
const xmlChar* C_TAG_COMPONENT = (xmlChar*)"component";
const xmlChar* C_TAG_STEPS = (xmlChar*)"steps";
const xmlChar* C_TAG_STEP = (xmlChar*)"step";
Xao::Xao()
{
m_author = "";
@ -93,10 +54,15 @@ Xao::~Xao()
m_geometry = NULL;
}
// for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
// {
// delete (*it);
// }
for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
delete (*it);
}
for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it)
{
delete (*it);
}
}
int Xao::countGroups()
@ -131,10 +97,10 @@ int Xao::countFields()
return m_fields.size();
}
IField* Xao::getField(const int index)
Field* Xao::getField(const int index)
{
int i = 0;
for (std::list<IField*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it, ++i)
for (std::list<Field*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it, ++i)
{
if (i == index)
return (*it);
@ -143,429 +109,46 @@ IField* Xao::getField(const int index)
return NULL;
}
void Xao::addField(IField* field)
void Xao::addField(Field* field)
{
m_fields.push_back(field);
}
void Xao::removeField(IField* field)
void Xao::removeField(Field* field)
{
m_fields.remove(field);
}
bool Xao::exportXAO(const char* fileName)
{
xmlDocPtr doc = exportXMLDoc();
xmlSaveFormatFileEnc(fileName, doc, "UTF-8", 2);
xmlFreeDoc(doc);
return true;
// xmlDocPtr doc = exportXMLDoc();
// xmlSaveFormatFileEnc(fileName, doc, "UTF-8", 2);
// xmlFreeDoc(doc);
//
// return true;
return XaoExporter::saveToFile(this, fileName);
}
const char* Xao::getXML()
{
xmlDocPtr doc = exportXMLDoc();
xmlChar *xmlbuff;
int buffersize;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
xmlFreeDoc(doc);
xmlCleanupGlobals();
return (char*)xmlbuff;
}
xmlDocPtr Xao::exportXMLDoc()
{
// Creating the Xml document
xmlDocPtr masterDocument = xmlNewDoc(BAD_CAST "1.0");
xmlNodePtr xao = xmlNewNode(0, C_TAG_XAO);
xmlDocSetRootElement(masterDocument, xao);
xmlNewProp(xao, C_ATTR_XAO_VERSION, BAD_CAST getVersion());
xmlNewProp(xao, C_ATTR_XAO_AUTHOR, BAD_CAST getAuthor());
if (m_geometry != NULL)
{
exportGeometry(masterDocument, xao);
}
exportGroups(xao);
return masterDocument;
}
void Xao::exportGeometry(xmlDocPtr doc, xmlNodePtr xao)
{
// Geometric part
xmlNodePtr geometry = xmlNewChild(xao, 0, C_TAG_GEOMETRY, 0);
xmlNewProp(geometry, C_ATTR_GEOMETRY_NAME, BAD_CAST m_geometry->getName());
xmlNodePtr shape = xmlNewChild(geometry, 0, C_TAG_SHAPE, 0);
xmlNewProp(shape, C_ATTR_SHAPE_FORMAT, BAD_CAST m_geometry->getFormat());
const char* brep = m_geometry->getBREP();
xmlNodePtr cdata = xmlNewCDataBlock(doc, BAD_CAST brep, strlen(brep));
xmlAddChild(shape, cdata);
xmlNodePtr topology = xmlNewChild(geometry, 0, C_TAG_TOPOLOGY, 0);
// vertices
xmlNodePtr vertices = xmlNewChild(topology, 0, C_TAG_VERTICES, 0);
xmlNewProp(vertices, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(m_geometry->countVertices()));
for (int i = 0; i < m_geometry->countVertices(); ++i)
{
xmlNodePtr vertex = xmlNewChild(vertices, 0, C_TAG_VERTEX, 0);
xmlNewProp(vertex, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(i));
xmlNewProp(vertex, C_ATTR_ELT_NAME, BAD_CAST m_geometry->getVertexName(i));
xmlNewProp(vertex, C_ATTR_ELT_REFERENCE, BAD_CAST m_geometry->getVertexReference(i));
}
// edges
xmlNodePtr edges = xmlNewChild(topology, 0, C_TAG_EDGES, 0);
xmlNewProp(edges, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(m_geometry->countEdges()));
for (int i = 0; i < m_geometry->countEdges(); ++i)
{
xmlNodePtr edge = xmlNewChild(edges, 0, C_TAG_EDGE, 0);
xmlNewProp(edge, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(i));
xmlNewProp(edge, C_ATTR_ELT_NAME, BAD_CAST m_geometry->getEdgeName(i));
xmlNewProp(edge, C_ATTR_ELT_REFERENCE, BAD_CAST m_geometry->getEdgeReference(i));
}
// faces
xmlNodePtr faces = xmlNewChild(topology, 0, C_TAG_FACES, 0);
xmlNewProp(faces, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(m_geometry->countFaces()));
for (int i = 0; i < m_geometry->countFaces(); ++i)
{
xmlNodePtr face = xmlNewChild(faces, 0, C_TAG_FACE, 0);
xmlNewProp(face, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(i));
xmlNewProp(face, C_ATTR_ELT_NAME, BAD_CAST m_geometry->getFaceName(i));
xmlNewProp(face, C_ATTR_ELT_REFERENCE, BAD_CAST m_geometry->getFaceReference(i));
}
// soldids
xmlNodePtr solids = xmlNewChild(topology, 0, C_TAG_SOLIDS, 0);
xmlNewProp(solids, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(m_geometry->countSolids()));
for (int i = 0; i < m_geometry->countSolids(); ++i)
{
xmlNodePtr solid = xmlNewChild(solids, 0, C_TAG_SOLID, 0);
xmlNewProp(solid, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(i));
xmlNewProp(solid, C_ATTR_ELT_NAME, BAD_CAST m_geometry->getSolidName(i));
xmlNewProp(solid, C_ATTR_ELT_REFERENCE, BAD_CAST m_geometry->getSolidReference(i));
}
}
void Xao::exportGroups(xmlNodePtr xao)
{
xmlNodePtr groups = xmlNewChild(xao, 0, C_TAG_GROUPS, 0);
xmlNewProp(groups, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(countGroups()));
for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
Group* grp = (*it);
xmlNodePtr group = xmlNewChild(groups, 0, C_TAG_GROUP, 0);
xmlNewProp(group, C_ATTR_GROUP_NAME, BAD_CAST grp->getName());
xmlNewProp(group, C_ATTR_GROUP_DIM, BAD_CAST XaoUtils::dimensionToString(grp->getDimension()));
xmlNewProp(group, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(grp->getCount()));
for (int i = 0; i < grp->getCount(); ++i)
{
xmlNodePtr elt = xmlNewChild(group, 0, C_TAG_ELEMENT, 0);
xmlNewProp(elt, C_ATTR_ELEMENT_INDEX, BAD_CAST XaoUtils::intToString(grp->getElement(i)));
}
}
// xmlDocPtr doc = exportXMLDoc();
//
// xmlChar *xmlbuff;
// int buffersize;
// xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
// xmlFreeDoc(doc);
// xmlCleanupGlobals();
//
// return (char*)xmlbuff;
return XaoExporter::saveToXml(this);
}
bool Xao::importXAO(const char* fileName)
{
// parse the file and get the DOM
int options = XML_PARSE_HUGE || XML_PARSE_NOCDATA;
xmlDocPtr doc = xmlReadFile(fileName, NULL, options);
if (doc == NULL)
{
throw SALOME_Exception("Cannot read XAO file");
}
parseXMLDoc(doc);
return true;
return XaoExporter::readFromFile(fileName, this);
}
bool Xao::setXML(const char* xml)
{
int options = XML_PARSE_HUGE || XML_PARSE_NOCDATA;
xmlDocPtr doc = xmlReadDoc(BAD_CAST xml, "", NULL, options);
if (doc == NULL)
{
throw SALOME_Exception("Cannot read XAO stream");
}
parseXMLDoc(doc);
return true;
}
void Xao::parseXMLDoc(xmlDocPtr doc)
{
// Get the root element node
xmlNodePtr root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name , C_TAG_XAO) != 0)
throw SALOME_Exception("Cannot read XAO file: invalid format XAO node not found");
parseXaoNode(doc, root);
xmlFreeDoc(doc); // free document
xmlCleanupParser(); // Free globals
}
void Xao::parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode)
{
xmlChar* version = xmlGetProp(xaoNode, C_ATTR_XAO_VERSION);
if (version != NULL)
{
setVersion((char*)version);
xmlFree(version);
}
xmlChar* author = xmlGetProp(xaoNode, C_ATTR_XAO_AUTHOR);
if (author != NULL)
{
setAuthor((char*)author);
xmlFree(author);
}
for (xmlNodePtr node = xaoNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_GEOMETRY) == 0)
parseGeometryNode(doc, node);
else if (xmlStrcmp(node->name, C_TAG_GROUPS) == 0)
parseGroupsNode(node);
}
}
void Xao::parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode)
{
//std::cout << ">> Geometry" << std::endl;
if (m_geometry == NULL)
m_geometry = new Geometry();
xmlChar* name = xmlGetProp(geometryNode, C_ATTR_GEOMETRY_NAME);
if (name != NULL)
{
m_geometry->setName((char*)name);
xmlFree(name);
}
for (xmlNodePtr node = geometryNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_SHAPE) == 0)
parseShapeNode(doc, node);
else if (xmlStrcmp(node->name, C_TAG_TOPOLOGY) == 0)
parseTopologyNode(node);
// else
// std::cout << "skip:" << node->name << std::endl;
}
}
void Xao::parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode)
{
xmlChar* shapeType = xmlGetProp(shapeNode, C_ATTR_SHAPE_FORMAT);
if (xmlStrcmp(shapeType, (xmlChar*)"BREP") == 0)
{
xmlChar* data = xmlNodeGetContent(shapeNode->children);
if (data == NULL)
throw SALOME_Exception("Missing BREP");
m_geometry->setShape((char*)data);
xmlFree(data);
}
else
{
throw SALOME_Exception("Shape format not supported");
}
}
void Xao::parseTopologyNode(xmlNodePtr topologyNode)
{
// std::cout << ">> Topology" << std::endl;
for (xmlNodePtr node = topologyNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_VERTICES) == 0)
parseVerticesNode(node);
else if (xmlStrcmp(node->name, C_TAG_EDGES) == 0)
parseEdgesNode(node);
else if (xmlStrcmp(node->name, C_TAG_FACES) == 0)
parseFacesNode(node);
else if (xmlStrcmp(node->name, C_TAG_SOLIDS) == 0)
parseSolidsNode(node);
}
}
void Xao::parseVerticesNode(xmlNodePtr verticesNode)
{
xmlChar* count = xmlGetProp(verticesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for vertices");
m_geometry->setCountVertices(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = verticesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_VERTEX) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for vertex");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for vertex");
m_geometry->setVertex(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void Xao::parseEdgesNode(xmlNodePtr edgesNode)
{
xmlChar* count = xmlGetProp(edgesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for edges");
m_geometry->setCountEdges(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = edgesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_EDGE) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for edge");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for edge");
m_geometry->setEdge(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void Xao::parseFacesNode(xmlNodePtr facesNode)
{
xmlChar* count = xmlGetProp(facesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for faces");
m_geometry->setCountFaces(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = facesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_FACE) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for face");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for face");
m_geometry->setFace(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void Xao::parseSolidsNode(xmlNodePtr solidsNode)
{
xmlChar* count = xmlGetProp(solidsNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for solids");
m_geometry->setCountSolids(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = solidsNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_SOLID) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for solid");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for solid");
m_geometry->setSolid(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void Xao::parseGroupsNode(xmlNodePtr groupsNode)
{
// xmlChar* count = xmlGetProp(groupsNode, C_ATTR_COUNT);
// if (count == NULL)
// throw SALOME_Exception("No count attribute for groups");
for (xmlNodePtr node = groupsNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_GROUP) == 0)
{
parseGroupNode(node);
}
}
}
void Xao::parseGroupNode(xmlNodePtr groupNode)
{
// xmlChar* count = xmlGetProp(groupNode, C_ATTR_COUNT);
// if (count == NULL)
// throw SALOME_Exception("Bad count for group");
xmlChar* dimension = xmlGetProp(groupNode, C_ATTR_GROUP_DIM);
if (dimension == NULL)
throw SALOME_Exception("Bad dimension for group");
Group* group = new Group();
xmlChar* name = xmlGetProp(groupNode, C_ATTR_GROUP_NAME);
if (name == NULL) name = (xmlChar*)"";
group->setName((char*)name);
xmlFree(name);
group->setDimension(XaoUtils::stringToDimension((char*)dimension));
xmlFree(dimension);
addGroup(group);
for (xmlNodePtr node = groupNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_ELEMENT) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELEMENT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for group element");
group->addElement(atoi((char*)index));
xmlFree(index);
}
}
return XaoExporter::setXML(xml, this);
}

View File

@ -35,12 +35,21 @@ namespace XAO
VERTEX = 0,//!< VERTEX
EDGE = 1, //!< EDGE
FACE = 2, //!< FACE
SOLID = 3 //!< SOLID
SOLID = 3, //!< SOLID
WHOLE = -1 //!< WHOLE
};
enum Type
{
BOOLEAN = 0,
INTEGER = 1,
DOUBLE = 2,
STRING = 3
};
class Geometry;
class Group;
class IField;
class Field;
/**
* @class Xao
@ -49,23 +58,50 @@ namespace XAO
class Xao
{
public:
/**
* Default constructor.
*/
Xao();
/**
* Constructor with author and version.
* \param author the author of the file.
* \param version the version of the XAO format.
*/
Xao(const char* author, const char* version);
~Xao();
/**
* Destructor.
*/
virtual ~Xao();
/**
* Gets the author of the file.
* \return the author of the file.
*/
const char* getAuthor()
{
return m_author.c_str();
}
/**
* Sets the author of the file.
* \param author the author to set.
*/
void setAuthor(const char* author)
{
m_author = author;
}
/**
* Gets the version of the file.
* \return the version of the file.
*/
const char* getVersion()
{
return m_version.c_str();
}
/**
* Sets the version of the file.
* \param version the version to set.
*/
void setVersion(const char* version)
{
m_version = version;
@ -75,10 +111,18 @@ namespace XAO
// Geometry
//
/**
* Gets the geometry.
* \return the geometry.
*/
Geometry* getGeometry()
{
return m_geometry;
}
/**
* Sets the geometry.
* \param geometry the geometry to set.
*/
void setGeometry(Geometry* geometry)
{
m_geometry = geometry;
@ -88,51 +132,93 @@ namespace XAO
// Groups
//
/**
* Gets the number of groups.
* \return the number of groups.
*/
int countGroups();
/**
* Gets a group.
* \param index the index of the wanted group.
* \return the group or NULL if index is bigger than the number of groups.
*/
Group* getGroup(const int index);
/**
* Adds a group.
* \param group the group to add.
*/
void addGroup(Group* group);
/**
* Removes a group.
* \param group the group to remove.
*/
void removeGroup(Group* group);
//
// Fields
//
/**
* Gets the number of fields.
* \return the number of fields.
*/
int countFields();
IField* getField(const int index);
void addField(IField* field);
void removeField(IField* field);
/**
* Gets a field.
* \param index the index of the wanted field.
* \return the field or NULL if the index is bigger than the number of fields.
*/
Field* getField(const int index);
/**
* Adds a field.
* \param field the field to add.
*/
void addField(Field* field);
/**
* Removes a field.
* \param field the field to remove.
*/
void removeField(Field* field);
//
// Import / Export
//
/**
* Exports this XAO object to a file.
* \param fileName the name of the file to create.
* \return true is the export is successful.
*/
bool exportXAO(const char* fileName);
/**
* Gets the XML corresponding to this XAO.
* \return the XML as a string.
*/
const char* getXML();
/**
* Imports an XAO file into this object.
* \param fileName the name of the file to import.
* \return true if the import is successful.
*/
bool importXAO(const char* fileName);
/**
* Sets an XML describing an XAO format to this object.
* \param xml the XML to set.
* \return true if the import is successful.
*/
bool setXML(const char* xml);
private:
/** The author of the file. */
std::string m_author;
/** The version of the file. */
std::string m_version;
/** The geometry. */
Geometry* m_geometry;
/** The list of groups. */
std::list<Group*> m_groups;
std::list<IField*> m_fields;
xmlDocPtr exportXMLDoc();
void exportGeometry(xmlDocPtr doc, xmlNodePtr xao);
void exportGroups(xmlNodePtr xao);
void parseXMLDoc(xmlDocPtr doc);
void parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode);
void parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode);
void parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode);
void parseTopologyNode(xmlNodePtr topologyNode);
void parseVerticesNode(xmlNodePtr verticesNode);
void parseEdgesNode(xmlNodePtr edgesNode);
void parseFacesNode(xmlNodePtr facesNode);
void parseSolidsNode(xmlNodePtr solidsNode);
void parseGroupsNode(xmlNodePtr groupsNode);
void parseGroupNode(xmlNodePtr groupNode);
/** The list of fields. */
std::list<Field*> m_fields;
};
}

540
src/XAO/XaoExporter.cxx Normal file
View File

@ -0,0 +1,540 @@
#include <libxml/parser.h>
#include <Utils_SALOME_Exception.hxx>
#include "XaoExporter.hxx"
#include "Xao.hxx"
#include "Geometry.hxx"
#include "Group.hxx"
#include "Field.hxx"
#include "XaoUtils.hxx"
namespace XAO
{
const xmlChar* C_TAG_XAO = (xmlChar*)"XAO";
const xmlChar* C_ATTR_XAO_AUTHOR = (xmlChar*)"author";
const xmlChar* C_ATTR_XAO_VERSION = (xmlChar*)"version";
const xmlChar* C_TAG_GEOMETRY = (xmlChar*)"geometry";
const xmlChar* C_ATTR_GEOMETRY_NAME = (xmlChar*)"name";
const xmlChar* C_TAG_SHAPE = (xmlChar*)"shape";
const xmlChar* C_ATTR_SHAPE_FORMAT = (xmlChar*)"format";
const xmlChar* C_TAG_TOPOLOGY = (xmlChar*)"topology";
const xmlChar* C_TAG_VERTICES = (xmlChar*)"vertices";
const xmlChar* C_TAG_VERTEX = (xmlChar*)"vertex";
const xmlChar* C_TAG_EDGES = (xmlChar*)"edges";
const xmlChar* C_TAG_EDGE = (xmlChar*)"edge";
const xmlChar* C_TAG_FACES = (xmlChar*)"faces";
const xmlChar* C_TAG_FACE = (xmlChar*)"face";
const xmlChar* C_TAG_SOLIDS = (xmlChar*)"solids";
const xmlChar* C_TAG_SOLID = (xmlChar*)"solid";
const xmlChar* C_ATTR_COUNT = (xmlChar*)"count";
const xmlChar* C_ATTR_ELT_INDEX = (xmlChar*)"index";
const xmlChar* C_ATTR_ELT_NAME = (xmlChar*)"name";
const xmlChar* C_ATTR_ELT_REFERENCE = (xmlChar*)"reference";
const xmlChar* C_TAG_GROUPS = (xmlChar*)"groups";
const xmlChar* C_TAG_GROUP = (xmlChar*)"group";
const xmlChar* C_ATTR_GROUP_NAME = (xmlChar*)"name";
const xmlChar* C_ATTR_GROUP_DIM = (xmlChar*)"dimension";
const xmlChar* C_TAG_ELEMENT = (xmlChar*)"element";
const xmlChar* C_ATTR_ELEMENT_INDEX = (xmlChar*)"index";
const xmlChar* C_TAG_VALUE = (xmlChar*)"value";
const xmlChar* C_ATTR_VALUE_COMPONENT = (xmlChar*)"component";
const xmlChar* C_TAG_FIELDS = (xmlChar*)"fields";
const xmlChar* C_TAG_FIELD = (xmlChar*)"field";
const xmlChar* C_ATTR_FIELD_NAME = (xmlChar*)"name";
const xmlChar* C_ATTR_FIELD_TYPE = (xmlChar*)"type";
const xmlChar* C_ATTR_FIELD_DIMENSION = (xmlChar*)"dimension";
const xmlChar* C_TAG_COMPONENTS = (xmlChar*)"components";
const xmlChar* C_TAG_COMPONENT = (xmlChar*)"component";
const xmlChar* C_ATTR_COMPONENT_COLUMN = (xmlChar*)"column";
const xmlChar* C_ATTR_COMPONENT_NAME = (xmlChar*)"name";
const xmlChar* C_TAG_STEPS = (xmlChar*)"steps";
const xmlChar* C_TAG_STEP = (xmlChar*)"step";
const xmlChar* C_ATTR_STEP_NUMBER = (xmlChar*)"number";
const xmlChar* C_ATTR_STEP_STAMP = (xmlChar*)"stamp";
}
using namespace XAO;
bool XaoExporter::saveToFile(Xao* xaoObject, const char* fileName)
{
xmlDocPtr doc = exportXMLDoc(xaoObject);
xmlSaveFormatFileEnc(fileName, doc, "UTF-8", 2);
xmlFreeDoc(doc);
return true;
}
const char* XaoExporter::saveToXml(Xao* xaoObject)
{
xmlDocPtr doc = exportXMLDoc(xaoObject);
xmlChar *xmlbuff;
int buffersize;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);
xmlFreeDoc(doc);
xmlCleanupGlobals();
return (char*)xmlbuff;
}
xmlDocPtr XaoExporter::exportXMLDoc(Xao* xaoObject)
{
// Creating the Xml document
xmlDocPtr masterDocument = xmlNewDoc(BAD_CAST "1.0");
xmlNodePtr xao = xmlNewNode(0, C_TAG_XAO);
xmlDocSetRootElement(masterDocument, xao);
xmlNewProp(xao, C_ATTR_XAO_VERSION, BAD_CAST xaoObject->getVersion());
xmlNewProp(xao, C_ATTR_XAO_AUTHOR, BAD_CAST xaoObject->getAuthor());
if (xaoObject->getGeometry() != NULL)
{
exportGeometry(xaoObject->getGeometry(), masterDocument, xao);
}
exportGroups(xaoObject, xao);
exportFields(xaoObject, xao);
return masterDocument;
}
void XaoExporter::exportGeometricElements(Geometry* xaoGeometry,
xmlNodePtr topology, Dimension dim, const xmlChar* colTag, const xmlChar* eltTag)
{
xmlNodePtr vertices = xmlNewChild(topology, 0, colTag, 0);
xmlNewProp(vertices, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoGeometry->countVertices()).c_str());
GeometricElementList::iterator it = xaoGeometry->begin(dim);
for (; it != xaoGeometry->end(dim); it++)
{
int index = it->first;
GeometricElement elt = it->second;
xmlNodePtr vertex = xmlNewChild(vertices, 0, eltTag, 0);
xmlNewProp(vertex, C_ATTR_ELT_INDEX, BAD_CAST XaoUtils::intToString(index).c_str());
//xmlNewProp(vertex, C_ATTR_ELT_NAME, BAD_CAST xaoGeometry->getVertexName(i));
//xmlNewProp(vertex, C_ATTR_ELT_REFERENCE, BAD_CAST xaoGeometry->getVertexReference(i));
xmlNewProp(vertex, C_ATTR_ELT_NAME, BAD_CAST elt.getName());
xmlNewProp(vertex, C_ATTR_ELT_REFERENCE, BAD_CAST elt.getReference());
}
}
void XaoExporter::exportGeometry(Geometry* xaoGeometry, xmlDocPtr doc, xmlNodePtr xao)
{
// Geometric part
xmlNodePtr geometry = xmlNewChild(xao, 0, C_TAG_GEOMETRY, 0);
xmlNewProp(geometry, C_ATTR_GEOMETRY_NAME, BAD_CAST xaoGeometry->getName());
xmlNodePtr shape = xmlNewChild(geometry, 0, C_TAG_SHAPE, 0);
xmlNewProp(shape, C_ATTR_SHAPE_FORMAT, BAD_CAST xaoGeometry->getFormat());
const char* brep = xaoGeometry->getBREP();
xmlNodePtr cdata = xmlNewCDataBlock(doc, BAD_CAST brep, strlen(brep));
xmlAddChild(shape, cdata);
xmlNodePtr topology = xmlNewChild(geometry, 0, C_TAG_TOPOLOGY, 0);
exportGeometricElements(xaoGeometry, topology, VERTEX, C_TAG_VERTICES, C_TAG_VERTEX);
exportGeometricElements(xaoGeometry, topology, EDGE, C_TAG_EDGES, C_TAG_EDGE);
exportGeometricElements(xaoGeometry, topology, FACE, C_TAG_FACES, C_TAG_FACE);
exportGeometricElements(xaoGeometry, topology, SOLID, C_TAG_SOLIDS, C_TAG_SOLID);
}
void XaoExporter::exportGroups(Xao* xaoObject, xmlNodePtr xao)
{
xmlNodePtr groups = xmlNewChild(xao, 0, C_TAG_GROUPS, 0);
xmlNewProp(groups, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoObject->countGroups()).c_str());
//for (std::list<Group*>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
for (int i = 0; i < xaoObject->countGroups(); i++)
{
//Group* grp = (*it);
Group* grp = xaoObject->getGroup(i);
xmlNodePtr group = xmlNewChild(groups, 0, C_TAG_GROUP, 0);
xmlNewProp(group, C_ATTR_GROUP_NAME, BAD_CAST grp->getName());
xmlNewProp(group, C_ATTR_GROUP_DIM, BAD_CAST XaoUtils::dimensionToString(grp->getDimension()));
xmlNewProp(group, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(grp->getCount()).c_str());
for (int j = 0; j < grp->getCount(); j++)
{
xmlNodePtr elt = xmlNewChild(group, 0, C_TAG_ELEMENT, 0);
xmlNewProp(elt, C_ATTR_ELEMENT_INDEX, BAD_CAST XaoUtils::intToString(grp->getElement(j)).c_str());
}
}
}
void XaoExporter::exportFields(Xao* xaoObject, xmlNodePtr xao)
{
xmlNodePtr fields = xmlNewChild(xao, 0, C_TAG_FIELDS, 0);
xmlNewProp(fields, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(xaoObject->countFields()).c_str());
for (int i = 0; i < xaoObject->countFields(); i++)
{
Field* field = xaoObject->getField(i);
xmlNodePtr nodeField = xmlNewChild(fields, 0, C_TAG_FIELD, 0);
xmlNewProp(nodeField, C_ATTR_FIELD_NAME, BAD_CAST field->getName().c_str());
xmlNewProp(nodeField, C_ATTR_FIELD_TYPE, BAD_CAST XaoUtils::fieldTypeToString(field->getType()));
xmlNewProp(nodeField, C_ATTR_FIELD_DIMENSION, BAD_CAST XaoUtils::dimensionToString(field->getDimension()));
int nbComponents = field->countComponents();
xmlNodePtr components = xmlNewChild(nodeField, 0, C_TAG_COMPONENTS, 0);
xmlNewProp(components, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(nbComponents).c_str());
for (int j = 0; j < nbComponents; j++)
{
xmlNodePtr nodeComponent = xmlNewChild(components, 0, C_TAG_COMPONENT, 0);
xmlNewProp(nodeComponent, C_ATTR_COMPONENT_COLUMN, BAD_CAST XaoUtils::intToString(j).c_str());
xmlNewProp(nodeComponent, C_ATTR_COMPONENT_NAME, BAD_CAST field->getComponentName(j).c_str());
}
int nbSteps = field->countSteps();
xmlNodePtr nodeSteps = xmlNewChild(nodeField, 0, C_TAG_STEPS, 0);
xmlNewProp(nodeSteps, C_ATTR_COUNT, BAD_CAST XaoUtils::intToString(nbSteps).c_str());
//for (int j = 0; j < nbSteps; j++)
// for (stepIterator itStep = field->begin(); itStep != field->end(); itStep++)
// {
// Step* step = (*itStep).second;
// exportStep(step, field, nodeSteps);
// }
}
}
void XaoExporter::exportStep(Step* step, Field* field, xmlNodePtr nodeSteps)
{
xmlNodePtr nodeStep = xmlNewChild(nodeSteps, 0, C_TAG_STEP, 0);
xmlNewProp(nodeStep, C_ATTR_STEP_NUMBER, BAD_CAST XaoUtils::intToString(step->getStep()).c_str());
if (step->getStamp() >= 0)
{
xmlNewProp(nodeStep, C_ATTR_STEP_STAMP, BAD_CAST XaoUtils::intToString(step->getStamp()).c_str());
}
/*
for(stepElementIterator itElt = step->begin(); itElt != step->end(); itElt++)
{
StepElement* elt = (*itElt);
xmlNodePtr nodeElt = xmlNewChild(nodeStep, 0, C_TAG_ELEMENT, 0);
xmlNewProp(nodeElt, C_ATTR_ELEMENT_INDEX, BAD_CAST XaoUtils::intToString(elt->getElement()).c_str());
for (int i = 0; i < elt->getNbComponents(); i++)
{
// TODO: do other type
std::string content;
switch (field->getType())
{
case FIELD_BOOLEAN:
{
BooleanStepElement* boolElt = (BooleanStepElement*)elt;
content = (boolElt->getValue(i) ? "true" : "false");
break;
}
case FIELD_STRING:
{
StringStepElement* strElt = (StringStepElement*)elt;
content = strElt->getValue(i);
break;
}
case FIELD_INTEGER:
{
IntegerStepElement* intElt = (IntegerStepElement*)elt;
content = XaoUtils::intToString(intElt->getValue(i));
break;
}
case FIELD_DOUBLE:
{
DoubleStepElement* dElt = (DoubleStepElement*)elt;
content = XaoUtils::doubleToString(dElt->getValue(i));
break;
}
}
xmlNodePtr nodeValue= xmlNewTextChild(nodeElt, 0, C_TAG_VALUE, BAD_CAST content.c_str());
xmlNewProp(nodeValue, C_ATTR_VALUE_COMPONENT, BAD_CAST XaoUtils::intToString(i).c_str());
}
}*/
}
bool XaoExporter::readFromFile(const char* fileName, Xao* xaoObject)
{
// parse the file and get the DOM
int options = XML_PARSE_HUGE || XML_PARSE_NOCDATA;
xmlDocPtr doc = xmlReadFile(fileName, NULL, options);
if (doc == NULL)
{
throw SALOME_Exception("Cannot read XAO file");
}
parseXMLDoc(doc, xaoObject);
return true;
}
bool XaoExporter::setXML(const char* xml, Xao* xaoObject)
{
int options = XML_PARSE_HUGE || XML_PARSE_NOCDATA;
xmlDocPtr doc = xmlReadDoc(BAD_CAST xml, "", NULL, options);
if (doc == NULL)
{
throw SALOME_Exception("Cannot read XAO stream");
}
parseXMLDoc(doc, xaoObject);
return true;
}
void XaoExporter::parseXMLDoc(xmlDocPtr doc, Xao* xaoObject)
{
// Get the root element node
xmlNodePtr root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name , C_TAG_XAO) != 0)
throw SALOME_Exception("Cannot read XAO file: invalid format XAO node not found");
parseXaoNode(doc, root, xaoObject);
xmlFreeDoc(doc); // free document
xmlCleanupParser(); // free globals
}
void XaoExporter::parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode, Xao* xaoObject)
{
xmlChar* version = xmlGetProp(xaoNode, C_ATTR_XAO_VERSION);
if (version != NULL)
{
xaoObject->setVersion((char*)version);
xmlFree(version);
}
xmlChar* author = xmlGetProp(xaoNode, C_ATTR_XAO_AUTHOR);
if (author != NULL)
{
xaoObject->setAuthor((char*)author);
xmlFree(author);
}
for (xmlNodePtr node = xaoNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_GEOMETRY) == 0)
parseGeometryNode(doc, node, xaoObject);
else if (xmlStrcmp(node->name, C_TAG_GROUPS) == 0)
parseGroupsNode(node, xaoObject);
}
}
void XaoExporter::parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode, Xao* xaoObject)
{
Geometry* geometry = new Geometry();
xmlChar* name = xmlGetProp(geometryNode, C_ATTR_GEOMETRY_NAME);
if (name != NULL)
{
geometry->setName((char*)name);
xmlFree(name);
}
for (xmlNodePtr node = geometryNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_SHAPE) == 0)
parseShapeNode(doc, node, geometry);
else if (xmlStrcmp(node->name, C_TAG_TOPOLOGY) == 0)
parseTopologyNode(node, geometry);
}
xaoObject->setGeometry(geometry);
}
void XaoExporter::parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode, Geometry* geometry)
{
xmlChar* shapeType = xmlGetProp(shapeNode, C_ATTR_SHAPE_FORMAT);
if (xmlStrcmp(shapeType, (xmlChar*)"BREP") == 0)
{
xmlChar* data = xmlNodeGetContent(shapeNode->children);
if (data == NULL)
throw SALOME_Exception("Missing BREP");
geometry->setShape((char*)data);
xmlFree(data);
}
else
{
throw SALOME_Exception("Shape format not supported");
}
}
void XaoExporter::parseTopologyNode(xmlNodePtr topologyNode, Geometry* geometry)
{
for (xmlNodePtr node = topologyNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_VERTICES) == 0)
parseVerticesNode(node, geometry);
else if (xmlStrcmp(node->name, C_TAG_EDGES) == 0)
parseEdgesNode(node, geometry);
else if (xmlStrcmp(node->name, C_TAG_FACES) == 0)
parseFacesNode(node, geometry);
else if (xmlStrcmp(node->name, C_TAG_SOLIDS) == 0)
parseSolidsNode(node, geometry);
}
}
void XaoExporter::parseVerticesNode(xmlNodePtr verticesNode, Geometry* geometry)
{
xmlChar* count = xmlGetProp(verticesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for vertices");
geometry->setCountVertices(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = verticesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_VERTEX) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for vertex");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for vertex");
geometry->setVertex(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void XaoExporter::parseEdgesNode(xmlNodePtr edgesNode, Geometry* geometry)
{
xmlChar* count = xmlGetProp(edgesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for edges");
geometry->setCountEdges(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = edgesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_EDGE) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for edge");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for edge");
geometry->setEdge(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void XaoExporter::parseFacesNode(xmlNodePtr facesNode, Geometry* geometry)
{
xmlChar* count = xmlGetProp(facesNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for faces");
geometry->setCountFaces(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = facesNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_FACE) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for face");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for face");
geometry->setFace(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void XaoExporter::parseSolidsNode(xmlNodePtr solidsNode, Geometry* geometry)
{
xmlChar* count = xmlGetProp(solidsNode, C_ATTR_COUNT);
if (count == NULL)
throw SALOME_Exception("No count attribute for solids");
geometry->setCountSolids(atoi((char*)count));
xmlFree(count);
for (xmlNodePtr node = solidsNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_SOLID) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for solid");
xmlChar* name = xmlGetProp(node, C_ATTR_ELT_NAME);
if (name == NULL)
name = (xmlChar*)"";
xmlChar* reference = xmlGetProp(node, C_ATTR_ELT_REFERENCE);
if (reference == NULL)
throw SALOME_Exception("Bad reference for solid");
geometry->setSolid(atoi((char*)index), (char*)name, (char*)reference);
}
}
}
void XaoExporter::parseGroupsNode(xmlNodePtr groupsNode, Xao* xaoObject)
{
for (xmlNodePtr node = groupsNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_GROUP) == 0)
{
parseGroupNode(node, xaoObject);
}
}
}
void XaoExporter::parseGroupNode(xmlNodePtr groupNode, Xao* xaoObject)
{
xmlChar* dimension = xmlGetProp(groupNode, C_ATTR_GROUP_DIM);
if (dimension == NULL)
throw SALOME_Exception("Bad dimension for group");
Group* group = new Group();
xmlChar* name = xmlGetProp(groupNode, C_ATTR_GROUP_NAME);
if (name == NULL) name = (xmlChar*)"";
group->setName((char*)name);
xmlFree(name);
group->setDimension(XaoUtils::stringToDimension((char*)dimension));
xmlFree(dimension);
xaoObject->addGroup(group);
for (xmlNodePtr node = groupNode->children; node; node = node->next)
{
if (xmlStrcmp(node->name, C_TAG_ELEMENT) == 0)
{
xmlChar* index = xmlGetProp(node, C_ATTR_ELEMENT_INDEX);
if (index == NULL)
throw SALOME_Exception("Bad index for group element");
group->addElement(atoi((char*)index));
xmlFree(index);
}
}
}

46
src/XAO/XaoExporter.hxx Normal file
View File

@ -0,0 +1,46 @@
#ifndef __XAO_XAOEXPORTER_HXX__
#define __XAO_XAOEXPORTER_HXX__
#include <libxml/parser.h>
#include "Xao.hxx"
#include "Geometry.hxx"
#include "Group.hxx"
#include "Field.hxx"
namespace XAO
{
class XaoExporter
{
public:
static bool saveToFile(Xao* xaoObject, const char* fileName);
static const char* saveToXml(Xao* xaoObject);
static bool readFromFile(const char* fileName, Xao* xaoObject);
static bool setXML(const char* xml, Xao* xaoObject);
private:
static xmlDocPtr exportXMLDoc(Xao* xaoObject);
static void exportGeometry(Geometry* xaoGeometry, xmlDocPtr doc, xmlNodePtr xao);
static void exportGeometricElements(Geometry* xaoGeometry, xmlNodePtr topology,
Dimension dim, const xmlChar* colTag, const xmlChar* eltTag);
static void exportGroups(Xao* xaoObject, xmlNodePtr xao);
static void exportFields(Xao* xaoObject, xmlNodePtr xao);
static void exportStep(Step* step, Field* field, xmlNodePtr nodeSteps);
static void parseXMLDoc(xmlDocPtr doc, Xao* xaoObject);
static void parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode, Xao* xaoObject);
static void parseGeometryNode(xmlDocPtr doc, xmlNodePtr geometryNode, Xao* xaoObject);
static void parseShapeNode(xmlDocPtr doc, xmlNodePtr shapeNode, Geometry* geometry);
static void parseTopologyNode(xmlNodePtr topologyNode, Geometry* geometry);
static void parseVerticesNode(xmlNodePtr verticesNode, Geometry* geometry);
static void parseEdgesNode(xmlNodePtr edgesNode, Geometry* geometry);
static void parseFacesNode(xmlNodePtr facesNode, Geometry* geometry);
static void parseSolidsNode(xmlNodePtr solidsNode, Geometry* geometry);
static void parseGroupsNode(xmlNodePtr groupsNode, Xao* xaoObject);
static void parseGroupNode(xmlNodePtr groupNode, Xao* xaoObject);
};
}
#endif /* __XAO_XAOEXPORTER_HXX__ */

View File

@ -24,19 +24,42 @@
#include <Utils_SALOME_Exception.hxx>
#include "Xao.hxx"
#include "Field.hxx"
#include "XaoUtils.hxx"
using namespace XAO;
const char* XaoUtils::intToString(const int value)
/*const char* XaoUtils::intToString(const int value)
{
std::stringstream str;
std::ostringstream str;
str << value;
return str.str().c_str();
}*/
const std::string XaoUtils::intToString(const int value)
{
std::ostringstream str;
str << value;
return str.str();
}
const char* XaoUtils::dimensionToString(const Dimension dimension)
const int XaoUtils::stringToInt(const std::string value)
{
int res;
std::istringstream convert(value);
if ( !(convert >> res) )
res = 0;
return res;
}
const std::string XaoUtils::doubleToString(const double value)
{
std::ostringstream str;
str << value;
return str.str();
}
const char* XaoUtils::dimensionToString(const XAO::Dimension dimension)
{
if (dimension == VERTEX)
return "vertex";
@ -46,10 +69,12 @@ const char* XaoUtils::dimensionToString(const Dimension dimension)
return "face";
if (dimension == SOLID)
return "solid";
if (dimension == WHOLE)
return "whole";
throw SALOME_Exception("Bad dimension");
}
const Dimension XaoUtils::stringToDimension(const char* dimension)
const XAO::Dimension XaoUtils::stringToDimension(const char* dimension)
{
if (strcmp(dimension, "vertex") == 0)
return VERTEX;
@ -59,5 +84,33 @@ const Dimension XaoUtils::stringToDimension(const char* dimension)
return FACE;
if (strcmp(dimension, "solid") == 0)
return SOLID;
if (strcmp(dimension, "whole") == 0)
return WHOLE;
throw SALOME_Exception("Bad dimension");
}
const char* XaoUtils::fieldTypeToString(const XAO::Type type)
{
if (type == BOOLEAN)
return "boolean";
if (type == INTEGER)
return "integer";
if (type == DOUBLE)
return "double";
if (type == STRING)
return "string";
throw SALOME_Exception("Bad type");
}
const XAO::Type XaoUtils::stringToFieldType(const char* type)
{
if (strcmp(type, "boolean") == 0)
return BOOLEAN;
if (strcmp(type, "integer") == 0)
return INTEGER;
if (strcmp(type, "double") == 0)
return DOUBLE;
if (strcmp(type, "string") == 0)
return STRING;
throw SALOME_Exception("Bad type");
}

View File

@ -22,6 +22,7 @@
#define __XAO_UTILS_HXX__
#include "Xao.hxx"
#include "Field.hxx"
namespace XAO
{
@ -37,7 +38,11 @@ namespace XAO
* \param value the integer to convert.
* \return the string.
*/
static const char* intToString(const int value);
//static const char* intToString(const int value);
static const std::string intToString(const int value);
static const int stringToInt(const std::string value);
static const std::string doubleToString(const double value);
/**
* Converts a Dimension to string.
@ -45,15 +50,31 @@ namespace XAO
* \return the dimension as a string.
* \throw SALOME_Exception
*/
static const char* dimensionToString(const Dimension dimension);
static const char* dimensionToString(const XAO::Dimension dimension);
/**
* Converts a string into a Dimension.
* \param dimension the dimension as a string.
* \return the converted Dimension.
* \throw SALOME_Exception if
* \throw SALOME_Exception
*/
static const Dimension stringToDimension(const char* dimension);
static const XAO::Dimension stringToDimension(const char* dimension);
/**
* Converts a Type to string.
* \param type the Type to convert.
* \return the Type as a string.
* \throw SALOME_Exception
*/
static const char* fieldTypeToString(const XAO::Type type);
/**
* Converts a string into a Type.
* \param type the Type as a string.
* \return the converted Type.
* \throw SALOME_Exception
*/
static const XAO::Type stringToFieldType(const char* type);
};
}