1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
30 #include <TNaming_CopyShape.hxx>
32 #include <ShapeFix_ShapeTolerance.hxx>
33 #include <ShapeFix_Shape.hxx>
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 #include <BOPAlgo_CheckerSI.hxx>
43 #include <BOPDS_DS.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Iterator.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
54 #include <Precision.hxx>
56 #include <Standard_ConstructionError.hxx>
57 #include <StdFail_NotDone.hxx>
59 // Comment next macro in order to avoid check of boolean operations arguments for self-intersections
60 #define BOP_CHECK_SELF_INTERSECTIONS
61 // Depth of self-intersection check (see BOPAlgo_CheckerSI::SetLevelOfCheck() for more details)
62 // Default value for BOPAlgo_CheckerSI gives very long computation when checking face-to-face intersections;
63 // here check level is decreased to more appropriate value to avoid problems with performance).
64 #define BOP_SELF_INTERSECTIONS_LEVEL 4
66 //=======================================================================
69 //=======================================================================
70 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
72 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
73 return aBooleanDriver;
76 //=======================================================================
77 //function : GEOMImpl_BooleanDriver
79 //=======================================================================
80 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
84 //=======================================================================
87 //=======================================================================
88 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
90 if (Label().IsNull()) return 0;
91 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
93 GEOMImpl_IBoolean aCI (aFunction);
94 Standard_Integer aType = aFunction->GetType();
102 case BOOLEAN_SECTION:
104 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
105 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
106 TopoDS_Shape aShape1 = aRefShape1->GetValue();
107 TopoDS_Shape aShape2 = aRefShape2->GetValue();
109 if (!aShape1.IsNull() && !aShape2.IsNull()) {
110 // check arguments for Mantis issue 0021019
111 BRepCheck_Analyzer ana (aShape1, Standard_True);
113 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
116 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
118 #ifdef BOP_CHECK_SELF_INTERSECTIONS
119 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
120 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
121 BOPCol_ListOfShape aList1, aList2;
122 aList1.Append(aShape1);
123 aList2.Append(aShape2);
124 aCSI.SetArguments(aList1);
126 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
127 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
128 aCSI.SetArguments(aList2);
130 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
131 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
134 aShape = performOperation (aShape1, aShape2, aType);
141 case BOOLEAN_COMMON_LIST:
142 case BOOLEAN_FUSE_LIST:
144 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
145 const Standard_Integer nbShapes = aShapes->Length();
147 Handle(GEOM_Function) aRefShape;
148 TopoDS_Shape aShape2;
149 Standard_Integer aSimpleType =
150 (aType == BOOLEAN_FUSE_LIST ? BOOLEAN_FUSE : BOOLEAN_COMMON);
153 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
154 aShape = aRefShape->GetValue();
156 if (!aShape.IsNull()) {
157 BRepCheck_Analyzer anAna (aShape, Standard_True);
158 #ifdef BOP_CHECK_SELF_INTERSECTIONS
159 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
160 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
162 if (!anAna.IsValid()) {
163 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
166 #ifdef BOP_CHECK_SELF_INTERSECTIONS
167 BOPCol_ListOfShape aList1;
168 aList1.Append(aShape);
169 aCSI.SetArguments(aList1);
171 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
172 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
175 for (i = 2; i <= nbShapes; i++) {
176 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
177 aShape2 = aRefShape->GetValue();
180 if (!anAna.IsValid()) {
181 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
184 #ifdef BOP_CHECK_SELF_INTERSECTIONS
185 BOPCol_ListOfShape aList2;
186 aList2.Append(aShape2);
187 aCSI.SetArguments(aList2);
189 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
190 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
193 aShape = performOperation (aShape, aShape2, aSimpleType);
195 if (aShape.IsNull()) {
203 case BOOLEAN_CUT_LIST:
205 Handle(GEOM_Function) aRefObject = aCI.GetShape1();
207 aShape = aRefObject->GetValue();
209 if (!aShape.IsNull()) {
210 // check arguments for Mantis issue 0021019
211 BRepCheck_Analyzer anAna (aShape, Standard_True);
212 #ifdef BOP_CHECK_SELF_INTERSECTIONS
213 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
214 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
216 if (!anAna.IsValid()) {
217 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
220 #ifdef BOP_CHECK_SELF_INTERSECTIONS
221 BOPCol_ListOfShape aList1;
222 aList1.Append(aShape);
223 aCSI.SetArguments(aList1);
225 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
226 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
230 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
231 const Standard_Integer nbShapes = aTools->Length();
233 Handle(GEOM_Function) aRefTool;
236 for (i = 1; i <= nbShapes; i++) {
237 aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
238 aTool = aRefTool->GetValue();
241 if (!anAna.IsValid()) {
242 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
245 #ifdef BOP_CHECK_SELF_INTERSECTIONS
246 BOPCol_ListOfShape aList2;
247 aList2.Append(aTool);
248 aCSI.SetArguments(aList2);
250 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
251 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
255 aShape = performOperation (aShape, aTool, BOOLEAN_CUT);
257 if (aShape.IsNull()) {
268 aFunction->SetValue(aShape);
270 log.SetTouched(Label());
275 //=======================================================================
276 //function : performOperation
278 //=======================================================================
279 TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
280 (const TopoDS_Shape theShape1,
281 const TopoDS_Shape theShape2,
282 const Standard_Integer theType)const
286 // perform COMMON operation
287 if (theType == BOOLEAN_COMMON) {
292 TopTools_ListOfShape listShape1, listShape2;
293 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
294 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
296 Standard_Boolean isCompound =
297 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
299 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
300 for (; itSub1.More(); itSub1.Next()) {
301 TopoDS_Shape aValue1 = itSub1.Value();
302 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
303 for (; itSub2.More(); itSub2.Next()) {
304 TopoDS_Shape aValue2 = itSub2.Value();
305 BRepAlgoAPI_Common BO (aValue1, aValue2);
307 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
310 TopoDS_Shape aStepResult = BO.Shape();
312 // check result of this step: if it is a compound (boolean operations
313 // allways return a compound), we add all sub-shapes of it.
314 // This allows to avoid adding empty compounds,
315 // resulting from COMMON on two non-intersecting shapes.
316 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
317 TopoDS_Iterator aCompIter (aStepResult);
318 for (; aCompIter.More(); aCompIter.Next()) {
319 // add shape in a result
320 B.Add(C, aCompIter.Value());
324 // add shape in a result
325 B.Add(C, aStepResult);
334 // As GlueFaces has been improved to keep all kind of shapes
335 TopExp_Explorer anExp (C, TopAbs_VERTEX);
337 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
343 // perform CUT operation
344 else if (theType == BOOLEAN_CUT) {
349 TopTools_ListOfShape listShapes, listTools;
350 GEOMUtils::AddSimpleShapes(theShape1, listShapes);
351 GEOMUtils::AddSimpleShapes(theShape2, listTools);
353 Standard_Boolean isCompound = (listShapes.Extent() > 1);
355 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
356 for (; itSub1.More(); itSub1.Next()) {
357 TopoDS_Shape aCut = itSub1.Value();
359 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
360 for (; itSub2.More(); itSub2.Next()) {
361 TopoDS_Shape aTool = itSub2.Value();
362 BRepAlgoAPI_Cut BO (aCut, aTool);
364 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
369 // check result of this step: if it is a compound (boolean operations
370 // allways return a compound), we add all sub-shapes of it.
371 // This allows to avoid adding empty compounds,
372 // resulting from CUT of parts
373 if (aCut.ShapeType() == TopAbs_COMPOUND) {
374 TopoDS_Iterator aCompIter (aCut);
375 for (; aCompIter.More(); aCompIter.Next()) {
376 // add shape in a result
377 B.Add(C, aCompIter.Value());
381 // add shape in a result
390 // As GlueFaces has been improved to keep all kind of shapes
391 TopExp_Explorer anExp (C, TopAbs_VERTEX);
393 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
399 // perform FUSE operation
400 else if (theType == BOOLEAN_FUSE) {
402 BRepAlgoAPI_Fuse BO (theShape1, theShape2);
404 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
409 // perform SECTION operation
410 else if (theType == BOOLEAN_SECTION) {
415 TopTools_ListOfShape listShape1, listShape2;
416 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
417 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
419 Standard_Boolean isCompound =
420 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
422 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
423 for (; itSub1.More(); itSub1.Next()) {
424 TopoDS_Shape aValue1 = itSub1.Value();
425 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
426 for (; itSub2.More(); itSub2.Next()) {
427 TopoDS_Shape aValue2 = itSub2.Value();
428 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
429 // Set approximation to have an attached 3D BSpline geometry to each edge,
430 // where analytic curve is not possible. Without this flag in some cases
431 // we obtain BSpline curve of degree 1 (C0), which is slowly
432 // processed by some algorithms (Partition for example).
433 BO.Approximation(Standard_True);
434 //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
435 BO.ComputePCurveOn1(Standard_True);
436 BO.ComputePCurveOn2(Standard_True);
437 //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
441 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
444 TopoDS_Shape aStepResult = BO.Shape();
446 // check result of this step: if it is a compound (boolean operations
447 // allways return a compound), we add all sub-shapes of it.
448 // This allows to avoid adding empty compounds,
449 // resulting from SECTION on two non-intersecting shapes.
450 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
451 TopoDS_Iterator aCompIter (aStepResult);
452 for (; aCompIter.More(); aCompIter.Next()) {
453 // add shape in a result
454 B.Add(C, aCompIter.Value());
458 // add shape in a result
459 B.Add(C, aStepResult);
468 // As GlueFaces has been improved to keep all kind of shapes
469 TopExp_Explorer anExp (C, TopAbs_VERTEX);
471 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
481 if (aShape.IsNull()) return aShape;
483 // as boolean operations always produce compound, lets simplify it
484 // for the case, if it contains only one sub-shape
485 TopTools_ListOfShape listShapeRes;
486 GEOMUtils::AddSimpleShapes(aShape, listShapeRes);
487 if (listShapeRes.Extent() == 1) {
488 aShape = listShapeRes.First();
489 if (aShape.IsNull()) return aShape;
492 // 08.07.2008 skl for bug 19761 from Mantis
493 BRepCheck_Analyzer ana (aShape, Standard_True);
495 if (!ana.IsValid()) {
496 ShapeFix_ShapeTolerance aSFT;
497 aSFT.LimitTolerance(aShape, Precision::Confusion(),
498 Precision::Confusion(), TopAbs_SHAPE);
499 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
501 aShape = aSfs->Shape();
504 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
507 // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
508 // 1. Get shape parameters for comparison
509 int nbTypes [TopAbs_SHAPE];
511 for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
513 nbTypes[aShape.ShapeType()]++;
515 TopTools_MapOfShape aMapOfShape;
516 aMapOfShape.Add(aShape);
517 TopTools_ListOfShape aListOfShape;
518 aListOfShape.Append(aShape);
520 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
521 for (; itL.More(); itL.Next()) {
522 TopoDS_Iterator it (itL.Value());
523 for (; it.More(); it.Next()) {
524 TopoDS_Shape s = it.Value();
525 if (aMapOfShape.Add(s)) {
526 aListOfShape.Append(s);
527 nbTypes[s.ShapeType()]++;
533 // 2. Limit tolerance
534 TopoDS_Shape aShapeCopy;
535 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
536 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
537 ShapeFix_ShapeTolerance aSFT;
538 aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
539 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
541 aShapeCopy = aSfs->Shape();
543 // 3. Check parameters
544 ana.Init(aShapeCopy);
546 int iType, nbTypesCopy [TopAbs_SHAPE];
548 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
549 nbTypesCopy[iType] = 0;
550 nbTypesCopy[aShapeCopy.ShapeType()]++;
552 TopTools_MapOfShape aMapOfShape;
553 aMapOfShape.Add(aShapeCopy);
554 TopTools_ListOfShape aListOfShape;
555 aListOfShape.Append(aShapeCopy);
557 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
558 for (; itL.More(); itL.Next()) {
559 TopoDS_Iterator it (itL.Value());
560 for (; it.More(); it.Next()) {
561 TopoDS_Shape s = it.Value();
562 if (aMapOfShape.Add(s)) {
563 aListOfShape.Append(s);
564 nbTypesCopy[s.ShapeType()]++;
570 for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
571 if (nbTypes[iType] != nbTypesCopy[iType])
577 // END: Mantis issue 0021060
582 //================================================================================
584 * \brief Returns a name of creation operation and names and values of creation parameters
586 //================================================================================
588 bool GEOMImpl_BooleanDriver::
589 GetCreationInformation(std::string& theOperationName,
590 std::vector<GEOM_Param>& theParams)
592 if (Label().IsNull()) return 0;
593 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
595 GEOMImpl_IBoolean aCI (function);
596 Standard_Integer aType = function->GetType();
600 theOperationName = "COMMON";
601 AddParam( theParams, "Object 1", aCI.GetShape1() );
602 AddParam( theParams, "Object 2", aCI.GetShape2() );
605 theOperationName = "CUT";
606 AddParam( theParams, "Main Object", aCI.GetShape1() );
607 AddParam( theParams, "Tool Object", aCI.GetShape2() );
610 theOperationName = "FUSE";
611 AddParam( theParams, "Object 1", aCI.GetShape1() );
612 AddParam( theParams, "Object 2", aCI.GetShape2() );
614 case BOOLEAN_SECTION:
615 theOperationName = "SECTION";
616 AddParam( theParams, "Object 1", aCI.GetShape1() );
617 AddParam( theParams, "Object 2", aCI.GetShape2() );
619 case BOOLEAN_COMMON_LIST:
620 theOperationName = "COMMON";
621 AddParam( theParams, "Selected objects", aCI.GetShapes() );
623 case BOOLEAN_FUSE_LIST:
624 theOperationName = "FUSE";
625 AddParam( theParams, "Selected objects", aCI.GetShapes() );
627 case BOOLEAN_CUT_LIST:
628 theOperationName = "CUT";
629 AddParam( theParams, "Main Object", aCI.GetShape1() );
630 AddParam( theParams, "Tool Objects", aCI.GetShapes() );
639 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_BooleanDriver,GEOM_BaseDriver);
641 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);