add generic method to get element by type

use enum instead of integer
This commit is contained in:
fps 2013-04-25 14:46:57 +00:00
parent f74d6b98cb
commit e2e1c99ebc
15 changed files with 509 additions and 169 deletions

View File

@ -18,31 +18,32 @@
// //
// Author : Nathalie Gore (OpenCascade) // Author : Nathalie Gore (OpenCascade)
#include "Xao.hxx"
#include "Field.hxx" #include "Field.hxx"
using namespace XAO; using namespace XAO;
Field::Field() template<typename T>
Field<T>::Field(const FieldDimension dim, const int nbComponents)
: m_name(""), m_dimension(dim)
{ {
_myName = ""; m_components.reserve(nbComponents);
_myDimension = -1;
_myType = -1;
_myValuesCount = 0;
_myComponentCount = 0;
_myComponentNames = NULL;
_myStepCount = 0;
_mySteps = NULL;
_myStamps = NULL;
_myValues = NULL;
} }
Field::~Field() template<typename T>
Field<T>::Field(const char* name, const FieldDimension dim, const int nbComponents)
: m_name(name), m_dimension(dim)
{ {
m_components.reserve(nbComponents);
} }
Field *Field::New() template<typename T>
void Field<T>::setComponentName(const int index, const char* name)
{ {
return new Field; if (index < m_components.size())
{
m_components[index] = name;
}
// TODO: throw
} }

View File

@ -18,62 +18,112 @@
// //
// Author : Nathalie Gore (OpenCascade) // Author : Nathalie Gore (OpenCascade)
#ifndef __XAO_XAO_HXX__ #ifndef __XAO_FIELD_HXX__
#define __XAO_XAO_HXX__ #define __XAO_FIELD_HXX__
#include <string> #include <string>
#include <vector>
#include <map>
#include "Xao.hxx"
namespace XAO namespace XAO
{ {
typedef enum enum FieldType
{ {
VERTEX = 0, FIELD_BOOLEAN = 0,
EDGE = 1, FIELD_INTEGER = 1,
FACE = 2, FIELD_DOUBLE = 2,
SOLID = 3 FIELD_STRING = 3
} FieldDimension; };
typedef enum enum FieldDimension
{ {
BOOLEAN = 0, FIELD_VERTEX = 0,
INTEGER = 1, FIELD_EDGE = 1,
DOUBLE = 2, FIELD_FACE = 2,
STRING = 3 FIELD_SOLID = 3,
} FieldType; FIELD_WHOLE = -1
};
class Field template <typename T>
{ class Step
public: {
static Field *New(); public:
void setName(const char *name) { _myName=name; } private:
const char *getName() const { return _myName.c_str(); } int m_number;
void setDimension(int nb) { _myDimension=nb; } int m_stamp;
int getDimension() { return _myDimension; } std::map<int, T> m_values;
void setType(int type) { _myType=type; }
int getType() { return _myType; }
void setValuesCount(int nb) { _myValuesCount=nb; }
int getValuesCount() { return _myValuesCount; }
void setComponentCount(int nb) { _myComponentCount=nb; }
int getComponentCount() { return _myComponentCount; }
void setStepCount(int nb) { _myStepCount=nb; }
int getStepCount() { return _myStepCount; }
private: };
Field();
~Field();
private: class IField
std::string _myName; {
int _myDimension; public:
int _myType; virtual const char* getName() const = 0;
int _myValuesCount; virtual void setName(const char* name) = 0;
int _myComponentCount; virtual const FieldType getType() = 0;
std::string *_myComponentNames; virtual const FieldDimension getDimension() = 0;
int _myStepCount; virtual void setComponentName(const int index, const char* name) = 0;
int *_mySteps; virtual const int countComponents() = 0;
double *_myStamps; virtual const int getStepCount() = 0;
std::string **_myValues; };
};
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
{
return m_name.c_str();
}
void setName(const char* name)
{
m_name = name;
}
virtual const FieldType getType();
const FieldDimension getDimension()
{
return m_dimension;
}
void setComponentName(const int index, const char* name);
const int countComponents()
{
return m_components.size();
}
const int getStepCount()
{
return m_steps.size();
}
private:
std::string m_name;
FieldDimension m_dimension;
FieldType m_type;
std::vector<std::string> m_components;
std::list< Step<T>* > 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;
}
};
} }
#endif #endif

