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