Salome HOME
Typo-fix by Kunda
[modules/geom.git] / src / XAO / XAO_BrepGeometry.cxx
1 // Copyright (C) 2013-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Frederic Pons (OpenCascade)
20
21 #include <cassert>
22
23 #include <Standard_TypeMismatch.hxx>
24
25 #include <BRepTools.hxx>
26 #include <BRep_Builder.hxx>
27 #include <TopAbs.hxx>
28 #include <TopTools_MapOfShape.hxx>
29 #include <TopTools_ListOfShape.hxx>
30 #include <TopTools_ListIteratorOfListOfShape.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopExp.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <GProp_GProps.hxx>
35 #include <BRepGProp.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Vertex.hxx>
38
39 #include "XAO_BrepGeometry.hxx"
40 #include "XAO_XaoUtils.hxx"
41
42 using namespace XAO;
43
44 BrepGeometry::BrepGeometry() : Geometry("")
45 {
46 }
47
48 BrepGeometry::BrepGeometry(const std::string& name) : Geometry(name)
49 {
50 }
51
52 const std::string BrepGeometry::getShapeString()
53 {
54     std::ostringstream streamShape;
55     BRepTools::Write(m_shape, streamShape);
56     return streamShape.str();
57 }
58
59 void BrepGeometry::setShapeString(const std::string& shape)
60 {
61     std::istringstream streamBrep(shape.c_str());
62     BRep_Builder builder;
63     BRepTools::Read(m_shape, streamBrep, builder);
64
65     initIds();
66 }
67
68 void BrepGeometry::writeShapeFile(const std::string& fileName)
69 throw (XAO_Exception)
70 {
71     bool res = BRepTools::Write(m_shape, fileName.c_str());
72     if (!res)
73         throw XAO_Exception(MsgBuilder() << "Cannot write BRep file: " << fileName);
74 }
75
76 void BrepGeometry::readShapeFile(const std::string& fileName)
77 throw (XAO_Exception)
78  {
79     BRep_Builder builder;
80     bool res = BRepTools::Read(m_shape, fileName.c_str(), builder);
81     if (!res)
82         throw XAO_Exception(MsgBuilder() << "Cannot read BRep file: " << fileName);
83
84     initIds();
85 }
86
87 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
88 {
89     return m_shape;
90 }
91
92 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
93 {
94     m_shape = shape;
95     initIds();
96 }
97
98 void BrepGeometry::initIds()
99 {
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);
105 }
106
107 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
108 {
109     TopTools_MapOfShape mapShape;
110     TopTools_ListOfShape listShape;
111
112     int nbElt = 0;
113     TopExp_Explorer exp(m_shape, shapeType);
114     for (; exp.More(); exp.Next())
115     {
116         if (mapShape.Add(exp.Current()))
117         {
118             listShape.Append(exp.Current());
119             nbElt++;
120         }
121     }
122
123     if (listShape.IsEmpty())
124         return;
125
126     TopTools_IndexedMapOfShape indices;
127     TopExp::MapShapes(m_shape, indices);
128
129     eltList.setSize(nbElt);
130     TopTools_ListIteratorOfListOfShape itSub(listShape);
131     for (int index = 0; itSub.More(); itSub.Next(), ++index)
132     {
133         TopoDS_Shape value = itSub.Value();
134         int ref = indices.FindIndex(value);
135         eltList.setReference(index, XaoUtils::intToString(ref));
136     }
137 }
138
139 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, const int& shapeIndex)
140 throw (XAO_Exception)
141 {
142     TopTools_MapOfShape mapShape;
143     TopTools_ListOfShape listShape;
144
145     TopExp_Explorer exp(mainShape, shapeType);
146     for (; exp.More(); exp.Next())
147     {
148         if (mapShape.Add(exp.Current()))
149             listShape.Append(exp.Current());
150     }
151
152     if (!listShape.IsEmpty())
153     {
154         TopTools_ListIteratorOfListOfShape itSub(listShape);
155         for (int index = 0; itSub.More(); itSub.Next(), ++index)
156         {
157             if (shapeIndex == index)
158             {
159                 TopoDS_Shape value = itSub.Value();
160                 return value;
161             }
162         }
163     }
164
165     throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "]  not found.");
166 }
167
168 // -----------------------------
169 const int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
170 {
171     int res = 0;
172     TopExp_Explorer exp(shape, shapeType);
173     for (; exp.More(); exp.Next())
174         res++;
175
176     return res;
177 }
178
179 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, const XAO::Dimension& dim)
180 {
181     std::vector<int> indexList;
182
183     TopTools_MapOfShape mapShape;
184     TopTools_ListOfShape listShape;
185
186     TopExp_Explorer exp(shape, shapeType);
187     for (; exp.More(); exp.Next())
188     {
189         if (mapShape.Add(exp.Current()))
190             listShape.Append(exp.Current());
191     }
192
193     if (!listShape.IsEmpty())
194     {
195         // use the shape of the geometry for the indices
196         TopTools_IndexedMapOfShape indices;
197         TopExp::MapShapes(m_shape, indices);
198
199         TopTools_ListIteratorOfListOfShape itSub(listShape);
200         for (int index = 0; itSub.More(); itSub.Next(), ++index)
201         {
202             TopoDS_Shape value = itSub.Value();
203             int id = indices.FindIndex(value);
204             indexList.push_back(findElement(dim, id));
205         }
206     }
207
208     return indexList;
209 }
210
211 void BrepGeometry::getEdgeVertices(const int& edgeIndex, int& vertexA, int& vertexB)
212 {
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);
216
217     vertexA = vertices[0];
218     vertexB = vertices[1];
219 }
220
221 const int BrepGeometry::countFaceWires(const int& faceIndex)
222 {
223     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
224     return countGeometricalElements(face, TopAbs_WIRE);
225 }
226
227 std::vector<int> BrepGeometry::getFaceEdges(const int& faceIndex, const int& wireIndex)
228 {
229     // get the face
230     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
231     // get the wire
232     TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
233     return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
234 }
235
236 const int BrepGeometry::countSolidShells(const int& solidIndex)
237 {
238     TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
239     return countGeometricalElements(solid, TopAbs_SHELL);
240 }
241
242 std::vector<int> BrepGeometry::getSolidFaces(const int& solidIndex, const int& shellIndex)
243 {
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);
247 }
248
249 void BrepGeometry::getVertexXYZ(const int& vertexIndex, double& xCoord, double& yCoord, double& zCoord)
250 throw (XAO_Exception)
251 {
252     xCoord = 0.;
253     yCoord = 0.;
254     zCoord = 0.;
255
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.");
259
260     TopoDS_Vertex point = TopoDS::Vertex(vertex);
261     if (!point.IsNull())
262     {
263         gp_Pnt aPnt = BRep_Tool::Pnt(point);
264         xCoord = aPnt.X();
265         yCoord = aPnt.Y();
266         zCoord = aPnt.Z();
267     }
268 }
269
270 // -----------------------------
271 const double BrepGeometry::getEdgeLength(const int& edgeIndex)
272 {
273     TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
274     GProp_GProps system;
275     BRepGProp::LinearProperties(edge, system);
276     return system.Mass();
277 }
278
279 const double BrepGeometry::getFaceArea(const int& faceIndex)
280 {
281     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
282     GProp_GProps system;
283     BRepGProp::SurfaceProperties(face, system);
284     return system.Mass();
285 }
286
287 const double BrepGeometry::getSolidVolume(const int& solidIndex)
288 {
289     TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
290     GProp_GProps system;
291     BRepGProp::VolumeProperties(solid, system);
292     return system.Mass();
293 }
294
295 // -----------------------------
296 const int BrepGeometry::getVertexID(const int& index)
297 {
298     return XaoUtils::stringToInt(getVertexReference(index));
299 }
300
301 const int BrepGeometry::getEdgeID(const int& index)
302 {
303     return XaoUtils::stringToInt(getEdgeReference(index));
304 }
305
306 const int BrepGeometry::getFaceID(const int& index)
307 {
308     return XaoUtils::stringToInt(getFaceReference(index));
309 }
310
311 const int BrepGeometry::getSolidID(const int& index)
312 {
313     return XaoUtils::stringToInt(getSolidReference(index));
314 }
315
316 // -----------------------------
317 void BrepGeometry::setVertexID(const int& index, const int& id)
318 {
319     setVertexReference(index, XaoUtils::intToString(id));
320 }
321
322 void BrepGeometry::setEdgeID(const int& index, const int& id)
323 {
324     setEdgeReference(index, XaoUtils::intToString(id));
325 }
326
327 void BrepGeometry::setFaceID(const int& index, const int& id)
328 {
329     setEdgeReference(index, XaoUtils::intToString(id));
330 }
331
332 void BrepGeometry::setSolidID(const int& index, const int& id)
333 {
334     setEdgeReference(index, XaoUtils::intToString(id));
335 }
336
337 // -----------------------------
338 const int BrepGeometry::findElement(const XAO::Dimension& dim, const int& id)
339 throw (XAO_Exception)
340 {
341     if (dim == XAO::VERTEX)
342         return findVertex(id);
343     if (dim == XAO::EDGE)
344         return findEdge(id);
345     if (dim == XAO::FACE)
346         return findFace(id);
347     if (dim == XAO::SOLID)
348         return findSolid(id);
349
350     throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
351 }
352
353 const int BrepGeometry::findVertex(const int& id)
354 {
355     return getVertexIndexByReference(XaoUtils::intToString(id));
356 }
357
358 const int BrepGeometry::findEdge(const int& id)
359 {
360     return getEdgeIndexByReference(XaoUtils::intToString(id));
361 }
362
363 const int BrepGeometry::findFace(const int& id)
364 {
365     return getFaceIndexByReference(XaoUtils::intToString(id));
366 }
367
368 const int BrepGeometry::findSolid(const int& id)
369 {
370     return getSolidIndexByReference(XaoUtils::intToString(id));
371 }
372
373 // -----------------------------
374 const std::string BrepGeometry::findVertexName(const int& id)
375 {
376     return getVertexName(findVertex(id));
377 }
378
379 const std::string BrepGeometry::findEdgeName(const int& id)
380 {
381     return getEdgeName(findEdge(id));
382 }
383
384 const std::string BrepGeometry::findFaceName(const int& id)
385 {
386     return getFaceName(findFace(id));
387 }
388
389 const std::string BrepGeometry::findSolidName(const int& id)
390 {
391     return getSolidName(findSolid(id));
392 }
393
394 // -----------------------------
395 void BrepGeometry::changeVertexName(const int& id, const std::string& name)
396 throw (XAO_Exception)
397 {
398     setVertexName(findVertex(id), name);
399 }
400
401 void BrepGeometry::changeEdgeName(const int& id, const std::string& name)
402 throw (XAO_Exception)
403 {
404     setEdgeName(findEdge(id), name);
405 }
406
407 void BrepGeometry::changeFaceName(const int& id, const std::string& name)
408 throw (XAO_Exception)
409 {
410     setFaceName(findFace(id), name);
411 }
412
413 void BrepGeometry::changeSolidName(const int& id, const std::string& name)
414 throw (XAO_Exception)
415 {
416     setSolidName(findSolid(id), name);
417 }