View File

@ -40,7 +40,6 @@ GeometricElement::GeometricElement(const char* name, const char* reference)
GeometricElement::~GeometricElement() GeometricElement::~GeometricElement()
{ {
} }
GeometricElementList::GeometricElementList() GeometricElementList::GeometricElementList()
@ -83,7 +82,9 @@ const char* GeometricElementList::getName(const int index)
void GeometricElementList::setName(const int index, const char* name) void GeometricElementList::setName(const int index, const char* name)
{ {
if (m_count == 0 || index > m_count) if (m_count == 0 || index > m_count)
{
throw SALOME_Exception("Problem with number of elements"); throw SALOME_Exception("Problem with number of elements");
}
m_elements[index].setName(name); m_elements[index].setName(name);
} }
@ -113,9 +114,5 @@ const int GeometricElementList::getIndexByReference(const char* ref)
return index; return index;
} }
} }
return -1;
return 0;
// std::string msg = "Cannot find element with reference ";
// msg += name;
// throw SALOME_Exception(msg.c_str());
} }

View File

@ -24,32 +24,59 @@
#include <string> #include <string>
#include <map> #include <map>
namespace XAO namespace XAO
{ {
/**
* \class GeometricElement
* Generic class to manipulate a topologic element (vertex, edge, face or solid).
*/
class GeometricElement class GeometricElement
{ {
public: public:
/**
* Default constructor.
*/
GeometricElement(); GeometricElement();
/**
* Constructor with name and reference.
* \param name the name of the element.
* \param reference the reference of the element.
*/
GeometricElement(const char* name, const char* reference); GeometricElement(const char* name, const char* reference);
~GeometricElement(); /**
* Destructor.
*/
virtual ~GeometricElement();
/**
* Gets the name of the element.
* \return the name.
*/
const char* getName() const char* getName()
{ {
return m_name.c_str(); return m_name.c_str();
} }
/**
* Sets the name of the element
* \param name the name to set.
*/
void setName(const char* name) void setName(const char* name)
{ {
m_name = name; m_name = name;
} }
/**
* Gets the reference of the element.
* \return the reference.
*/
const char* getReference() const char* getReference()
{ {
return m_reference.c_str(); return m_reference.c_str();
} }
/**
* Sets the reference of the element.
* \param reference the reference to set.
*/
void setReference(const char* reference) void setReference(const char* reference)
{ {
m_reference = reference; m_reference = reference;
@ -60,21 +87,82 @@ namespace XAO
std::string m_reference; std::string m_reference;
}; };
/**
* \class GeometricElementList
* Generic class to manipulate a list of topologic element.
*/
class GeometricElementList class GeometricElementList
{ {
public: public:
/**
* Default constructor.
*/
GeometricElementList(); GeometricElementList();
/**
* Constructor with size.
* \param nb the size to set.
*/
GeometricElementList(const int nb); GeometricElementList(const int nb);
~GeometricElementList() {} /**
* Destructor.
*/
virtual ~GeometricElementList() {}
/**
* Gets the size of the list.
* \return the size of the list.
*/
int getSize() { return m_count; } int getSize() { return m_count; }
/**
* Sets the size of the list.
* \param nb the size to set.
* \warning the list will be cleared.
*/
void setSize(const int nb); void setSize(const int nb);
/**
* Sets the name and the reference of an element.
* \param index the index of the element to set.
* \param name the name to set.
* \param reference the reference to set.
* \throw SALOME_Exception if index is bigger than the size of the list.
*/
void setElement(const int index, const char* name, const char* reference); void setElement(const int index, const char* name, const char* reference);
/**
* Gets the name of an element.
* \param index the index of the element to set.
* \return the name of the element with the given index.
* \throw SALOME_Exception if index is bigger than the size of the list.
*/
const char* getName(const int index); const char* getName(const int index);
/**
* Sets the name of an element.
* \param index the index of the element.
* \param name the name to set.
* \throw SALOME_Exception if index is bigger than the size of the list.
*/
void setName(const int index, const char* name); void setName(const int index, const char* name);
/**
* Gets the reference of an element.
* \param index the index of the element.
* \return the reference of the element.
* \throw SALOME_Exception if index is bigger than the size of the list.
*/
const char* getReference(const int index); const char* getReference(const int index);
/**
* Sets the reference of an element.
* \param index the index of the element to set.
* \param reference the reference to set.
* \throw SALOME_Exception if index is bigger than the size of the list.
*/
void setReference(const int index, const char* reference); void setReference(const int index, const char* reference);
/**
* Gets the index of an element using its reference.
* \param reference the searched reference.
* \return the index of the element or -1 if no element found.
*/
const int getIndexByReference(const char* reference); const int getIndexByReference(const char* reference);
private: private:

View File

@ -43,6 +43,8 @@
#include <TColStd_HArray1OfInteger.hxx> #include <TColStd_HArray1OfInteger.hxx>
#include <TColStd_HSequenceOfInteger.hxx> #include <TColStd_HSequenceOfInteger.hxx>
#include <Utils_SALOME_Exception.hxx>
using namespace XAO; using namespace XAO;
Geometry::Geometry() Geometry::Geometry()
@ -105,42 +107,40 @@ void Geometry::initListIds(const Standard_Integer shapeType)
TopTools_IndexedMapOfShape indices; TopTools_IndexedMapOfShape indices;
TopExp::MapShapes(m_shape, indices); TopExp::MapShapes(m_shape, indices);
//Handle (TColStd_HArray1OfInteger) anArray;
std::list<int> indexList; std::list<int> indexList;
TopTools_ListIteratorOfListOfShape itSub(listShape); TopTools_ListIteratorOfListOfShape itSub(listShape);
for (int index = 1; itSub.More(); itSub.Next(), ++index) for (int index = 1; itSub.More(); itSub.Next(), ++index)
{ {
TopoDS_Shape value = itSub.Value(); TopoDS_Shape value = itSub.Value();
//std::cout << "index = " << indices.FindIndex(value) << std::endl;
indexList.push_back(indices.FindIndex(value)); indexList.push_back(indices.FindIndex(value));
} }
std::list<int>::iterator it = indexList.begin(); std::list<int>::iterator it = indexList.begin();
switch (shapeType) switch (shapeType)
{ {
case TopAbs_VERTEX: /* Fill vertices ids */ case TopAbs_VERTEX:
{ {
m_vertices.setSize(indexList.size()); m_vertices.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++) for (int i = 0; it != indexList.end(); it++, i++)
m_vertices.setReference(i, XaoUtils::intToString((*it))); m_vertices.setReference(i, XaoUtils::intToString((*it)));
break; break;
} }
case TopAbs_EDGE: /* Fill edges ids */ case TopAbs_EDGE:
{ {
m_edges.setSize(indexList.size()); m_edges.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++) for (int i = 0; it != indexList.end(); it++, i++)
m_edges.setReference(i, XaoUtils::intToString((*it))); m_edges.setReference(i, XaoUtils::intToString((*it)));
break; break;
} }
case TopAbs_FACE: /* Fill faces ids */ case TopAbs_FACE:
{ {
m_faces.setSize(indexList.size()); m_faces.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++) for (int i = 0; it != indexList.end(); it++, i++)
m_faces.setReference(i, XaoUtils::intToString((*it))); m_faces.setReference(i, XaoUtils::intToString((*it)));
break; break;
} }
case TopAbs_SOLID: /* Fill solids ids */ case TopAbs_SOLID:
{ {
m_solids.setSize(indexList.size()); m_solids.setSize(indexList.size());
for (int i = 0; it != indexList.end(); it++, i++) for (int i = 0; it != indexList.end(); it++, i++)
@ -148,6 +148,34 @@ void Geometry::initListIds(const Standard_Integer shapeType)
break; break;
} }
} }
}
const char* Geometry::getElementReference(const Dimension dim, const int index)
{
if (dim == VERTEX)
return getVertexReference(index);
if (dim == EDGE)
return getEdgeReference(index);
if (dim == FACE)
return getFaceReference(index);
if (dim == SOLID)
return getSolidReference(index);
std::cout << "getElementReference: unknown dimension" << std::endl;
throw SALOME_Exception("Unknown dimension");
}
const int Geometry::getElementIndexByReference(const Dimension dim, const char* reference)
{
if (dim == VERTEX)
return getVertexIndexByReference(reference);
if (dim == EDGE)
return getEdgeIndexByReference(reference);
if (dim == FACE)
return getFaceIndexByReference(reference);
if (dim == SOLID)
return getSolidIndexByReference(reference);
std::cout << "getElementIndexByReference: unknown dimension" << std::endl;
throw SALOME_Exception("Unknown dimension");
} }

View File

@ -26,7 +26,8 @@
#include <SALOMEconfig.h> #include <SALOMEconfig.h>
#include <TopoDS_Shape.hxx> #include <TopoDS_Shape.hxx>
# include "GeometricElement.hxx" #include "Xao.hxx"
#include "GeometricElement.hxx"
namespace XAO namespace XAO
{ {
@ -92,6 +93,7 @@ namespace XAO
const char* getEdgeReference(const int index) { return m_edges.getReference(index); } const char* getEdgeReference(const int index) { return m_edges.getReference(index); }
const char* getFaceReference(const int index) { return m_faces.getReference(index); } const char* getFaceReference(const int index) { return m_faces.getReference(index); }
const char* getSolidReference(const int index) { return m_solids.getReference(index); } const char* getSolidReference(const int index) { return m_solids.getReference(index); }
const char* getElementReference(const Dimension dim, const int index);
void setVertexReference(const int index, const char* reference) { m_vertices.setReference(index, reference); } void setVertexReference(const int index, const char* reference) { m_vertices.setReference(index, reference); }
void setEdgeReference(const int index, const char* reference) { m_edges.setReference(index, reference); } void setEdgeReference(const int index, const char* reference) { m_edges.setReference(index, reference); }
@ -102,6 +104,7 @@ namespace XAO
const int getEdgeIndexByReference(const char* reference) { return m_edges.getIndexByReference(reference); } const int getEdgeIndexByReference(const char* reference) { return m_edges.getIndexByReference(reference); }
const int getFaceIndexByReference(const char* reference) { return m_faces.getIndexByReference(reference); } const int getFaceIndexByReference(const char* reference) { return m_faces.getIndexByReference(reference); }
const int getSolidIndexByReference(const char* reference) { return m_solids.getIndexByReference(reference); } const int getSolidIndexByReference(const char* reference) { return m_solids.getIndexByReference(reference); }
const int getElementIndexByReference(const Dimension dim, const char* reference);
private: private:
void initListIds(const Standard_Integer shapeType); void initListIds(const Standard_Integer shapeType);

View File

@ -19,12 +19,13 @@
// Author : Nathalie Gore (OpenCascade), Frederic Pons (OpenCascade) // Author : Nathalie Gore (OpenCascade), Frederic Pons (OpenCascade)
#include "Group.hxx" #include "Group.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO; using namespace XAO;
Group::Group() Group::Group()
{ {
m_dimension = 0; m_dimension = VERTEX;
m_count = 0; m_count = 0;
} }
@ -32,3 +33,12 @@ Group::~Group()
{ {
} }
const Dimension Group::getDimension()
{
return m_dimension;
}
void Group::setDimension(const Dimension dimension)
{
m_dimension = dimension;
}

View File

@ -25,6 +25,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Xao.hxx"
namespace XAO namespace XAO
{ {
class Group class Group
@ -42,14 +44,8 @@ namespace XAO
return m_name.c_str(); return m_name.c_str();
} }
void setDimension(int dimension) const Dimension getDimension();
{ void setDimension(const Dimension dim);
m_dimension = dimension;
}
int getDimension()
{
return m_dimension;
}
int getCount() int getCount()
{ {
@ -68,7 +64,7 @@ namespace XAO
private: private:
std::string m_name; std::string m_name;
int m_dimension; Dimension m_dimension;
int m_count; int m_count;
std::vector<int> m_elements; std::vector<int> m_elements;
}; };

View File

@ -76,7 +76,6 @@ Xao::Xao()
m_author = ""; m_author = "";
m_version = (char*)C_XAO_VERSION; m_version = (char*)C_XAO_VERSION;
m_geometry = NULL; m_geometry = NULL;
m_nbGroups = 0;
} }
Xao::Xao(const char* author, const char* version) Xao::Xao(const char* author, const char* version)
@ -84,7 +83,6 @@ Xao::Xao(const char* author, const char* version)
m_author = author; m_author = author;
m_version = version; m_version = version;
m_geometry = NULL; m_geometry = NULL;
m_nbGroups = 0;
} }
Xao::~Xao() Xao::~Xao()
@ -101,6 +99,11 @@ Xao::~Xao()
// } // }
} }
int Xao::countGroups()
{
return m_groups.size();
}
Group* Xao::getGroup(const int index) Group* Xao::getGroup(const int index)
{ {
int i = 0; int i = 0;
@ -113,8 +116,46 @@ Group* Xao::getGroup(const int index)
return NULL; return NULL;
} }
void Xao::addGroup(Group* group)
{
m_groups.push_back(group);
}
bool Xao::exportToFile(const char* fileName) void Xao::removeGroup(Group* group)
{
m_groups.remove(group);
}
int Xao::countFields()
{
return m_fields.size();
}
IField* Xao::getField(const int index)
{
int i = 0;
for (std::list<IField*>::iterator it = m_fields.begin(); it != m_fields.end(); ++it, ++i)
{
if (i == index)
return (*it);
}
return NULL;
}
void Xao::addField(IField* field)
{
m_fields.push_back(field);
}
void Xao::removeField(IField* field)
{
m_fields.remove(field);
}
bool Xao::exportXAO(const char* fileName)
{ {
xmlDocPtr doc = exportXMLDoc(); xmlDocPtr doc = exportXMLDoc();
xmlSaveFormatFileEnc(fileName, doc, "UTF-8", 2); xmlSaveFormatFileEnc(fileName, doc, "UTF-8", 2);
@ -236,10 +277,10 @@ void Xao::exportGroups(xmlNodePtr xao)
} }
} }
bool Xao::importFromFile(const char* fileName) bool Xao::importXAO(const char* fileName)
{ {
// parse the file and get the DOM // parse the file and get the DOM
int options = 16384; // merge cdata as text node int options = XML_PARSE_HUGE || XML_PARSE_NOCDATA;
xmlDocPtr doc = xmlReadFile(fileName, NULL, options); xmlDocPtr doc = xmlReadFile(fileName, NULL, options);
if (doc == NULL) if (doc == NULL)
{ {
@ -250,6 +291,19 @@ bool Xao::importFromFile(const char* fileName)
return true; return true;
} }
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) void Xao::parseXMLDoc(xmlDocPtr doc)
{ {
// Get the root element node // Get the root element node

View File

@ -27,72 +27,113 @@
namespace XAO namespace XAO
{ {
enum Kind /**
{ * @enum Dimension
VERTEX, */
EDGE, enum Dimension
FACE, {
SOLID VERTEX = 0,//!< VERTEX
}; EDGE = 1, //!< EDGE
FACE = 2, //!< FACE
SOLID = 3 //!< SOLID
};
class Geometry; class Geometry;
class Group; class Group;
class Field; class IField;
class Xao /**
{ * @class Xao
public: * The Xao class describes the XAO format.
Xao(); */
Xao(const char* author, const char* version); class Xao
~Xao(); {
public:
Xao();
Xao(const char* author, const char* version);
~Xao();
void setAuthor(const char* author) { m_author = author; } const char* getAuthor()
const char* getAuthor() { return m_author.c_str(); } {
return m_author.c_str();
}
void setAuthor(const char* author)
{
m_author = author;
}
void setVersion(const char* version) { m_version = version; } const char* getVersion()
const char* getVersion() { return m_version.c_str(); } {
return m_version.c_str();
}
void setVersion(const char* version)
{
m_version = version;
}
void setGeometry(Geometry* geometry) { m_geometry = geometry; } //
Geometry* getGeometry() { return m_geometry; } // Geometry
//
int countGroups() { return m_groups.size(); } Geometry* getGeometry()
Group* getGroup(const int index); {
void addGroup(Group* group) { m_groups.push_back(group); } return m_geometry;
void removeGroup(Group* group) { m_groups.remove(group); } }
void setGeometry(Geometry* geometry)
{
m_geometry = geometry;
}
bool exportToFile(const char* fileName); //
const char* getXML(); // Groups
//
bool importFromFile(const char* fileName); int countGroups();
bool setXML(const char* xml); Group* getGroup(const int index);
void addGroup(Group* group);
void removeGroup(Group* group);
//
// Fields
//
private: int countFields();
std::string m_author; IField* getField(const int index);
std::string m_version; void addField(IField* field);
Geometry* m_geometry; void removeField(IField* field);
int m_nbGroups;
std::list<Group*> m_groups;
//int m_nbFields;
//std::list<Field*> m_fields;
xmlDocPtr exportXMLDoc(); //
void exportGeometry(xmlDocPtr doc, xmlNodePtr xao); // Import / Export
void exportGroups(xmlNodePtr xao); //
bool exportXAO(const char* fileName);
const char* getXML();
void parseXMLDoc(xmlDocPtr doc); bool importXAO(const char* fileName);
void parseXaoNode(xmlDocPtr doc, xmlNodePtr xaoNode); bool setXML(const char* xml);
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);
}; private:
std::string m_author;
std::string m_version;
Geometry* m_geometry;
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);
};
} }

