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