1 // Copyright (C) 2013-2024 CEA, EDF, 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)
70 bool res = BRepTools::Write(m_shape, fileName.c_str());
72 throw XAO_Exception(MsgBuilder() << "Cannot write BRep file: " << fileName);
75 void BrepGeometry::readShapeFile(const std::string& fileName)
78 bool res = BRepTools::Read(m_shape, fileName.c_str(), builder);
80 throw XAO_Exception(MsgBuilder() << "Cannot read BRep file: " << fileName);
85 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
90 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
96 void BrepGeometry::initIds()
98 // initialization of Ids
99 initListIds(TopAbs_VERTEX, m_vertices);
100 initListIds(TopAbs_EDGE, m_edges);
101 initListIds(TopAbs_FACE, m_faces);
102 initListIds(TopAbs_SOLID, m_solids);
105 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
107 TopTools_MapOfShape mapShape;
108 TopTools_ListOfShape listShape;
111 TopExp_Explorer exp(m_shape, shapeType);
112 for (; exp.More(); exp.Next())
114 if (mapShape.Add(exp.Current()))
116 listShape.Append(exp.Current());
121 if (listShape.IsEmpty())
124 TopTools_IndexedMapOfShape indices;
125 TopExp::MapShapes(m_shape, indices);
127 eltList.setSize(nbElt);
128 TopTools_ListIteratorOfListOfShape itSub(listShape);
129 for (int index = 0; itSub.More(); itSub.Next(), ++index)
131 TopoDS_Shape value = itSub.Value();
132 int ref = indices.FindIndex(value);
133 eltList.setReference(index, XaoUtils::intToString(ref));
137 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, int shapeIndex)
139 TopTools_MapOfShape mapShape;
140 TopTools_ListOfShape listShape;
142 TopExp_Explorer exp(mainShape, shapeType);
143 for (; exp.More(); exp.Next())
145 if (mapShape.Add(exp.Current()))
146 listShape.Append(exp.Current());
149 if (!listShape.IsEmpty())
151 TopTools_ListIteratorOfListOfShape itSub(listShape);
152 for (int index = 0; itSub.More(); itSub.Next(), ++index)
154 if (shapeIndex == index)
156 TopoDS_Shape value = itSub.Value();
162 throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "] not found.");
165 // -----------------------------
166 int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
169 TopExp_Explorer exp(shape, shapeType);
170 for (; exp.More(); exp.Next())
176 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, XAO::Dimension dim)
178 std::vector<int> indexList;
180 TopTools_MapOfShape mapShape;
181 TopTools_ListOfShape listShape;
183 TopExp_Explorer exp(shape, shapeType);
184 for (; exp.More(); exp.Next())
186 if (mapShape.Add(exp.Current()))
187 listShape.Append(exp.Current());
190 if (!listShape.IsEmpty())
192 // use the shape of the geometry for the indices
193 TopTools_IndexedMapOfShape indices;
194 TopExp::MapShapes(m_shape, indices);
196 TopTools_ListIteratorOfListOfShape itSub(listShape);
197 for (int index = 0; itSub.More(); itSub.Next(), ++index)
199 TopoDS_Shape value = itSub.Value();
200 int id = indices.FindIndex(value);
201 indexList.push_back(findElement(dim, id));
208 void BrepGeometry::getEdgeVertices(int edgeIndex, int& vertexA, int& vertexB)
210 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
211 std::vector<int> vertices = getGeometricalElements(edge, TopAbs_VERTEX, XAO::VERTEX);
212 assert(vertices.size() == 2);
214 vertexA = vertices[0];
215 vertexB = vertices[1];
218 int BrepGeometry::countFaceWires(int faceIndex)
220 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
221 return countGeometricalElements(face, TopAbs_WIRE);
224 std::vector<int> BrepGeometry::getFaceEdges(int faceIndex, int wireIndex)
227 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
229 TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
230 return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
233 int BrepGeometry::countSolidShells(int solidIndex)
235 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
236 return countGeometricalElements(solid, TopAbs_SHELL);
239 std::vector<int> BrepGeometry::getSolidFaces(int solidIndex, int shellIndex)
241 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
242 TopoDS_Shape shell = getSubShape(solid, TopAbs_SHELL, shellIndex);
243 return getGeometricalElements(shell, TopAbs_FACE, XAO::FACE);
246 void BrepGeometry::getVertexXYZ(int vertexIndex, double& xCoord, double& yCoord, double& zCoord)
252 TopoDS_Shape vertex = getSubShape(m_shape, TopAbs_VERTEX, vertexIndex);
253 if (vertex.ShapeType() != TopAbs_VERTEX)
254 throw XAO_Exception(MsgBuilder() << "Shape " << vertexIndex<< " is not a point.");
256 TopoDS_Vertex point = TopoDS::Vertex(vertex);
259 gp_Pnt aPnt = BRep_Tool::Pnt(point);
266 // -----------------------------
267 double BrepGeometry::getEdgeLength(int edgeIndex)
269 TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
271 BRepGProp::LinearProperties(edge, system);
272 return system.Mass();
275 double BrepGeometry::getFaceArea(int faceIndex)
277 TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
279 BRepGProp::SurfaceProperties(face, system);
280 return system.Mass();
283 double BrepGeometry::getSolidVolume(int solidIndex)
285 TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
287 BRepGProp::VolumeProperties(solid, system);
288 return system.Mass();
291 // -----------------------------
292 int BrepGeometry::getVertexID(int index)
294 return XaoUtils::stringToInt(getVertexReference(index));
297 int BrepGeometry::getEdgeID(int index)
299 return XaoUtils::stringToInt(getEdgeReference(index));
302 int BrepGeometry::getFaceID(int index)
304 return XaoUtils::stringToInt(getFaceReference(index));
307 int BrepGeometry::getSolidID(int index)
309 return XaoUtils::stringToInt(getSolidReference(index));
312 // -----------------------------
313 void BrepGeometry::setVertexID(int index, int id)
315 setVertexReference(index, XaoUtils::intToString(id));
318 void BrepGeometry::setEdgeID(int index, int id)
320 setEdgeReference(index, XaoUtils::intToString(id));
323 void BrepGeometry::setFaceID(int index, int id)
325 setEdgeReference(index, XaoUtils::intToString(id));
328 void BrepGeometry::setSolidID(int index, int id)
330 setEdgeReference(index, XaoUtils::intToString(id));
333 // -----------------------------
334 int BrepGeometry::findElement(XAO::Dimension dim, int id)
336 if (dim == XAO::VERTEX)
337 return findVertex(id);
338 if (dim == XAO::EDGE)
340 if (dim == XAO::FACE)
342 if (dim == XAO::SOLID)
343 return findSolid(id);
345 throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
348 int BrepGeometry::findVertex(int id)
350 return getVertexIndexByReference(XaoUtils::intToString(id));
353 int BrepGeometry::findEdge(int id)
355 return getEdgeIndexByReference(XaoUtils::intToString(id));
358 int BrepGeometry::findFace(int id)
360 return getFaceIndexByReference(XaoUtils::intToString(id));
363 int BrepGeometry::findSolid(int id)
365 return getSolidIndexByReference(XaoUtils::intToString(id));
368 // -----------------------------
369 const std::string BrepGeometry::findVertexName(int id)
371 return getVertexName(findVertex(id));
374 const std::string BrepGeometry::findEdgeName(int id)
376 return getEdgeName(findEdge(id));
379 const std::string BrepGeometry::findFaceName(int id)
381 return getFaceName(findFace(id));
384 const std::string BrepGeometry::findSolidName(int id)
386 return getSolidName(findSolid(id));
389 // -----------------------------
390 void BrepGeometry::changeVertexName(int id, const std::string& name)
392 setVertexName(findVertex(id), name);
395 void BrepGeometry::changeEdgeName(int id, const std::string& name)
397 setEdgeName(findEdge(id), name);
400 void BrepGeometry::changeFaceName(int id, const std::string& name)
402 setFaceName(findFace(id), name);
405 void BrepGeometry::changeSolidName(int id, const std::string& name)
407 setSolidName(findSolid(id), name);