View File

@ -22,6 +22,8 @@
#include <sstream> #include <sstream>
#include <iosfwd> #include <iosfwd>
#include <Utils_SALOME_Exception.hxx> #include <Utils_SALOME_Exception.hxx>
#include "Xao.hxx"
#include "XaoUtils.hxx" #include "XaoUtils.hxx"
@ -34,28 +36,28 @@ const char* XaoUtils::intToString(const int value)
return str.str().c_str(); return str.str().c_str();
} }
const char* XaoUtils::dimensionToString(const int dimension) const char* XaoUtils::dimensionToString(const Dimension dimension)
{ {
if (dimension == 0) if (dimension == VERTEX)
return "vertex"; return "vertex";
if (dimension == 1) if (dimension == EDGE)
return "edge"; return "edge";
if (dimension == 2) if (dimension == FACE)
return "face"; return "face";
if (dimension == 3) if (dimension == SOLID)
return "solid"; return "solid";
throw SALOME_Exception("Bad dimension"); throw SALOME_Exception("Bad dimension");
} }
const int XaoUtils::stringToDimension(const char* dimension) const Dimension XaoUtils::stringToDimension(const char* dimension)
{ {
if (strcmp(dimension, "vertex") == 0) if (strcmp(dimension, "vertex") == 0)
return 0; return VERTEX;
if (strcmp(dimension, "edge") == 0) if (strcmp(dimension, "edge") == 0)
return 1; return EDGE;
if (strcmp(dimension, "face") == 0) if (strcmp(dimension, "face") == 0)
return 2; return FACE;
if (strcmp(dimension, "solid") == 0) if (strcmp(dimension, "solid") == 0)
return 3; return SOLID;
throw SALOME_Exception("Bad dimension"); throw SALOME_Exception("Bad dimension");
} }

