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>
42 #include <Standard_ConstructionError.hxx>
43 #include <StdFail_NotDone.hxx>
45 //=======================================================================
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
51 static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
52 return aBooleanDriver;
56 //=======================================================================
57 //function : GEOMImpl_BooleanDriver
59 //=======================================================================
60 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
64 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
66 if (theShape.ShapeType() != TopAbs_COMPOUND &&
67 theShape.ShapeType() != TopAbs_COMPSOLID) {
68 theList.Append(theShape);
72 TopTools_MapOfShape mapShape;
73 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
75 for (; It.More(); It.Next()) {
76 TopoDS_Shape aShape_i = It.Value();
77 if (mapShape.Add(aShape_i)) {
78 if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
79 aShape_i.ShapeType() == TopAbs_COMPSOLID) {
80 AddSimpleShapes(aShape_i, theList);
82 theList.Append(aShape_i);
88 //=======================================================================
91 //=======================================================================
92 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
94 if (Label().IsNull()) return 0;
95 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
97 GEOMImpl_IBoolean aCI (aFunction);
98 Standard_Integer aType = aFunction->GetType();
102 Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
103 Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
104 TopoDS_Shape aShape1 = aRefShape1->GetValue();
105 TopoDS_Shape aShape2 = aRefShape2->GetValue();
106 if (!aShape1.IsNull() && !aShape2.IsNull()) {
108 // perform COMMON operation
109 if (aType == BOOLEAN_COMMON) {
114 TopTools_ListOfShape listShape1, listShape2;
115 AddSimpleShapes(aShape1, listShape1);
116 AddSimpleShapes(aShape2, listShape2);
118 Standard_Boolean isCompound =
119 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
121 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
122 for (; itSub1.More(); itSub1.Next()) {
123 TopoDS_Shape aValue1 = itSub1.Value();
124 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
125 for (; itSub2.More(); itSub2.Next()) {
126 TopoDS_Shape aValue2 = itSub2.Value();
127 BRepAlgoAPI_Common BO (aValue1, aValue2);
129 StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
132 TopoDS_Shape aStepResult = BO.Shape();
134 // check result of this step: if it is a compound (boolean operations
135 // allways return a compound), we add all sub-shapes of it.
136 // This allows to avoid adding empty compounds,
137 // resulting from COMMON on two non-intersecting shapes.
138 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
139 TopoDS_Iterator aCompIter (aStepResult);
140 for (; aCompIter.More(); aCompIter.Next()) {
141 // add shape in a result
142 B.Add(C, aCompIter.Value());
146 // add shape in a result
147 B.Add(C, aStepResult);
157 TopTools_ListOfShape listShapeC;
158 AddSimpleShapes(C, listShapeC);
159 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
160 bool isOnlySolids = true;
161 for (; itSubC.More(); itSubC.Next()) {
162 TopoDS_Shape aValueC = itSubC.Value();
163 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
166 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
171 // As GlueFaces has been improved to keep all kind of shapes
172 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
176 // perform CUT operation
177 else if (aType == BOOLEAN_CUT) {
182 TopTools_ListOfShape listShapes, listTools;
183 AddSimpleShapes(aShape1, listShapes);
184 AddSimpleShapes(aShape2, listTools);
186 Standard_Boolean isCompound = (listShapes.Extent() > 1);
188 TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
189 for (; itSub1.More(); itSub1.Next()) {
190 TopoDS_Shape aCut = itSub1.Value();
192 TopTools_ListIteratorOfListOfShape itSub2 (listTools);
193 for (; itSub2.More(); itSub2.Next()) {
194 TopoDS_Shape aTool = itSub2.Value();
195 BRepAlgoAPI_Cut BO (aCut, aTool);
197 StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
202 // check result of this step: if it is a compound (boolean operations
203 // allways return a compound), we add all sub-shapes of it.
204 // This allows to avoid adding empty compounds,
205 // resulting from CUT of parts
206 if (aCut.ShapeType() == TopAbs_COMPOUND) {
207 TopoDS_Iterator aCompIter (aCut);
208 for (; aCompIter.More(); aCompIter.Next()) {
209 // add shape in a result
210 B.Add(C, aCompIter.Value());
214 // add shape in a result
224 TopTools_ListOfShape listShapeC;
225 AddSimpleShapes(C, listShapeC);
226 TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
227 bool isOnlySolids = true;
228 for (; itSubC.More(); itSubC.Next()) {
229 TopoDS_Shape aValueC = itSubC.Value();
230 if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
233 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
238 // As GlueFaces has been improved to keep all kind of shapes
239 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
243 // perform FUSE operation
244 else if (aType == BOOLEAN_FUSE) {
245 /* Fix for NPAL15379: refused
247 TopTools_ListOfShape listShape1, listShape2;
248 AddSimpleShapes(aShape1, listShape1);
249 AddSimpleShapes(aShape2, listShape2);
251 Standard_Boolean isIntersect = Standard_False;
253 if (listShape1.Extent() > 1 && !isIntersect) {
254 // check intersections inside the first compound
255 TopTools_ListIteratorOfListOfShape it1 (listShape1);
256 for (; it1.More() && !isIntersect; it1.Next()) {
257 TopoDS_Shape aValue1 = it1.Value();
258 TopTools_ListIteratorOfListOfShape it2 (listShape1);
259 for (; it2.More() && !isIntersect; it2.Next()) {
260 TopoDS_Shape aValue2 = it2.Value();
261 if (aValue2 != aValue1) {
262 BRepAlgoAPI_Section BO (aValue1, aValue2);
264 TopoDS_Shape aSect = BO.Shape();
265 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
267 isIntersect = Standard_True;
275 if (listShape2.Extent() > 1 && !isIntersect) {
276 // check intersections inside the second compound
277 TopTools_ListIteratorOfListOfShape it1 (listShape2);
278 for (; it1.More() && !isIntersect; it1.Next()) {
279 TopoDS_Shape aValue1 = it1.Value();
280 TopTools_ListIteratorOfListOfShape it2 (listShape2);
281 for (; it2.More() && !isIntersect; it2.Next()) {
282 TopoDS_Shape aValue2 = it2.Value();
283 if (aValue2 != aValue1) {
284 BRepAlgoAPI_Section BO (aValue1, aValue2);
286 TopoDS_Shape aSect = BO.Shape();
287 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
289 isIntersect = Standard_True;
298 // have intersections inside compounds
299 // check intersections between compounds
300 TopTools_ListIteratorOfListOfShape it1 (listShape1);
301 for (; it1.More(); it1.Next()) {
302 TopoDS_Shape aValue1 = it1.Value();
303 TopTools_ListIteratorOfListOfShape it2 (listShape2);
304 for (; it2.More(); 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 StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
322 BRepAlgoAPI_Fuse BO (aShape1, aShape2);
324 StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
329 // perform SECTION operation
330 else if (aType == BOOLEAN_SECTION) {
335 TopTools_ListOfShape listShape1, listShape2;
336 AddSimpleShapes(aShape1, listShape1);
337 AddSimpleShapes(aShape2, listShape2);
339 Standard_Boolean isCompound =
340 (listShape1.Extent() > 1 || listShape2.Extent() > 1);
342 TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
343 for (; itSub1.More(); itSub1.Next()) {
344 TopoDS_Shape aValue1 = itSub1.Value();
345 TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
346 for (; itSub2.More(); itSub2.Next()) {
347 TopoDS_Shape aValue2 = itSub2.Value();
348 BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
349 // Set approximation to have an attached 3D BSpline geometry to each edge,
350 // where analytic curve is not possible. Without this flag in some cases
351 // we obtain BSpline curve of degree 1 (C0), which is slowly
352 // processed by some algorithms (Partition for example).
353 BO.Approximation(Standard_True);
356 StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
359 TopoDS_Shape aStepResult = BO.Shape();
361 // check result of this step: if it is a compound (boolean operations
362 // allways return a compound), we add all sub-shapes of it.
363 // This allows to avoid adding empty compounds,
364 // resulting from SECTION on two non-intersecting shapes.
365 if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
366 TopoDS_Iterator aCompIter (aStepResult);
367 for (; aCompIter.More(); aCompIter.Next()) {
368 // add shape in a result
369 B.Add(C, aCompIter.Value());
373 // add shape in a result
374 B.Add(C, aStepResult);
385 // As GlueFaces has been improved to keep all kind of shapes
386 aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
395 if (aShape.IsNull()) return 0;
396 if (!BRepAlgo::IsValid(aShape)) {
397 Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
400 aFunction->SetValue(aShape);
402 log.SetTouched(Label());
408 //=======================================================================
409 //function : GEOMImpl_BooleanDriver_Type_
411 //=======================================================================
412 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
415 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
416 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
417 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
418 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
419 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
420 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
423 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
424 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
425 sizeof(GEOMImpl_BooleanDriver),
427 (Standard_Address)_Ancestors,
428 (Standard_Address)NULL);
433 //=======================================================================
434 //function : DownCast
436 //=======================================================================
437 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
439 Handle(GEOMImpl_BooleanDriver) _anOtherObject;
441 if (!AnObject.IsNull()) {
442 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
443 _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
447 return _anOtherObject ;