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