1 // Copyright (C) 2013-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : 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 return streamShape.str();
59 void BrepGeometry::setShapeString(const std::string& shape)
61 std::istringstream streamBrep(shape.c_str());
63 BRepTools::Read(m_shape, streamBrep, builder);
68 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
73 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
79 void BrepGeometry::initIds()
81 // intialization of Ids
82 initListIds(TopAbs_VERTEX, m_vertices);
83 initListIds(TopAbs_EDGE, m_edges);
84 initListIds(TopAbs_FACE, m_faces);
85 initListIds(TopAbs_SOLID, m_solids);
88 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
90 TopTools_MapOfShape mapShape;
91 TopTools_ListOfShape listShape;
94 TopExp_Explorer exp(m_shape, shapeType);
95 for (; exp.More(); exp.Next())
97 if (mapShape.Add(exp.Current()))
99 listShape.Append(exp.Current());
104 if (listShape.IsEmpty())
107 TopTools_IndexedMapOfShape indices;
108 TopExp::MapShapes(m_shape, indices);
110 eltList.setSize(nbElt);
111 TopTools_ListIteratorOfListOfShape itSub(listShape);
112 for (int index = 0; itSub.More(); itSub.Next(), ++index)
114 TopoDS_Shape value = itSub.Value();
115 int ref = indices.FindIndex(value);
116 eltList.setReference(index, XaoUtils::intToString(ref));
120 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, const int& shapeIndex)
121 throw (XAO_Exception)
123 TopTools_MapOfShape mapShape;
124 TopTools_ListOfShape listShape;
126 TopExp_Explorer exp(mainShape, shapeType);
127 for (; exp.More(); exp.Next())
129 if (mapShape.Add(exp.Current()))
130 listShape.Append(exp.Current());
133 if (!listShape.IsEmpty())
135 TopTools_ListIteratorOfListOfShape itSub(listShape);
136 for (int index = 0; itSub.More(); itSub.Next(), ++index)
138 if (shapeIndex == index)
140 TopoDS_Shape value = itSub.Value();
146 throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "] not found.");
149 // -----------------------------
150 const int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
153 TopExp_Explorer exp(shape, shapeType);
154 for (; exp.More(); exp.Next())
160 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, const XAO::Dimension& dim)
162 std::vector<int> indexList;
164 TopTools_MapOfShape mapShape;
165 TopTools_ListOfShape listShape;
167 TopExp_Explorer exp(shape, shapeType);
168 for (; exp.More(); exp.Next())
170 if (mapShape.Add(exp.Current()))
171 listShape.Append(exp.Current());
174 if (!listShape.IsEmpty())
176 // use the shape of the geometry for the indices
177 TopTools_IndexedMapOfShape indices;
178 TopExp::MapShapes(m_shape, indices);
180 TopTools_ListIteratorOfListOfShape itSub(listShape);
181 for (int index = 0; itSub.More(); itSub.Next(), ++index)
183 TopoDS_Shape value = itSub.Value();
184 int id = indices.FindIndex(value);
185 indexList.push_back(findElement(dim, id));
192 void BrepGeometry::getEdgeVertices(const int& edgeIndex, int& vertexA, int& vertexB)
194 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
195 std::vector<int> vertices = getGeometricalElements(edge, TopAbs_VERTEX, XAO::VERTEX);
196 assert(vertices.size() == 2);
198 vertexA = vertices[0];
199 vertexB = vertices[1];
202 const int BrepGeometry::countFaceWires(const int& faceIndex)
204 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
205 return countGeometricalElements(face, TopAbs_WIRE);
208 std::vector<int> BrepGeometry::getFaceEdges(const int& faceIndex, const int& wireIndex)
211 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
213 TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
214 return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
217 const int BrepGeometry::countSolidShells(const int& solidIndex)
219 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
220 return countGeometricalElements(solid, TopAbs_SHELL);
223 std::vector<int> BrepGeometry::getSolidFaces(const int& solidIndex, const int& shellIndex)
225 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
226 TopoDS_Shape shell = getSubShape(solid, TopAbs_SHELL, shellIndex);
227 return getGeometricalElements(shell, TopAbs_FACE, XAO::FACE);
230 void BrepGeometry::getVertexXYZ(const int& vertexIndex, double& xCoord, double& yCoord, double& zCoord)
231 throw (XAO_Exception)
237 TopoDS_Shape vertex = getSubShape(m_shape, TopAbs_VERTEX, vertexIndex);
238 if (vertex.ShapeType() != TopAbs_VERTEX)
239 throw XAO_Exception(MsgBuilder() << "Shape " << vertexIndex<< " is not a point.");
241 TopoDS_Vertex point = TopoDS::Vertex(vertex);
244 gp_Pnt aPnt = BRep_Tool::Pnt(point);
251 // -----------------------------
252 const double BrepGeometry::getEdgeLength(const int& edgeIndex)
254 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
256 BRepGProp::LinearProperties(edge, system);
257 return system.Mass();
260 const double BrepGeometry::getFaceArea(const int& faceIndex)
262 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
264 BRepGProp::SurfaceProperties(face, system);
265 return system.Mass();
268 const double BrepGeometry::getSolidVolume(const int& solidIndex)
270 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
272 BRepGProp::VolumeProperties(solid, system);
273 return system.Mass();
276 // -----------------------------
277 const int BrepGeometry::getVertexID(const int& index)
279 return XaoUtils::stringToInt(getVertexReference(index));
282 const int BrepGeometry::getEdgeID(const int& index)
284 return XaoUtils::stringToInt(getEdgeReference(index));
287 const int BrepGeometry::getFaceID(const int& index)
289 return XaoUtils::stringToInt(getFaceReference(index));
292 const int BrepGeometry::getSolidID(const int& index)
294 return XaoUtils::stringToInt(getSolidReference(index));
297 // -----------------------------
298 void BrepGeometry::setVertexID(const int& index, const int& id)
300 setVertexReference(index, XaoUtils::intToString(id));
303 void BrepGeometry::setEdgeID(const int& index, const int& id)
305 setEdgeReference(index, XaoUtils::intToString(id));
308 void BrepGeometry::setFaceID(const int& index, const int& id)
310 setEdgeReference(index, XaoUtils::intToString(id));
313 void BrepGeometry::setSolidID(const int& index, const int& id)
315 setEdgeReference(index, XaoUtils::intToString(id));
318 // -----------------------------
319 const int BrepGeometry::findElement(const XAO::Dimension& dim, const int& id)
320 throw (XAO_Exception)
322 if (dim == XAO::VERTEX)
323 return findVertex(id);
324 if (dim == XAO::EDGE)
326 if (dim == XAO::FACE)
328 if (dim == XAO::SOLID)
329 return findSolid(id);
331 throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
334 const int BrepGeometry::findVertex(const int& id)
336 return getVertexIndexByReference(XaoUtils::intToString(id));
339 const int BrepGeometry::findEdge(const int& id)
341 return getEdgeIndexByReference(XaoUtils::intToString(id));
344 const int BrepGeometry::findFace(const int& id)
346 return getFaceIndexByReference(XaoUtils::intToString(id));
349 const int BrepGeometry::findSolid(const int& id)
351 return getSolidIndexByReference(XaoUtils::intToString(id));
354 // -----------------------------
355 const std::string BrepGeometry::findVertexName(const int& id)
357 return getVertexName(findVertex(id));
360 const std::string BrepGeometry::findEdgeName(const int& id)
362 return getEdgeName(findEdge(id));
365 const std::string BrepGeometry::findFaceName(const int& id)
367 return getFaceName(findFace(id));
370 const std::string BrepGeometry::findSolidName(const int& id)
372 return getSolidName(findSolid(id));
375 // -----------------------------
376 void BrepGeometry::changeVertexName(const int& id, const std::string& name)
377 throw (XAO_Exception)
379 setVertexName(findVertex(id), name);
382 void BrepGeometry::changeEdgeName(const int& id, const std::string& name)
383 throw (XAO_Exception)
385 setEdgeName(findEdge(id), name);
388 void BrepGeometry::changeFaceName(const int& id, const std::string& name)
389 throw (XAO_Exception)
391 setFaceName(findFace(id), name);
394 void BrepGeometry::changeSolidName(const int& id, const std::string& name)
395 throw (XAO_Exception)
397 setSolidName(findSolid(id), name);