1 // Copyright (C) 2007-2012 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>
29 #include <TNaming_CopyShape.hxx>
31 #include <ShapeFix_ShapeTolerance.hxx>
32 #include <ShapeFix_Shape.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRepAlgo.hxx>
36 #include <BRepAlgoAPI_Common.hxx>
37 #include <BRepAlgoAPI_Cut.hxx>
38 #include <BRepAlgoAPI_Fuse.hxx>
39 #include <BRepAlgoAPI_Section.hxx>
40 #include <BRepCheck_Analyzer.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS_Shape.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>
50 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
52 #include <Precision.hxx>
54 #include <Standard_ConstructionError.hxx>
55 #include <StdFail_NotDone.hxx>
57 //=======================================================================
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
63 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
64 return aBooleanDriver;
67 //=======================================================================
68 //function : GEOMImpl_BooleanDriver
70 //=======================================================================
71 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
75 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
77 if (theShape.ShapeType() != TopAbs_COMPOUND &&
78 theShape.ShapeType() != TopAbs_COMPSOLID) {
79 theList.Append(theShape);
83 TopTools_MapOfShape mapShape;
84 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
86 for (; It.More(); It.Next()) {
87 TopoDS_Shape aShape_i = It.Value();
88 if (mapShape.Add(aShape_i)) {
89 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
90 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
91 AddSimpleShapes(aShape_i, theList);
93 theList.Append(aShape_i);
99 //=======================================================================
102 //=======================================================================
103 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
105 if (Label().IsNull()) return 0;
106 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
108 GEOMImpl_IBoolean aCI (aFunction);
109 Standard_Integer aType = aFunction->GetType();
113 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
114 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
115 TopoDS_Shape aShape1 = aRefShape1->GetValue();
116 TopoDS_Shape aShape2 = aRefShape2->GetValue();
118 if (!aShape1.IsNull() && !aShape2.IsNull()) {
119 // check arguments for Mantis issue 0021019
120 BRepCheck_Analyzer ana (aShape1, Standard_True);
122 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
125 StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
127 // perform COMMON operation
128 if (aType == BOOLEAN_COMMON) {
133 TopTools_ListOfShape listShape1, listShape2;
134 AddSimpleShapes(aShape1, listShape1);
135 AddSimpleShapes(aShape2, listShape2);
137 Standard_Boolean isCompound =
138 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
140 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
141 for (; itSub1.More(); itSub1.Next()) {
142 TopoDS_Shape aValue1 = itSub1.Value();
143 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
144 for (; itSub2.More(); itSub2.Next()) {
145 TopoDS_Shape aValue2 = itSub2.Value();
146 BRepAlgoAPI_Common BO (aValue1, aValue2);
148 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
151 TopoDS_Shape aStepResult = BO.Shape();
153 // check result of this step: if it is a compound (boolean operations
154 // allways return a compound), we add all sub-shapes of it.
155 // This allows to avoid adding empty compounds,
156 // resulting from COMMON on two non-intersecting shapes.
157 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
158 TopoDS_Iterator aCompIter (aStepResult);
159 for (; aCompIter.More(); aCompIter.Next()) {
160 // add shape in a result
161 B.Add(C, aCompIter.Value());
165 // add shape in a result
166 B.Add(C, aStepResult);
176 TopTools_ListOfShape listShapeC;
177 AddSimpleShapes(C, listShapeC);
178 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
179 bool isOnlySolids = true;
180 for (; itSubC.More(); itSubC.Next()) {
181 TopoDS_Shape aValueC = itSubC.Value();
182 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
185 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
190 // As GlueFaces has been improved to keep all kind of shapes
191 TopExp_Explorer anExp (C, TopAbs_VERTEX);
193 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
199 // perform CUT operation
200 else if (aType == BOOLEAN_CUT) {
205 TopTools_ListOfShape listShapes, listTools;
206 AddSimpleShapes(aShape1, listShapes);
207 AddSimpleShapes(aShape2, listTools);
209 Standard_Boolean isCompound = (listShapes.Extent() > 1);
211 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
212 for (; itSub1.More(); itSub1.Next()) {
213 TopoDS_Shape aCut = itSub1.Value();
215 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
216 for (; itSub2.More(); itSub2.Next()) {
217 TopoDS_Shape aTool = itSub2.Value();
218 BRepAlgoAPI_Cut BO (aCut, aTool);
220 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
225 // check result of this step: if it is a compound (boolean operations
226 // allways return a compound), we add all sub-shapes of it.
227 // This allows to avoid adding empty compounds,
228 // resulting from CUT of parts
229 if (aCut.ShapeType() == TopAbs_COMPOUND) {
230 TopoDS_Iterator aCompIter (aCut);
231 for (; aCompIter.More(); aCompIter.Next()) {
232 // add shape in a result
233 B.Add(C, aCompIter.Value());
237 // add shape in a result
247 TopTools_ListOfShape listShapeC;
248 AddSimpleShapes(C, listShapeC);
249 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
250 bool isOnlySolids = true;
251 for (; itSubC.More(); itSubC.Next()) {
252 TopoDS_Shape aValueC = itSubC.Value();
253 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
256 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
261 // As GlueFaces has been improved to keep all kind of shapes
262 TopExp_Explorer anExp (C, TopAbs_VERTEX);
264 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
270 // perform FUSE operation
271 else if (aType == BOOLEAN_FUSE) {
272 /* Fix for NPAL15379: refused
274 TopTools_ListOfShape listShape1, listShape2;
275 AddSimpleShapes(aShape1, listShape1);
276 AddSimpleShapes(aShape2, listShape2);
278 Standard_Boolean isIntersect = Standard_False;
280 if (listShape1.Extent() > 1 && !isIntersect) {
281 // check intersections inside the first compound
282 TopTools_ListIteratorOfListOfShape it1 (listShape1);
283 for (; it1.More() && !isIntersect; it1.Next()) {
284 TopoDS_Shape aValue1 = it1.Value();
285 TopTools_ListIteratorOfListOfShape it2 (listShape1);
286 for (; it2.More() && !isIntersect; it2.Next()) {
287 TopoDS_Shape aValue2 = it2.Value();
288 if (aValue2 != aValue1) {
289 BRepAlgoAPI_Section BO (aValue1, aValue2);
291 TopoDS_Shape aSect = BO.Shape();
292 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
294 isIntersect = Standard_True;
302 if (listShape2.Extent() > 1 && !isIntersect) {
303 // check intersections inside the second compound
304 TopTools_ListIteratorOfListOfShape it1 (listShape2);
305 for (; it1.More() && !isIntersect; it1.Next()) {
306 TopoDS_Shape aValue1 = it1.Value();
307 TopTools_ListIteratorOfListOfShape it2 (listShape2);
308 for (; it2.More() && !isIntersect; it2.Next()) {
309 TopoDS_Shape aValue2 = it2.Value();
310 if (aValue2 != aValue1) {
311 BRepAlgoAPI_Section BO (aValue1, aValue2);
313 TopoDS_Shape aSect = BO.Shape();
314 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
316 isIntersect = Standard_True;
325 // have intersections inside compounds
326 // check intersections between compounds
327 TopTools_ListIteratorOfListOfShape it1 (listShape1);
328 for (; it1.More(); it1.Next()) {
329 TopoDS_Shape aValue1 = it1.Value();
330 TopTools_ListIteratorOfListOfShape it2 (listShape2);
331 for (; it2.More(); it2.Next()) {
332 TopoDS_Shape aValue2 = it2.Value();
333 if (aValue2 != aValue1) {
334 BRepAlgoAPI_Section BO (aValue1, aValue2);
336 TopoDS_Shape aSect = BO.Shape();
337 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
339 StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
349 BRepAlgoAPI_Fuse BO (aShape1, aShape2);
351 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
356 // perform SECTION operation
357 else if (aType == BOOLEAN_SECTION) {
362 TopTools_ListOfShape listShape1, listShape2;
363 AddSimpleShapes(aShape1, listShape1);
364 AddSimpleShapes(aShape2, listShape2);
366 Standard_Boolean isCompound =
367 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
369 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
370 for (; itSub1.More(); itSub1.Next()) {
371 TopoDS_Shape aValue1 = itSub1.Value();
372 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
373 for (; itSub2.More(); itSub2.Next()) {
374 TopoDS_Shape aValue2 = itSub2.Value();
375 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
376 // Set approximation to have an attached 3D BSpline geometry to each edge,
377 // where analytic curve is not possible. Without this flag in some cases
378 // we obtain BSpline curve of degree 1 (C0), which is slowly
379 // processed by some algorithms (Partition for example).
380 BO.Approximation(Standard_True);
381 //modified by NIZNHY-PKV Tue Oct 18 14:34:16 2011f
382 BO.ComputePCurveOn1(Standard_True);
383 BO.ComputePCurveOn2(Standard_True);
384 //modified by NIZNHY-PKV Tue Oct 18 14:34:18 2011t
388 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
391 TopoDS_Shape aStepResult = BO.Shape();
393 // check result of this step: if it is a compound (boolean operations
394 // allways return a compound), we add all sub-shapes of it.
395 // This allows to avoid adding empty compounds,
396 // resulting from SECTION on two non-intersecting shapes.
397 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
398 TopoDS_Iterator aCompIter (aStepResult);
399 for (; aCompIter.More(); aCompIter.Next()) {
400 // add shape in a result
401 B.Add(C, aCompIter.Value());
405 // add shape in a result
406 B.Add(C, aStepResult);
417 // As GlueFaces has been improved to keep all kind of shapes
418 TopExp_Explorer anExp (C, TopAbs_VERTEX);
420 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
431 if (aShape.IsNull()) return 0;
433 // as boolean operations always produce compound, lets simplify it
434 // for the case, if it contains only one sub-shape
435 TopTools_ListOfShape listShapeRes;
436 AddSimpleShapes(aShape, listShapeRes);
437 if (listShapeRes.Extent() == 1) {
438 aShape = listShapeRes.First();
439 if (aShape.IsNull()) return 0;
442 // 08.07.2008 skl for bug 19761 from Mantis
443 BRepCheck_Analyzer ana (aShape, Standard_True);
445 if (!ana.IsValid()) {
446 ShapeFix_ShapeTolerance aSFT;
447 aSFT.LimitTolerance(aShape, Precision::Confusion(),
448 Precision::Confusion(), TopAbs_SHAPE);
449 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
451 aShape = aSfs->Shape();
454 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
456 //if (!BRepAlgo::IsValid(aShape)) {
457 // Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
460 // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
461 // 1. Get shape parameters for comparison
462 int nbTypes [TopAbs_SHAPE];
464 for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
466 nbTypes[aShape.ShapeType()]++;
468 TopTools_MapOfShape aMapOfShape;
469 aMapOfShape.Add(aShape);
470 TopTools_ListOfShape aListOfShape;
471 aListOfShape.Append(aShape);
473 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
474 for (; itL.More(); itL.Next()) {
475 TopoDS_Iterator it (itL.Value());
476 for (; it.More(); it.Next()) {
477 TopoDS_Shape s = it.Value();
478 if (aMapOfShape.Add(s)) {
479 aListOfShape.Append(s);
480 nbTypes[s.ShapeType()]++;
486 // 2. Limit tolerance
487 TopoDS_Shape aShapeCopy;
488 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
489 TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
490 ShapeFix_ShapeTolerance aSFT;
491 aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
492 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
494 aShapeCopy = aSfs->Shape();
496 // 3. Check parameters
497 ana.Init(aShapeCopy);
499 int iType, nbTypesCopy [TopAbs_SHAPE];
501 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
502 nbTypesCopy[iType] = 0;
503 nbTypesCopy[aShapeCopy.ShapeType()]++;
505 TopTools_MapOfShape aMapOfShape;
506 aMapOfShape.Add(aShapeCopy);
507 TopTools_ListOfShape aListOfShape;
508 aListOfShape.Append(aShapeCopy);
510 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
511 for (; itL.More(); itL.Next()) {
512 TopoDS_Iterator it (itL.Value());
513 for (; it.More(); it.Next()) {
514 TopoDS_Shape s = it.Value();
515 if (aMapOfShape.Add(s)) {
516 aListOfShape.Append(s);
517 nbTypesCopy[s.ShapeType()]++;
523 for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
524 if (nbTypes[iType] != nbTypesCopy[iType])
530 // END: Mantis issue 0021060
532 //Alternative case to check shape result Mantis 0020604: EDF 1172
533 /* TopoDS_Iterator It (aShape, Standard_True, Standard_True);
535 for (; It.More(); It.Next())
538 Standard_ConstructionError::Raise("Boolean operation aborted : result object is empty compound");*/
539 //end of 0020604: EDF 1172
540 //! the changes temporary commented because of customer needs (see the same mantis bug)
542 aFunction->SetValue(aShape);
544 log.SetTouched(Label());
550 //=======================================================================
551 //function : GEOMImpl_BooleanDriver_Type_
553 //=======================================================================
554 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
556 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
557 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
558 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
559 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
560 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
561 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
563 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
564 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
565 sizeof(GEOMImpl_BooleanDriver),
567 (Standard_Address)_Ancestors,
568 (Standard_Address)NULL);
573 //=======================================================================
574 //function : DownCast
576 //=======================================================================
577 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
579 Handle(GEOMImpl_BooleanDriver) _anOtherObject;
581 if (!AnObject.IsNull()) {
582 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
583 _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
587 return _anOtherObject;