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