Salome HOME
57cfdf98518872e4048059ca0c37f32d76cfd359
[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 <BRepExtrema_DistShapeShape.hxx>
31 #include <BRepOffsetAPI_MakePipe.hxx>
32 #include <BRepOffsetAPI_MakePipeShell.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
36 #include <gp_Lin.hxx>
37 #include <NCollection_List.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <Precision.hxx>
42
43 static bool getBase(TopoDS_Shape& theBaseOut,
44                     TopAbs_ShapeEnum& theBaseTypeOut,
45                     const GeomShapePtr theBaseShape);
46 static bool getPath(TopoDS_Wire& thePathOut,
47                     const GeomShapePtr thePathShape);
48 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
49                                         const TopoDS_Shape& thePath);
50 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
51 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
52
53 //==================================================================================================
54 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
55                                    const GeomShapePtr thePathShape)
56 {
57   build(theBaseShape, thePathShape);
58 }
59
60 //==================================================================================================
61 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
62                                    const GeomShapePtr thePathShape,
63                                    const GeomShapePtr theBiNormal)
64 {
65   build(theBaseShape, thePathShape, theBiNormal);
66 }
67
68 //==================================================================================================
69 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
70                                    const ListOfShape& theLocations,
71                                    const GeomShapePtr thePathShape)
72 {
73   build(theBaseShapes, theLocations, thePathShape);
74 }
75
76 //==================================================================================================
77 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
78                              const GeomShapePtr thePathShape)
79 {
80   // Getting base shape.
81   if(!theBaseShape.get()) {
82     return;
83   }
84   TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
85   if(aBaseShape.IsNull()) {
86     return;
87   }
88   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
89   if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
90      aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
91      aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
92     return;
93   }
94
95   // Getting path.
96   TopoDS_Wire aPathWire;
97   if(!getPath(aPathWire, thePathShape)) {
98     return;
99   }
100   aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
101
102   // Making pipe.
103   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
104   if(!aPipeBuilder) {
105     return;
106   }
107   aPipeBuilder->Build();
108
109   // Checking result.
110   if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
111     delete aPipeBuilder;
112     return;
113   }
114   this->initialize(aPipeBuilder);
115
116   // Setting naming.
117   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
118   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
119
120   // Setting result.
121   TopoDS_Shape aResult = aPipeBuilder->Shape();
122   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
123   GeomShapePtr aGeomSh(new GeomAPI_Shape());
124   aGeomSh->setImpl(new TopoDS_Shape(aResult));
125   this->setShape(aGeomSh);
126   this->setDone(true);
127 }
128
129 //==================================================================================================
130 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
131                              const GeomShapePtr thePathShape,
132                              const GeomShapePtr theBiNormal)
133 {
134   // Getting base shape and path.
135   TopoDS_Shape aBaseShape;
136   TopAbs_ShapeEnum aBaseShapeType;
137   TopoDS_Wire aPathWire;
138   if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
139       !getPath(aPathWire, thePathShape) ||
140       !theBiNormal.get()) {
141     return;
142   }
143
144   aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
145
146   // Getting Bi-Normal.
147   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
148   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
149     return;
150   }
151   TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
152   Standard_Real aFirst, aLast;
153   Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
154   Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
155   if(aBiNormalLine.IsNull()) {
156     return;
157   }
158   gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
159
160   // Making pipe.
161   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
162   if(!aPipeBuilder) {
163     return;
164   }
165   aPipeBuilder->Add(aBaseShape);
166   aPipeBuilder->SetMode(aBiNormalDir);
167   if(!buildPipe(aPipeBuilder)) {
168     delete aPipeBuilder;
169     return;
170   }
171   this->initialize(aPipeBuilder);
172
173   // Checking result.
174   if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
175     return;
176   }
177   TopoDS_Shape aResult = aPipeBuilder->Shape();
178   if(aResult.IsNull()) {
179     return;
180   }
181
182   // Setting naming.
183   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
184   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
185
186   // Setting result.
187   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
188   GeomShapePtr aGeomSh(new GeomAPI_Shape());
189   aGeomSh->setImpl(new TopoDS_Shape(aResult));
190   this->setShape(aGeomSh);
191   this->setDone(true);
192 }
193
194 //==================================================================================================
195 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
196                              const ListOfShape& theLocations,
197                              const GeomShapePtr thePathShape)
198 {
199   if(theBaseShapes.empty() ||
200      (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
201     return;
202   }
203
204   // Getting base shape and path.
205   TopoDS_Shape aBaseShape;
206   TopAbs_ShapeEnum aBaseShapeType;
207   TopoDS_Wire aPathWire;
208   if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
209       !getPath(aPathWire, thePathShape)) {
210     return;
211   }
212
213   TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
214   gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
215   aPathWire.Move(aTrsf);
216
217   // Get locations after moving path shape.
218   std::list<TopoDS_Vertex> aLocations;
219   for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
220        aLocIt != theLocations.cend();
221        ++aLocIt)
222   {
223     GeomShapePtr aLocation = *aLocIt;
224     if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
225       return;
226     }
227
228     TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
229     TopoDS_Vertex aMovedVertex;
230     for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
231       if (anExp.Current().IsPartner(aLocationVertex)) {
232         aMovedVertex = TopoDS::Vertex(anExp.Current());
233         aLocations.push_back(aMovedVertex);
234         break;
235       }
236     }
237     if (aMovedVertex.IsNull()) {
238       return;
239     }
240   }
241
242   if (theLocations.size() != aLocations.size()) {
243     return;
244   }
245
246   bool aHasLocations = !aLocations.empty();
247
248   // Making pipe.
249   Standard_Boolean isDone = Standard_False;
250   bool anIsSolidNeeded = false;
251   BRepOffsetAPI_MakePipeShell* aPipeBuilder;
252   for(int i = 0; i < 2; ++i) {
253     aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
254     if(!aPipeBuilder) {
255       return;
256     }
257     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
258     std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
259     while(aBaseIt != theBaseShapes.cend()) {
260       GeomShapePtr aBase = *aBaseIt;
261       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
262         delete aPipeBuilder;
263         return;
264       }
265       ++aBaseIt;
266       if(aBaseShapeType == TopAbs_FACE) {
267         anIsSolidNeeded = true;
268       }
269
270       if(aHasLocations) {
271         aPipeBuilder->Add(aBaseShape, *aLocationsIt);
272         ++aLocationsIt;
273       } else {
274         aPipeBuilder->Add(aBaseShape);
275       }
276     }
277
278     if(aPipeBuilder->IsReady() == Standard_False) {
279       delete aPipeBuilder;
280       return;
281     }
282
283     if (i == 1) {
284        // Try to use Descrete Trihedron mode.
285       aPipeBuilder->SetDiscreteMode();
286     }
287     aPipeBuilder->Build();
288     isDone = aPipeBuilder->IsDone();
289
290     if (isDone) {
291       break;
292     }
293
294     delete aPipeBuilder;
295   }
296
297   if (!isDone) {
298     return;
299   }
300
301   this->initialize(aPipeBuilder);
302
303   // Checking result.
304   if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
305     return;
306   }
307   TopoDS_Shape aResult = aPipeBuilder->Shape();
308
309   // Setting naming.
310   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
311   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
312   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
313   fixOrientation(aFromShape);
314   fixOrientation(aToShape);
315   this->addFromShape(aFromShape);
316   this->addToShape(aToShape);
317
318   // Setting result.
319   if(aResult.IsNull()) {
320     return;
321   }
322   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
323   GeomShapePtr aGeomSh(new GeomAPI_Shape());
324   aGeomSh->setImpl(new TopoDS_Shape(aResult));
325   this->setShape(aGeomSh);
326   this->setDone(true);
327 }
328
329 //==================================================================================================
330 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
331                                  ListOfShape& theHistory)
332 {
333   GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
334 }
335
336 // Auxilary functions:
337 //==================================================================================================
338 bool getBase(TopoDS_Shape& theBaseOut,
339              TopAbs_ShapeEnum& theBaseTypeOut,
340              const GeomShapePtr theBaseShape)
341 {
342   if(!theBaseShape.get()) {
343     return false;
344   }
345
346   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
347   if(theBaseOut.IsNull()) {
348     return false;
349   }
350   theBaseTypeOut = theBaseOut.ShapeType();
351   if(theBaseTypeOut == TopAbs_VERTEX) {
352     // Do nothing.
353   } else if(theBaseTypeOut == TopAbs_EDGE) {
354     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
355   } else if(theBaseTypeOut == TopAbs_WIRE) {
356     // Do nothing.
357   } else if(theBaseTypeOut == TopAbs_FACE) {
358     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
359     theBaseOut = anExp.Current();
360   } else {
361     return false;
362   }
363
364   return true;
365 }
366
367 //==================================================================================================
368 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
369 {
370   if(!thePathShape.get()) {
371     return false;
372   }
373
374   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
375   if(aPathShape.IsNull()) {
376     return false;
377   }
378   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
379   if(aPathShapeType == TopAbs_EDGE) {
380     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
381     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
382   } else if(aPathShapeType == TopAbs_WIRE) {
383     thePathOut = TopoDS::Wire(aPathShape);
384   } else {
385     return false;
386   }
387
388   return true;
389 }
390
391 //==================================================================================================
392 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
393 {
394   gp_Trsf aTranslation;
395
396   BRepExtrema_DistShapeShape aDist(theBase, thePath);
397   aDist.Perform();
398   if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
399     gp_Pnt aPntBase = aDist.PointOnShape1(1);
400     gp_Pnt aPntPath = aDist.PointOnShape2(1);
401     aTranslation.SetTranslation(aPntPath, aPntBase);
402   }
403
404   return aTranslation;
405 }
406
407 //==================================================================================================
408 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
409 {
410   thePipeBuilder->Build();
411
412   Standard_Boolean isDone = thePipeBuilder->IsDone();
413
414   if (!isDone) {
415     // Try to use Descrete Trihedron mode.
416     thePipeBuilder->SetDiscreteMode();
417     thePipeBuilder->Build();
418     isDone = thePipeBuilder->IsDone();
419   }
420
421   return isDone == Standard_True;
422 }
423
424 //==================================================================================================
425 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
426 {
427   ListOfShape aList;
428
429   TopAbs_ShapeEnum aType = theShape.ShapeType();
430   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
431     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
432       GeomShapePtr aGeomShape(new GeomAPI_Shape());
433       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
434       aList.push_back(aGeomShape);
435     }
436   } else {
437     GeomShapePtr aGeomShape(new GeomAPI_Shape());
438     aGeomShape->setImpl(new TopoDS_Shape(theShape));
439     aList.push_back(aGeomShape);
440   }
441
442   return aList;
443 }