Salome HOME
Heal invalid shape after scaling, because different scaling along axes can produce...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <GEOMImpl_BooleanDriver.hxx>
22 #include <GEOMImpl_IBoolean.hxx>
23 #include <GEOMImpl_Types.hxx>
24 #include <GEOMImpl_GlueDriver.hxx>
25 #include <GEOM_Function.hxx>
26
27 #include <BRep_Builder.hxx>
28 #include <BRepAlgo.hxx>
29 #include <BRepAlgoAPI_Common.hxx>
30 #include <BRepAlgoAPI_Cut.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopTools_MapOfShape.hxx>
38 #include <TopTools_ListOfShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <Precision.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <ShapeFix_ShapeTolerance.hxx>
43 #include <ShapeFix_Shape.hxx>
44
45 #include <Standard_ConstructionError.hxx>
46 #include <StdFail_NotDone.hxx>
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
53 {
54   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
55   return aBooleanDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_BooleanDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
64 {
65 }
66
67 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
68 {
69   if (theShape.ShapeType() != TopAbs_COMPOUND &&
70       theShape.ShapeType() != TopAbs_COMPSOLID) {
71     theList.Append(theShape);
72     return;
73   }
74
75   TopTools_MapOfShape mapShape;
76   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
77
78   for (; It.More(); It.Next()) {
79     TopoDS_Shape aShape_i = It.Value();
80     if (mapShape.Add(aShape_i)) {
81       if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
82           aShape_i.ShapeType() == TopAbs_COMPSOLID) {
83         AddSimpleShapes(aShape_i, theList);
84       } else {
85         theList.Append(aShape_i);
86       }
87     }
88   }
89 }
90
91 //=======================================================================
92 //function : Execute
93 //purpose  :
94 //=======================================================================
95 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
96 {
97   if (Label().IsNull()) return 0;
98   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
99
100   GEOMImpl_IBoolean aCI (aFunction);
101   Standard_Integer aType = aFunction->GetType();
102
103   TopoDS_Shape aShape;
104
105   Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
106   Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
107   TopoDS_Shape aShape1 = aRefShape1->GetValue();
108   TopoDS_Shape aShape2 = aRefShape2->GetValue();
109   if (!aShape1.IsNull() && !aShape2.IsNull()) {
110
111     // perform COMMON operation
112     if (aType == BOOLEAN_COMMON) {
113       BRep_Builder B;
114       TopoDS_Compound C;
115       B.MakeCompound(C);
116
117       TopTools_ListOfShape listShape1, listShape2;
118       AddSimpleShapes(aShape1, listShape1);
119       AddSimpleShapes(aShape2, listShape2);
120
121       Standard_Boolean isCompound =
122         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
123
124       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
125       for (; itSub1.More(); itSub1.Next()) {
126         TopoDS_Shape aValue1 = itSub1.Value();
127         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
128         for (; itSub2.More(); itSub2.Next()) {
129           TopoDS_Shape aValue2 = itSub2.Value();
130           BRepAlgoAPI_Common BO (aValue1, aValue2);
131           if (!BO.IsDone()) {
132             StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
133           }
134           if (isCompound) {
135             TopoDS_Shape aStepResult = BO.Shape();
136
137             // check result of this step: if it is a compound (boolean operations
138             // allways return a compound), we add all sub-shapes of it.
139             // This allows to avoid adding empty compounds,
140             // resulting from COMMON on two non-intersecting shapes.
141             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
142               TopoDS_Iterator aCompIter (aStepResult);
143               for (; aCompIter.More(); aCompIter.Next()) {
144                 // add shape in a result
145                 B.Add(C, aCompIter.Value());
146               }
147             }
148             else {
149               // add shape in a result
150               B.Add(C, aStepResult);
151             }
152           }
153           else
154             aShape = BO.Shape();
155         }
156       }
157
158       if (isCompound) {
159         /*
160         TopTools_ListOfShape listShapeC;
161         AddSimpleShapes(C, listShapeC);
162         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
163         bool isOnlySolids = true;
164         for (; itSubC.More(); itSubC.Next()) {
165           TopoDS_Shape aValueC = itSubC.Value();
166           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
167         }
168         if (isOnlySolids)
169           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
170         else
171           aShape = C;
172         */
173
174         // As GlueFaces has been improved to keep all kind of shapes
175         TopExp_Explorer anExp (C, TopAbs_VERTEX);
176         if (anExp.More())
177           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
178         else
179           aShape = C;
180       }
181     }
182
183     // perform CUT operation
184     else if (aType == BOOLEAN_CUT) {
185       BRep_Builder B;
186       TopoDS_Compound C;
187       B.MakeCompound(C);
188
189       TopTools_ListOfShape listShapes, listTools;
190       AddSimpleShapes(aShape1, listShapes);
191       AddSimpleShapes(aShape2, listTools);
192
193       Standard_Boolean isCompound = (listShapes.Extent() > 1);
194
195       TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
196       for (; itSub1.More(); itSub1.Next()) {
197         TopoDS_Shape aCut = itSub1.Value();
198         // tools
199         TopTools_ListIteratorOfListOfShape itSub2 (listTools);
200         for (; itSub2.More(); itSub2.Next()) {
201           TopoDS_Shape aTool = itSub2.Value();
202           BRepAlgoAPI_Cut BO (aCut, aTool);
203           if (!BO.IsDone()) {
204             StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
205           }
206           aCut = BO.Shape();
207         }
208         if (isCompound) {
209           // check result of this step: if it is a compound (boolean operations
210           // allways return a compound), we add all sub-shapes of it.
211           // This allows to avoid adding empty compounds,
212           // resulting from CUT of parts
213           if (aCut.ShapeType() == TopAbs_COMPOUND) {
214             TopoDS_Iterator aCompIter (aCut);
215             for (; aCompIter.More(); aCompIter.Next()) {
216               // add shape in a result
217               B.Add(C, aCompIter.Value());
218             }
219           }
220           else {
221             // add shape in a result
222             B.Add(C, aCut);
223           }
224         }
225         else
226           aShape = aCut;
227       }
228
229       if (isCompound) {
230         /*
231         TopTools_ListOfShape listShapeC;
232         AddSimpleShapes(C, listShapeC);
233         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
234         bool isOnlySolids = true;
235         for (; itSubC.More(); itSubC.Next()) {
236           TopoDS_Shape aValueC = itSubC.Value();
237           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
238         }
239         if (isOnlySolids)
240           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
241         else
242           aShape = C;
243         */
244
245         // As GlueFaces has been improved to keep all kind of shapes
246         TopExp_Explorer anExp (C, TopAbs_VERTEX);
247         if (anExp.More())
248           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
249         else
250           aShape = C;
251       }
252     }
253
254     // perform FUSE operation
255     else if (aType == BOOLEAN_FUSE) {
256       /* Fix for NPAL15379: refused
257       // Check arguments
258       TopTools_ListOfShape listShape1, listShape2;
259       AddSimpleShapes(aShape1, listShape1);
260       AddSimpleShapes(aShape2, listShape2);
261
262       Standard_Boolean isIntersect = Standard_False;
263
264       if (listShape1.Extent() > 1 && !isIntersect) {
265         // check intersections inside the first compound
266         TopTools_ListIteratorOfListOfShape it1 (listShape1);
267         for (; it1.More() && !isIntersect; it1.Next()) {
268           TopoDS_Shape aValue1 = it1.Value();
269           TopTools_ListIteratorOfListOfShape it2 (listShape1);
270           for (; it2.More() && !isIntersect; it2.Next()) {
271             TopoDS_Shape aValue2 = it2.Value();
272             if (aValue2 != aValue1) {
273               BRepAlgoAPI_Section BO (aValue1, aValue2);
274               if (BO.IsDone()) {
275                 TopoDS_Shape aSect = BO.Shape();
276                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
277                 if (anExp.More()) {
278                   isIntersect = Standard_True;
279                 }
280               }
281             }
282           }
283         }
284       }
285
286       if (listShape2.Extent() > 1 && !isIntersect) {
287         // check intersections inside the second compound
288         TopTools_ListIteratorOfListOfShape it1 (listShape2);
289         for (; it1.More() && !isIntersect; it1.Next()) {
290           TopoDS_Shape aValue1 = it1.Value();
291           TopTools_ListIteratorOfListOfShape it2 (listShape2);
292           for (; it2.More() && !isIntersect; it2.Next()) {
293             TopoDS_Shape aValue2 = it2.Value();
294             if (aValue2 != aValue1) {
295               BRepAlgoAPI_Section BO (aValue1, aValue2);
296               if (BO.IsDone()) {
297                 TopoDS_Shape aSect = BO.Shape();
298                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
299                 if (anExp.More()) {
300                   isIntersect = Standard_True;
301                 }
302               }
303             }
304           }
305         }
306       }
307
308       if (isIntersect) {
309         // have intersections inside compounds
310         // check intersections between compounds
311         TopTools_ListIteratorOfListOfShape it1 (listShape1);
312         for (; it1.More(); it1.Next()) {
313           TopoDS_Shape aValue1 = it1.Value();
314           TopTools_ListIteratorOfListOfShape it2 (listShape2);
315           for (; it2.More(); it2.Next()) {
316             TopoDS_Shape aValue2 = it2.Value();
317             if (aValue2 != aValue1) {
318               BRepAlgoAPI_Section BO (aValue1, aValue2);
319               if (BO.IsDone()) {
320                 TopoDS_Shape aSect = BO.Shape();
321                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
322                 if (anExp.More()) {
323                   StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
324                 }
325               }
326             }
327           }
328         }
329       }
330       */
331
332       // Perform
333       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
334       if (!BO.IsDone()) {
335         StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
336       }
337       aShape = BO.Shape();
338     }
339
340     // perform SECTION operation
341     else if (aType == BOOLEAN_SECTION) {
342       BRep_Builder B;
343       TopoDS_Compound C;
344       B.MakeCompound(C);
345
346       TopTools_ListOfShape listShape1, listShape2;
347       AddSimpleShapes(aShape1, listShape1);
348       AddSimpleShapes(aShape2, listShape2);
349
350       Standard_Boolean isCompound =
351         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
352
353       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
354       for (; itSub1.More(); itSub1.Next()) {
355         TopoDS_Shape aValue1 = itSub1.Value();
356         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
357         for (; itSub2.More(); itSub2.Next()) {
358           TopoDS_Shape aValue2 = itSub2.Value();
359           BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
360           // Set approximation to have an attached 3D BSpline geometry to each edge,
361           // where analytic curve is not possible. Without this flag in some cases
362           // we obtain BSpline curve of degree 1 (C0), which is slowly
363           // processed by some algorithms (Partition for example).
364           BO.Approximation(Standard_True);
365           BO.Build();
366           if (!BO.IsDone()) {
367             StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
368           }
369           if (isCompound) {
370             TopoDS_Shape aStepResult = BO.Shape();
371
372             // check result of this step: if it is a compound (boolean operations
373             // allways return a compound), we add all sub-shapes of it.
374             // This allows to avoid adding empty compounds,
375             // resulting from SECTION on two non-intersecting shapes.
376             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
377               TopoDS_Iterator aCompIter (aStepResult);
378               for (; aCompIter.More(); aCompIter.Next()) {
379                 // add shape in a result
380                 B.Add(C, aCompIter.Value());
381               }
382             }
383             else {
384               // add shape in a result
385               B.Add(C, aStepResult);
386             }
387           }
388           else
389             aShape = BO.Shape();
390         }
391       }
392
393       if (isCompound) {
394         //aShape = C;
395
396         // As GlueFaces has been improved to keep all kind of shapes
397         TopExp_Explorer anExp (C, TopAbs_VERTEX);
398         if (anExp.More())
399           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
400         else
401           aShape = C;
402       }
403     }
404
405     // UNKNOWN operation
406     else {
407     }
408   }
409
410   if (aShape.IsNull()) return 0;
411
412   // 08.07.2008 skl for bug 19761 from Mantis
413   BRepCheck_Analyzer ana (aShape, Standard_True);
414   ana.Init(aShape);
415   if (!ana.IsValid()) {
416     ShapeFix_ShapeTolerance aSFT;
417     aSFT.LimitTolerance(aShape, Precision::Confusion(),
418                         Precision::Confusion(), TopAbs_SHAPE);
419     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
420     aSfs->Perform();
421     aShape = aSfs->Shape();
422     ana.Init(aShape);
423     if (!ana.IsValid())
424       Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
425   }
426   //if (!BRepAlgo::IsValid(aShape)) {
427   //  Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
428   //}
429
430   aFunction->SetValue(aShape);
431
432   log.SetTouched(Label());
433
434   return 1;
435 }
436
437
438 //=======================================================================
439 //function :  GEOMImpl_BooleanDriver_Type_
440 //purpose  :
441 //=======================================================================
442 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
443 {
444
445   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
446   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
447   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
448   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
449   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
450   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
451
452
453   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
454   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
455                                                          sizeof(GEOMImpl_BooleanDriver),
456                                                          1,
457                                                          (Standard_Address)_Ancestors,
458                                                          (Standard_Address)NULL);
459
460   return _aType;
461 }
462
463 //=======================================================================
464 //function : DownCast
465 //purpose  :
466 //=======================================================================
467 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
468 {
469   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
470
471   if (!AnObject.IsNull()) {
472      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
473        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
474      }
475   }
476
477   return _anOtherObject ;
478 }