]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Shape.cpp
Salome HOME
Fix for the issue #2401
[modules/shaper.git] / src / GeomAPI / GeomAPI_Shape.cpp
1 // Copyright (C) 2014-2017  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, 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAPI_Shape.h"
22
23 #include <BRep_Tool.hxx>
24 #include <BRepAlgoAPI_Section.hxx>
25 #include <BRepBndLib.hxx>
26 #include <BRepBuilderAPI_FindPlane.hxx>
27 #include <BRepExtrema_DistShapeShape.hxx>
28 #include <BRepTools.hxx>
29 #include <Bnd_Box.hxx>
30 #include <Geom_Circle.hxx>
31 #include <Geom_Conic.hxx>
32 #include <Geom_Curve.hxx>
33 #include <Geom_Ellipse.hxx>
34 #include <Geom_Hyperbola.hxx>
35 #include <Geom_Line.hxx>
36 #include <Geom_Parabola.hxx>
37 #include <Geom_Plane.hxx>
38 #include <Geom_RectangularTrimmedSurface.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Iterator.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <NCollection_List.hxx>
45
46 #include <sstream>
47 #include <algorithm> // for std::transform
48
49 #include <BRepTools.hxx>
50
51 #define MY_SHAPE implPtr<TopoDS_Shape>()
52
53 GeomAPI_Shape::GeomAPI_Shape()
54     : GeomAPI_Interface(new TopoDS_Shape())
55 {
56 }
57
58 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
59 {
60   GeomShapePtr aShape(new GeomAPI_Shape());
61   aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
62   return aShape;
63 }
64
65 bool GeomAPI_Shape::isNull() const
66 {
67   return MY_SHAPE->IsNull() == Standard_True;
68 }
69
70 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
71 {
72   if (!theShape.get())
73     return false;
74   if (isNull())
75     return theShape->isNull();
76   if (theShape->isNull())
77     return false;
78
79   return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
80 }
81
82 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
83 {
84   if (!theShape.get())
85     return false;
86   if (isNull())
87     return theShape->isNull();
88   if (theShape->isNull())
89     return false;
90
91   return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
92 }
93
94 bool GeomAPI_Shape::isVertex() const
95 {
96   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
97   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
98 }
99
100 bool GeomAPI_Shape::isEdge() const
101 {
102   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
103   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
104 }
105
106 bool GeomAPI_Shape::isWire() const
107 {
108   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
109   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_WIRE;
110 }
111
112 bool GeomAPI_Shape::isFace() const
113 {
114   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
115   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
116 }
117
118 bool GeomAPI_Shape::isCompound() const
119 {
120   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
121   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
122 }
123
124 bool GeomAPI_Shape::isCompoundOfSolids() const
125 {
126   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
127   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
128     return false;
129   bool isAtLeastOne = false;
130   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
131     if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
132       return false;
133     isAtLeastOne = true;
134   }
135   return isAtLeastOne;
136 }
137
138 GeomAPI_Shape::ShapeType GeomAPI_Shape::typeOfCompoundShapes() const
139 {
140   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
141   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
142     return SHAPE;
143   int aType = -1;
144   for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
145     if (!aSubs.Value().IsNull()) {
146       if (aType == -1)
147         aType = aSubs.Value().ShapeType();
148       else if (aSubs.Value().ShapeType() != aType)
149         return SHAPE;
150     }
151   }
152   return (GeomAPI_Shape::ShapeType) aType;
153 }
154
155 // adds the nopt-compound elements recursively to the list
156 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
157 {
158   if (!theShape.IsNull()) {
159     if (theShape.ShapeType() == TopAbs_COMPOUND) {
160       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
161         addSimpleToList(aSubs.Value(), theList);
162       }
163     } else {
164       theList.Append(theShape);
165     }
166   }
167 }
168
169 bool GeomAPI_Shape::isConnectedTopology() const
170 {
171   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
172   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
173     return false;
174   // list of simple elements that are not detected in connection to others
175   NCollection_List<TopoDS_Shape> aNotConnected;
176   addSimpleToList(aShape, aNotConnected);
177   if (aNotConnected.IsEmpty()) // an empty compound
178     return false;
179
180   // collect here the group of connected subs, starting with one first element
181   NCollection_List<TopoDS_Shape> aNewConnected;
182   aNewConnected.Append(aNotConnected.First());
183   aNotConnected.RemoveFirst();
184   // iterate until some new element become connected
185   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
186     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
187     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
188     while(aNotIter.More()) {
189       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
190       NCollection_List<TopoDS_Shape> aNotVertices;
191       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
192         aNotVertices.Append(anExp1.Current());
193       }
194
195       bool aConnected =  false;
196       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
197       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
198         // checking topological connecion of aNotIter and aNewIter
199         // (if shapes are connected, vertices are connected for sure)
200         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
201         for(; !aConnected && anExp2.More(); anExp2.Next()) {
202           NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
203           for(; aNotIter.More(); aNotIter.Next()) {
204             if (aNotIter.Value().IsSame(anExp2.Current())) {
205               aConnected = true;
206               break;
207             }
208           }
209         }
210       }
211       if (aConnected) {
212         aNew.Append(aNotIter.Value());
213         aNotConnected.Remove(aNotIter);
214       } else {
215         aNotIter.Next();
216       }
217     }
218     // remove all new connected and put to this list very new connected
219     aNewConnected.Clear();
220     aNewConnected.Append(aNew);
221   }
222   return aNotConnected.IsEmpty() == Standard_True;
223 }
224
225 bool GeomAPI_Shape::isSolid() const
226 {
227   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
228   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
229 }
230
231 bool GeomAPI_Shape::isCompSolid() const
232 {
233   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
234   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
235 }
236
237 bool GeomAPI_Shape::isPlanar() const
238 {
239   TopoDS_Shape aShape = impl<TopoDS_Shape>();
240
241   if(aShape.IsNull()) {
242     return false;
243   }
244
245   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
246   if(aShapeType == TopAbs_COMPOUND) {
247     TopoDS_Iterator anIt(aShape);
248     int aShNum = 0;
249     for(; anIt.More(); anIt.Next()) {
250       ++aShNum;
251     }
252     if(aShNum == 1) {
253       anIt.Initialize(aShape);
254       aShape = anIt.Value();
255     }
256   }
257
258   aShapeType = aShape.ShapeType();
259   if(aShapeType == TopAbs_VERTEX) {
260     return true;
261   } else if(aShapeType == TopAbs_FACE) {
262     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
263     Handle(Standard_Type) aType = aSurface->DynamicType();
264
265     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
266       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
267         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
268       aType = aTrimSurface->BasisSurface()->DynamicType();
269     }
270     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
271   } else {
272     BRepBuilderAPI_FindPlane aFindPlane(aShape);
273     bool isFound = aFindPlane.Found() == Standard_True;
274
275     if(!isFound && aShapeType == TopAbs_EDGE) {
276       Standard_Real aFirst, aLast;
277       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
278       Handle(Standard_Type) aType = aCurve->DynamicType();
279
280       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
281         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
282         aType = aTrimCurve->BasisCurve()->DynamicType();
283       }
284
285       if(aType == STANDARD_TYPE(Geom_Line)
286           || aType == STANDARD_TYPE(Geom_Conic)
287           || aType == STANDARD_TYPE(Geom_Circle)
288           || aType == STANDARD_TYPE(Geom_Ellipse)
289           || aType == STANDARD_TYPE(Geom_Hyperbola)
290           || aType == STANDARD_TYPE(Geom_Parabola)) {
291         isFound = true;
292       }
293     }
294
295     return isFound;
296   }
297
298   return false;
299 }
300
301 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
302 {
303   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
304
305   ShapeType aST = GeomAPI_Shape::SHAPE;
306
307   switch(aShape.ShapeType()) {
308   case TopAbs_COMPOUND:
309     aST = GeomAPI_Shape::COMPOUND;
310     break;
311   case TopAbs_COMPSOLID:
312     aST = GeomAPI_Shape::COMPSOLID;
313     break;
314   case TopAbs_SOLID:
315     aST = GeomAPI_Shape::SOLID;
316     break;
317   case TopAbs_SHELL:
318     aST = GeomAPI_Shape::SHELL;
319     break;
320   case TopAbs_FACE:
321     aST = GeomAPI_Shape::FACE;
322     break;
323   case TopAbs_WIRE:
324     aST = GeomAPI_Shape::WIRE;
325     break;
326   case TopAbs_EDGE:
327     aST = GeomAPI_Shape::EDGE;
328     break;
329   case TopAbs_VERTEX:
330     aST = GeomAPI_Shape::VERTEX;
331     break;
332   case TopAbs_SHAPE:
333     aST = GeomAPI_Shape::SHAPE;
334     break;
335   }
336
337   return aST;
338 }
339
340 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
341 {
342   std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
343   if (theType == "COMPOUND")
344     return COMPOUND;
345   if (theType == "COMPSOLID")
346     return COMPSOLID;
347   if (theType == "SOLID")
348     return SOLID;
349   if (theType == "SHELL")
350     return SHELL;
351   if (theType == "FACE")
352     return FACE;
353   if (theType == "WIRE")
354     return WIRE;
355   if (theType == "EDGE")
356     return EDGE;
357   if (theType == "VERTEX")
358     return VERTEX;
359   return SHAPE; // default
360 }
361
362 std::string GeomAPI_Shape::shapeTypeStr() const
363 {
364   ShapeType aShapeType = shapeType();
365   std::string aShapeTypeStr;
366
367   switch(aShapeType) {
368     case COMPOUND: {
369       aShapeTypeStr = "COMPOUND";
370       break;
371     }
372     case COMPSOLID: {
373       aShapeTypeStr = "COMPSOLID";
374       break;
375     }
376     case SOLID: {
377       aShapeTypeStr = "SOLID";
378       break;
379     }
380     case SHELL: {
381       aShapeTypeStr = "SHELL";
382       break;
383     }
384     case FACE: {
385       aShapeTypeStr = "FACE";
386       break;
387     }
388     case WIRE: {
389       aShapeTypeStr = "WIRE";
390       break;
391     }
392     case EDGE: {
393       aShapeTypeStr = "EDGE";
394       break;
395     }
396     case VERTEX: {
397       aShapeTypeStr = "VERTEX";
398       break;
399     }
400     case SHAPE: {
401       aShapeTypeStr = "SHAPE";
402       break;
403     }
404   }
405
406   return aShapeTypeStr;
407 }
408
409 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
410 {
411   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
412
413   switch(anOrientation) {
414     case TopAbs_FORWARD:  return FORWARD;
415     case TopAbs_REVERSED: return REVERSED;
416     case TopAbs_INTERNAL: return INTERNAL;
417     case TopAbs_EXTERNAL: return EXTERNAL;
418     default:              return FORWARD;
419   }
420 }
421
422 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
423 {
424   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
425
426   switch(theOrientation) {
427     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
428     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
429     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
430     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
431   }
432 }
433
434 void GeomAPI_Shape::reverse()
435 {
436   MY_SHAPE->Reverse();
437 }
438
439 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape,
440                                const bool theCheckOrientation) const
441 {
442   if(!theShape.get()) {
443     return false;
444   }
445
446   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
447   if(aShapeToSearch.IsNull()) {
448     return false;
449   }
450
451   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
452     if(theCheckOrientation ?
453        aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) {
454       return true;
455     }
456   }
457
458   return false;
459 }
460
461 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
462                                 double& theXmax, double& theYmax, double& theZmax) const
463 {
464   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
465   if (aShape.IsNull())
466     return false;
467   Bnd_Box aBndBox;
468   BRepBndLib::Add(aShape, aBndBox);
469   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
470   return true;
471 }
472
473 std::string GeomAPI_Shape::getShapeStream() const
474 {
475   std::ostringstream aStream;
476   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
477   BRepTools::Write(aShape, aStream);
478   return aStream.str();
479 }
480
481 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
482 {
483   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
484   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
485
486   BRepAlgoAPI_Section aCommon(aShape1, aShape2);
487   if (!aCommon.IsDone())
488     return GeomShapePtr();
489
490   TopoDS_Shape aResult = aCommon.Shape();
491   if (aResult.ShapeType() == TopAbs_COMPOUND) {
492     NCollection_List<TopoDS_Shape> aSubs;
493     addSimpleToList(aResult, aSubs);
494     if(aSubs.Size() == 1) {
495       aResult = aSubs.First();
496     } else if(aSubs.Size() == 0) {
497       return GeomShapePtr();
498     }
499   }
500
501   GeomShapePtr aResShape(new GeomAPI_Shape);
502   aResShape->setImpl(new TopoDS_Shape(aResult));
503   return aResShape;
504 }
505
506 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
507 {
508   if(!theShape.get()) {
509     return false;
510   }
511
512   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
513   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
514
515   BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
516   aDist.Perform();
517   if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
518     return true;
519   }
520
521   return false;
522 }
523
524 void GeomAPI_Shape::translate(const std::shared_ptr<GeomAPI_Dir> theDir, const double theOffset)
525 {
526   gp_Dir aDir = theDir->impl<gp_Dir>();
527   gp_Vec aTrsfVec(aDir.XYZ() * theOffset);
528   gp_Trsf aTranslation;
529   aTranslation.SetTranslation(aTrsfVec);
530   TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation);
531   setImpl(new TopoDS_Shape(aResult));
532 }