Salome HOME
Filling operation: create a face from a set of edges/wires
[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 // adds the nopt-compound elements recursively to the list
139 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
140 {
141   if (!theShape.IsNull()) {
142     if (theShape.ShapeType() == TopAbs_COMPOUND) {
143       for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
144         addSimpleToList(aSubs.Value(), theList);
145       }
146     } else {
147       theList.Append(theShape);
148     }
149   }
150 }
151
152 bool GeomAPI_Shape::isConnectedTopology() const
153 {
154   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
155   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
156     return false;
157   // list of simple elements that are not detected in connection to others
158   NCollection_List<TopoDS_Shape> aNotConnected;
159   addSimpleToList(aShape, aNotConnected);
160   if (aNotConnected.IsEmpty()) // an empty compound
161     return false;
162
163   // collect here the group of connected subs, starting with one first element
164   NCollection_List<TopoDS_Shape> aNewConnected;
165   aNewConnected.Append(aNotConnected.First());
166   aNotConnected.RemoveFirst();
167   // iterate until some new element become connected
168   while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
169     NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
170     NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
171     while(aNotIter.More()) {
172       // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
173       NCollection_List<TopoDS_Shape> aNotVertices;
174       for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
175         aNotVertices.Append(anExp1.Current());
176       }
177
178       bool aConnected =  false;
179       NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
180       for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
181         // checking topological connecion of aNotIter and aNewIter
182         // (if shapes are connected, vertices are connected for sure)
183         TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
184         for(; !aConnected && anExp2.More(); anExp2.Next()) {
185           NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
186           for(; aNotIter.More(); aNotIter.Next()) {
187             if (aNotIter.Value().IsSame(anExp2.Current())) {
188               aConnected = true;
189               break;
190             }
191           }
192         }
193       }
194       if (aConnected) {
195         aNew.Append(aNotIter.Value());
196         aNotConnected.Remove(aNotIter);
197       } else {
198         aNotIter.Next();
199       }
200     }
201     // remove all new connected and put to this list very new connected
202     aNewConnected.Clear();
203     aNewConnected.Append(aNew);
204   }
205   return aNotConnected.IsEmpty() == Standard_True;
206 }
207
208 bool GeomAPI_Shape::isSolid() const
209 {
210   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
211   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
212 }
213
214 bool GeomAPI_Shape::isCompSolid() const
215 {
216   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
217   return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
218 }
219
220 bool GeomAPI_Shape::isPlanar() const
221 {
222   TopoDS_Shape aShape = impl<TopoDS_Shape>();
223
224   if(aShape.IsNull()) {
225     return false;
226   }
227
228   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
229   if(aShapeType == TopAbs_COMPOUND) {
230     TopoDS_Iterator anIt(aShape);
231     int aShNum = 0;
232     for(; anIt.More(); anIt.Next()) {
233       ++aShNum;
234     }
235     if(aShNum == 1) {
236       anIt.Initialize(aShape);
237       aShape = anIt.Value();
238     }
239   }
240
241   aShapeType = aShape.ShapeType();
242   if(aShapeType == TopAbs_VERTEX) {
243     return true;
244   } else if(aShapeType == TopAbs_FACE) {
245     const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
246     Handle(Standard_Type) aType = aSurface->DynamicType();
247
248     if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
249       Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
250         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
251       aType = aTrimSurface->BasisSurface()->DynamicType();
252     }
253     return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
254   } else {
255     BRepBuilderAPI_FindPlane aFindPlane(aShape);
256     bool isFound = aFindPlane.Found() == Standard_True;
257
258     if(!isFound && aShapeType == TopAbs_EDGE) {
259       Standard_Real aFirst, aLast;
260       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
261       Handle(Standard_Type) aType = aCurve->DynamicType();
262
263       if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
264         Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
265         aType = aTrimCurve->BasisCurve()->DynamicType();
266       }
267
268       if(aType == STANDARD_TYPE(Geom_Line)
269           || aType == STANDARD_TYPE(Geom_Conic)
270           || aType == STANDARD_TYPE(Geom_Circle)
271           || aType == STANDARD_TYPE(Geom_Ellipse)
272           || aType == STANDARD_TYPE(Geom_Hyperbola)
273           || aType == STANDARD_TYPE(Geom_Parabola)) {
274         isFound = true;
275       }
276     }
277
278     return isFound;
279   }
280
281   return false;
282 }
283
284 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
285 {
286   const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
287
288   ShapeType aST = GeomAPI_Shape::SHAPE;
289
290   switch(aShape.ShapeType()) {
291   case TopAbs_COMPOUND:
292     aST = GeomAPI_Shape::COMPOUND;
293     break;
294   case TopAbs_COMPSOLID:
295     aST = GeomAPI_Shape::COMPSOLID;
296     break;
297   case TopAbs_SOLID:
298     aST = GeomAPI_Shape::SOLID;
299     break;
300   case TopAbs_SHELL:
301     aST = GeomAPI_Shape::SHELL;
302     break;
303   case TopAbs_FACE:
304     aST = GeomAPI_Shape::FACE;
305     break;
306   case TopAbs_WIRE:
307     aST = GeomAPI_Shape::WIRE;
308     break;
309   case TopAbs_EDGE:
310     aST = GeomAPI_Shape::EDGE;
311     break;
312   case TopAbs_VERTEX:
313     aST = GeomAPI_Shape::VERTEX;
314     break;
315   case TopAbs_SHAPE:
316     aST = GeomAPI_Shape::SHAPE;
317     break;
318   }
319
320   return aST;
321 }
322
323 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
324 {
325   std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
326   if (theType == "COMPOUND")
327     return COMPOUND;
328   if (theType == "COMPSOLID")
329     return COMPSOLID;
330   if (theType == "SOLID")
331     return SOLID;
332   if (theType == "SHELL")
333     return SHELL;
334   if (theType == "FACE")
335     return FACE;
336   if (theType == "WIRE")
337     return WIRE;
338   if (theType == "EDGE")
339     return EDGE;
340   if (theType == "VERTEX")
341     return VERTEX;
342   return SHAPE; // default
343 }
344
345 std::string GeomAPI_Shape::shapeTypeStr() const
346 {
347   ShapeType aShapeType = shapeType();
348   std::string aShapeTypeStr;
349
350   switch(aShapeType) {
351     case COMPOUND: {
352       aShapeTypeStr = "COMPOUND";
353       break;
354     }
355     case COMPSOLID: {
356       aShapeTypeStr = "COMPSOLID";
357       break;
358     }
359     case SOLID: {
360       aShapeTypeStr = "SOLID";
361       break;
362     }
363     case SHELL: {
364       aShapeTypeStr = "SHELL";
365       break;
366     }
367     case FACE: {
368       aShapeTypeStr = "FACE";
369       break;
370     }
371     case WIRE: {
372       aShapeTypeStr = "WIRE";
373       break;
374     }
375     case EDGE: {
376       aShapeTypeStr = "EDGE";
377       break;
378     }
379     case VERTEX: {
380       aShapeTypeStr = "VERTEX";
381       break;
382     }
383     case SHAPE: {
384       aShapeTypeStr = "SHAPE";
385       break;
386     }
387   }
388
389   return aShapeTypeStr;
390 }
391
392 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
393 {
394   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
395
396   switch(anOrientation) {
397     case TopAbs_FORWARD:  return FORWARD;
398     case TopAbs_REVERSED: return REVERSED;
399     case TopAbs_INTERNAL: return INTERNAL;
400     case TopAbs_EXTERNAL: return EXTERNAL;
401     default:              return FORWARD;
402   }
403 }
404
405 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
406 {
407   TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
408
409   switch(theOrientation) {
410     case FORWARD:  MY_SHAPE->Orientation(TopAbs_FORWARD);  break;
411     case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
412     case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
413     case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
414   }
415 }
416
417 void GeomAPI_Shape::reverse()
418 {
419   MY_SHAPE->Reverse();
420 }
421
422 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape,
423                                const bool theCheckOrientation) const
424 {
425   if(!theShape.get()) {
426     return false;
427   }
428
429   const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
430   if(aShapeToSearch.IsNull()) {
431     return false;
432   }
433
434   for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
435     if(theCheckOrientation ?
436        aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) {
437       return true;
438     }
439   }
440
441   return false;
442 }
443
444 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
445                                 double& theXmax, double& theYmax, double& theZmax) const
446 {
447   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
448   if (aShape.IsNull())
449     return false;
450   Bnd_Box aBndBox;
451   BRepBndLib::Add(aShape, aBndBox);
452   aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
453   return true;
454 }
455
456 std::string GeomAPI_Shape::getShapeStream() const
457 {
458   std::ostringstream aStream;
459   const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
460   BRepTools::Write(aShape, aStream);
461   return aStream.str();
462 }
463
464 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
465 {
466   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
467   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
468
469   BRepAlgoAPI_Section aCommon(aShape1, aShape2);
470   if (!aCommon.IsDone())
471     return GeomShapePtr();
472
473   TopoDS_Shape aResult = aCommon.Shape();
474   if (aResult.ShapeType() == TopAbs_COMPOUND) {
475     NCollection_List<TopoDS_Shape> aSubs;
476     addSimpleToList(aResult, aSubs);
477     if(aSubs.Size() == 1) {
478       aResult = aSubs.First();
479     } else if(aSubs.Size() == 0) {
480       return GeomShapePtr();
481     }
482   }
483
484   GeomShapePtr aResShape(new GeomAPI_Shape);
485   aResShape->setImpl(new TopoDS_Shape(aResult));
486   return aResShape;
487 }
488
489 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
490 {
491   if(!theShape.get()) {
492     return false;
493   }
494
495   const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
496   const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
497
498   BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
499   aDist.Perform();
500   if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
501     return true;
502   }
503
504   return false;
505 }
506
507 void GeomAPI_Shape::translate(const std::shared_ptr<GeomAPI_Dir> theDir, const double theOffset)
508 {
509   gp_Dir aDir = theDir->impl<gp_Dir>();
510   gp_Vec aTrsfVec(aDir.XYZ() * theOffset);
511   gp_Trsf aTranslation;
512   aTranslation.SetTranslation(aTrsfVec);
513   TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation);
514   setImpl(new TopoDS_Shape(aResult));
515 }