Salome HOME
Merge branch 'master' of salome:modules/geom
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include <GEOMUtils.hxx>
29 #include <BlockFix_BlockFixAPI.hxx>
30 #include <ShHealOper_ShapeProcess.hxx>
31
32 #include <TNaming_CopyShape.hxx>
33
34 #include <BRep_Builder.hxx>
35 #include <BRepAlgoAPI_Common.hxx>
36 #include <BRepAlgoAPI_Cut.hxx>
37 #include <BRepAlgoAPI_Fuse.hxx>
38 #include <BRepAlgoAPI_Section.hxx>
39 #include <BOPAlgo_CheckerSI.hxx>
40 #include <BOPDS_DS.hxx>
41 #include <BOPTools_AlgoTools.hxx>
42
43 #include <TopExp_Explorer.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 // Depth of self-intersection check (see BOPAlgo_CheckerSI::SetLevelOfCheck() for more details)
58 // Default value for BOPAlgo_CheckerSI gives very long computation when checking face-to-face intersections;
59 // here check level is decreased to more appropriate value to avoid problems with performance).
60 #define BOP_SELF_INTERSECTIONS_LEVEL 4
61
62 /**
63  * This function performs extra edges removal.
64  *
65  * \param theShape the shape to be processed.
66  * \return the modified shape or null shape in case of failure.
67  */
68 static TopoDS_Shape RemoveExtraEdges(const TopoDS_Shape &theShape)
69 {
70   TopoDS_Shape aResult;
71
72   if (!theShape.IsNull()) {
73     BlockFix_BlockFixAPI aTool;
74
75     aTool.OptimumNbFaces() = 0;
76     aTool.SetShape(theShape);
77     aTool.Perform();
78     TopoDS_Shape aShape = aTool.Shape();
79
80     if (GEOMUtils::CheckShape(aShape)) {
81       aResult = aShape;
82     }
83     else {
84       TopoDS_Shape aFixed;
85       ShHealOper_ShapeProcess aHealer;
86       aHealer.Perform(aShape, aFixed);
87       if (aHealer.isDone() && GEOMUtils::CheckShape(aFixed))
88         aResult = aFixed;
89     }
90   }
91
92   return aResult;
93 }
94
95 //=======================================================================
96 //function : GetID
97 //purpose  :
98 //=======================================================================
99 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
100 {
101   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
102   return aBooleanDriver;
103 }
104
105 //=======================================================================
106 //function : GEOMImpl_BooleanDriver
107 //purpose  :
108 //=======================================================================
109 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
110 {
111 }
112
113 //=======================================================================
114 //function : Execute
115 //purpose  :
116 //=======================================================================
117 Standard_Integer GEOMImpl_BooleanDriver::Execute(LOGBOOK& log) const
118 {
119   if (Label().IsNull()) return 0;
120   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
121
122   GEOMImpl_IBoolean aCI (aFunction);
123   Standard_Integer aType = aFunction->GetType();
124   const Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
125   const Standard_Boolean isRmExtraEdges  = aCI.GetRmExtraEdges();
126
127   TopoDS_Shape aShape;
128
129   switch (aType) {
130   case BOOLEAN_COMMON:
131   case BOOLEAN_CUT:
132   case BOOLEAN_FUSE:
133   case BOOLEAN_SECTION:
134     {
135       Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
136       Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
137       TopoDS_Shape aShape1 = aRefShape1->GetValue();
138       TopoDS_Shape aShape2 = aRefShape2->GetValue();
139
140       if (!aShape1.IsNull() && !aShape2.IsNull()) {
141         // check arguments for Mantis issue 0021019
142         if (!GEOMUtils::CheckShape(aShape1, true) || !GEOMUtils::CheckShape(aShape2, true))
143           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
144
145         if (isCheckSelfInte) {
146           BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
147           aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
148           BOPCol_ListOfShape aList1, aList2;
149           aList1.Append(aShape1);
150           aList2.Append(aShape2);
151           aCSI.SetArguments(aList1);
152           aCSI.Perform();
153           if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
154             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
155           aCSI.SetArguments(aList2);
156           aCSI.Perform();
157           if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
158             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
159         }
160
161         // Make a copy to prevent the original shape changes.
162         TopoDS_Shape aShapeCopy1;
163         TopoDS_Shape aShapeCopy2;
164         TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
165         TNaming_CopyShape::CopyTool(aShape1, aMapTShapes, aShapeCopy1);
166         TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy2);
167
168         aShape = performOperation (aShapeCopy1, aShapeCopy2, aType);
169
170         if (isRmExtraEdges) {
171           aShape = RemoveExtraEdges(aShape);
172         }
173
174         if (aShape.IsNull()) {
175           return 0;
176         }
177       }
178     }
179     break;
180   case BOOLEAN_COMMON_LIST:
181   case BOOLEAN_FUSE_LIST:
182     {
183       Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
184       const Standard_Integer nbShapes = aShapes->Length();
185       Standard_Integer i;
186       Handle(GEOM_Function) aRefShape;
187       TopoDS_Shape aShape2;
188       Standard_Integer aSimpleType =
189         (aType == BOOLEAN_FUSE_LIST ? BOOLEAN_FUSE : BOOLEAN_COMMON);
190
191       if (nbShapes > 0) {
192         aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
193         aShape = aRefShape->GetValue();
194
195         if (!aShape.IsNull()) {
196           // check arguments for Mantis issue 0021019
197           if (!GEOMUtils::CheckShape(aShape, true))
198             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
199
200           BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
201
202           if (isCheckSelfInte) {
203             aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
204             BOPCol_ListOfShape aList1;
205             aList1.Append(aShape);
206             aCSI.SetArguments(aList1);
207             aCSI.Perform();
208             if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
209               StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
210             }
211           }
212
213           // Copy shape
214           TopoDS_Shape aShapeCopy;
215           TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
216
217           TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
218           aShape = aShapeCopy;
219
220           for (i = 2; i <= nbShapes; i++) {
221             aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
222             aShape2 = aRefShape->GetValue();
223
224             if (!GEOMUtils::CheckShape(aShape2, true))
225               StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
226
227             if (isCheckSelfInte) {
228               BOPCol_ListOfShape aList2;
229               aList2.Append(aShape2);
230               aCSI.SetArguments(aList2);
231               aCSI.Perform();
232               if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
233                 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
234               }
235             }
236
237             // Copy shape
238             aShapeCopy.Nullify();
239             TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy);
240             aShape = performOperation (aShape, aShapeCopy, aSimpleType);
241
242             if (isRmExtraEdges) {
243               aShape = RemoveExtraEdges(aShape);
244             }
245
246             if (aShape.IsNull()) {
247               return 0;
248             }
249           }
250         }
251       }
252     }
253     break;
254   case BOOLEAN_CUT_LIST:
255     {
256       Handle(GEOM_Function) aRefObject = aCI.GetShape1();
257
258       aShape = aRefObject->GetValue();
259
260       if (!aShape.IsNull()) {
261         // check arguments for Mantis issue 0021019
262         if (!GEOMUtils::CheckShape(aShape, true))
263           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
264
265         BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
266
267         if (isCheckSelfInte) {
268           aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
269           BOPCol_ListOfShape aList1;
270           aList1.Append(aShape);
271           aCSI.SetArguments(aList1);
272           aCSI.Perform();
273           if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
274             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
275           }
276         }
277
278         // Copy shape
279         TopoDS_Shape aShapeCopy;
280         TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
281
282         TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
283         aShape = aShapeCopy;
284
285         Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
286         const Standard_Integer nbShapes = aTools->Length();
287         Standard_Integer i;
288         Handle(GEOM_Function) aRefTool;
289         TopoDS_Shape aTool;
290
291         for (i = 1; i <= nbShapes; i++) {
292           aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
293           aTool = aRefTool->GetValue();
294
295           if (!GEOMUtils::CheckShape(aTool, true))
296             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
297
298           if (isCheckSelfInte) {
299             BOPCol_ListOfShape aList2;
300             aList2.Append(aTool);
301             aCSI.SetArguments(aList2);
302             aCSI.Perform();
303             if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
304               StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
305             }
306           }
307
308           // Copy shape
309           aShapeCopy.Nullify();
310           TNaming_CopyShape::CopyTool(aTool, aMapTShapes, aShapeCopy);
311           aShape = performOperation (aShape, aShapeCopy, BOOLEAN_CUT);
312
313           if (aShape.IsNull()) {
314             return 0;
315           }
316         }
317       }
318     }
319     break;
320   default:
321     break;
322   }
323
324   aFunction->SetValue(aShape);
325
326 #if OCC_VERSION_MAJOR < 7
327   log.SetTouched(Label());
328 #else
329   log->SetTouched(Label());
330 #endif
331
332   return 1;
333 }
334
335 //=======================================================================
336 //function : makeCompoundShellFromFaces
337 //purpose  :
338 //=======================================================================
339 TopoDS_Shape GEOMImpl_BooleanDriver::makeCompoundShellFromFaces
340                                (const TopoDS_Shape theShape)const
341 {
342   if (theShape.ShapeType() != TopAbs_COMPOUND)
343     return theShape;
344
345   BRep_Builder B;
346   TopoDS_Compound aFaces;
347   B.MakeCompound(aFaces);
348
349   // simplify compound structure for
350   // Mantis issue 0023419 (note 0021712)
351   TopExp_Explorer aExp;
352   TopTools_MapOfShape aMapFaces;
353   aExp.Init(theShape, TopAbs_FACE);
354   for (; aExp.More(); aExp.Next()) {
355     const TopoDS_Shape& aFace = aExp.Current();
356     if (aMapFaces.Add(aFace)) {
357       B.Add(aFaces, aFace);
358     }
359   }
360
361   BOPCol_ListOfShape aListShapes;
362   BOPTools_AlgoTools::MakeConnexityBlocks(aFaces, TopAbs_EDGE, TopAbs_FACE, aListShapes);
363
364   if (aListShapes.IsEmpty())
365     return theShape;
366
367   TopoDS_Compound aResult;
368   B.MakeCompound(aResult);
369   BOPCol_ListIteratorOfListOfShape anIter(aListShapes);
370
371   for (; anIter.More(); anIter.Next()) {
372     TopoDS_Shell aShell;
373     B.MakeShell(aShell);
374     TopoDS_Shape aShapeFromFaces = anIter.Value();
375     aExp.Init(aShapeFromFaces, TopAbs_FACE);
376     for (; aExp.More(); aExp.Next()) {
377       const TopoDS_Shape& aFace = aExp.Current();
378       B.Add(aShell, aFace);
379     }
380     if (!aShell.IsNull()) {
381       BOPTools_AlgoTools::OrientFacesOnShell(aShell);
382       B.Add(aResult, aShell);
383     }
384     else
385       B.Add(aResult, aShapeFromFaces);
386   }
387
388   return aResult;
389 }
390
391 //=======================================================================
392 //function : performOperation
393 //purpose  :
394 //=======================================================================
395 TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
396                                (const TopoDS_Shape theShape1,
397                                 const TopoDS_Shape theShape2,
398                                 const Standard_Integer theType)const
399 {
400   TopoDS_Shape aShape;
401
402   // perform COMMON operation
403   if (theType == BOOLEAN_COMMON) {
404     BRep_Builder B;
405     TopoDS_Compound C;
406     B.MakeCompound(C);
407
408     TopTools_ListOfShape listShape1, listShape2;
409     GEOMUtils::AddSimpleShapes(theShape1, listShape1);
410     GEOMUtils::AddSimpleShapes(theShape2, listShape2);
411
412     Standard_Boolean isCompound =
413       (listShape1.Extent() > 1 || listShape2.Extent() > 1);
414
415     TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
416     for (; itSub1.More(); itSub1.Next()) {
417       TopoDS_Shape aValue1 = itSub1.Value();
418       TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
419       for (; itSub2.More(); itSub2.Next()) {
420         TopoDS_Shape aValue2 = itSub2.Value();
421         BRepAlgoAPI_Common BO (aValue1, aValue2);
422         if (!BO.IsDone()) {
423           StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
424         }
425         if (isCompound) {
426           TopoDS_Shape aStepResult = BO.Shape();
427
428           // check result of this step: if it is a compound (boolean operations
429           // allways return a compound), we add all sub-shapes of it.
430           // This allows to avoid adding empty compounds,
431           // resulting from COMMON on two non-intersecting shapes.
432           if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
433 #if OCC_VERSION_MAJOR >= 7
434             if ((aValue1.ShapeType() == TopAbs_FACE || aValue1.ShapeType() == TopAbs_SHELL) &&
435                 (aValue2.ShapeType() == TopAbs_FACE || aValue2.ShapeType() == TopAbs_SHELL)) {
436               aStepResult = makeCompoundShellFromFaces(aStepResult);
437             }
438 #endif
439             TopoDS_Iterator aCompIter (aStepResult);
440             for (; aCompIter.More(); aCompIter.Next()) {
441               // add shape in a result
442               B.Add(C, aCompIter.Value());
443             }
444           }
445           else {
446             // add shape in a result
447             B.Add(C, aStepResult);
448           }
449         }
450         else
451           aShape = BO.Shape();
452       }
453     }
454
455     if (isCompound) {
456       // As GlueFaces has been improved to keep all kind of shapes
457       TopExp_Explorer anExp (C, TopAbs_VERTEX);
458       if (anExp.More())
459         aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
460       else
461         aShape = C;
462     }
463   }
464
465   // perform CUT operation
466   else if (theType == BOOLEAN_CUT) {
467     BRep_Builder B;
468     TopoDS_Compound C;
469     B.MakeCompound(C);
470
471     TopTools_ListOfShape listShapes, listTools;
472     GEOMUtils::AddSimpleShapes(theShape1, listShapes);
473     GEOMUtils::AddSimpleShapes(theShape2, listTools);
474
475     Standard_Boolean isCompound = (listShapes.Extent() > 1);
476
477     TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
478     for (; itSub1.More(); itSub1.Next()) {
479       TopoDS_Shape aCut = itSub1.Value();
480       // tools
481       TopTools_ListIteratorOfListOfShape itSub2 (listTools);
482       for (; itSub2.More(); itSub2.Next()) {
483         TopoDS_Shape aTool = itSub2.Value();
484         BRepAlgoAPI_Cut BO (aCut, aTool);
485         if (!BO.IsDone()) {
486           StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
487         }
488         aCut = BO.Shape();
489       }
490       if (isCompound) {
491         // check result of this step: if it is a compound (boolean operations
492         // allways return a compound), we add all sub-shapes of it.
493         // This allows to avoid adding empty compounds,
494         // resulting from CUT of parts
495         if (aCut.ShapeType() == TopAbs_COMPOUND) {
496 #if OCC_VERSION_MAJOR >= 7
497           if (itSub1.Value().ShapeType() == TopAbs_FACE ||
498               itSub1.Value().ShapeType() == TopAbs_SHELL) {
499             aCut = makeCompoundShellFromFaces(aCut);
500           }
501 #endif
502           TopoDS_Iterator aCompIter (aCut);
503           for (; aCompIter.More(); aCompIter.Next()) {
504             // add shape in a result
505             B.Add(C, aCompIter.Value());
506           }
507         }
508         else {
509           // add shape in a result
510           B.Add(C, aCut);
511         }
512       }
513       else
514         aShape = aCut;
515     }
516
517     if (isCompound) {
518       // As GlueFaces has been improved to keep all kind of shapes
519       TopExp_Explorer anExp (C, TopAbs_VERTEX);
520       if (anExp.More())
521         aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
522       else
523         aShape = C;
524     }
525   }
526
527   // perform FUSE operation
528   else if (theType == BOOLEAN_FUSE) {
529 #if OCC_VERSION_MAJOR >= 7
530     Standard_Boolean isFaces = Standard_False;
531     TopTools_ListOfShape listShape1, listShape2;
532     GEOMUtils::AddSimpleShapes(theShape1, listShape1);
533     GEOMUtils::AddSimpleShapes(theShape2, listShape2);
534
535     TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
536     for (; itSub1.More(); itSub1.Next()) {
537       TopoDS_Shape aValue1 = itSub1.Value();
538       TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
539       for (; itSub2.More(); itSub2.Next()) {
540         TopoDS_Shape aValue2 = itSub2.Value();
541         if ((aValue1.ShapeType() == TopAbs_FACE || aValue1.ShapeType() == TopAbs_SHELL) &&
542             (aValue2.ShapeType() == TopAbs_FACE || aValue2.ShapeType() == TopAbs_SHELL)) {
543           isFaces = Standard_True;
544         }
545       }
546     }
547 #endif
548
549     // Perform
550     BRepAlgoAPI_Fuse BO (theShape1, theShape2);
551     if (!BO.IsDone()) {
552       StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
553     }
554     aShape = BO.Shape();
555 #if OCC_VERSION_MAJOR >= 7
556     if (isFaces)
557       aShape = makeCompoundShellFromFaces(aShape);
558 #endif
559   }
560
561   // perform SECTION operation
562   else if (theType == BOOLEAN_SECTION) {
563     BRep_Builder B;
564     TopoDS_Compound C;
565     B.MakeCompound(C);
566
567     TopTools_ListOfShape listShape1, listShape2;
568     GEOMUtils::AddSimpleShapes(theShape1, listShape1);
569     GEOMUtils::AddSimpleShapes(theShape2, listShape2);
570
571     Standard_Boolean isCompound =
572       (listShape1.Extent() > 1 || listShape2.Extent() > 1);
573
574     TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
575     for (; itSub1.More(); itSub1.Next()) {
576       TopoDS_Shape aValue1 = itSub1.Value();
577       TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
578       for (; itSub2.More(); itSub2.Next()) {
579         TopoDS_Shape aValue2 = itSub2.Value();
580         BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
581         // Set approximation to have an attached 3D BSpline geometry to each edge,
582         // where analytic curve is not possible. Without this flag in some cases
583         // we obtain BSpline curve of degree 1 (C0), which is slowly
584         // processed by some algorithms (Partition for example).
585         BO.Approximation(Standard_True);
586         //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
587         BO.ComputePCurveOn1(Standard_True);
588         BO.ComputePCurveOn2(Standard_True);
589         //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
590
591         BO.Build();
592         if (!BO.IsDone()) {
593           StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
594         }
595         if (isCompound) {
596           TopoDS_Shape aStepResult = BO.Shape();
597
598           // check result of this step: if it is a compound (boolean operations
599           // allways return a compound), we add all sub-shapes of it.
600           // This allows to avoid adding empty compounds,
601           // resulting from SECTION on two non-intersecting shapes.
602           if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
603             TopoDS_Iterator aCompIter (aStepResult);
604             for (; aCompIter.More(); aCompIter.Next()) {
605               // add shape in a result
606               B.Add(C, aCompIter.Value());
607             }
608           }
609           else {
610             // add shape in a result
611             B.Add(C, aStepResult);
612           }
613         }
614         else
615           aShape = BO.Shape();
616       }
617     }
618
619     if (isCompound) {
620       // As GlueFaces has been improved to keep all kind of shapes
621       TopExp_Explorer anExp (C, TopAbs_VERTEX);
622       if (anExp.More())
623         aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
624       else
625         aShape = C;
626     }
627   }
628
629   // UNKNOWN operation
630   else {
631   }
632
633   if (aShape.IsNull()) return aShape;
634
635   // as boolean operations always produce compound, lets simplify it
636   // for the case, if it contains only one sub-shape
637   TopTools_ListOfShape listShapeRes;
638   GEOMUtils::AddSimpleShapes(aShape, listShapeRes);
639   if (listShapeRes.Extent() == 1) {
640     aShape = listShapeRes.First();
641     if (aShape.IsNull()) return aShape;
642   }
643
644   // 08.07.2008 skl for bug 19761 from Mantis
645   if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
646     Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
647
648   return aShape;
649 }
650
651 //================================================================================
652 /*!
653  * \brief Returns a name of creation operation and names and values of creation parameters
654  */
655 //================================================================================
656
657 bool GEOMImpl_BooleanDriver::
658 GetCreationInformation(std::string&             theOperationName,
659                        std::vector<GEOM_Param>& theParams)
660 {
661   if (Label().IsNull()) return 0;
662   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
663
664   GEOMImpl_IBoolean aCI (function);
665   Standard_Integer aType = function->GetType();
666   Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
667
668   switch ( aType ) {
669   case BOOLEAN_COMMON:
670     theOperationName = "COMMON";
671     AddParam( theParams, "Object 1", aCI.GetShape1() );
672     AddParam( theParams, "Object 2", aCI.GetShape2() );
673     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
674     break;
675   case BOOLEAN_CUT:
676     theOperationName = "CUT";
677     AddParam( theParams, "Main Object", aCI.GetShape1() );
678     AddParam( theParams, "Tool Object", aCI.GetShape2() );
679     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
680     break;
681   case BOOLEAN_FUSE:
682     theOperationName = "FUSE";
683     AddParam( theParams, "Object 1", aCI.GetShape1() );
684     AddParam( theParams, "Object 2", aCI.GetShape2() );
685     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
686     AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
687     break;
688   case BOOLEAN_SECTION:
689     theOperationName = "SECTION";
690     AddParam( theParams, "Object 1", aCI.GetShape1() );
691     AddParam( theParams, "Object 2", aCI.GetShape2() );
692     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
693     break;
694   case BOOLEAN_COMMON_LIST:
695     theOperationName = "COMMON";
696     AddParam( theParams, "Selected objects", aCI.GetShapes() );
697     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
698     break;
699   case BOOLEAN_FUSE_LIST:
700     theOperationName = "FUSE";
701     AddParam( theParams, "Selected objects", aCI.GetShapes() );
702     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
703     AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
704     break;
705   case BOOLEAN_CUT_LIST:
706     theOperationName = "CUT";
707     AddParam( theParams, "Main Object", aCI.GetShape1() );
708     AddParam( theParams, "Tool Objects", aCI.GetShapes() );
709     AddParam( theParams, "Check self-intersections", isCheckSelfInte );
710     break;
711   default:
712     return false;
713   }
714
715   return true;
716 }
717
718 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);