Salome HOME
aea67b6e66881828dff2a0b4e33e9b27bee2ccf1
[modules/geom.git] / src / XAO / XAO_BrepGeometry.cxx
1 // Copyright (C) 2013-2014  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 TopoDS_Shape BrepGeometry::getTopoDS_Shape()
69 {
70     return m_shape;
71 }
72
73 void BrepGeometry::setTopoDS_Shape(const TopoDS_Shape& shape)
74 {
75     m_shape = shape;
76     initIds();
77 }
78
79 void BrepGeometry::initIds()
80 {
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);
86 }
87
88 void BrepGeometry::initListIds(const TopAbs_ShapeEnum& shapeType, GeometricElementList& eltList)
89 {
90     TopTools_MapOfShape mapShape;
91     TopTools_ListOfShape listShape;
92
93     int nbElt = 0;
94     TopExp_Explorer exp(m_shape, shapeType);
95     for (; exp.More(); exp.Next())
96     {
97         if (mapShape.Add(exp.Current()))
98         {
99             listShape.Append(exp.Current());
100             nbElt++;
101         }
102     }
103
104     if (listShape.IsEmpty())
105         return;
106
107     TopTools_IndexedMapOfShape indices;
108     TopExp::MapShapes(m_shape, indices);
109
110     eltList.setSize(nbElt);
111     TopTools_ListIteratorOfListOfShape itSub(listShape);
112     for (int index = 0; itSub.More(); itSub.Next(), ++index)
113     {
114         TopoDS_Shape value = itSub.Value();
115         int ref = indices.FindIndex(value);
116         eltList.setReference(index, XaoUtils::intToString(ref));
117     }
118 }
119
120 TopoDS_Shape BrepGeometry::getSubShape(const TopoDS_Shape& mainShape, const TopAbs_ShapeEnum& shapeType, const int& shapeIndex)
121 throw (XAO_Exception)
122 {
123     TopTools_MapOfShape mapShape;
124     TopTools_ListOfShape listShape;
125
126     TopExp_Explorer exp(mainShape, shapeType);
127     for (; exp.More(); exp.Next())
128     {
129         if (mapShape.Add(exp.Current()))
130             listShape.Append(exp.Current());
131     }
132
133     if (!listShape.IsEmpty())
134     {
135         TopTools_ListIteratorOfListOfShape itSub(listShape);
136         for (int index = 0; itSub.More(); itSub.Next(), ++index)
137         {
138             if (shapeIndex == index)
139             {
140                 TopoDS_Shape value = itSub.Value();
141                 return value;
142             }
143         }
144     }
145
146     throw XAO_Exception(MsgBuilder() << "Shape with reference [" << shapeIndex << "]  not found.");
147 }
148
149 // -----------------------------
150 const int BrepGeometry::countGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType)
151 {
152     int res = 0;
153     TopExp_Explorer exp(shape, shapeType);
154     for (; exp.More(); exp.Next())
155         res++;
156
157     return res;
158 }
159
160 std::vector<int> BrepGeometry::getGeometricalElements(const TopoDS_Shape& shape, const TopAbs_ShapeEnum& shapeType, const XAO::Dimension& dim)
161 {
162     std::vector<int> indexList;
163
164     TopTools_MapOfShape mapShape;
165     TopTools_ListOfShape listShape;
166
167     TopExp_Explorer exp(shape, shapeType);
168     for (; exp.More(); exp.Next())
169     {
170         if (mapShape.Add(exp.Current()))
171             listShape.Append(exp.Current());
172     }
173
174     if (!listShape.IsEmpty())
175     {
176         // use the shape of the geometry for the indices
177         TopTools_IndexedMapOfShape indices;
178         TopExp::MapShapes(m_shape, indices);
179
180         TopTools_ListIteratorOfListOfShape itSub(listShape);
181         for (int index = 0; itSub.More(); itSub.Next(), ++index)
182         {
183             TopoDS_Shape value = itSub.Value();
184             int id = indices.FindIndex(value);
185             indexList.push_back(findElement(dim, id));
186         }
187     }
188
189     return indexList;
190 }
191
192 void BrepGeometry::getEdgeVertices(const int& edgeIndex, int& vertexA, int& vertexB)
193 {
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);
197
198     vertexA = vertices[0];
199     vertexB = vertices[1];
200 }
201
202 const int BrepGeometry::countFaceWires(const int& faceIndex)
203 {
204     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
205     return countGeometricalElements(face, TopAbs_WIRE);
206 }
207
208 std::vector<int> BrepGeometry::getFaceEdges(const int& faceIndex, const int& wireIndex)
209 {
210     // get the face
211     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
212     // get the wire
213     TopoDS_Shape wire = getSubShape(face, TopAbs_WIRE, wireIndex);
214     return getGeometricalElements(wire, TopAbs_EDGE, XAO::EDGE);
215 }
216
217 const int BrepGeometry::countSolidShells(const int& solidIndex)
218 {
219     TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
220     return countGeometricalElements(solid, TopAbs_SHELL);
221 }
222
223 std::vector<int> BrepGeometry::getSolidFaces(const int& solidIndex, const int& shellIndex)
224 {
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);
228 }
229
230 void BrepGeometry::getVertexXYZ(const int& vertexIndex, double& xCoord, double& yCoord, double& zCoord)
231 throw (XAO_Exception)
232 {
233     xCoord = 0.;
234     yCoord = 0.;
235     zCoord = 0.;
236
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.");
240
241     TopoDS_Vertex point = TopoDS::Vertex(vertex);
242     if (!point.IsNull())
243     {
244         gp_Pnt aPnt = BRep_Tool::Pnt(point);
245         xCoord = aPnt.X();
246         yCoord = aPnt.Y();
247         zCoord = aPnt.Z();
248     }
249 }
250
251 // -----------------------------
252 const double BrepGeometry::getEdgeLength(const int& edgeIndex)
253 {
254     TopoDS_Shape edge = getSubShape(m_shape, TopAbs_EDGE, edgeIndex);
255     GProp_GProps system;
256     BRepGProp::LinearProperties(edge, system);
257     return system.Mass();
258 }
259
260 const double BrepGeometry::getFaceArea(const int& faceIndex)
261 {
262     TopoDS_Shape face = getSubShape(m_shape, TopAbs_FACE, faceIndex);
263     GProp_GProps system;
264     BRepGProp::SurfaceProperties(face, system);
265     return system.Mass();
266 }
267
268 const double BrepGeometry::getSolidVolume(const int& solidIndex)
269 {
270     TopoDS_Shape solid = getSubShape(m_shape, TopAbs_SOLID, solidIndex);
271     GProp_GProps system;
272     BRepGProp::VolumeProperties(solid, system);
273     return system.Mass();
274 }
275
276 // -----------------------------
277 const int BrepGeometry::getVertexID(const int& index)
278 {
279     return XaoUtils::stringToInt(getVertexReference(index));
280 }
281
282 const int BrepGeometry::getEdgeID(const int& index)
283 {
284     return XaoUtils::stringToInt(getEdgeReference(index));
285 }
286
287 const int BrepGeometry::getFaceID(const int& index)
288 {
289     return XaoUtils::stringToInt(getFaceReference(index));
290 }
291
292 const int BrepGeometry::getSolidID(const int& index)
293 {
294     return XaoUtils::stringToInt(getSolidReference(index));
295 }
296
297 // -----------------------------
298 void BrepGeometry::setVertexID(const int& index, const int& id)
299 {
300     setVertexReference(index, XaoUtils::intToString(id));
301 }
302
303 void BrepGeometry::setEdgeID(const int& index, const int& id)
304 {
305     setEdgeReference(index, XaoUtils::intToString(id));
306 }
307
308 void BrepGeometry::setFaceID(const int& index, const int& id)
309 {
310     setEdgeReference(index, XaoUtils::intToString(id));
311 }
312
313 void BrepGeometry::setSolidID(const int& index, const int& id)
314 {
315     setEdgeReference(index, XaoUtils::intToString(id));
316 }
317
318 // -----------------------------
319 const int BrepGeometry::findElement(const XAO::Dimension& dim, const int& id)
320 throw (XAO_Exception)
321 {
322     if (dim == XAO::VERTEX)
323         return findVertex(id);
324     if (dim == XAO::EDGE)
325         return findEdge(id);
326     if (dim == XAO::FACE)
327         return findFace(id);
328     if (dim == XAO::SOLID)
329         return findSolid(id);
330
331     throw XAO_Exception(MsgBuilder() << "Unknown Dimension: " << dim);
332 }
333
334 const int BrepGeometry::findVertex(const int& id)
335 {
336     return getVertexIndexByReference(XaoUtils::intToString(id));
337 }
338
339 const int BrepGeometry::findEdge(const int& id)
340 {
341     return getEdgeIndexByReference(XaoUtils::intToString(id));
342 }
343
344 const int BrepGeometry::findFace(const int& id)
345 {
346     return getFaceIndexByReference(XaoUtils::intToString(id));
347 }
348
349 const int BrepGeometry::findSolid(const int& id)
350 {
351     return getSolidIndexByReference(XaoUtils::intToString(id));
352 }
353
354 // -----------------------------
355 const std::string BrepGeometry::findVertexName(const int& id)
356 {
357     return getVertexName(findVertex(id));
358 }
359
360 const std::string BrepGeometry::findEdgeName(const int& id)
361 {
362     return getEdgeName(findEdge(id));
363 }
364
365 const std::string BrepGeometry::findFaceName(const int& id)
366 {
367     return getFaceName(findFace(id));
368 }
369
370 const std::string BrepGeometry::findSolidName(const int& id)
371 {
372     return getSolidName(findSolid(id));
373 }
374
375 // -----------------------------
376 void BrepGeometry::changeVertexName(const int& id, const std::string& name)
377 throw (XAO_Exception)
378 {
379     setVertexName(findVertex(id), name);
380 }
381
382 void BrepGeometry::changeEdgeName(const int& id, const std::string& name)
383 throw (XAO_Exception)
384 {
385     setEdgeName(findEdge(id), name);
386 }
387
388 void BrepGeometry::changeFaceName(const int& id, const std::string& name)
389 throw (XAO_Exception)
390 {
391     setFaceName(findFace(id), name);
392 }
393
394 void BrepGeometry::changeSolidName(const int& id, const std::string& name)
395 throw (XAO_Exception)
396 {
397     setSolidName(findSolid(id), name);
398 }