Salome HOME
PAL14122: EDF307: GetInPlace --> COMM_FAILURE. Use new method GEOMAlgo_BuilderShape...
[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 <TopoDS_Shape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopoDS_Iterator.hxx>
36 #include <TopTools_MapOfShape.hxx>
37 #include <TopTools_ListOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <Precision.hxx>
40
41 #include <Standard_ConstructionError.hxx>
42 #include <StdFail_NotDone.hxx>
43
44 //=======================================================================
45 //function : GetID
46 //purpose  :
47 //=======================================================================
48 const Standard_GUID& GEOMImpl_BooleanDriver::GetID()
49 {
50   static Standard_GUID aBooleanDriver("FF1BBB21-5D14-4df2-980B-3A668264EA16");
51   return aBooleanDriver;
52 }
53
54
55 //=======================================================================
56 //function : GEOMImpl_BooleanDriver
57 //purpose  :
58 //=======================================================================
59 GEOMImpl_BooleanDriver::GEOMImpl_BooleanDriver()
60 {
61 }
62
63 void AddSimpleShapes(TopoDS_Shape theShape, TopTools_ListOfShape& theList)
64 {
65   if (theShape.ShapeType() != TopAbs_COMPOUND &&
66       theShape.ShapeType() != TopAbs_COMPSOLID) {
67     theList.Append(theShape);
68     return;
69   }
70
71   TopTools_MapOfShape mapShape;
72   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
73
74   for (; It.More(); It.Next()) {
75     TopoDS_Shape aShape_i = It.Value();
76     if (mapShape.Add(aShape_i)) {
77       if (aShape_i.ShapeType() == TopAbs_COMPOUND ||
78           aShape_i.ShapeType() == TopAbs_COMPSOLID) {
79         AddSimpleShapes(aShape_i, theList);
80       } else {
81         theList.Append(aShape_i);
82       }
83     }
84   }
85 }
86
87 //=======================================================================
88 //function : Execute
89 //purpose  :
90 //=======================================================================
91 Standard_Integer GEOMImpl_BooleanDriver::Execute(TFunction_Logbook& log) const
92 {
93   if (Label().IsNull()) return 0;
94   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
95
96   GEOMImpl_IBoolean aCI (aFunction);
97   Standard_Integer aType = aFunction->GetType();
98
99   TopoDS_Shape aShape;
100
101   Handle(GEOM_Function) aRefShape1 = aCI.GetShape1();
102   Handle(GEOM_Function) aRefShape2 = aCI.GetShape2();
103   TopoDS_Shape aShape1 = aRefShape1->GetValue();
104   TopoDS_Shape aShape2 = aRefShape2->GetValue();
105   if (!aShape1.IsNull() && !aShape2.IsNull()) {
106
107     // perform COMMON operation
108     if (aType == BOOLEAN_COMMON) {
109       BRep_Builder B;
110       TopoDS_Compound C;
111       B.MakeCompound(C);
112
113       TopTools_ListOfShape listShape1, listShape2;
114       AddSimpleShapes(aShape1, listShape1);
115       AddSimpleShapes(aShape2, listShape2);
116
117       Standard_Boolean isCompound =
118         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
119
120       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
121       for (; itSub1.More(); itSub1.Next()) {
122         TopoDS_Shape aValue1 = itSub1.Value();
123         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
124         for (; itSub2.More(); itSub2.Next()) {
125           TopoDS_Shape aValue2 = itSub2.Value();
126           BRepAlgoAPI_Common BO (aValue1, aValue2);
127           if (!BO.IsDone()) {
128             StdFail_NotDone::Raise("Common operation can not be performed on the given shapes");
129           }
130           if (isCompound) {
131             TopoDS_Shape aStepResult = BO.Shape();
132
133             // check result of this step: if it is a compound (boolean operations
134             // allways return a compound), we add all sub-shapes of it.
135             // This allows to avoid adding empty compounds,
136             // resulting from COMMON on two non-intersecting shapes.
137             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
138               TopoDS_Iterator aCompIter (aStepResult);
139               for (; aCompIter.More(); aCompIter.Next()) {
140                 // add shape in a result
141                 B.Add(C, aCompIter.Value());
142               }
143             }
144             else {
145               // add shape in a result
146               B.Add(C, aStepResult);
147             }
148           }
149           else
150             aShape = BO.Shape();
151         }
152       }
153
154       if (isCompound) {
155         TopTools_ListOfShape listShapeC;
156         AddSimpleShapes(C, listShapeC);
157         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
158         bool isOnlySolids = true;
159         for (; itSubC.More(); itSubC.Next()) {
160           TopoDS_Shape aValueC = itSubC.Value();
161           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
162         }
163         if (isOnlySolids)
164           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
165         else
166           aShape = C;
167       }
168     }
169
170     // perform CUT operation
171     else if (aType == BOOLEAN_CUT) {
172       BRep_Builder B;
173       TopoDS_Compound C;
174       B.MakeCompound(C);
175
176       TopTools_ListOfShape listShapes, listTools;
177       AddSimpleShapes(aShape1, listShapes);
178       AddSimpleShapes(aShape2, listTools);
179
180       Standard_Boolean isCompound = (listShapes.Extent() > 1);
181
182       TopTools_ListIteratorOfListOfShape itSub1 (listShapes);
183       for (; itSub1.More(); itSub1.Next()) {
184         TopoDS_Shape aCut = itSub1.Value();
185         // tools
186         TopTools_ListIteratorOfListOfShape itSub2 (listTools);
187         for (; itSub2.More(); itSub2.Next()) {
188           TopoDS_Shape aTool = itSub2.Value();
189           BRepAlgoAPI_Cut BO (aCut, aTool);
190           if (!BO.IsDone()) {
191             StdFail_NotDone::Raise("Cut operation can not be performed on the given shapes");
192           }
193           aCut = BO.Shape();
194         }
195         if (isCompound) {
196           // check result of this step: if it is a compound (boolean operations
197           // allways return a compound), we add all sub-shapes of it.
198           // This allows to avoid adding empty compounds,
199           // resulting from CUT of parts
200           if (aCut.ShapeType() == TopAbs_COMPOUND) {
201             TopoDS_Iterator aCompIter (aCut);
202             for (; aCompIter.More(); aCompIter.Next()) {
203               // add shape in a result
204               B.Add(C, aCompIter.Value());
205             }
206           }
207           else {
208             // add shape in a result
209             B.Add(C, aCut);
210           }
211         }
212         else
213           aShape = aCut;
214       }
215
216       if (isCompound) {
217         TopTools_ListOfShape listShapeC;
218         AddSimpleShapes(C, listShapeC);
219         TopTools_ListIteratorOfListOfShape itSubC (listShapeC);
220         bool isOnlySolids = true;
221         for (; itSubC.More(); itSubC.Next()) {
222           TopoDS_Shape aValueC = itSubC.Value();
223           if (aValueC.ShapeType() != TopAbs_SOLID) isOnlySolids = false;
224         }
225         if (isOnlySolids)
226           aShape = GEOMImpl_GlueDriver::GlueFaces(C, Precision::Confusion());
227         else
228           aShape = C;
229       }
230     }
231
232     // perform FUSE operation
233     else if (aType == BOOLEAN_FUSE) {
234       BRepAlgoAPI_Fuse BO (aShape1, aShape2);
235       if (!BO.IsDone()) {
236         StdFail_NotDone::Raise("Fuse operation can not be performed on the given shapes");
237       }
238       aShape = BO.Shape();
239     }
240
241     // perform SECTION operation
242     else if (aType == BOOLEAN_SECTION) {
243       BRep_Builder B;
244       TopoDS_Compound C;
245       B.MakeCompound(C);
246
247       TopTools_ListOfShape listShape1, listShape2;
248       AddSimpleShapes(aShape1, listShape1);
249       AddSimpleShapes(aShape2, listShape2);
250
251       Standard_Boolean isCompound =
252         (listShape1.Extent() > 1 || listShape2.Extent() > 1);
253
254       TopTools_ListIteratorOfListOfShape itSub1 (listShape1);
255       for (; itSub1.More(); itSub1.Next()) {
256         TopoDS_Shape aValue1 = itSub1.Value();
257         TopTools_ListIteratorOfListOfShape itSub2 (listShape2);
258         for (; itSub2.More(); itSub2.Next()) {
259           TopoDS_Shape aValue2 = itSub2.Value();
260           BRepAlgoAPI_Section BO (aValue1, aValue2, Standard_False);
261           // Set approximation to have an attached 3D BSpline geometry to each edge,
262           // where analytic curve is not possible. Without this flag in some cases
263           // we obtain BSpline curve of degree 1 (C0), which is slowly
264           // processed by some algorithms (Partition for example).
265           BO.Approximation(Standard_True);
266           BO.Build();
267           if (!BO.IsDone()) {
268             StdFail_NotDone::Raise("Section operation can not be performed on the given shapes");
269           }
270           if (isCompound) {
271             TopoDS_Shape aStepResult = BO.Shape();
272
273             // check result of this step: if it is a compound (boolean operations
274             // allways return a compound), we add all sub-shapes of it.
275             // This allows to avoid adding empty compounds,
276             // resulting from SECTION on two non-intersecting shapes.
277             if (aStepResult.ShapeType() == TopAbs_COMPOUND) {
278               TopoDS_Iterator aCompIter (aStepResult);
279               for (; aCompIter.More(); aCompIter.Next()) {
280                 // add shape in a result
281                 B.Add(C, aCompIter.Value());
282               }
283             }
284             else {
285               // add shape in a result
286               B.Add(C, aStepResult);
287             }
288           }
289           else
290             aShape = BO.Shape();
291         }
292       }
293
294       if (isCompound)
295         aShape = C;
296     }
297
298     // UNKNOWN operation
299     else {
300     }
301   }
302
303   if (aShape.IsNull()) return 0;
304   if (!BRepAlgo::IsValid(aShape)) {
305     Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
306   }
307
308   aFunction->SetValue(aShape);
309
310   log.SetTouched(Label());
311
312   return 1;
313 }
314
315
316 //=======================================================================
317 //function :  GEOMImpl_BooleanDriver_Type_
318 //purpose  :
319 //=======================================================================
320 Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
321 {
322
323   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
324   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
325   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
326   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
327   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
328   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
329
330
331   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
332   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
333                                                          sizeof(GEOMImpl_BooleanDriver),
334                                                          1,
335                                                          (Standard_Address)_Ancestors,
336                                                          (Standard_Address)NULL);
337
338   return _aType;
339 }
340
341 //=======================================================================
342 //function : DownCast
343 //purpose  :
344 //=======================================================================
345 const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
346 {
347   Handle(GEOMImpl_BooleanDriver) _anOtherObject;
348
349   if (!AnObject.IsNull()) {
350      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
351        _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
352      }
353   }
354
355   return _anOtherObject ;
356 }