Salome HOME
Small fix in the dialog box
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
1 // Copyright (C) 2007-2012  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 <TNaming_CopyShape.hxx>
30
31 #include <ShapeFix_ShapeTolerance.hxx>
32 #include <ShapeFix_Shape.hxx>
33
34 #include <BRep_Builder.hxx>
35 #include <BRepAlgo.hxx>
36 #include <BRepAlgoAPI_Common.hxx>
37 #include <BRepAlgoAPI_Cut.hxx>
38 #include <BRepAlgoAPI_Fuse.hxx>
39 #include <BRepAlgoAPI_Section.hxx>
40 #include <BRepCheck_Analyzer.hxx>
41
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_ListIteratorOfListOfShape.hxx>
49
50 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
51
52 #include <Precision.hxx>
53
54 #include <Standard_ConstructionError.hxx>
55 #include <StdFail_NotDone.hxx>
56
57 //=======================================================================
58 //function : GetID
59 //purpose  :
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
62 {
63   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
64   return aBooleanDriver;
65 }
66
67 //=======================================================================
68 //function : GEOMImpl_BooleanDriver
69 //purpose  :
70 //=======================================================================
71 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
72 {
73 }
74
75 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
76 {
77   if (theShape.ShapeType() != TopAbs_COMPOUND &&
78       theShape.ShapeType() != TopAbs_COMPSOLID) {
79     theList.Append(theShape);
80     return;
81   }
82
83   TopTools_MapOfShape mapShape;
84   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
85
86   for (; It.More(); It.Next()) {
87     TopoDS_Shape aShape_i = It.Value();
88     if (mapShape.Add(aShape_i)) {
89       if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
90           aShape_i.ShapeType() == TopAbs_COMPSOLID) {
91         AddSimpleShapes(aShape_i, theList);
92       } else {
93         theList.Append(aShape_i);
94       }
95     }
96   }
97 }
98
99 //=======================================================================
100 //function : Execute
101 //purpose  :
102 //=======================================================================
103 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
104 {
105   if (Label().IsNull()) return 0;
106   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
107
108   GEOMImpl_IBoolean aCI (aFunction);
109   Standard_Integer aType = aFunction->GetType();
110
111   TopoDS_Shape aShape;
112
113   Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
114   Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
115   TopoDS_Shape aShape1 = aRefShape1->GetValue();
116   TopoDS_Shape aShape2 = aRefShape2->GetValue();
117
118   if (!aShape1.IsNull() && !aShape2.IsNull()) {
119     // check arguments for Mantis issue 0021019
120     BRepCheck_Analyzer ana (aShape1, Standard_True);
121     if (!ana.IsValid())
122       StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
123     ana.Init(aShape2);
124     if (!ana.IsValid())
125       StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
126
127     // perform COMMON operation
128     if (aType == BOOLEAN_COMMON) {
129       BRep_Builder B;
130       TopoDS_Compound C;
131       B.MakeCompound(C);
132
133       TopTools_ListOfShape listShape1, listShape2;
134       AddSimpleShapes(aShape1, listShape1);
135       AddSimpleShapes(aShape2, listShape2);
136
137       Standard_Boolean isCompound =
138         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
139
140       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
141       for (; itSub1.More(); itSub1.Next()) {
142         TopoDS_Shape aValue1 = itSub1.Value();
143         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
144         for (; itSub2.More(); itSub2.Next()) {
145           TopoDS_Shape aValue2 = itSub2.Value();
146           BRepAlgoAPI_Common BO (aValue1, aValue2);
147           if (!BO.IsDone()) {
148             StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
149           }
150           if (isCompound) {
151             TopoDS_Shape aStepResult = BO.Shape();
152
153             // check result of this step: if it is a compound (boolean operations
154             // allways return a compound), we add all sub-shapes of it.
155             // This allows to avoid adding empty compounds,
156             // resulting from COMMON on two non-intersecting shapes.
157             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
158               TopoDS_Iterator aCompIter (aStepResult);
159               for (; aCompIter.More(); aCompIter.Next()) {
160                 // add shape in a result
161                 B.Add(C, aCompIter.Value());
162               }
163             }
164             else {
165               // add shape in a result
166               B.Add(C, aStepResult);
167             }
168           }
169           else
170             aShape = BO.Shape();
171         }
172       }
173
174       if (isCompound) {
175         /*
176         TopTools_ListOfShape listShapeC;
177         AddSimpleShapes(C, listShapeC);
178         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
179         bool isOnlySolids = true;
180         for (; itSubC.More(); itSubC.Next()) {
181           TopoDS_Shape aValueC = itSubC.Value();
182           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
183         }
184         if (isOnlySolids)
185           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
186         else
187           aShape = C;
188         */
189
190         // As GlueFaces has been improved to keep all kind of shapes
191         TopExp_Explorer anExp (C, TopAbs_VERTEX);
192         if (anExp.More())
193           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
194         else
195           aShape = C;
196       }
197     }
198
199     // perform CUT operation
200     else if (aType == BOOLEAN_CUT) {
201       BRep_Builder B;
202       TopoDS_Compound C;
203       B.MakeCompound(C);
204
205       TopTools_ListOfShape listShapes, listTools;
206       AddSimpleShapes(aShape1, listShapes);
207       AddSimpleShapes(aShape2, listTools);
208
209       Standard_Boolean isCompound = (listShapes.Extent() > 1);
210
211       TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
212       for (; itSub1.More(); itSub1.Next()) {
213         TopoDS_Shape aCut = itSub1.Value();
214         // tools
215         TopTools_ListIteratorOfListOfShape itSub2 (listTools);
216         for (; itSub2.More(); itSub2.Next()) {
217           TopoDS_Shape aTool = itSub2.Value();
218           BRepAlgoAPI_Cut BO (aCut, aTool);
219           if (!BO.IsDone()) {
220             StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
221           }
222           aCut = BO.Shape();
223         }
224         if (isCompound) {
225           // check result of this step: if it is a compound (boolean operations
226           // allways return a compound), we add all sub-shapes of it.
227           // This allows to avoid adding empty compounds,
228           // resulting from CUT of parts
229           if (aCut.ShapeType() == TopAbs_COMPOUND) {
230             TopoDS_Iterator aCompIter (aCut);
231             for (; aCompIter.More(); aCompIter.Next()) {
232               // add shape in a result
233               B.Add(C, aCompIter.Value());
234             }
235           }
236           else {
237             // add shape in a result
238             B.Add(C, aCut);
239           }
240         }
241         else
242           aShape = aCut;
243       }
244
245       if (isCompound) {
246         /*
247         TopTools_ListOfShape listShapeC;
248         AddSimpleShapes(C, listShapeC);
249         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
250         bool isOnlySolids = true;
251         for (; itSubC.More(); itSubC.Next()) {
252           TopoDS_Shape aValueC = itSubC.Value();
253           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
254         }
255         if (isOnlySolids)
256           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
257         else
258           aShape = C;
259         */
260
261         // As GlueFaces has been improved to keep all kind of shapes
262         TopExp_Explorer anExp (C, TopAbs_VERTEX);
263         if (anExp.More())
264           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
265         else
266           aShape = C;
267       }
268     }
269
270     // perform FUSE operation
271     else if (aType == BOOLEAN_FUSE) {
272       /* Fix for NPAL15379: refused
273       // Check arguments
274       TopTools_ListOfShape listShape1, listShape2;
275       AddSimpleShapes(aShape1, listShape1);
276       AddSimpleShapes(aShape2, listShape2);
277
278       Standard_Boolean isIntersect = Standard_False;
279
280       if (listShape1.Extent() > 1 && !isIntersect) {
281         // check intersections inside the first compound
282         TopTools_ListIteratorOfListOfShape it1 (listShape1);
283         for (; it1.More() && !isIntersect; it1.Next()) {
284           TopoDS_Shape aValue1 = it1.Value();
285           TopTools_ListIteratorOfListOfShape it2 (listShape1);
286           for (; it2.More() && !isIntersect; it2.Next()) {
287             TopoDS_Shape aValue2 = it2.Value();
288             if (aValue2 != aValue1) {
289               BRepAlgoAPI_Section BO (aValue1, aValue2);
290               if (BO.IsDone()) {
291                 TopoDS_Shape aSect = BO.Shape();
292                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
293                 if (anExp.More()) {
294                   isIntersect = Standard_True;
295                 }
296               }
297             }
298           }
299         }
300       }
301
302       if (listShape2.Extent() > 1 && !isIntersect) {
303         // check intersections inside the second compound
304         TopTools_ListIteratorOfListOfShape it1 (listShape2);
305         for (; it1.More() && !isIntersect; it1.Next()) {
306           TopoDS_Shape aValue1 = it1.Value();
307           TopTools_ListIteratorOfListOfShape it2 (listShape2);
308           for (; it2.More() && !isIntersect; it2.Next()) {
309             TopoDS_Shape aValue2 = it2.Value();
310             if (aValue2 != aValue1) {
311               BRepAlgoAPI_Section BO (aValue1, aValue2);
312               if (BO.IsDone()) {
313                 TopoDS_Shape aSect = BO.Shape();
314                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
315                 if (anExp.More()) {
316                   isIntersect = Standard_True;
317                 }
318               }
319             }
320           }
321         }
322       }
323
324       if (isIntersect) {
325         // have intersections inside compounds
326         // check intersections between compounds
327         TopTools_ListIteratorOfListOfShape it1 (listShape1);
328         for (; it1.More(); it1.Next()) {
329           TopoDS_Shape aValue1 = it1.Value();
330           TopTools_ListIteratorOfListOfShape it2 (listShape2);
331           for (; it2.More(); it2.Next()) {
332             TopoDS_Shape aValue2 = it2.Value();
333             if (aValue2 != aValue1) {
334               BRepAlgoAPI_Section BO (aValue1, aValue2);
335               if (BO.IsDone()) {
336                 TopoDS_Shape aSect = BO.Shape();
337                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
338                 if (anExp.More()) {
339                   StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
340                 }
341               }
342             }
343           }
344         }
345       }
346       */
347
348       // Perform
349       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
350       if (!BO.IsDone()) {
351         StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
352       }
353       aShape = BO.Shape();
354     }
355
356     // perform SECTION operation
357     else if (aType == BOOLEAN_SECTION) {
358       BRep_Builder B;
359       TopoDS_Compound C;
360       B.MakeCompound(C);
361
362       TopTools_ListOfShape listShape1, listShape2;
363       AddSimpleShapes(aShape1, listShape1);
364       AddSimpleShapes(aShape2, listShape2);
365
366       Standard_Boolean isCompound =
367         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
368
369       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
370       for (; itSub1.More(); itSub1.Next()) {
371         TopoDS_Shape aValue1 = itSub1.Value();
372         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
373         for (; itSub2.More(); itSub2.Next()) {
374           TopoDS_Shape aValue2 = itSub2.Value();
375           BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
376           // Set approximation to have an attached 3D BSpline geometry to each edge,
377           // where analytic curve is not possible. Without this flag in some cases
378           // we obtain BSpline curve of degree 1 (C0), which is slowly
379           // processed by some algorithms (Partition for example).
380           BO.Approximation(Standard_True);
381           //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
382           BO.ComputePCurveOn1(Standard_True);
383           BO.ComputePCurveOn2(Standard_True);
384           //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
385           
386           BO.Build();
387           if (!BO.IsDone()) {
388             StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
389           }
390           if (isCompound) {
391             TopoDS_Shape aStepResult = BO.Shape();
392
393             // check result of this step: if it is a compound (boolean operations
394             // allways return a compound), we add all sub-shapes of it.
395             // This allows to avoid adding empty compounds,
396             // resulting from SECTION on two non-intersecting shapes.
397             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
398               TopoDS_Iterator aCompIter (aStepResult);
399               for (; aCompIter.More(); aCompIter.Next()) {
400                 // add shape in a result
401                 B.Add(C, aCompIter.Value());
402               }
403             }
404             else {
405               // add shape in a result
406               B.Add(C, aStepResult);
407             }
408           }
409           else
410             aShape = BO.Shape();
411         }
412       }
413
414       if (isCompound) {
415         //aShape = C;
416
417         // As GlueFaces has been improved to keep all kind of shapes
418         TopExp_Explorer anExp (C, TopAbs_VERTEX);
419         if (anExp.More())
420           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
421         else
422           aShape = C;
423       }
424     }
425
426     // UNKNOWN operation
427     else {
428     }
429   }
430
431   if (aShape.IsNull()) return 0;
432
433   // as boolean operations always produce compound, lets simplify it
434   // for the case, if it contains only one sub-shape
435   TopTools_ListOfShape listShapeRes;
436   AddSimpleShapes(aShape, listShapeRes);
437   if (listShapeRes.Extent() == 1) {
438     aShape = listShapeRes.First();
439     if (aShape.IsNull()) return 0;
440   }
441
442   // 08.07.2008 skl for bug 19761 from Mantis
443   BRepCheck_Analyzer ana (aShape, Standard_True);
444   ana.Init(aShape);
445   if (!ana.IsValid()) {
446     ShapeFix_ShapeTolerance aSFT;
447     aSFT.LimitTolerance(aShape, Precision::Confusion(),
448                         Precision::Confusion(), TopAbs_SHAPE);
449     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
450     aSfs->Perform();
451     aShape = aSfs->Shape();
452     ana.Init(aShape);
453     if (!ana.IsValid())
454       Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
455   }
456   //if (!BRepAlgo::IsValid(aShape)) {
457   //  Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
458   //}
459
460   // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
461   // 1. Get shape parameters for comparison
462   int nbTypes [TopAbs_SHAPE];
463   {
464     for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
465       nbTypes[iType] = 0;
466     nbTypes[aShape.ShapeType()]++;
467
468     TopTools_MapOfShape aMapOfShape;
469     aMapOfShape.Add(aShape);
470     TopTools_ListOfShape aListOfShape;
471     aListOfShape.Append(aShape);
472
473     TopTools_ListIteratorOfListOfShape itL (aListOfShape);
474     for (; itL.More(); itL.Next()) {
475       TopoDS_Iterator it (itL.Value());
476       for (; it.More(); it.Next()) {
477         TopoDS_Shape s = it.Value();
478         if (aMapOfShape.Add(s)) {
479           aListOfShape.Append(s);
480           nbTypes[s.ShapeType()]++;
481         }
482       }
483     }
484   }
485
486   // 2. Limit tolerance
487   TopoDS_Shape aShapeCopy;
488   TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
489   TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
490   ShapeFix_ShapeTolerance aSFT;
491   aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
492   Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
493   aSfs->Perform();
494   aShapeCopy = aSfs->Shape();
495
496   // 3. Check parameters
497   ana.Init(aShapeCopy);
498   if (ana.IsValid()) {
499     int iType, nbTypesCopy [TopAbs_SHAPE];
500
501     for (iType = 0; iType < TopAbs_SHAPE; ++iType)
502       nbTypesCopy[iType] = 0;
503     nbTypesCopy[aShapeCopy.ShapeType()]++;
504
505     TopTools_MapOfShape aMapOfShape;
506     aMapOfShape.Add(aShapeCopy);
507     TopTools_ListOfShape aListOfShape;
508     aListOfShape.Append(aShapeCopy);
509
510     TopTools_ListIteratorOfListOfShape itL (aListOfShape);
511     for (; itL.More(); itL.Next()) {
512       TopoDS_Iterator it (itL.Value());
513       for (; it.More(); it.Next()) {
514         TopoDS_Shape s = it.Value();
515         if (aMapOfShape.Add(s)) {
516           aListOfShape.Append(s);
517           nbTypesCopy[s.ShapeType()]++;
518         }
519       }
520     }
521
522     bool isEqual = true;
523     for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
524       if (nbTypes[iType] != nbTypesCopy[iType])
525         isEqual = false;
526     }
527     if (isEqual)
528       aShape = aShapeCopy;
529   }
530   // END: Mantis issue 0021060
531
532   //Alternative case to check shape result Mantis 0020604: EDF 1172
533 /*  TopoDS_Iterator It (aShape, Standard_True, Standard_True);
534   int nbSubshapes=0;
535   for (; It.More(); It.Next())
536     nbSubshapes++;
537   if (!nbSubshapes)
538     Standard_ConstructionError::Raise("Boolean operation aborted : result object is empty compound");*/
539   //end of 0020604: EDF 1172
540   //! the changes temporary commented because of customer needs (see the same mantis bug)
541
542   aFunction->SetValue(aShape);
543
544   log.SetTouched(Label());
545
546   return 1;
547 }
548
549
550 //=======================================================================
551 //function :  GEOMImpl_BooleanDriver_Type_
552 //purpose  :
553 //=======================================================================
554 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
555 {
556   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
557   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
558   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
559   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
560   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
561   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
562
563   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
564   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
565                                                          sizeof(GEOMImpl_BooleanDriver),
566                                                          1,
567                                                          (Standard_Address)_Ancestors,
568                                                          (Standard_Address)NULL);
569
570   return _aType;
571 }
572
573 //=======================================================================
574 //function : DownCast
575 //purpose  :
576 //=======================================================================
577 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
578 {
579   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
580
581   if (!AnObject.IsNull()) {
582     if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
583       _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
584     }
585   }
586
587   return _anOtherObject;
588 }