Salome HOME
Merge branch 'master' into cgt/devCEA
[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   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
218   if(!aPipeBuilder) {
219     return;
220   }
221   bool anIsSolidNeeded = false;
222   ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
223   ListOfShape::const_iterator aLocIt = theLocations.cbegin();
224   while(aBaseIt != theBaseShapes.cend()) {
225     GeomShapePtr aBase = *aBaseIt;
226     TopoDS_Shape aBaseShape;
227     TopAbs_ShapeEnum aBaseShapeType;
228     if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
229       delete aPipeBuilder;
230       return;
231     }
232     ++aBaseIt;
233     if(aBaseShapeType == TopAbs_FACE) {
234       anIsSolidNeeded = true;
235     }
236
237     if(aHasLocations) {
238       GeomShapePtr aLocation = *aLocIt;
239       if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
240         delete aPipeBuilder;
241         return;
242       }
243       TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
244       ++aLocIt;
245       aPipeBuilder->Add(aBaseShape, aLocationVertex);
246     } else {
247       aPipeBuilder->Add(aBaseShape);
248     }
249   }
250
251   if(aPipeBuilder->IsReady() == Standard_False) {
252     delete aPipeBuilder;
253     return;
254   }
255
256   if(!buildPipe(aPipeBuilder)) {
257     delete aPipeBuilder;
258     return;
259   }
260   this->initialize(aPipeBuilder);
261
262   // Checking result.
263   if(anIsSolidNeeded) {
264     if(aPipeBuilder->MakeSolid() == Standard_False) {
265       return;
266     }
267   }
268   TopoDS_Shape aResult = aPipeBuilder->Shape();
269
270   // Setting naming.
271   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
272   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
273   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
274   this->addFromShape(aFromShape);
275   this->addToShape(aToShape);
276
277   // Setting result.
278   if(aResult.IsNull()) {
279     return;
280   }
281   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
282   GeomShapePtr aGeomSh(new GeomAPI_Shape());
283   aGeomSh->setImpl(new TopoDS_Shape(aResult));
284   this->setShape(aGeomSh);
285   this->setDone(true);
286 }
287
288 //==================================================================================================
289 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
290                                  ListOfShape& theHistory)
291 {
292   GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
293 }
294
295 // Auxilary functions:
296 //==================================================================================================
297 bool getBase(TopoDS_Shape& theBaseOut,
298              TopAbs_ShapeEnum& theBaseTypeOut,
299              const GeomShapePtr theBaseShape)
300 {
301   if(!theBaseShape.get()) {
302     return false;
303   }
304
305   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
306   if(theBaseOut.IsNull()) {
307     return false;
308   }
309   theBaseTypeOut = theBaseOut.ShapeType();
310   if(theBaseTypeOut == TopAbs_VERTEX) {
311     // Do nothing.
312   } else if(theBaseTypeOut == TopAbs_EDGE) {
313     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
314   } else if(theBaseTypeOut == TopAbs_WIRE) {
315     // Do nothing.
316   } else if(theBaseTypeOut == TopAbs_FACE) {
317     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
318     theBaseOut = anExp.Current();
319   } else {
320     return false;
321   }
322
323   return true;
324 }
325
326 //==================================================================================================
327 bool getPath(TopoDS_Wire& thePathOut,
328              const GeomShapePtr thePathShape)
329 {
330   if(!thePathShape.get()) {
331     return false;
332   }
333
334   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
335   if(aPathShape.IsNull()) {
336     return false;
337   }
338   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
339   if(aPathShapeType == TopAbs_EDGE) {
340     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
341     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
342   } else if(aPathShapeType == TopAbs_WIRE) {
343     thePathOut = TopoDS::Wire(aPathShape);
344   } else {
345     return false;
346   }
347
348   return true;
349 }
350
351 //==================================================================================================
352 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
353 {
354   thePipeBuilder->Build();
355
356   Standard_Boolean isDone = thePipeBuilder->IsDone();
357
358   if (!isDone) {
359     // Try to use Descrete Trihedron mode.
360     thePipeBuilder->SetDiscreteMode();
361     thePipeBuilder->Build();
362     isDone = thePipeBuilder->IsDone();
363   }
364
365   return isDone == Standard_True;
366 }
367
368 //==================================================================================================
369 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
370 {
371   ListOfShape aList;
372
373   TopAbs_ShapeEnum aType = theShape.ShapeType();
374   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
375     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
376       GeomShapePtr aGeomShape(new GeomAPI_Shape());
377       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
378       aList.push_back(aGeomShape);
379     }
380   } else {
381     GeomShapePtr aGeomShape(new GeomAPI_Shape());
382     aGeomShape->setImpl(new TopoDS_Shape(theShape));
383     aList.push_back(aGeomShape);
384   }
385
386   return aList;
387 }