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 // Make a copy to prevent the original shape changes.
135 TopoDS_Shape aShapeCopy1;
136 TopoDS_Shape aShapeCopy2;
137 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
138 TNaming_CopyShape::CopyTool(aShape1, aMapTShapes, aShapeCopy1);
139 TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy2);
141 aShape = performOperation (aShapeCopy1, aShapeCopy2, aType);
148 case BOOLEAN_COMMON_LIST:
149 case BOOLEAN_FUSE_LIST:
151 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
152 const Standard_Integer nbShapes = aShapes->Length();
154 Handle(GEOM_Function) aRefShape;
155 TopoDS_Shape aShape2;
156 Standard_Integer aSimpleType =
157 (aType == BOOLEAN_FUSE_LIST ? BOOLEAN_FUSE : BOOLEAN_COMMON);
160 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
161 aShape = aRefShape->GetValue();
163 if (!aShape.IsNull()) {
164 BRepCheck_Analyzer anAna (aShape, Standard_True);
165 #ifdef BOP_CHECK_SELF_INTERSECTIONS
166 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
167 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
169 if (!anAna.IsValid()) {
170 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
173 #ifdef BOP_CHECK_SELF_INTERSECTIONS
174 BOPCol_ListOfShape aList1;
175 aList1.Append(aShape);
176 aCSI.SetArguments(aList1);
178 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
179 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
184 TopoDS_Shape aShapeCopy;
185 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
187 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
190 for (i = 2; i <= nbShapes; i++) {
191 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
192 aShape2 = aRefShape->GetValue();
195 if (!anAna.IsValid()) {
196 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
199 #ifdef BOP_CHECK_SELF_INTERSECTIONS
200 BOPCol_ListOfShape aList2;
201 aList2.Append(aShape2);
202 aCSI.SetArguments(aList2);
204 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
205 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
209 TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy);
210 aShape = performOperation (aShape, aShapeCopy, aSimpleType);
212 if (aShape.IsNull()) {
220 case BOOLEAN_CUT_LIST:
222 Handle(GEOM_Function) aRefObject = aCI.GetShape1();
224 aShape = aRefObject->GetValue();
226 if (!aShape.IsNull()) {
227 // check arguments for Mantis issue 0021019
228 BRepCheck_Analyzer anAna (aShape, Standard_True);
229 #ifdef BOP_CHECK_SELF_INTERSECTIONS
230 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
231 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
233 if (!anAna.IsValid()) {
234 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
237 #ifdef BOP_CHECK_SELF_INTERSECTIONS
238 BOPCol_ListOfShape aList1;
239 aList1.Append(aShape);
240 aCSI.SetArguments(aList1);
242 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
243 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
247 TopoDS_Shape aShapeCopy;
248 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
250 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
253 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
254 const Standard_Integer nbShapes = aTools->Length();
256 Handle(GEOM_Function) aRefTool;
259 for (i = 1; i <= nbShapes; i++) {
260 aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
261 aTool = aRefTool->GetValue();
264 if (!anAna.IsValid()) {
265 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
268 #ifdef BOP_CHECK_SELF_INTERSECTIONS
269 BOPCol_ListOfShape aList2;
270 aList2.Append(aTool);
271 aCSI.SetArguments(aList2);
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");
279 TNaming_CopyShape::CopyTool(aTool, aMapTShapes, aShapeCopy);
280 aShape = performOperation (aShape, aShapeCopy, BOOLEAN_CUT);
282 if (aShape.IsNull()) {
293 aFunction->SetValue(aShape);
295 log.SetTouched(Label());
300 //=======================================================================
301 //function : performOperation
303 //=======================================================================
304 TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
305 (const TopoDS_Shape theShape1,
306 const TopoDS_Shape theShape2,
307 const Standard_Integer theType)const
311 // perform COMMON operation
312 if (theType == BOOLEAN_COMMON) {
317 TopTools_ListOfShape listShape1, listShape2;
318 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
319 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
321 Standard_Boolean isCompound =
322 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
324 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
325 for (; itSub1.More(); itSub1.Next()) {
326 TopoDS_Shape aValue1 = itSub1.Value();
327 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
328 for (; itSub2.More(); itSub2.Next()) {
329 TopoDS_Shape aValue2 = itSub2.Value();
330 BRepAlgoAPI_Common BO (aValue1, aValue2);
332 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
335 TopoDS_Shape aStepResult = BO.Shape();
337 // check result of this step: if it is a compound (boolean operations
338 // allways return a compound), we add all sub-shapes of it.
339 // This allows to avoid adding empty compounds,
340 // resulting from COMMON on two non-intersecting shapes.
341 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
342 TopoDS_Iterator aCompIter (aStepResult);
343 for (; aCompIter.More(); aCompIter.Next()) {
344 // add shape in a result
345 B.Add(C, aCompIter.Value());
349 // add shape in a result
350 B.Add(C, aStepResult);
359 // As GlueFaces has been improved to keep all kind of shapes
360 TopExp_Explorer anExp (C, TopAbs_VERTEX);
362 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
368 // perform CUT operation
369 else if (theType == BOOLEAN_CUT) {
374 TopTools_ListOfShape listShapes, listTools;
375 GEOMUtils::AddSimpleShapes(theShape1, listShapes);
376 GEOMUtils::AddSimpleShapes(theShape2, listTools);
378 Standard_Boolean isCompound = (listShapes.Extent() > 1);
380 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
381 for (; itSub1.More(); itSub1.Next()) {
382 TopoDS_Shape aCut = itSub1.Value();
384 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
385 for (; itSub2.More(); itSub2.Next()) {
386 TopoDS_Shape aTool = itSub2.Value();
387 BRepAlgoAPI_Cut BO (aCut, aTool);
389 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
394 // check result of this step: if it is a compound (boolean operations
395 // allways return a compound), we add all sub-shapes of it.
396 // This allows to avoid adding empty compounds,
397 // resulting from CUT of parts
398 if (aCut.ShapeType() == TopAbs_COMPOUND) {
399 TopoDS_Iterator aCompIter (aCut);
400 for (; aCompIter.More(); aCompIter.Next()) {
401 // add shape in a result
402 B.Add(C, aCompIter.Value());
406 // add shape in a result
415 // As GlueFaces has been improved to keep all kind of shapes
416 TopExp_Explorer anExp (C, TopAbs_VERTEX);
418 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
424 // perform FUSE operation
425 else if (theType == BOOLEAN_FUSE) {
427 BRepAlgoAPI_Fuse BO (theShape1, theShape2);
429 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
434 // perform SECTION operation
435 else if (theType == BOOLEAN_SECTION) {
440 TopTools_ListOfShape listShape1, listShape2;
441 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
442 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
444 Standard_Boolean isCompound =
445 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
447 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
448 for (; itSub1.More(); itSub1.Next()) {
449 TopoDS_Shape aValue1 = itSub1.Value();
450 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
451 for (; itSub2.More(); itSub2.Next()) {
452 TopoDS_Shape aValue2 = itSub2.Value();
453 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
454 // Set approximation to have an attached 3D BSpline geometry to each edge,
455 // where analytic curve is not possible. Without this flag in some cases
456 // we obtain BSpline curve of degree 1 (C0), which is slowly
457 // processed by some algorithms (Partition for example).
458 BO.Approximation(Standard_True);
459 //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
460 BO.ComputePCurveOn1(Standard_True);
461 BO.ComputePCurveOn2(Standard_True);
462 //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
466 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
469 TopoDS_Shape aStepResult = BO.Shape();
471 // check result of this step: if it is a compound (boolean operations
472 // allways return a compound), we add all sub-shapes of it.
473 // This allows to avoid adding empty compounds,
474 // resulting from SECTION on two non-intersecting shapes.
475 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
476 TopoDS_Iterator aCompIter (aStepResult);
477 for (; aCompIter.More(); aCompIter.Next()) {
478 // add shape in a result
479 B.Add(C, aCompIter.Value());
483 // add shape in a result
484 B.Add(C, aStepResult);
493 // As GlueFaces has been improved to keep all kind of shapes
494 TopExp_Explorer anExp (C, TopAbs_VERTEX);
496 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
506 if (aShape.IsNull()) return aShape;
508 // as boolean operations always produce compound, lets simplify it
509 // for the case, if it contains only one sub-shape
510 TopTools_ListOfShape listShapeRes;
511 GEOMUtils::AddSimpleShapes(aShape, listShapeRes);
512 if (listShapeRes.Extent() == 1) {
513 aShape = listShapeRes.First();
514 if (aShape.IsNull()) return aShape;
517 // 08.07.2008 skl for bug 19761 from Mantis
518 BRepCheck_Analyzer ana (aShape, Standard_True);
520 if (!ana.IsValid()) {
521 ShapeFix_ShapeTolerance aSFT;
522 aSFT.LimitTolerance(aShape, Precision::Confusion(),
523 Precision::Confusion(), TopAbs_SHAPE);
524 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
526 aShape = aSfs->Shape();
529 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
532 // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
533 // 1. Get shape parameters for comparison
534 int nbTypes [TopAbs_SHAPE];
536 for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
538 nbTypes[aShape.ShapeType()]++;
540 TopTools_MapOfShape aMapOfShape;
541 aMapOfShape.Add(aShape);
542 TopTools_ListOfShape aListOfShape;
543 aListOfShape.Append(aShape);
545 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
546 for (; itL.More(); itL.Next()) {
547 TopoDS_Iterator it (itL.Value());
548 for (; it.More(); it.Next()) {
549 TopoDS_Shape s = it.Value();
550 if (aMapOfShape.Add(s)) {
551 aListOfShape.Append(s);
552 nbTypes[s.ShapeType()]++;
558 // 2. Limit tolerance
559 TopoDS_Shape aShapeCopy;
560 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
561 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
562 ShapeFix_ShapeTolerance aSFT;
563 aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
564 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
566 aShapeCopy = aSfs->Shape();
568 // 3. Check parameters
569 ana.Init(aShapeCopy);
571 int iType, nbTypesCopy [TopAbs_SHAPE];
573 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
574 nbTypesCopy[iType] = 0;
575 nbTypesCopy[aShapeCopy.ShapeType()]++;
577 TopTools_MapOfShape aMapOfShape;
578 aMapOfShape.Add(aShapeCopy);
579 TopTools_ListOfShape aListOfShape;
580 aListOfShape.Append(aShapeCopy);
582 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
583 for (; itL.More(); itL.Next()) {
584 TopoDS_Iterator it (itL.Value());
585 for (; it.More(); it.Next()) {
586 TopoDS_Shape s = it.Value();
587 if (aMapOfShape.Add(s)) {
588 aListOfShape.Append(s);
589 nbTypesCopy[s.ShapeType()]++;
595 for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
596 if (nbTypes[iType] != nbTypesCopy[iType])
602 // END: Mantis issue 0021060
607 //================================================================================
609 * \brief Returns a name of creation operation and names and values of creation parameters
611 //================================================================================
613 bool GEOMImpl_BooleanDriver::
614 GetCreationInformation(std::string& theOperationName,
615 std::vector<GEOM_Param>& theParams)
617 if (Label().IsNull()) return 0;
618 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
620 GEOMImpl_IBoolean aCI (function);
621 Standard_Integer aType = function->GetType();
625 theOperationName = "COMMON";
626 AddParam( theParams, "Object 1", aCI.GetShape1() );
627 AddParam( theParams, "Object 2", aCI.GetShape2() );
630 theOperationName = "CUT";
631 AddParam( theParams, "Main Object", aCI.GetShape1() );
632 AddParam( theParams, "Tool Object", aCI.GetShape2() );
635 theOperationName = "FUSE";
636 AddParam( theParams, "Object 1", aCI.GetShape1() );
637 AddParam( theParams, "Object 2", aCI.GetShape2() );
639 case BOOLEAN_SECTION:
640 theOperationName = "SECTION";
641 AddParam( theParams, "Object 1", aCI.GetShape1() );
642 AddParam( theParams, "Object 2", aCI.GetShape2() );
644 case BOOLEAN_COMMON_LIST:
645 theOperationName = "COMMON";
646 AddParam( theParams, "Selected objects", aCI.GetShapes() );
648 case BOOLEAN_FUSE_LIST:
649 theOperationName = "FUSE";
650 AddParam( theParams, "Selected objects", aCI.GetShapes() );
652 case BOOLEAN_CUT_LIST:
653 theOperationName = "CUT";
654 AddParam( theParams, "Main Object", aCI.GetShape1() );
655 AddParam( theParams, "Tool Objects", aCI.GetShapes() );
664 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_BooleanDriver,GEOM_BaseDriver);
666 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);