View File

@ -21,14 +21,39 @@
#ifndef __XAO_UTILS_HXX__ #ifndef __XAO_UTILS_HXX__
#define __XAO_UTILS_HXX__ #define __XAO_UTILS_HXX__
#include "Xao.hxx"
namespace XAO namespace XAO
{ {
/**
* \class XaoUtils
* Utilities class to convert types.
*/
class XaoUtils class XaoUtils
{ {
public: public:
/**
* Converts an integer into a string.
* \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 char* dimensionToString(const int dimension);
static const int stringToDimension(const char* dimension); /**
* Converts a Dimension to string.
* \param dimension the Dimension to convert.
* \return the dimension as a string.
* \throw SALOME_Exception
*/
static const char* dimensionToString(const Dimension dimension);
/**
* Converts a string into a Dimension.
* \param dimension the dimension as a string.
* \return the converted Dimension.
* \throw SALOME_Exception if
*/
static const Dimension stringToDimension(const char* dimension);
}; };
} }

View File

@ -50,7 +50,7 @@ void ImportExportTest::testExportNoGeometry()
{ {
Xao xao("me", "1.0"); Xao xao("me", "1.0");
bool res = xao.exportToFile("empty.xao"); bool res = xao.exportXAO("empty.xao");
CPPUNIT_ASSERT(res); CPPUNIT_ASSERT(res);
} }
@ -82,18 +82,18 @@ void ImportExportTest::testExportGeometry()
Group* group = new Group(); Group* group = new Group();
xao.addGroup(group); xao.addGroup(group);
group->setName("boite1"); group->setName("boite1");
group->setDimension(3); group->setDimension(XAO::SOLID);
group->addElement(1); group->addElement(1);
group = new Group(); group = new Group();
xao.addGroup(group); xao.addGroup(group);
group->setName("faces"); group->setName("faces");
group->setDimension(2); group->setDimension(XAO::FACE);
group->addElement(5); group->addElement(5);
group->addElement(8); group->addElement(8);
group->addElement(9); group->addElement(9);
bool res = xao.exportToFile("mygeom.xao"); bool res = xao.exportXAO("mygeom.xao");
CPPUNIT_ASSERT(res); CPPUNIT_ASSERT(res);
const char* xml = xao.getXML(); const char* xml = xao.getXML();
@ -115,7 +115,12 @@ void ImportExportTest::testImportXao()
{ {
//std::cout << std::endl; //std::cout << std::endl;
Xao xao; Xao xao;
xao.importFromFile(getTestFile("test.xao").c_str()); xao.importXAO(getTestFile("test.xao").c_str());
checkImport(xao);
}
void ImportExportTest::checkImport(Xao& xao)
{
CPPUNIT_ASSERT(strcmp(xao.getAuthor(), "me") == 0); CPPUNIT_ASSERT(strcmp(xao.getAuthor(), "me") == 0);
CPPUNIT_ASSERT(strcmp(xao.getVersion(), "1.0") == 0); CPPUNIT_ASSERT(strcmp(xao.getVersion(), "1.0") == 0);
@ -155,13 +160,30 @@ void ImportExportTest::testImportXao()
Group* group = xao.getGroup(0); Group* group = xao.getGroup(0);
CPPUNIT_ASSERT(group->getCount() == 1); CPPUNIT_ASSERT(group->getCount() == 1);
CPPUNIT_ASSERT(strcmp(group->getName(), "boite_1") == 0); CPPUNIT_ASSERT(strcmp(group->getName(), "boite_1") == 0);
CPPUNIT_ASSERT(group->getDimension() == 3); CPPUNIT_ASSERT(group->getDimension() == XAO::SOLID);
CPPUNIT_ASSERT(group->getElement(0) == 1); CPPUNIT_ASSERT(group->getElement(0) == 1);
group = xao.getGroup(1); group = xao.getGroup(1);
CPPUNIT_ASSERT(group->getCount() == 3); CPPUNIT_ASSERT(group->getCount() == 3);
CPPUNIT_ASSERT(strcmp(group->getName(), "") == 0); CPPUNIT_ASSERT(strcmp(group->getName(), "") == 0);
CPPUNIT_ASSERT(group->getDimension() == 2); CPPUNIT_ASSERT(group->getDimension() == XAO::FACE);
CPPUNIT_ASSERT(group->getElement(0) == 5); CPPUNIT_ASSERT(group->getElement(0) == 5);
CPPUNIT_ASSERT(group->getElement(1) == 8); CPPUNIT_ASSERT(group->getElement(1) == 8);
CPPUNIT_ASSERT(group->getElement(2) == 9); CPPUNIT_ASSERT(group->getElement(2) == 9);
} }
void ImportExportTest::testImportXaoFromText()
{
std::ifstream rstr;
int length;
rstr.open(getTestFile("test.xao").c_str());
rstr.seekg(0, rstr.end); // go to the end
length = rstr.tellg(); // report location (this is the length)
rstr.seekg(0, rstr.beg); // go back to the beginning
char* txt = new char[length]; // allocate memory for a buffer of appropriate dimension
rstr.read(txt, length); // read the whole file into the buffer
rstr.close();
Xao xao;
xao.setXML(txt);
checkImport(xao);
}

View File

@ -14,6 +14,7 @@ namespace XAO
CPPUNIT_TEST(testExportGeometry); CPPUNIT_TEST(testExportGeometry);
CPPUNIT_TEST(testGeometryError); CPPUNIT_TEST(testGeometryError);
CPPUNIT_TEST(testImportXao); CPPUNIT_TEST(testImportXao);
CPPUNIT_TEST(testImportXaoFromText);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
public: public:
@ -25,6 +26,9 @@ namespace XAO
void testExportGeometry(); void testExportGeometry();
void testGeometryError(); void testGeometryError();
void testImportXao(); void testImportXao();
void testImportXaoFromText();
void checkImport(Xao& xao);
}; };
} }

View File

@ -1,3 +1,22 @@
# 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 : Nathalie Gore (OpenCascade)
include $(top_srcdir)/adm_local/unix/make_common_starter.am include $(top_srcdir)/adm_local/unix/make_common_starter.am