Salome HOME
1449a1112b17800e33de8d6853f9bd3ede9c6adf
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
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.
8 //
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
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>
26
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
42 #include <Standard_ConstructionError.hxx>
43 #include <StdFail_NotDone.hxx>
44
45 //=======================================================================
46 //function : GetID
47 //purpose  :
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
50 {
51   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
52   return aBooleanDriver;
53 }
54
55
56 //=======================================================================
57 //function : GEOMImpl_BooleanDriver
58 //purpose  :
59 //=======================================================================
60 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
61 {
62 }
63
64 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
65 {
66   if (theShape.ShapeType() != TopAbs_COMPOUND &&
67       theShape.ShapeType() != TopAbs_COMPSOLID) {
68     theList.Append(theShape);
69     return;
70   }
71
72   TopTools_MapOfShape mapShape;
73   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
74
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);
81       } else {
82         theList.Append(aShape_i);
83       }
84     }
85   }
86 }
87
88 //=======================================================================
89 //function : Execute
90 //purpose  :
91 //=======================================================================
92 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
93 {
94   if (Label().IsNull()) return 0;
95   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
96
97   GEOMImpl_IBoolean aCI (aFunction);
98   Standard_Integer aType = aFunction->GetType();
99
100   TopoDS_Shape aShape;
101
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()) {
107
108     // perform COMMON operation
109     if (aType == BOOLEAN_COMMON) {
110       BRep_Builder B;
111       TopoDS_Compound C;
112       B.MakeCompound(C);
113
114       TopTools_ListOfShape listShape1, listShape2;
115       AddSimpleShapes(aShape1, listShape1);
116       AddSimpleShapes(aShape2, listShape2);
117
118       Standard_Boolean isCompound =
119         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
120
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);
128           if (!BO.IsDone()) {
129             StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
130           }
131           if (isCompound) {
132             TopoDS_Shape aStepResult = BO.Shape();
133
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());
143               }
144             }
145             else {
146               // add shape in a result
147               B.Add(C, aStepResult);
148             }
149           }
150           else
151             aShape = BO.Shape();
152         }
153       }
154
155       if (isCompound) {
156         TopTools_ListOfShape listShapeC;
157         AddSimpleShapes(C, listShapeC);
158         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
159         bool isOnlySolids = true;
160         for (; itSubC.More(); itSubC.Next()) {
161           TopoDS_Shape aValueC = itSubC.Value();
162           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
163         }
164         if (isOnlySolids)
165           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
166         else
167           aShape = C;
168       }
169     }
170
171     // perform CUT operation
172     else if (aType == BOOLEAN_CUT) {
173       BRep_Builder B;
174       TopoDS_Compound C;
175       B.MakeCompound(C);
176
177       TopTools_ListOfShape listShapes, listTools;
178       AddSimpleShapes(aShape1, listShapes);
179       AddSimpleShapes(aShape2, listTools);
180
181       Standard_Boolean isCompound = (listShapes.Extent() > 1);
182
183       TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
184       for (; itSub1.More(); itSub1.Next()) {
185         TopoDS_Shape aCut = itSub1.Value();
186         // tools
187         TopTools_ListIteratorOfListOfShape itSub2 (listTools);
188         for (; itSub2.More(); itSub2.Next()) {
189           TopoDS_Shape aTool = itSub2.Value();
190           BRepAlgoAPI_Cut BO (aCut, aTool);
191           if (!BO.IsDone()) {
192             StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
193           }
194           aCut = BO.Shape();
195         }
196         if (isCompound) {
197           // check result of this step: if it is a compound (boolean operations
198           // allways return a compound), we add all sub-shapes of it.
199           // This allows to avoid adding empty compounds,
200           // resulting from CUT of parts
201           if (aCut.ShapeType() == TopAbs_COMPOUND) {
202             TopoDS_Iterator aCompIter (aCut);
203             for (; aCompIter.More(); aCompIter.Next()) {
204               // add shape in a result
205               B.Add(C, aCompIter.Value());
206             }
207           }
208           else {
209             // add shape in a result
210             B.Add(C, aCut);
211           }
212         }
213         else
214           aShape = aCut;
215       }
216
217       if (isCompound) {
218         TopTools_ListOfShape listShapeC;
219         AddSimpleShapes(C, listShapeC);
220         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
221         bool isOnlySolids = true;
222         for (; itSubC.More(); itSubC.Next()) {
223           TopoDS_Shape aValueC = itSubC.Value();
224           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
225         }
226         if (isOnlySolids)
227           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
228         else
229           aShape = C;
230       }
231     }
232
233     // perform FUSE operation
234     else if (aType == BOOLEAN_FUSE) {
235       /* Fix for NPAL15379: refused
236       // Check arguments
237       TopTools_ListOfShape listShape1, listShape2;
238       AddSimpleShapes(aShape1, listShape1);
239       AddSimpleShapes(aShape2, listShape2);
240
241       Standard_Boolean isIntersect = Standard_False;
242
243       if (listShape1.Extent() > 1 && !isIntersect) {
244         // check intersections inside the first compound
245         TopTools_ListIteratorOfListOfShape it1 (listShape1);
246         for (; it1.More() && !isIntersect; it1.Next()) {
247           TopoDS_Shape aValue1 = it1.Value();
248           TopTools_ListIteratorOfListOfShape it2 (listShape1);
249           for (; it2.More() && !isIntersect; it2.Next()) {
250             TopoDS_Shape aValue2 = it2.Value();
251             if (aValue2 != aValue1) {
252               BRepAlgoAPI_Section BO (aValue1, aValue2);
253               if (BO.IsDone()) {
254                 TopoDS_Shape aSect = BO.Shape();
255                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
256                 if (anExp.More()) {
257                   isIntersect = Standard_True;
258                 }
259               }
260             }
261           }
262         }
263       }
264
265       if (listShape2.Extent() > 1 && !isIntersect) {
266         // check intersections inside the second compound
267         TopTools_ListIteratorOfListOfShape it1 (listShape2);
268         for (; it1.More() && !isIntersect; it1.Next()) {
269           TopoDS_Shape aValue1 = it1.Value();
270           TopTools_ListIteratorOfListOfShape it2 (listShape2);
271           for (; it2.More() && !isIntersect; it2.Next()) {
272             TopoDS_Shape aValue2 = it2.Value();
273             if (aValue2 != aValue1) {
274               BRepAlgoAPI_Section BO (aValue1, aValue2);
275               if (BO.IsDone()) {
276                 TopoDS_Shape aSect = BO.Shape();
277                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
278                 if (anExp.More()) {
279                   isIntersect = Standard_True;
280                 }
281               }
282             }
283           }
284         }
285       }
286
287       if (isIntersect) {
288         // have intersections inside compounds
289         // check intersections between compounds
290         TopTools_ListIteratorOfListOfShape it1 (listShape1);
291         for (; it1.More(); it1.Next()) {
292           TopoDS_Shape aValue1 = it1.Value();
293           TopTools_ListIteratorOfListOfShape it2 (listShape2);
294           for (; it2.More(); it2.Next()) {
295             TopoDS_Shape aValue2 = it2.Value();
296             if (aValue2 != aValue1) {
297               BRepAlgoAPI_Section BO (aValue1, aValue2);
298               if (BO.IsDone()) {
299                 TopoDS_Shape aSect = BO.Shape();
300                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
301                 if (anExp.More()) {
302                   StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
303                 }
304               }
305             }
306           }
307         }
308       }
309       */
310
311       // Perform
312       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
313       if (!BO.IsDone()) {
314         StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
315       }
316       aShape = BO.Shape();
317     }
318
319     // perform SECTION operation
320     else if (aType == BOOLEAN_SECTION) {
321       BRep_Builder B;
322       TopoDS_Compound C;
323       B.MakeCompound(C);
324
325       TopTools_ListOfShape listShape1, listShape2;
326       AddSimpleShapes(aShape1, listShape1);
327       AddSimpleShapes(aShape2, listShape2);
328
329       Standard_Boolean isCompound =
330         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
331
332       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
333       for (; itSub1.More(); itSub1.Next()) {
334         TopoDS_Shape aValue1 = itSub1.Value();
335         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
336         for (; itSub2.More(); itSub2.Next()) {
337           TopoDS_Shape aValue2 = itSub2.Value();
338           BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
339           // Set approximation to have an attached 3D BSpline geometry to each edge,
340           // where analytic curve is not possible. Without this flag in some cases
341           // we obtain BSpline curve of degree 1 (C0), which is slowly
342           // processed by some algorithms (Partition for example).
343           BO.Approximation(Standard_True);
344           BO.Build();
345           if (!BO.IsDone()) {
346             StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
347           }
348           if (isCompound) {
349             TopoDS_Shape aStepResult = BO.Shape();
350
351             // check result of this step: if it is a compound (boolean operations
352             // allways return a compound), we add all sub-shapes of it.
353             // This allows to avoid adding empty compounds,
354             // resulting from SECTION on two non-intersecting shapes.
355             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
356               TopoDS_Iterator aCompIter (aStepResult);
357               for (; aCompIter.More(); aCompIter.Next()) {
358                 // add shape in a result
359                 B.Add(C, aCompIter.Value());
360               }
361             }
362             else {
363               // add shape in a result
364               B.Add(C, aStepResult);
365             }
366           }
367           else
368             aShape = BO.Shape();
369         }
370       }
371
372       if (isCompound)
373         aShape = C;
374     }
375
376     // UNKNOWN operation
377     else {
378     }
379   }
380
381   if (aShape.IsNull()) return 0;
382   if (!BRepAlgo::IsValid(aShape)) {
383     Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
384   }
385
386   aFunction->SetValue(aShape);
387
388   log.SetTouched(Label());
389
390   return 1;
391 }
392
393
394 //=======================================================================
395 //function :  GEOMImpl_BooleanDriver_Type_
396 //purpose  :
397 //=======================================================================
398 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
399 {
400
401   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
402   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
403   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
404   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
405   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
406   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
407
408
409   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
410   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
411                                                          sizeof(GEOMImpl_BooleanDriver),
412                                                          1,
413                                                          (Standard_Address)_Ancestors,
414                                                          (Standard_Address)NULL);
415
416   return _aType;
417 }
418
419 //=======================================================================
420 //function : DownCast
421 //purpose  :
422 //=======================================================================
423 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
424 {
425   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
426
427   if (!AnObject.IsNull()) {
428      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
429        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
430      }
431   }
432
433   return _anOtherObject ;
434 }