1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <GEOMImpl_BooleanDriver.hxx>
22 #include <GEOMImpl_IBoolean.hxx>
23 #include <GEOMImpl_Types.hxx>
24 #include <GEOMImpl_GlueDriver.hxx>
25 #include <GEOM_Function.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepAlgo.hxx>
29 #include <BRepAlgoAPI_Common.hxx>
30 #include <BRepAlgoAPI_Cut.hxx>
31 #include <BRepAlgoAPI_Fuse.hxx>
32 #include <BRepAlgoAPI_Section.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopTools_MapOfShape.hxx>
38 #include <TopTools_ListOfShape.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <Precision.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <ShapeFix_ShapeTolerance.hxx>
43 #include <ShapeFix_Shape.hxx>
45 #include <Standard_ConstructionError.hxx>
46 #include <StdFail_NotDone.hxx>
48 //=======================================================================
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
54 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
55 return aBooleanDriver;
59 //=======================================================================
60 //function : GEOMImpl_BooleanDriver
62 //=======================================================================
63 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
67 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
69 if (theShape.ShapeType() != TopAbs_COMPOUND &&
70 theShape.ShapeType() != TopAbs_COMPSOLID) {
71 theList.Append(theShape);
75 TopTools_MapOfShape mapShape;
76 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
78 for (; It.More(); It.Next()) {
79 TopoDS_Shape aShape_i = It.Value();
80 if (mapShape.Add(aShape_i)) {
81 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
82 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
83 AddSimpleShapes(aShape_i, theList);
85 theList.Append(aShape_i);
91 //=======================================================================
94 //=======================================================================
95 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
97 if (Label().IsNull()) return 0;
98 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
100 GEOMImpl_IBoolean aCI (aFunction);
101 Standard_Integer aType = aFunction->GetType();
105 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
106 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
107 TopoDS_Shape aShape1 = aRefShape1->GetValue();
108 TopoDS_Shape aShape2 = aRefShape2->GetValue();
109 if (!aShape1.IsNull() && !aShape2.IsNull()) {
111 // perform COMMON operation
112 if (aType == BOOLEAN_COMMON) {
117 TopTools_ListOfShape listShape1, listShape2;
118 AddSimpleShapes(aShape1, listShape1);
119 AddSimpleShapes(aShape2, listShape2);
121 Standard_Boolean isCompound =
122 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
124 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
125 for (; itSub1.More(); itSub1.Next()) {
126 TopoDS_Shape aValue1 = itSub1.Value();
127 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
128 for (; itSub2.More(); itSub2.Next()) {
129 TopoDS_Shape aValue2 = itSub2.Value();
130 BRepAlgoAPI_Common BO (aValue1, aValue2);
132 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
135 TopoDS_Shape aStepResult = BO.Shape();
137 // check result of this step: if it is a compound (boolean operations
138 // allways return a compound), we add all sub-shapes of it.
139 // This allows to avoid adding empty compounds,
140 // resulting from COMMON on two non-intersecting shapes.
141 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
142 TopoDS_Iterator aCompIter (aStepResult);
143 for (; aCompIter.More(); aCompIter.Next()) {
144 // add shape in a result
145 B.Add(C, aCompIter.Value());
149 // add shape in a result
150 B.Add(C, aStepResult);
160 TopTools_ListOfShape listShapeC;
161 AddSimpleShapes(C, listShapeC);
162 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
163 bool isOnlySolids = true;
164 for (; itSubC.More(); itSubC.Next()) {
165 TopoDS_Shape aValueC = itSubC.Value();
166 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
169 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
174 // As GlueFaces has been improved to keep all kind of shapes
175 TopExp_Explorer anExp (C, TopAbs_VERTEX);
177 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
183 // perform CUT operation
184 else if (aType == BOOLEAN_CUT) {
189 TopTools_ListOfShape listShapes, listTools;
190 AddSimpleShapes(aShape1, listShapes);
191 AddSimpleShapes(aShape2, listTools);
193 Standard_Boolean isCompound = (listShapes.Extent() > 1);
195 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
196 for (; itSub1.More(); itSub1.Next()) {
197 TopoDS_Shape aCut = itSub1.Value();
199 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
200 for (; itSub2.More(); itSub2.Next()) {
201 TopoDS_Shape aTool = itSub2.Value();
202 BRepAlgoAPI_Cut BO (aCut, aTool);
204 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
209 // check result of this step: if it is a compound (boolean operations
210 // allways return a compound), we add all sub-shapes of it.
211 // This allows to avoid adding empty compounds,
212 // resulting from CUT of parts
213 if (aCut.ShapeType() == TopAbs_COMPOUND) {
214 TopoDS_Iterator aCompIter (aCut);
215 for (; aCompIter.More(); aCompIter.Next()) {
216 // add shape in a result
217 B.Add(C, aCompIter.Value());
221 // add shape in a result
231 TopTools_ListOfShape listShapeC;
232 AddSimpleShapes(C, listShapeC);
233 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
234 bool isOnlySolids = true;
235 for (; itSubC.More(); itSubC.Next()) {
236 TopoDS_Shape aValueC = itSubC.Value();
237 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
240 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
245 // As GlueFaces has been improved to keep all kind of shapes
246 TopExp_Explorer anExp (C, TopAbs_VERTEX);
248 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
254 // perform FUSE operation
255 else if (aType == BOOLEAN_FUSE) {
256 /* Fix for NPAL15379: refused
258 TopTools_ListOfShape listShape1, listShape2;
259 AddSimpleShapes(aShape1, listShape1);
260 AddSimpleShapes(aShape2, listShape2);
262 Standard_Boolean isIntersect = Standard_False;
264 if (listShape1.Extent() > 1 && !isIntersect) {
265 // check intersections inside the first compound
266 TopTools_ListIteratorOfListOfShape it1 (listShape1);
267 for (; it1.More() && !isIntersect; it1.Next()) {
268 TopoDS_Shape aValue1 = it1.Value();
269 TopTools_ListIteratorOfListOfShape it2 (listShape1);
270 for (; it2.More() && !isIntersect; it2.Next()) {
271 TopoDS_Shape aValue2 = it2.Value();
272 if (aValue2 != aValue1) {
273 BRepAlgoAPI_Section BO (aValue1, aValue2);
275 TopoDS_Shape aSect = BO.Shape();
276 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
278 isIntersect = Standard_True;
286 if (listShape2.Extent() > 1 && !isIntersect) {
287 // check intersections inside the second compound
288 TopTools_ListIteratorOfListOfShape it1 (listShape2);
289 for (; it1.More() && !isIntersect; it1.Next()) {
290 TopoDS_Shape aValue1 = it1.Value();
291 TopTools_ListIteratorOfListOfShape it2 (listShape2);
292 for (; it2.More() && !isIntersect; it2.Next()) {
293 TopoDS_Shape aValue2 = it2.Value();
294 if (aValue2 != aValue1) {
295 BRepAlgoAPI_Section BO (aValue1, aValue2);
297 TopoDS_Shape aSect = BO.Shape();
298 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
300 isIntersect = Standard_True;
309 // have intersections inside compounds
310 // check intersections between compounds
311 TopTools_ListIteratorOfListOfShape it1 (listShape1);
312 for (; it1.More(); it1.Next()) {
313 TopoDS_Shape aValue1 = it1.Value();
314 TopTools_ListIteratorOfListOfShape it2 (listShape2);
315 for (; it2.More(); it2.Next()) {
316 TopoDS_Shape aValue2 = it2.Value();
317 if (aValue2 != aValue1) {
318 BRepAlgoAPI_Section BO (aValue1, aValue2);
320 TopoDS_Shape aSect = BO.Shape();
321 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
323 StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
333 BRepAlgoAPI_Fuse BO (aShape1, aShape2);
335 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
340 // perform SECTION operation
341 else if (aType == BOOLEAN_SECTION) {
346 TopTools_ListOfShape listShape1, listShape2;
347 AddSimpleShapes(aShape1, listShape1);
348 AddSimpleShapes(aShape2, listShape2);
350 Standard_Boolean isCompound =
351 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
353 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
354 for (; itSub1.More(); itSub1.Next()) {
355 TopoDS_Shape aValue1 = itSub1.Value();
356 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
357 for (; itSub2.More(); itSub2.Next()) {
358 TopoDS_Shape aValue2 = itSub2.Value();
359 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
360 // Set approximation to have an attached 3D BSpline geometry to each edge,
361 // where analytic curve is not possible. Without this flag in some cases
362 // we obtain BSpline curve of degree 1 (C0), which is slowly
363 // processed by some algorithms (Partition for example).
364 BO.Approximation(Standard_True);
367 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
370 TopoDS_Shape aStepResult = BO.Shape();
372 // check result of this step: if it is a compound (boolean operations
373 // allways return a compound), we add all sub-shapes of it.
374 // This allows to avoid adding empty compounds,
375 // resulting from SECTION on two non-intersecting shapes.
376 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
377 TopoDS_Iterator aCompIter (aStepResult);
378 for (; aCompIter.More(); aCompIter.Next()) {
379 // add shape in a result
380 B.Add(C, aCompIter.Value());
384 // add shape in a result
385 B.Add(C, aStepResult);
396 // As GlueFaces has been improved to keep all kind of shapes
397 TopExp_Explorer anExp (C, TopAbs_VERTEX);
399 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
410 if (aShape.IsNull()) return 0;
412 // 08.07.2008 skl for bug 19761 from Mantis
413 BRepCheck_Analyzer ana (aShape, Standard_True);
415 if (!ana.IsValid()) {
416 ShapeFix_ShapeTolerance aSFT;
417 aSFT.LimitTolerance(aShape, Precision::Confusion(),
418 Precision::Confusion(), TopAbs_SHAPE);
419 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
421 aShape = aSfs->Shape();
424 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
426 //if (!BRepAlgo::IsValid(aShape)) {
427 // Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
430 aFunction->SetValue(aShape);
432 log.SetTouched(Label());
438 //=======================================================================
439 //function : GEOMImpl_BooleanDriver_Type_
441 //=======================================================================
442 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
445 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
446 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
447 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
448 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
449 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
450 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
453 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
454 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
455 sizeof(GEOMImpl_BooleanDriver),
457 (Standard_Address)_Ancestors,
458 (Standard_Address)NULL);
463 //=======================================================================
464 //function : DownCast
466 //=======================================================================
467 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
469 Handle(GEOMImpl_BooleanDriver) _anOtherObject;
471 if (!AnObject.IsNull()) {
472 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
473 _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
477 return _anOtherObject ;