1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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>
29 #include <BlockFix_BlockFixAPI.hxx>
30 #include <ShHealOper_ShapeProcess.hxx>
32 #include <TNaming_CopyShape.hxx>
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>
42 #include <TopExp_Explorer.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>
49 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
51 #include <Precision.hxx>
53 #include <Standard_ConstructionError.hxx>
54 #include <StdFail_NotDone.hxx>
56 // Depth of self-intersection check (see BOPAlgo_CheckerSI::SetLevelOfCheck() for more details)
57 // Default value for BOPAlgo_CheckerSI gives very long computation when checking face-to-face intersections;
58 // here check level is decreased to more appropriate value to avoid problems with performance).
59 #define BOP_SELF_INTERSECTIONS_LEVEL 4
62 * This function performs extra edges removal.
64 * \param theShape the shape to be processed.
65 * \return the modified shape or null shape in case of failure.
67 static TopoDS_Shape RemoveExtraEdges(const TopoDS_Shape &theShape)
71 if (!theShape.IsNull()) {
72 BlockFix_BlockFixAPI aTool;
74 aTool.OptimumNbFaces() = 0;
75 aTool.SetShape(theShape);
77 TopoDS_Shape aShape = aTool.Shape();
79 if (GEOMUtils::CheckShape(aShape)) {
84 ShHealOper_ShapeProcess aHealer;
85 aHealer.Perform(aResult, aFixed);
86 if (aHealer.isDone() && GEOMUtils::CheckShape(aFixed))
94 //=======================================================================
97 //=======================================================================
98 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
100 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
101 return aBooleanDriver;
104 //=======================================================================
105 //function : GEOMImpl_BooleanDriver
107 //=======================================================================
108 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
112 //=======================================================================
115 //=======================================================================
116 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
118 if (Label().IsNull()) return 0;
119 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
121 GEOMImpl_IBoolean aCI (aFunction);
122 Standard_Integer aType = aFunction->GetType();
123 const Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
124 const Standard_Boolean isRmExtraEdges = aCI.GetRmExtraEdges();
132 case BOOLEAN_SECTION:
134 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
135 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
136 TopoDS_Shape aShape1 = aRefShape1->GetValue();
137 TopoDS_Shape aShape2 = aRefShape2->GetValue();
139 if (!aShape1.IsNull() && !aShape2.IsNull()) {
140 // check arguments for Mantis issue 0021019
141 if (!GEOMUtils::CheckShape(aShape1, true) || !GEOMUtils::CheckShape(aShape2, true))
142 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
144 if (isCheckSelfInte) {
145 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
146 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
147 BOPCol_ListOfShape aList1, aList2;
148 aList1.Append(aShape1);
149 aList2.Append(aShape2);
150 aCSI.SetArguments(aList1);
152 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
153 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
154 aCSI.SetArguments(aList2);
156 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
157 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
160 // Make a copy to prevent the original shape changes.
161 TopoDS_Shape aShapeCopy1;
162 TopoDS_Shape aShapeCopy2;
163 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
164 TNaming_CopyShape::CopyTool(aShape1, aMapTShapes, aShapeCopy1);
165 TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy2);
167 aShape = performOperation (aShapeCopy1, aShapeCopy2, aType);
169 if (isRmExtraEdges) {
170 aShape = RemoveExtraEdges(aShape);
173 if (aShape.IsNull()) {
179 case BOOLEAN_COMMON_LIST:
180 case BOOLEAN_FUSE_LIST:
182 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
183 const Standard_Integer nbShapes = aShapes->Length();
185 Handle(GEOM_Function) aRefShape;
186 TopoDS_Shape aShape2;
187 Standard_Integer aSimpleType =
188 (aType == BOOLEAN_FUSE_LIST ? BOOLEAN_FUSE : BOOLEAN_COMMON);
191 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
192 aShape = aRefShape->GetValue();
194 if (!aShape.IsNull()) {
195 // check arguments for Mantis issue 0021019
196 if (!GEOMUtils::CheckShape(aShape, true))
197 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
199 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
201 if (isCheckSelfInte) {
202 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
203 BOPCol_ListOfShape aList1;
204 aList1.Append(aShape);
205 aCSI.SetArguments(aList1);
207 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
208 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
213 TopoDS_Shape aShapeCopy;
214 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
216 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
219 for (i = 2; i <= nbShapes; i++) {
220 aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
221 aShape2 = aRefShape->GetValue();
223 if (!GEOMUtils::CheckShape(aShape2, true))
224 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
226 if (isCheckSelfInte) {
227 BOPCol_ListOfShape aList2;
228 aList2.Append(aShape2);
229 aCSI.SetArguments(aList2);
231 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
232 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
237 aShapeCopy.Nullify();
238 TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy);
239 aShape = performOperation (aShape, aShapeCopy, aSimpleType);
241 if (isRmExtraEdges) {
242 aShape = RemoveExtraEdges(aShape);
245 if (aShape.IsNull()) {
253 case BOOLEAN_CUT_LIST:
255 Handle(GEOM_Function) aRefObject = aCI.GetShape1();
257 aShape = aRefObject->GetValue();
259 if (!aShape.IsNull()) {
260 // check arguments for Mantis issue 0021019
261 if (!GEOMUtils::CheckShape(aShape, true))
262 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
264 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
266 if (isCheckSelfInte) {
267 aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
268 BOPCol_ListOfShape aList1;
269 aList1.Append(aShape);
270 aCSI.SetArguments(aList1);
272 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
273 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
278 TopoDS_Shape aShapeCopy;
279 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
281 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
284 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
285 const Standard_Integer nbShapes = aTools->Length();
287 Handle(GEOM_Function) aRefTool;
290 for (i = 1; i <= nbShapes; i++) {
291 aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
292 aTool = aRefTool->GetValue();
294 if (!GEOMUtils::CheckShape(aTool, true))
295 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
297 if (isCheckSelfInte) {
298 BOPCol_ListOfShape aList2;
299 aList2.Append(aTool);
300 aCSI.SetArguments(aList2);
302 if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
303 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
308 aShapeCopy.Nullify();
309 TNaming_CopyShape::CopyTool(aTool, aMapTShapes, aShapeCopy);
310 aShape = performOperation (aShape, aShapeCopy, BOOLEAN_CUT);
312 if (aShape.IsNull()) {
323 aFunction->SetValue(aShape);
325 log.SetTouched(Label());
330 //=======================================================================
331 //function : performOperation
333 //=======================================================================
334 TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
335 (const TopoDS_Shape theShape1,
336 const TopoDS_Shape theShape2,
337 const Standard_Integer theType)const
341 // perform COMMON operation
342 if (theType == BOOLEAN_COMMON) {
347 TopTools_ListOfShape listShape1, listShape2;
348 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
349 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
351 Standard_Boolean isCompound =
352 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
354 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
355 for (; itSub1.More(); itSub1.Next()) {
356 TopoDS_Shape aValue1 = itSub1.Value();
357 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
358 for (; itSub2.More(); itSub2.Next()) {
359 TopoDS_Shape aValue2 = itSub2.Value();
360 BRepAlgoAPI_Common BO (aValue1, aValue2);
362 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
365 TopoDS_Shape aStepResult = BO.Shape();
367 // check result of this step: if it is a compound (boolean operations
368 // allways return a compound), we add all sub-shapes of it.
369 // This allows to avoid adding empty compounds,
370 // resulting from COMMON on two non-intersecting shapes.
371 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
372 TopoDS_Iterator aCompIter (aStepResult);
373 for (; aCompIter.More(); aCompIter.Next()) {
374 // add shape in a result
375 B.Add(C, aCompIter.Value());
379 // add shape in a result
380 B.Add(C, aStepResult);
389 // As GlueFaces has been improved to keep all kind of shapes
390 TopExp_Explorer anExp (C, TopAbs_VERTEX);
392 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
398 // perform CUT operation
399 else if (theType == BOOLEAN_CUT) {
404 TopTools_ListOfShape listShapes, listTools;
405 GEOMUtils::AddSimpleShapes(theShape1, listShapes);
406 GEOMUtils::AddSimpleShapes(theShape2, listTools);
408 Standard_Boolean isCompound = (listShapes.Extent() > 1);
410 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
411 for (; itSub1.More(); itSub1.Next()) {
412 TopoDS_Shape aCut = itSub1.Value();
414 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
415 for (; itSub2.More(); itSub2.Next()) {
416 TopoDS_Shape aTool = itSub2.Value();
417 BRepAlgoAPI_Cut BO (aCut, aTool);
419 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
424 // check result of this step: if it is a compound (boolean operations
425 // allways return a compound), we add all sub-shapes of it.
426 // This allows to avoid adding empty compounds,
427 // resulting from CUT of parts
428 if (aCut.ShapeType() == TopAbs_COMPOUND) {
429 TopoDS_Iterator aCompIter (aCut);
430 for (; aCompIter.More(); aCompIter.Next()) {
431 // add shape in a result
432 B.Add(C, aCompIter.Value());
436 // add shape in a result
445 // As GlueFaces has been improved to keep all kind of shapes
446 TopExp_Explorer anExp (C, TopAbs_VERTEX);
448 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
454 // perform FUSE operation
455 else if (theType == BOOLEAN_FUSE) {
457 BRepAlgoAPI_Fuse BO (theShape1, theShape2);
459 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
464 // perform SECTION operation
465 else if (theType == BOOLEAN_SECTION) {
470 TopTools_ListOfShape listShape1, listShape2;
471 GEOMUtils::AddSimpleShapes(theShape1, listShape1);
472 GEOMUtils::AddSimpleShapes(theShape2, listShape2);
474 Standard_Boolean isCompound =
475 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
477 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
478 for (; itSub1.More(); itSub1.Next()) {
479 TopoDS_Shape aValue1 = itSub1.Value();
480 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
481 for (; itSub2.More(); itSub2.Next()) {
482 TopoDS_Shape aValue2 = itSub2.Value();
483 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
484 // Set approximation to have an attached 3D BSpline geometry to each edge,
485 // where analytic curve is not possible. Without this flag in some cases
486 // we obtain BSpline curve of degree 1 (C0), which is slowly
487 // processed by some algorithms (Partition for example).
488 BO.Approximation(Standard_True);
489 //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
490 BO.ComputePCurveOn1(Standard_True);
491 BO.ComputePCurveOn2(Standard_True);
492 //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
496 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
499 TopoDS_Shape aStepResult = BO.Shape();
501 // check result of this step: if it is a compound (boolean operations
502 // allways return a compound), we add all sub-shapes of it.
503 // This allows to avoid adding empty compounds,
504 // resulting from SECTION on two non-intersecting shapes.
505 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
506 TopoDS_Iterator aCompIter (aStepResult);
507 for (; aCompIter.More(); aCompIter.Next()) {
508 // add shape in a result
509 B.Add(C, aCompIter.Value());
513 // add shape in a result
514 B.Add(C, aStepResult);
523 // As GlueFaces has been improved to keep all kind of shapes
524 TopExp_Explorer anExp (C, TopAbs_VERTEX);
526 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
536 if (aShape.IsNull()) return aShape;
538 // as boolean operations always produce compound, lets simplify it
539 // for the case, if it contains only one sub-shape
540 TopTools_ListOfShape listShapeRes;
541 GEOMUtils::AddSimpleShapes(aShape, listShapeRes);
542 if (listShapeRes.Extent() == 1) {
543 aShape = listShapeRes.First();
544 if (aShape.IsNull()) return aShape;
547 // 08.07.2008 skl for bug 19761 from Mantis
548 if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
549 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
551 // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
552 // 1. Get shape parameters for comparison
553 int nbTypes [TopAbs_SHAPE];
555 for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
557 nbTypes[aShape.ShapeType()]++;
559 TopTools_MapOfShape aMapOfShape;
560 aMapOfShape.Add(aShape);
561 TopTools_ListOfShape aListOfShape;
562 aListOfShape.Append(aShape);
564 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
565 for (; itL.More(); itL.Next()) {
566 TopoDS_Iterator it (itL.Value());
567 for (; it.More(); it.Next()) {
568 TopoDS_Shape s = it.Value();
569 if (aMapOfShape.Add(s)) {
570 aListOfShape.Append(s);
571 nbTypes[s.ShapeType()]++;
577 // 2. Limit tolerance
578 TopoDS_Shape aShapeCopy;
579 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
580 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
582 if ( GEOMUtils::FixShapeTolerance(aShapeCopy, true) ) {
583 int iType, nbTypesCopy [TopAbs_SHAPE];
585 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
586 nbTypesCopy[iType] = 0;
587 nbTypesCopy[aShapeCopy.ShapeType()]++;
589 TopTools_MapOfShape aMapOfShape;
590 aMapOfShape.Add(aShapeCopy);
591 TopTools_ListOfShape aListOfShape;
592 aListOfShape.Append(aShapeCopy);
594 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
595 for (; itL.More(); itL.Next()) {
596 TopoDS_Iterator it (itL.Value());
597 for (; it.More(); it.Next()) {
598 TopoDS_Shape s = it.Value();
599 if (aMapOfShape.Add(s)) {
600 aListOfShape.Append(s);
601 nbTypesCopy[s.ShapeType()]++;
607 for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
608 if (nbTypes[iType] != nbTypesCopy[iType])
614 // END: Mantis issue 0021060
619 //================================================================================
621 * \brief Returns a name of creation operation and names and values of creation parameters
623 //================================================================================
625 bool GEOMImpl_BooleanDriver::
626 GetCreationInformation(std::string& theOperationName,
627 std::vector<GEOM_Param>& theParams)
629 if (Label().IsNull()) return 0;
630 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
632 GEOMImpl_IBoolean aCI (function);
633 Standard_Integer aType = function->GetType();
634 Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
638 theOperationName = "COMMON";
639 AddParam( theParams, "Object 1", aCI.GetShape1() );
640 AddParam( theParams, "Object 2", aCI.GetShape2() );
641 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
644 theOperationName = "CUT";
645 AddParam( theParams, "Main Object", aCI.GetShape1() );
646 AddParam( theParams, "Tool Object", aCI.GetShape2() );
647 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
650 theOperationName = "FUSE";
651 AddParam( theParams, "Object 1", aCI.GetShape1() );
652 AddParam( theParams, "Object 2", aCI.GetShape2() );
653 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
654 AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
656 case BOOLEAN_SECTION:
657 theOperationName = "SECTION";
658 AddParam( theParams, "Object 1", aCI.GetShape1() );
659 AddParam( theParams, "Object 2", aCI.GetShape2() );
660 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
662 case BOOLEAN_COMMON_LIST:
663 theOperationName = "COMMON";
664 AddParam( theParams, "Selected objects", aCI.GetShapes() );
665 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
667 case BOOLEAN_FUSE_LIST:
668 theOperationName = "FUSE";
669 AddParam( theParams, "Selected objects", aCI.GetShapes() );
670 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
671 AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
673 case BOOLEAN_CUT_LIST:
674 theOperationName = "CUT";
675 AddParam( theParams, "Main Object", aCI.GetShape1() );
676 AddParam( theParams, "Tool Objects", aCI.GetShapes() );
677 AddParam( theParams, "Check self-intersections", isCheckSelfInte );
686 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_BooleanDriver,GEOM_BaseDriver);
688 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);