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
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 <ShapeFix_ShapeTolerance.hxx>
30 #include <ShapeFix_Shape.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRepAlgo.hxx>
34 #include <BRepAlgoAPI_Common.hxx>
35 #include <BRepAlgoAPI_Cut.hxx>
36 #include <BRepAlgoAPI_Fuse.hxx>
37 #include <BRepAlgoAPI_Section.hxx>
38 #include <BRepCheck_Analyzer.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TopoDS_Iterator.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <TopTools_ListOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <Precision.hxx>
50 #include <Standard_ConstructionError.hxx>
51 #include <StdFail_NotDone.hxx>
53 //=======================================================================
56 //=======================================================================
57 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
59 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
60 return aBooleanDriver;
63 //=======================================================================
64 //function : GEOMImpl_BooleanDriver
66 //=======================================================================
67 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
71 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
73 if (theShape.ShapeType() != TopAbs_COMPOUND &&
74 theShape.ShapeType() != TopAbs_COMPSOLID) {
75 theList.Append(theShape);
79 TopTools_MapOfShape mapShape;
80 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
82 for (; It.More(); It.Next()) {
83 TopoDS_Shape aShape_i = It.Value();
84 if (mapShape.Add(aShape_i)) {
85 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
86 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
87 AddSimpleShapes(aShape_i, theList);
89 theList.Append(aShape_i);
95 //=======================================================================
98 //=======================================================================
99 Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
101 if (Label().IsNull()) return 0;
102 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
104 GEOMImpl_IBoolean aCI (aFunction);
105 Standard_Integer aType = aFunction->GetType();
109 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
110 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
111 TopoDS_Shape aShape1 = aRefShape1->GetValue();
112 TopoDS_Shape aShape2 = aRefShape2->GetValue();
113 if (!aShape1.IsNull() && !aShape2.IsNull()) {
115 // check arguments for Mantis issue 0021019
116 BRepCheck_Analyzer ana (aShape1, Standard_True);
118 StdFail_NotDone::Raise("Common operation will not be performed, because argument shape is not valid");
121 StdFail_NotDone::Raise("Common operation will not be performed, because argument shape is not valid");
123 // perform COMMON operation
124 if (aType == BOOLEAN_COMMON) {
129 TopTools_ListOfShape listShape1, listShape2;
130 AddSimpleShapes(aShape1, listShape1);
131 AddSimpleShapes(aShape2, listShape2);
133 Standard_Boolean isCompound =
134 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
136 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
137 for (; itSub1.More(); itSub1.Next()) {
138 TopoDS_Shape aValue1 = itSub1.Value();
139 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
140 for (; itSub2.More(); itSub2.Next()) {
141 TopoDS_Shape aValue2 = itSub2.Value();
142 BRepAlgoAPI_Common BO (aValue1, aValue2);
144 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
147 TopoDS_Shape aStepResult = BO.Shape();
149 // check result of this step: if it is a compound (boolean operations
150 // allways return a compound), we add all sub-shapes of it.
151 // This allows to avoid adding empty compounds,
152 // resulting from COMMON on two non-intersecting shapes.
153 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
154 TopoDS_Iterator aCompIter (aStepResult);
155 for (; aCompIter.More(); aCompIter.Next()) {
156 // add shape in a result
157 B.Add(C, aCompIter.Value());
161 // add shape in a result
162 B.Add(C, aStepResult);
172 TopTools_ListOfShape listShapeC;
173 AddSimpleShapes(C, listShapeC);
174 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
175 bool isOnlySolids = true;
176 for (; itSubC.More(); itSubC.Next()) {
177 TopoDS_Shape aValueC = itSubC.Value();
178 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
181 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
186 // As GlueFaces has been improved to keep all kind of shapes
187 TopExp_Explorer anExp (C, TopAbs_VERTEX);
189 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
195 // perform CUT operation
196 else if (aType == BOOLEAN_CUT) {
201 TopTools_ListOfShape listShapes, listTools;
202 AddSimpleShapes(aShape1, listShapes);
203 AddSimpleShapes(aShape2, listTools);
205 Standard_Boolean isCompound = (listShapes.Extent() > 1);
207 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
208 for (; itSub1.More(); itSub1.Next()) {
209 TopoDS_Shape aCut = itSub1.Value();
211 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
212 for (; itSub2.More(); itSub2.Next()) {
213 TopoDS_Shape aTool = itSub2.Value();
214 BRepAlgoAPI_Cut BO (aCut, aTool);
216 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
221 // check result of this step: if it is a compound (boolean operations
222 // allways return a compound), we add all sub-shapes of it.
223 // This allows to avoid adding empty compounds,
224 // resulting from CUT of parts
225 if (aCut.ShapeType() == TopAbs_COMPOUND) {
226 TopoDS_Iterator aCompIter (aCut);
227 for (; aCompIter.More(); aCompIter.Next()) {
228 // add shape in a result
229 B.Add(C, aCompIter.Value());
233 // add shape in a result
243 TopTools_ListOfShape listShapeC;
244 AddSimpleShapes(C, listShapeC);
245 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
246 bool isOnlySolids = true;
247 for (; itSubC.More(); itSubC.Next()) {
248 TopoDS_Shape aValueC = itSubC.Value();
249 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
252 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
257 // As GlueFaces has been improved to keep all kind of shapes
258 TopExp_Explorer anExp (C, TopAbs_VERTEX);
260 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
266 // perform FUSE operation
267 else if (aType == BOOLEAN_FUSE) {
268 /* Fix for NPAL15379: refused
270 TopTools_ListOfShape listShape1, listShape2;
271 AddSimpleShapes(aShape1, listShape1);
272 AddSimpleShapes(aShape2, listShape2);
274 Standard_Boolean isIntersect = Standard_False;
276 if (listShape1.Extent() > 1 && !isIntersect) {
277 // check intersections inside the first compound
278 TopTools_ListIteratorOfListOfShape it1 (listShape1);
279 for (; it1.More() && !isIntersect; it1.Next()) {
280 TopoDS_Shape aValue1 = it1.Value();
281 TopTools_ListIteratorOfListOfShape it2 (listShape1);
282 for (; it2.More() && !isIntersect; it2.Next()) {
283 TopoDS_Shape aValue2 = it2.Value();
284 if (aValue2 != aValue1) {
285 BRepAlgoAPI_Section BO (aValue1, aValue2);
287 TopoDS_Shape aSect = BO.Shape();
288 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
290 isIntersect = Standard_True;
298 if (listShape2.Extent() > 1 && !isIntersect) {
299 // check intersections inside the second compound
300 TopTools_ListIteratorOfListOfShape it1 (listShape2);
301 for (; it1.More() && !isIntersect; it1.Next()) {
302 TopoDS_Shape aValue1 = it1.Value();
303 TopTools_ListIteratorOfListOfShape it2 (listShape2);
304 for (; it2.More() && !isIntersect; it2.Next()) {
305 TopoDS_Shape aValue2 = it2.Value();
306 if (aValue2 != aValue1) {
307 BRepAlgoAPI_Section BO (aValue1, aValue2);
309 TopoDS_Shape aSect = BO.Shape();
310 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
312 isIntersect = Standard_True;
321 // have intersections inside compounds
322 // check intersections between compounds
323 TopTools_ListIteratorOfListOfShape it1 (listShape1);
324 for (; it1.More(); it1.Next()) {
325 TopoDS_Shape aValue1 = it1.Value();
326 TopTools_ListIteratorOfListOfShape it2 (listShape2);
327 for (; it2.More(); it2.Next()) {
328 TopoDS_Shape aValue2 = it2.Value();
329 if (aValue2 != aValue1) {
330 BRepAlgoAPI_Section BO (aValue1, aValue2);
332 TopoDS_Shape aSect = BO.Shape();
333 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
335 StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
345 BRepAlgoAPI_Fuse BO (aShape1, aShape2);
347 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
352 // perform SECTION operation
353 else if (aType == BOOLEAN_SECTION) {
358 TopTools_ListOfShape listShape1, listShape2;
359 AddSimpleShapes(aShape1, listShape1);
360 AddSimpleShapes(aShape2, listShape2);
362 Standard_Boolean isCompound =
363 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
365 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
366 for (; itSub1.More(); itSub1.Next()) {
367 TopoDS_Shape aValue1 = itSub1.Value();
368 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
369 for (; itSub2.More(); itSub2.Next()) {
370 TopoDS_Shape aValue2 = itSub2.Value();
371 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
372 // Set approximation to have an attached 3D BSpline geometry to each edge,
373 // where analytic curve is not possible. Without this flag in some cases
374 // we obtain BSpline curve of degree 1 (C0), which is slowly
375 // processed by some algorithms (Partition for example).
376 BO.Approximation(Standard_True);
379 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
382 TopoDS_Shape aStepResult = BO.Shape();
384 // check result of this step: if it is a compound (boolean operations
385 // allways return a compound), we add all sub-shapes of it.
386 // This allows to avoid adding empty compounds,
387 // resulting from SECTION on two non-intersecting shapes.
388 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
389 TopoDS_Iterator aCompIter (aStepResult);
390 for (; aCompIter.More(); aCompIter.Next()) {
391 // add shape in a result
392 B.Add(C, aCompIter.Value());
396 // add shape in a result
397 B.Add(C, aStepResult);
408 // As GlueFaces has been improved to keep all kind of shapes
409 TopExp_Explorer anExp (C, TopAbs_VERTEX);
411 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
422 if (aShape.IsNull()) return 0;
424 // as boolean operations always produce compound, lets simplify it
425 // for the case, if it contans only one sub-shape
426 TopTools_ListOfShape listShapeRes;
427 AddSimpleShapes(aShape, listShapeRes);
428 if (listShapeRes.Extent() == 1) {
429 aShape = listShapeRes.First();
430 if (aShape.IsNull()) return 0;
433 // 08.07.2008 skl for bug 19761 from Mantis
434 BRepCheck_Analyzer ana (aShape, Standard_True);
436 if (!ana.IsValid()) {
437 ShapeFix_ShapeTolerance aSFT;
438 aSFT.LimitTolerance(aShape, Precision::Confusion(),
439 Precision::Confusion(), TopAbs_SHAPE);
440 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
442 aShape = aSfs->Shape();
445 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
447 //if (!BRepAlgo::IsValid(aShape)) {
448 // Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
451 //Alternative case to check shape result Mantis 0020604: EDF 1172
452 /* TopoDS_Iterator It (aShape, Standard_True, Standard_True);
454 for (; It.More(); It.Next())
457 Standard_ConstructionError::Raise("Boolean operation aborted : result object is empty compound");*/
458 //end of 0020604: EDF 1172
459 //! the changes temporary commented because of customer needs (see the same mantis bug)
461 aFunction->SetValue(aShape);
463 log.SetTouched(Label());
469 //=======================================================================
470 //function : GEOMImpl_BooleanDriver_Type_
472 //=======================================================================
473 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
476 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
477 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
478 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
479 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
480 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
481 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
484 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
485 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
486 sizeof(GEOMImpl_BooleanDriver),
488 (Standard_Address)_Ancestors,
489 (Standard_Address)NULL);
494 //=======================================================================
495 //function : DownCast
497 //=======================================================================
498 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
500 Handle(GEOMImpl_BooleanDriver) _anOtherObject;
502 if (!AnObject.IsNull()) {
503 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
504 _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
508 return _anOtherObject ;