1 // Copyright (C) 2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Frederic Pons (OpenCascade)
23 #include <Standard_TypeMismatch.hxx>
25 #include <BRepTools.hxx>
26 #include <BRep_Builder.hxx>
28 #include <TopTools_MapOfShape.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <GProp_GProps.hxx>
35 #include <BRepGProp.hxx>
37 #include <TopoDS_Vertex.hxx>
39 #include "XAO_BrepGeometry.hxx"
40 #include "XAO_XaoUtils.hxx"
44 BrepGeometry::BrepGeometry() : Geometry("")
48 BrepGeometry::BrepGeometry(const std::string& name) : Geometry(name)
52 const std::string BrepGeometry::getShapeString()
54 std::ostringstream streamShape;
55 BRepTools::Write(m_shape, streamShape);
56 std::string data = streamShape.str();
57 char* res = new char[data.size()];
58 strcpy(res, data.c_str());
62 void BrepGeometry::setShapeString(const std::string& shape)
64 std::istringstream streamBrep(shape.c_str());
66 BRepTools::Read(m_shape, streamBrep, builder);
71 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
76 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
82 void BrepGeometry::initIds()
84 // intialization of Ids
85 initListIds(TopAbs_VERTEX, m_vertices);
86 initListIds(TopAbs_EDGE, m_edges);
87 initListIds(TopAbs_FACE, m_faces);
88 initListIds(TopAbs_SOLID, m_solids);
91 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
93 TopTools_MapOfShape mapShape;
94 TopTools_ListOfShape listShape;
97 TopExp_Explorer exp(m_shape, shapeType);
98 for (; exp.More(); exp.Next())
100 if (mapShape.Add(exp.Current()))
102 listShape.Append(exp.Current());
107 if (listShape.IsEmpty())
110 TopTools_IndexedMapOfShape indices;
111 TopExp::MapShapes(m_shape, indices);
113 eltList.setSize(nbElt);
114 TopTools_ListIteratorOfListOfShape itSub(listShape);
115 for (int index = 0; itSub.More(); itSub.Next(), ++index)
117 TopoDS_Shape value = itSub.Value();
118 int ref = indices.FindIndex(value);
119 eltList.setReference(index, XaoUtils::intToString(ref));
123 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, const int& shapeIndex)
124 throw (XAO_Exception)
126 TopTools_MapOfShape mapShape;
127 TopTools_ListOfShape listShape;
129 TopExp_Explorer exp(mainShape, shapeType);
130 for (; exp.More(); exp.Next())
132 if (mapShape.Add(exp.Current()))
133 listShape.Append(exp.Current());
136 if (!listShape.IsEmpty())
138 TopTools_ListIteratorOfListOfShape itSub(listShape);
139 for (int index = 0; itSub.More(); itSub.Next(), ++index)
141 if (shapeIndex == index)
143 TopoDS_Shape value = itSub.Value();
149 throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "] not found.");
152 // -----------------------------
153 const int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
156 TopExp_Explorer exp(shape, shapeType);
157 for (; exp.More(); exp.Next())
163 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, const XAO::Dimension& dim)
165 std::vector<int> indexList;
167 TopTools_MapOfShape mapShape;
168 TopTools_ListOfShape listShape;
170 TopExp_Explorer exp(shape, shapeType);
171 for (; exp.More(); exp.Next())
173 if (mapShape.Add(exp.Current()))
174 listShape.Append(exp.Current());
177 if (!listShape.IsEmpty())
179 // use the shape of the geometry for the indices
180 TopTools_IndexedMapOfShape indices;
181 TopExp::MapShapes(m_shape, indices);
183 TopTools_ListIteratorOfListOfShape itSub(listShape);
184 for (int index = 0; itSub.More(); itSub.Next(), ++index)
186 TopoDS_Shape value = itSub.Value();
187 int id = indices.FindIndex(value);
188 indexList.push_back(findElement(dim, id));
195 void BrepGeometry::getEdgeVertices(const int& edgeIndex, int& vertexA, int& vertexB)
197 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
198 std::vector<int> vertices = getGeometricalElements(edge, TopAbs_VERTEX, XAO::VERTEX);
199 assert(vertices.size() == 2);
201 vertexA = vertices[0];
202 vertexB = vertices[1];
205 const int BrepGeometry::countFaceWires(const int& faceIndex)
207 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
208 return countGeometricalElements(face, TopAbs_WIRE);
211 std::vector<int> BrepGeometry::getFaceEdges(const int& faceIndex, const int& wireIndex)
214 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
216 TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
217 return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
220 const int BrepGeometry::countSolidShells(const int& solidIndex)
222 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
223 return countGeometricalElements(solid, TopAbs_SHELL);
226 std::vector<int> BrepGeometry::getSolidFaces(const int& solidIndex, const int& shellIndex)
228 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
229 TopoDS_Shape shell = getSubShape(solid, TopAbs_SHELL, shellIndex);
230 return getGeometricalElements(shell, TopAbs_FACE, XAO::FACE);
233 void BrepGeometry::getVertexXYZ(const int& vertexIndex, double& xCoord, double& yCoord, double& zCoord)
234 throw (XAO_Exception)
240 TopoDS_Shape vertex = getSubShape(m_shape, TopAbs_VERTEX, vertexIndex);
241 if (vertex.ShapeType() != TopAbs_VERTEX)
242 throw XAO_Exception(MsgBuilder() << "Shape " << vertexIndex<< " is not a point.");
244 TopoDS_Vertex point = TopoDS::Vertex(vertex);
247 gp_Pnt aPnt = BRep_Tool::Pnt(point);
254 // -----------------------------
255 const double BrepGeometry::getEdgeLength(const int& edgeIndex)
257 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
259 BRepGProp::LinearProperties(edge, system);
260 return system.Mass();
263 const double BrepGeometry::getFaceArea(const int& faceIndex)
265 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
267 BRepGProp::SurfaceProperties(face, system);
268 return system.Mass();
271 const double BrepGeometry::getSolidVolume(const int& solidIndex)
273 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
275 BRepGProp::VolumeProperties(solid, system);
276 return system.Mass();
279 // -----------------------------
280 const int BrepGeometry::getVertexID(const int& index)
282 return XaoUtils::stringToInt(getVertexReference(index));
285 const int BrepGeometry::getEdgeID(const int& index)
287 return XaoUtils::stringToInt(getEdgeReference(index));
290 const int BrepGeometry::getFaceID(const int& index)
292 return XaoUtils::stringToInt(getFaceReference(index));
295 const int BrepGeometry::getSolidID(const int& index)
297 return XaoUtils::stringToInt(getSolidReference(index));
300 // -----------------------------
301 void BrepGeometry::setVertexID(const int& index, const int& id)
303 setVertexReference(index, XaoUtils::intToString(id));
306 void BrepGeometry::setEdgeID(const int& index, const int& id)
308 setEdgeReference(index, XaoUtils::intToString(id));
311 void BrepGeometry::setFaceID(const int& index, const int& id)
313 setEdgeReference(index, XaoUtils::intToString(id));
316 void BrepGeometry::setSolidID(const int& index, const int& id)
318 setEdgeReference(index, XaoUtils::intToString(id));
321 // -----------------------------
322 const int BrepGeometry::findElement(const XAO::Dimension& dim, const int& id)
323 throw (XAO_Exception)
325 if (dim == XAO::VERTEX)
326 return findVertex(id);
327 if (dim == XAO::EDGE)
329 if (dim == XAO::FACE)
331 if (dim == XAO::SOLID)
332 return findSolid(id);
334 throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
337 const int BrepGeometry::findVertex(const int& id)
339 return getVertexIndexByReference(XaoUtils::intToString(id));
342 const int BrepGeometry::findEdge(const int& id)
344 return getEdgeIndexByReference(XaoUtils::intToString(id));
347 const int BrepGeometry::findFace(const int& id)
349 return getFaceIndexByReference(XaoUtils::intToString(id));
352 const int BrepGeometry::findSolid(const int& id)
354 return getSolidIndexByReference(XaoUtils::intToString(id));
357 // -----------------------------
358 const std::string BrepGeometry::findVertexName(const int& id)
360 return getVertexName(findVertex(id));
363 const std::string BrepGeometry::findEdgeName(const int& id)
365 return getEdgeName(findEdge(id));
368 const std::string BrepGeometry::findFaceName(const int& id)
370 return getFaceName(findFace(id));
373 const std::string BrepGeometry::findSolidName(const int& id)
375 return getSolidName(findSolid(id));
378 // -----------------------------
379 void BrepGeometry::changeVertexName(const int& id, const std::string& name)
380 throw (XAO_Exception)
382 setVertexName(findVertex(id), name);
385 void BrepGeometry::changeEdgeName(const int& id, const std::string& name)
386 throw (XAO_Exception)
388 setEdgeName(findEdge(id), name);
391 void BrepGeometry::changeFaceName(const int& id, const std::string& name)
392 throw (XAO_Exception)
394 setFaceName(findFace(id), name);
397 void BrepGeometry::changeSolidName(const int& id, const std::string& name)
398 throw (XAO_Exception)
400 setSolidName(findSolid(id), name);