1 // Copyright (C) 2013-2019 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 void BrepGeometry::writeShapeFile(const std::string& fileName)
71 bool res = BRepTools::Write(m_shape, fileName.c_str());
73 throw XAO_Exception(MsgBuilder() << "Cannot write BRep file: " << fileName);
76 void BrepGeometry::readShapeFile(const std::string& fileName)
80 bool res = BRepTools::Read(m_shape, fileName.c_str(), builder);
82 throw XAO_Exception(MsgBuilder() << "Cannot read BRep file: " << fileName);
87 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
92 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
98 void BrepGeometry::initIds()
100 // initialization of Ids
101 initListIds(TopAbs_VERTEX, m_vertices);
102 initListIds(TopAbs_EDGE, m_edges);
103 initListIds(TopAbs_FACE, m_faces);
104 initListIds(TopAbs_SOLID, m_solids);
107 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
109 TopTools_MapOfShape mapShape;
110 TopTools_ListOfShape listShape;
113 TopExp_Explorer exp(m_shape, shapeType);
114 for (; exp.More(); exp.Next())
116 if (mapShape.Add(exp.Current()))
118 listShape.Append(exp.Current());
123 if (listShape.IsEmpty())
126 TopTools_IndexedMapOfShape indices;
127 TopExp::MapShapes(m_shape, indices);
129 eltList.setSize(nbElt);
130 TopTools_ListIteratorOfListOfShape itSub(listShape);
131 for (int index = 0; itSub.More(); itSub.Next(), ++index)
133 TopoDS_Shape value = itSub.Value();
134 int ref = indices.FindIndex(value);
135 eltList.setReference(index, XaoUtils::intToString(ref));
139 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, const int& shapeIndex)
140 throw (XAO_Exception)
142 TopTools_MapOfShape mapShape;
143 TopTools_ListOfShape listShape;
145 TopExp_Explorer exp(mainShape, shapeType);
146 for (; exp.More(); exp.Next())
148 if (mapShape.Add(exp.Current()))
149 listShape.Append(exp.Current());
152 if (!listShape.IsEmpty())
154 TopTools_ListIteratorOfListOfShape itSub(listShape);
155 for (int index = 0; itSub.More(); itSub.Next(), ++index)
157 if (shapeIndex == index)
159 TopoDS_Shape value = itSub.Value();
165 throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "] not found.");
168 // -----------------------------
169 const int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
172 TopExp_Explorer exp(shape, shapeType);
173 for (; exp.More(); exp.Next())
179 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, const XAO::Dimension& dim)
181 std::vector<int> indexList;
183 TopTools_MapOfShape mapShape;
184 TopTools_ListOfShape listShape;
186 TopExp_Explorer exp(shape, shapeType);
187 for (; exp.More(); exp.Next())
189 if (mapShape.Add(exp.Current()))
190 listShape.Append(exp.Current());
193 if (!listShape.IsEmpty())
195 // use the shape of the geometry for the indices
196 TopTools_IndexedMapOfShape indices;
197 TopExp::MapShapes(m_shape, indices);
199 TopTools_ListIteratorOfListOfShape itSub(listShape);
200 for (int index = 0; itSub.More(); itSub.Next(), ++index)
202 TopoDS_Shape value = itSub.Value();
203 int id = indices.FindIndex(value);
204 indexList.push_back(findElement(dim, id));
211 void BrepGeometry::getEdgeVertices(const int& edgeIndex, int& vertexA, int& vertexB)
213 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
214 std::vector<int> vertices = getGeometricalElements(edge, TopAbs_VERTEX, XAO::VERTEX);
215 assert(vertices.size() == 2);
217 vertexA = vertices[0];
218 vertexB = vertices[1];
221 const int BrepGeometry::countFaceWires(const int& faceIndex)
223 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
224 return countGeometricalElements(face, TopAbs_WIRE);
227 std::vector<int> BrepGeometry::getFaceEdges(const int& faceIndex, const int& wireIndex)
230 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
232 TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
233 return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
236 const int BrepGeometry::countSolidShells(const int& solidIndex)
238 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
239 return countGeometricalElements(solid, TopAbs_SHELL);
242 std::vector<int> BrepGeometry::getSolidFaces(const int& solidIndex, const int& shellIndex)
244 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
245 TopoDS_Shape shell = getSubShape(solid, TopAbs_SHELL, shellIndex);
246 return getGeometricalElements(shell, TopAbs_FACE, XAO::FACE);
249 void BrepGeometry::getVertexXYZ(const int& vertexIndex, double& xCoord, double& yCoord, double& zCoord)
250 throw (XAO_Exception)
256 TopoDS_Shape vertex = getSubShape(m_shape, TopAbs_VERTEX, vertexIndex);
257 if (vertex.ShapeType() != TopAbs_VERTEX)
258 throw XAO_Exception(MsgBuilder() << "Shape " << vertexIndex<< " is not a point.");
260 TopoDS_Vertex point = TopoDS::Vertex(vertex);
263 gp_Pnt aPnt = BRep_Tool::Pnt(point);
270 // -----------------------------
271 const double BrepGeometry::getEdgeLength(const int& edgeIndex)
273 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
275 BRepGProp::LinearProperties(edge, system);
276 return system.Mass();
279 const double BrepGeometry::getFaceArea(const int& faceIndex)
281 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
283 BRepGProp::SurfaceProperties(face, system);
284 return system.Mass();
287 const double BrepGeometry::getSolidVolume(const int& solidIndex)
289 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
291 BRepGProp::VolumeProperties(solid, system);
292 return system.Mass();
295 // -----------------------------
296 const int BrepGeometry::getVertexID(const int& index)
298 return XaoUtils::stringToInt(getVertexReference(index));
301 const int BrepGeometry::getEdgeID(const int& index)
303 return XaoUtils::stringToInt(getEdgeReference(index));
306 const int BrepGeometry::getFaceID(const int& index)
308 return XaoUtils::stringToInt(getFaceReference(index));
311 const int BrepGeometry::getSolidID(const int& index)
313 return XaoUtils::stringToInt(getSolidReference(index));
316 // -----------------------------
317 void BrepGeometry::setVertexID(const int& index, const int& id)
319 setVertexReference(index, XaoUtils::intToString(id));
322 void BrepGeometry::setEdgeID(const int& index, const int& id)
324 setEdgeReference(index, XaoUtils::intToString(id));
327 void BrepGeometry::setFaceID(const int& index, const int& id)
329 setEdgeReference(index, XaoUtils::intToString(id));
332 void BrepGeometry::setSolidID(const int& index, const int& id)
334 setEdgeReference(index, XaoUtils::intToString(id));
337 // -----------------------------
338 const int BrepGeometry::findElement(const XAO::Dimension& dim, const int& id)
339 throw (XAO_Exception)
341 if (dim == XAO::VERTEX)
342 return findVertex(id);
343 if (dim == XAO::EDGE)
345 if (dim == XAO::FACE)
347 if (dim == XAO::SOLID)
348 return findSolid(id);
350 throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
353 const int BrepGeometry::findVertex(const int& id)
355 return getVertexIndexByReference(XaoUtils::intToString(id));
358 const int BrepGeometry::findEdge(const int& id)
360 return getEdgeIndexByReference(XaoUtils::intToString(id));
363 const int BrepGeometry::findFace(const int& id)
365 return getFaceIndexByReference(XaoUtils::intToString(id));
368 const int BrepGeometry::findSolid(const int& id)
370 return getSolidIndexByReference(XaoUtils::intToString(id));
373 // -----------------------------
374 const std::string BrepGeometry::findVertexName(const int& id)
376 return getVertexName(findVertex(id));
379 const std::string BrepGeometry::findEdgeName(const int& id)
381 return getEdgeName(findEdge(id));
384 const std::string BrepGeometry::findFaceName(const int& id)
386 return getFaceName(findFace(id));
389 const std::string BrepGeometry::findSolidName(const int& id)
391 return getSolidName(findSolid(id));
394 // -----------------------------
395 void BrepGeometry::changeVertexName(const int& id, const std::string& name)
396 throw (XAO_Exception)
398 setVertexName(findVertex(id), name);
401 void BrepGeometry::changeEdgeName(const int& id, const std::string& name)
402 throw (XAO_Exception)
404 setEdgeName(findEdge(id), name);
407 void BrepGeometry::changeFaceName(const int& id, const std::string& name)
408 throw (XAO_Exception)
410 setFaceName(findFace(id), name);
413 void BrepGeometry::changeSolidName(const int& id, const std::string& name)
414 throw (XAO_Exception)
416 setSolidName(findSolid(id), name);