Salome HOME
Fix crashes in Boolean operations
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Pipe.h"
22
23 #include "GeomAlgoAPI_DFLoader.h"
24
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepOffsetAPI_MakePipe.hxx>
31 #include <BRepOffsetAPI_MakePipeShell.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <Geom_Curve.hxx>
34 #include <Geom_Line.hxx>
35 #include <gp_Lin.hxx>
36 #include <NCollection_List.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Shape.hxx>
40
41 static bool getBase(TopoDS_Shape& theBaseOut,
42                     TopAbs_ShapeEnum& theBaseTypeOut,
43                     const GeomShapePtr theBaseShape);
44 static bool getPath(TopoDS_Wire& thePathOut,
45                     const GeomShapePtr thePathShape);
46 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
47 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
48
49 //==================================================================================================
50 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
51                                    const GeomShapePtr thePathShape)
52 {
53   build(theBaseShape, thePathShape);
54 }
55
56 //==================================================================================================
57 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
58                                    const GeomShapePtr thePathShape,
59                                    const GeomShapePtr theBiNormal)
60 {
61   build(theBaseShape, thePathShape, theBiNormal);
62 }
63
64 //==================================================================================================
65 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
66                                    const ListOfShape& theLocations,
67                                    const GeomShapePtr thePathShape)
68 {
69   build(theBaseShapes, theLocations, thePathShape);
70 }
71
72 //==================================================================================================
73 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
74                              const GeomShapePtr thePathShape)
75 {
76   // Getting base shape.
77   if(!theBaseShape.get()) {
78     return;
79   }
80   TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
81   if(aBaseShape.IsNull()) {
82     return;
83   }
84   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
85   if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
86      aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
87      aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
88     return;
89   }
90
91   // Getting path.
92   TopoDS_Wire aPathWire;
93   if(!getPath(aPathWire, thePathShape)) {
94     return;
95   }
96
97   // Making pipe.
98   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
99   if(!aPipeBuilder) {
100     return;
101   }
102   aPipeBuilder->Build();
103
104   // Checking result.
105   if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
106     delete aPipeBuilder;
107     return;
108   }
109   this->initialize(aPipeBuilder);
110
111   // Setting naming.
112   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
113   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
114
115   // Setting result.
116   TopoDS_Shape aResult = aPipeBuilder->Shape();
117   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
118   GeomShapePtr aGeomSh(new GeomAPI_Shape());
119   aGeomSh->setImpl(new TopoDS_Shape(aResult));
120   this->setShape(aGeomSh);
121   this->setDone(true);
122 }
123
124 //==================================================================================================
125 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
126                              const GeomShapePtr thePathShape,
127                              const GeomShapePtr theBiNormal)
128 {
129   // Getting base shape.
130   TopoDS_Shape aBaseShape;
131   TopAbs_ShapeEnum aBaseShapeType;
132   if(!getBase(aBaseShape, aBaseShapeType, theBaseShape)) {
133     return;
134   }
135
136   // Getting path.
137   TopoDS_Wire aPathWire;
138   if(!getPath(aPathWire, thePathShape)) {
139     return;
140   }
141
142   // Getting Bi-Normal.
143   if(!theBiNormal.get()) {
144     return;
145   }
146   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
147   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
148     return;
149   }
150   TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
151   Standard_Real aFirst, aLast;
152   Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
153   Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
154   if(aBiNormalLine.IsNull()) {
155     return;
156   }
157   gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
158
159   // Making pipe.
160   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
161   if(!aPipeBuilder) {
162     return;
163   }
164   aPipeBuilder->Add(aBaseShape);
165   aPipeBuilder->SetMode(aBiNormalDir);
166   if(!buildPipe(aPipeBuilder)) {
167     delete aPipeBuilder;
168     return;
169   }
170   this->initialize(aPipeBuilder);
171
172   // Checking result.
173   if(aBaseShapeType == TopAbs_FACE) {
174     if(aPipeBuilder->MakeSolid() == Standard_False) {
175       return;
176     }
177   }
178   TopoDS_Shape aResult = aPipeBuilder->Shape();
179   if(aResult.IsNull()) {
180     return;
181   }
182
183   // Setting naming.
184   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
185   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
186
187   // Setting result.
188   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
189   GeomShapePtr aGeomSh(new GeomAPI_Shape());
190   aGeomSh->setImpl(new TopoDS_Shape(aResult));
191   this->setShape(aGeomSh);
192   this->setDone(true);
193 }
194
195 //==================================================================================================
196 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
197                              const ListOfShape& theLocations,
198                              const GeomShapePtr thePathShape)
199 {
200   if(theBaseShapes.empty() ||
201      (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
202     return;
203   }
204
205   bool aHasLocations = false;
206   if(!theLocations.empty()) {
207     aHasLocations = true;
208   }
209
210   // Getting path.
211   TopoDS_Wire aPathWire;
212   if(!getPath(aPathWire, thePathShape)) {
213     return;
214   }
215
216   // Making pipe.
217   Standard_Boolean isDone = Standard_False;
218   bool anIsSolidNeeded = false;
219   BRepOffsetAPI_MakePipeShell* aPipeBuilder;
220   for(int i = 0; i < 2; ++i) {
221     aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
222     if(!aPipeBuilder) {
223       return;
224     }
225     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
226     ListOfShape::const_iterator aLocIt = theLocations.cbegin();
227     while(aBaseIt != theBaseShapes.cend()) {
228       GeomShapePtr aBase = *aBaseIt;
229       TopoDS_Shape aBaseShape;
230       TopAbs_ShapeEnum aBaseShapeType;
231       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
232         delete aPipeBuilder;
233         return;
234       }
235       ++aBaseIt;
236       if(aBaseShapeType == TopAbs_FACE) {
237         anIsSolidNeeded = true;
238       }
239
240       if(aHasLocations) {
241         GeomShapePtr aLocation = *aLocIt;
242         if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
243           delete aPipeBuilder;
244           return;
245         }
246         TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
247         ++aLocIt;
248         aPipeBuilder->Add(aBaseShape, aLocationVertex);
249       } else {
250         aPipeBuilder->Add(aBaseShape);
251       }
252     }
253
254     if(aPipeBuilder->IsReady() == Standard_False) {
255       delete aPipeBuilder;
256       return;
257     }
258
259     if (i == 1) {
260        // Try to use Descrete Trihedron mode.
261       aPipeBuilder->SetDiscreteMode();
262     }
263     aPipeBuilder->Build();
264     isDone = aPipeBuilder->IsDone();
265
266     if (isDone) {
267       break;
268     }
269
270     delete aPipeBuilder;
271   }
272
273   if (!isDone) {
274     return;
275   }
276
277   this->initialize(aPipeBuilder);
278
279   // Checking result.
280   if(anIsSolidNeeded) {
281     if(aPipeBuilder->MakeSolid() == Standard_False) {
282       return;
283     }
284   }
285   TopoDS_Shape aResult = aPipeBuilder->Shape();
286
287   // Setting naming.
288   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
289   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
290   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
291   this->addFromShape(aFromShape);
292   this->addToShape(aToShape);
293
294   // Setting result.
295   if(aResult.IsNull()) {
296     return;
297   }
298   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
299   GeomShapePtr aGeomSh(new GeomAPI_Shape());
300   aGeomSh->setImpl(new TopoDS_Shape(aResult));
301   this->setShape(aGeomSh);
302   this->setDone(true);
303 }
304
305 //==================================================================================================
306 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
307                                  ListOfShape& theHistory)
308 {
309   GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
310 }
311
312 // Auxilary functions:
313 //==================================================================================================
314 bool getBase(TopoDS_Shape& theBaseOut,
315              TopAbs_ShapeEnum& theBaseTypeOut,
316              const GeomShapePtr theBaseShape)
317 {
318   if(!theBaseShape.get()) {
319     return false;
320   }
321
322   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
323   if(theBaseOut.IsNull()) {
324     return false;
325   }
326   theBaseTypeOut = theBaseOut.ShapeType();
327   if(theBaseTypeOut == TopAbs_VERTEX) {
328     // Do nothing.
329   } else if(theBaseTypeOut == TopAbs_EDGE) {
330     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
331   } else if(theBaseTypeOut == TopAbs_WIRE) {
332     // Do nothing.
333   } else if(theBaseTypeOut == TopAbs_FACE) {
334     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
335     theBaseOut = anExp.Current();
336   } else {
337     return false;
338   }
339
340   return true;
341 }
342
343 //==================================================================================================
344 bool getPath(TopoDS_Wire& thePathOut,
345              const GeomShapePtr thePathShape)
346 {
347   if(!thePathShape.get()) {
348     return false;
349   }
350
351   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
352   if(aPathShape.IsNull()) {
353     return false;
354   }
355   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
356   if(aPathShapeType == TopAbs_EDGE) {
357     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
358     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
359   } else if(aPathShapeType == TopAbs_WIRE) {
360     thePathOut = TopoDS::Wire(aPathShape);
361   } else {
362     return false;
363   }
364
365   return true;
366 }
367
368 //==================================================================================================
369 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
370 {
371   thePipeBuilder->Build();
372
373   Standard_Boolean isDone = thePipeBuilder->IsDone();
374
375   if (!isDone) {
376     // Try to use Descrete Trihedron mode.
377     thePipeBuilder->SetDiscreteMode();
378     thePipeBuilder->Build();
379     isDone = thePipeBuilder->IsDone();
380   }
381
382   return isDone == Standard_True;
383 }
384
385 //==================================================================================================
386 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
387 {
388   ListOfShape aList;
389
390   TopAbs_ShapeEnum aType = theShape.ShapeType();
391   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
392     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
393       GeomShapePtr aGeomShape(new GeomAPI_Shape());
394       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
395       aList.push_back(aGeomShape);
396     }
397   } else {
398     GeomShapePtr aGeomShape(new GeomAPI_Shape());
399     aGeomShape->setImpl(new TopoDS_Shape(theShape));
400     aList.push_back(aGeomShape);
401   }
402
403   return aList;
404 }