Salome HOME
Fix for bug 0019870(Import/Export should show current directory).
[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         /*
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;
164         }
165         if (isOnlySolids)
166           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
167         else
168           aShape = C;
169         */
170
171         // As GlueFaces has been improved to keep all kind of shapes
172         TopExp_Explorer anExp (C, TopAbs_VERTEX);
173         if (anExp.More())
174           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
175         else
176           aShape = C;
177       }
178     }
179
180     // perform CUT operation
181     else if (aType == BOOLEAN_CUT) {
182       BRep_Builder B;
183       TopoDS_Compound C;
184       B.MakeCompound(C);
185
186       TopTools_ListOfShape listShapes, listTools;
187       AddSimpleShapes(aShape1, listShapes);
188       AddSimpleShapes(aShape2, listTools);
189
190       Standard_Boolean isCompound = (listShapes.Extent() > 1);
191
192       TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
193       for (; itSub1.More(); itSub1.Next()) {
194         TopoDS_Shape aCut = itSub1.Value();
195         // tools
196         TopTools_ListIteratorOfListOfShape itSub2 (listTools);
197         for (; itSub2.More(); itSub2.Next()) {
198           TopoDS_Shape aTool = itSub2.Value();
199           BRepAlgoAPI_Cut BO (aCut, aTool);
200           if (!BO.IsDone()) {
201             StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
202           }
203           aCut = BO.Shape();
204         }
205         if (isCompound) {
206           // check result of this step: if it is a compound (boolean operations
207           // allways return a compound), we add all sub-shapes of it.
208           // This allows to avoid adding empty compounds,
209           // resulting from CUT of parts
210           if (aCut.ShapeType() == TopAbs_COMPOUND) {
211             TopoDS_Iterator aCompIter (aCut);
212             for (; aCompIter.More(); aCompIter.Next()) {
213               // add shape in a result
214               B.Add(C, aCompIter.Value());
215             }
216           }
217           else {
218             // add shape in a result
219             B.Add(C, aCut);
220           }
221         }
222         else
223           aShape = aCut;
224       }
225
226       if (isCompound) {
227         /*
228         TopTools_ListOfShape listShapeC;
229         AddSimpleShapes(C, listShapeC);
230         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
231         bool isOnlySolids = true;
232         for (; itSubC.More(); itSubC.Next()) {
233           TopoDS_Shape aValueC = itSubC.Value();
234           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
235         }
236         if (isOnlySolids)
237           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
238         else
239           aShape = C;
240         */
241
242         // As GlueFaces has been improved to keep all kind of shapes
243         TopExp_Explorer anExp (C, TopAbs_VERTEX);
244         if (anExp.More())
245           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
246         else
247           aShape = C;
248       }
249     }
250
251     // perform FUSE operation
252     else if (aType == BOOLEAN_FUSE) {
253       /* Fix for NPAL15379: refused
254       // Check arguments
255       TopTools_ListOfShape listShape1, listShape2;
256       AddSimpleShapes(aShape1, listShape1);
257       AddSimpleShapes(aShape2, listShape2);
258
259       Standard_Boolean isIntersect = Standard_False;
260
261       if (listShape1.Extent() > 1 && !isIntersect) {
262         // check intersections inside the first compound
263         TopTools_ListIteratorOfListOfShape it1 (listShape1);
264         for (; it1.More() && !isIntersect; it1.Next()) {
265           TopoDS_Shape aValue1 = it1.Value();
266           TopTools_ListIteratorOfListOfShape it2 (listShape1);
267           for (; it2.More() && !isIntersect; it2.Next()) {
268             TopoDS_Shape aValue2 = it2.Value();
269             if (aValue2 != aValue1) {
270               BRepAlgoAPI_Section BO (aValue1, aValue2);
271               if (BO.IsDone()) {
272                 TopoDS_Shape aSect = BO.Shape();
273                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
274                 if (anExp.More()) {
275                   isIntersect = Standard_True;
276                 }
277               }
278             }
279           }
280         }
281       }
282
283       if (listShape2.Extent() > 1 && !isIntersect) {
284         // check intersections inside the second compound
285         TopTools_ListIteratorOfListOfShape it1 (listShape2);
286         for (; it1.More() && !isIntersect; it1.Next()) {
287           TopoDS_Shape aValue1 = it1.Value();
288           TopTools_ListIteratorOfListOfShape it2 (listShape2);
289           for (; it2.More() && !isIntersect; it2.Next()) {
290             TopoDS_Shape aValue2 = it2.Value();
291             if (aValue2 != aValue1) {
292               BRepAlgoAPI_Section BO (aValue1, aValue2);
293               if (BO.IsDone()) {
294                 TopoDS_Shape aSect = BO.Shape();
295                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
296                 if (anExp.More()) {
297                   isIntersect = Standard_True;
298                 }
299               }
300             }
301           }
302         }
303       }
304
305       if (isIntersect) {
306         // have intersections inside compounds
307         // check intersections between compounds
308         TopTools_ListIteratorOfListOfShape it1 (listShape1);
309         for (; it1.More(); it1.Next()) {
310           TopoDS_Shape aValue1 = it1.Value();
311           TopTools_ListIteratorOfListOfShape it2 (listShape2);
312           for (; it2.More(); it2.Next()) {
313             TopoDS_Shape aValue2 = it2.Value();
314             if (aValue2 != aValue1) {
315               BRepAlgoAPI_Section BO (aValue1, aValue2);
316               if (BO.IsDone()) {
317                 TopoDS_Shape aSect = BO.Shape();
318                 TopExp_Explorer anExp (aSect, TopAbs_EDGE);
319                 if (anExp.More()) {
320                   StdFail_NotDone::Raise("Bad argument for Fuse: compound with intersecting sub-shapes");
321                 }
322               }
323             }
324           }
325         }
326       }
327       */
328
329       // Perform
330       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
331       if (!BO.IsDone()) {
332         StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
333       }
334       aShape = BO.Shape();
335     }
336
337     // perform SECTION operation
338     else if (aType == BOOLEAN_SECTION) {
339       BRep_Builder B;
340       TopoDS_Compound C;
341       B.MakeCompound(C);
342
343       TopTools_ListOfShape listShape1, listShape2;
344       AddSimpleShapes(aShape1, listShape1);
345       AddSimpleShapes(aShape2, listShape2);
346
347       Standard_Boolean isCompound =
348         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
349
350       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
351       for (; itSub1.More(); itSub1.Next()) {
352         TopoDS_Shape aValue1 = itSub1.Value();
353         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
354         for (; itSub2.More(); itSub2.Next()) {
355           TopoDS_Shape aValue2 = itSub2.Value();
356           BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
357           // Set approximation to have an attached 3D BSpline geometry to each edge,
358           // where analytic curve is not possible. Without this flag in some cases
359           // we obtain BSpline curve of degree 1 (C0), which is slowly
360           // processed by some algorithms (Partition for example).
361           BO.Approximation(Standard_True);
362           BO.Build();
363           if (!BO.IsDone()) {
364             StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
365           }
366           if (isCompound) {
367             TopoDS_Shape aStepResult = BO.Shape();
368
369             // check result of this step: if it is a compound (boolean operations
370             // allways return a compound), we add all sub-shapes of it.
371             // This allows to avoid adding empty compounds,
372             // resulting from SECTION on two non-intersecting shapes.
373             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
374               TopoDS_Iterator aCompIter (aStepResult);
375               for (; aCompIter.More(); aCompIter.Next()) {
376                 // add shape in a result
377                 B.Add(C, aCompIter.Value());
378               }
379             }
380             else {
381               // add shape in a result
382               B.Add(C, aStepResult);
383             }
384           }
385           else
386             aShape = BO.Shape();
387         }
388       }
389
390       if (isCompound) {
391         //aShape = C;
392
393         // As GlueFaces has been improved to keep all kind of shapes
394         TopExp_Explorer anExp (C, TopAbs_VERTEX);
395         if (anExp.More())
396           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion(), Standard_True);
397         else
398           aShape = C;
399       }
400     }
401
402     // UNKNOWN operation
403     else {
404     }
405   }
406
407   if (aShape.IsNull()) return 0;
408   if (!BRepAlgo::IsValid(aShape)) {
409     Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
410   }
411
412   aFunction->SetValue(aShape);
413
414   log.SetTouched(Label());
415
416   return 1;
417 }
418
419
420 //=======================================================================
421 //function :  GEOMImpl_BooleanDriver_Type_
422 //purpose  :
423 //=======================================================================
424 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
425 {
426
427   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
428   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
429   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
430   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
431   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
432   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
433
434
435   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
436   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
437                                                          sizeof(GEOMImpl_BooleanDriver),
438                                                          1,
439                                                          (Standard_Address)_Ancestors,
440                                                          (Standard_Address)NULL);
441
442   return _aType;
443 }
444
445 //=======================================================================
446 //function : DownCast
447 //purpose  :
448 //=======================================================================
449 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
450 {
451   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
452
453   if (!AnObject.IsNull()) {
454      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
455        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
456      }
457   }
458
459   return _anOtherObject ;
460 }