1 // Copyright (C) 2007-2011 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
22 #include <GEOMImpl_BooleanDriver.hxx>
23 #include <GEOMImpl_IBoolean.hxx>
24 #include <GEOMImpl_Types.hxx>
25 #include <GEOMImpl_GlueDriver.hxx>
26 #include <GEOM_Function.hxx>
28 #include <TNaming_CopyShape.hxx>
30 #include <ShapeFix_ShapeTolerance.hxx>
31 #include <ShapeFix_Shape.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAlgo.hxx>
35 #include <BRepAlgoAPI_Common.hxx>
36 #include <BRepAlgoAPI_Cut.hxx>
37 #include <BRepAlgoAPI_Fuse.hxx>
38 #include <BRepAlgoAPI_Section.hxx>
39 #include <BRepCheck_Analyzer.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopoDS_Shape.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 //=======================================================================
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
62 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
63 return aBooleanDriver;
66 //=======================================================================
67 //function : GEOMImpl_BooleanDriver
69 //=======================================================================
70 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
74 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
76 if (theShape.ShapeType() != TopAbs_COMPOUND &&
77 theShape.ShapeType() != TopAbs_COMPSOLID) {
78 theList.Append(theShape);
82 TopTools_MapOfShape mapShape;
83 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
85 for (; It.More(); It.Next()) {
86 TopoDS_Shape aShape_i = It.Value();
87 if (mapShape.Add(aShape_i)) {
88 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
89 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
90 AddSimpleShapes(aShape_i, theList);
92 theList.Append(aShape_i);
98 //=======================================================================
101 //=======================================================================
102 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
104 if (Label().IsNull()) return 0;
105 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
107 GEOMImpl_IBoolean aCI (aFunction);
108 Standard_Integer aType = aFunction->GetType();
112 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
113 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
114 TopoDS_Shape aShape1 = aRefShape1->GetValue();
115 TopoDS_Shape aShape2 = aRefShape2->GetValue();
117 if (!aShape1.IsNull() && !aShape2.IsNull()) {
118 // check arguments for Mantis issue 0021019
119 BRepCheck_Analyzer ana (aShape1, Standard_True);
121 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
124 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
126 // perform COMMON operation
127 if (aType == BOOLEAN_COMMON) {
132 TopTools_ListOfShape listShape1, listShape2;
133 AddSimpleShapes(aShape1, listShape1);
134 AddSimpleShapes(aShape2, listShape2);
136 Standard_Boolean isCompound =
137 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
139 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
140 for (; itSub1.More(); itSub1.Next()) {
141 TopoDS_Shape aValue1 = itSub1.Value();
142 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
143 for (; itSub2.More(); itSub2.Next()) {
144 TopoDS_Shape aValue2 = itSub2.Value();
145 BRepAlgoAPI_Common BO (aValue1, aValue2);
147 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
150 TopoDS_Shape aStepResult = BO.Shape();
152 // check result of this step: if it is a compound (boolean operations
153 // allways return a compound), we add all sub-shapes of it.
154 // This allows to avoid adding empty compounds,
155 // resulting from COMMON on two non-intersecting shapes.
156 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
157 TopoDS_Iterator aCompIter (aStepResult);
158 for (; aCompIter.More(); aCompIter.Next()) {
159 // add shape in a result
160 B.Add(C, aCompIter.Value());
164 // add shape in a result
165 B.Add(C, aStepResult);
175 TopTools_ListOfShape listShapeC;
176 AddSimpleShapes(C, listShapeC);
177 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
178 bool isOnlySolids = true;
179 for (; itSubC.More(); itSubC.Next()) {
180 TopoDS_Shape aValueC = itSubC.Value();
181 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
184 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
189 // As GlueFaces has been improved to keep all kind of shapes
190 TopExp_Explorer anExp (C, TopAbs_VERTEX);
192 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
198 // perform CUT operation
199 else if (aType == BOOLEAN_CUT) {
204 TopTools_ListOfShape listShapes, listTools;
205 AddSimpleShapes(aShape1, listShapes);
206 AddSimpleShapes(aShape2, listTools);
208 Standard_Boolean isCompound = (listShapes.Extent() > 1);
210 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
211 for (; itSub1.More(); itSub1.Next()) {
212 TopoDS_Shape aCut = itSub1.Value();
214 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
215 for (; itSub2.More(); itSub2.Next()) {
216 TopoDS_Shape aTool = itSub2.Value();
217 BRepAlgoAPI_Cut BO (aCut, aTool);
219 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
224 // check result of this step: if it is a compound (boolean operations
225 // allways return a compound), we add all sub-shapes of it.
226 // This allows to avoid adding empty compounds,
227 // resulting from CUT of parts
228 if (aCut.ShapeType() == TopAbs_COMPOUND) {
229 TopoDS_Iterator aCompIter (aCut);
230 for (; aCompIter.More(); aCompIter.Next()) {
231 // add shape in a result
232 B.Add(C, aCompIter.Value());
236 // add shape in a result
246 TopTools_ListOfShape listShapeC;
247 AddSimpleShapes(C, listShapeC);
248 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
249 bool isOnlySolids = true;
250 for (; itSubC.More(); itSubC.Next()) {
251 TopoDS_Shape aValueC = itSubC.Value();
252 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
255 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
260 // As GlueFaces has been improved to keep all kind of shapes
261 TopExp_Explorer anExp (C, TopAbs_VERTEX);
263 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
269 // perform FUSE operation
270 else if (aType == BOOLEAN_FUSE) {
271 /* Fix for NPAL15379: refused
273 TopTools_ListOfShape listShape1, listShape2;
274 AddSimpleShapes(aShape1, listShape1);
275 AddSimpleShapes(aShape2, listShape2);
277 Standard_Boolean isIntersect = Standard_False;
279 if (listShape1.Extent() > 1 && !isIntersect) {
280 // check intersections inside the first compound
281 TopTools_ListIteratorOfListOfShape it1 (listShape1);
282 for (; it1.More() && !isIntersect; it1.Next()) {
283 TopoDS_Shape aValue1 = it1.Value();
284 TopTools_ListIteratorOfListOfShape it2 (listShape1);
285 for (; it2.More() && !isIntersect; it2.Next()) {
286 TopoDS_Shape aValue2 = it2.Value();
287 if (aValue2 != aValue1) {
288 BRepAlgoAPI_Section BO (aValue1, aValue2);
290 TopoDS_Shape aSect = BO.Shape();
291 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
293 isIntersect = Standard_True;
301 if (listShape2.Extent() > 1 && !isIntersect) {
302 // check intersections inside the second compound
303 TopTools_ListIteratorOfListOfShape it1 (listShape2);
304 for (; it1.More() && !isIntersect; it1.Next()) {
305 TopoDS_Shape aValue1 = it1.Value();
306 TopTools_ListIteratorOfListOfShape it2 (listShape2);
307 for (; it2.More() && !isIntersect; it2.Next()) {
308 TopoDS_Shape aValue2 = it2.Value();
309 if (aValue2 != aValue1) {
310 BRepAlgoAPI_Section BO (aValue1, aValue2);
312 TopoDS_Shape aSect = BO.Shape();
313 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
315 isIntersect = Standard_True;
324 // have intersections inside compounds
325 // check intersections between compounds
326 TopTools_ListIteratorOfListOfShape it1 (listShape1);
327 for (; it1.More(); it1.Next()) {
328 TopoDS_Shape aValue1 = it1.Value();
329 TopTools_ListIteratorOfListOfShape it2 (listShape2);
330 for (; it2.More(); it2.Next()) {
331 TopoDS_Shape aValue2 = it2.Value();
332 if (aValue2 != aValue1) {
333 BRepAlgoAPI_Section BO (aValue1, aValue2);
335 TopoDS_Shape aSect = BO.Shape();
336 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
338 StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
348 BRepAlgoAPI_Fuse BO (aShape1, aShape2);
350 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
355 // perform SECTION operation
356 else if (aType == BOOLEAN_SECTION) {
361 TopTools_ListOfShape listShape1, listShape2;
362 AddSimpleShapes(aShape1, listShape1);
363 AddSimpleShapes(aShape2, listShape2);
365 Standard_Boolean isCompound =
366 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
368 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
369 for (; itSub1.More(); itSub1.Next()) {
370 TopoDS_Shape aValue1 = itSub1.Value();
371 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
372 for (; itSub2.More(); itSub2.Next()) {
373 TopoDS_Shape aValue2 = itSub2.Value();
374 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
375 // Set approximation to have an attached 3D BSpline geometry to each edge,
376 // where analytic curve is not possible. Without this flag in some cases
377 // we obtain BSpline curve of degree 1 (C0), which is slowly
378 // processed by some algorithms (Partition for example).
379 BO.Approximation(Standard_True);
380 //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
381 BO.ComputePCurveOn1(Standard_True);
382 BO.ComputePCurveOn2(Standard_True);
383 //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
387 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
390 TopoDS_Shape aStepResult = BO.Shape();
392 // check result of this step: if it is a compound (boolean operations
393 // allways return a compound), we add all sub-shapes of it.
394 // This allows to avoid adding empty compounds,
395 // resulting from SECTION on two non-intersecting shapes.
396 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
397 TopoDS_Iterator aCompIter (aStepResult);
398 for (; aCompIter.More(); aCompIter.Next()) {
399 // add shape in a result
400 B.Add(C, aCompIter.Value());
404 // add shape in a result
405 B.Add(C, aStepResult);
416 // As GlueFaces has been improved to keep all kind of shapes
417 TopExp_Explorer anExp (C, TopAbs_VERTEX);
419 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
430 if (aShape.IsNull()) return 0;
432 // as boolean operations always produce compound, lets simplify it
433 // for the case, if it contains only one sub-shape
434 TopTools_ListOfShape listShapeRes;
435 AddSimpleShapes(aShape, listShapeRes);
436 if (listShapeRes.Extent() == 1) {
437 aShape = listShapeRes.First();
438 if (aShape.IsNull()) return 0;
441 // 08.07.2008 skl for bug 19761 from Mantis
442 BRepCheck_Analyzer ana (aShape, Standard_True);
444 if (!ana.IsValid()) {
445 ShapeFix_ShapeTolerance aSFT;
446 aSFT.LimitTolerance(aShape, Precision::Confusion(),
447 Precision::Confusion(), TopAbs_SHAPE);
448 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
450 aShape = aSfs->Shape();
453 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
455 //if (!BRepAlgo::IsValid(aShape)) {
456 // Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
459 // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
460 // 1. Get shape parameters for comparison
461 int nbTypes [TopAbs_SHAPE];
463 for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
465 nbTypes[aShape.ShapeType()]++;
467 TopTools_MapOfShape aMapOfShape;
468 aMapOfShape.Add(aShape);
469 TopTools_ListOfShape aListOfShape;
470 aListOfShape.Append(aShape);
472 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
473 for (; itL.More(); itL.Next()) {
474 TopoDS_Iterator it (itL.Value());
475 for (; it.More(); it.Next()) {
476 TopoDS_Shape s = it.Value();
477 if (aMapOfShape.Add(s)) {
478 aListOfShape.Append(s);
479 nbTypes[s.ShapeType()]++;
485 // 2. Limit tolerance
486 TopoDS_Shape aShapeCopy;
487 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
488 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
489 ShapeFix_ShapeTolerance aSFT;
490 aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
491 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
493 aShapeCopy = aSfs->Shape();
495 // 3. Check parameters
496 ana.Init(aShapeCopy);
498 int iType, nbTypesCopy [TopAbs_SHAPE];
500 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
501 nbTypesCopy[iType] = 0;
502 nbTypesCopy[aShapeCopy.ShapeType()]++;
504 TopTools_MapOfShape aMapOfShape;
505 aMapOfShape.Add(aShapeCopy);
506 TopTools_ListOfShape aListOfShape;
507 aListOfShape.Append(aShapeCopy);
509 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
510 for (; itL.More(); itL.Next()) {
511 TopoDS_Iterator it (itL.Value());
512 for (; it.More(); it.Next()) {
513 TopoDS_Shape s = it.Value();
514 if (aMapOfShape.Add(s)) {
515 aListOfShape.Append(s);
516 nbTypesCopy[s.ShapeType()]++;
522 for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
523 if (nbTypes[iType] != nbTypesCopy[iType])
529 // END: Mantis issue 0021060
531 //Alternative case to check shape result Mantis 0020604: EDF 1172
532 /* TopoDS_Iterator It (aShape, Standard_True, Standard_True);
534 for (; It.More(); It.Next())
537 Standard_ConstructionError::Raise("Boolean operation aborted : result object is empty compound");*/
538 //end of 0020604: EDF 1172
539 //! the changes temporary commented because of customer needs (see the same mantis bug)
541 aFunction->SetValue(aShape);
543 log.SetTouched(Label());
549 //=======================================================================
550 //function : GEOMImpl_BooleanDriver_Type_
552 //=======================================================================
553 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
555 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
556 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
557 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
558 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
559 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
560 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
562 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
563 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
564 sizeof(GEOMImpl_BooleanDriver),
566 (Standard_Address)_Ancestors,
567 (Standard_Address)NULL);
572 //=======================================================================
573 //function : DownCast
575 //=======================================================================
576 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
578 Handle(GEOMImpl_BooleanDriver) _anOtherObject;
580 if (!AnObject.IsNull()) {
581 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
582 _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
586 return _anOtherObject;