Salome HOME
Copyright update 2020
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
1 // Copyright (C) 2014-2020  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_Pipe.h"
21
22 #include "GeomAlgoAPI_DFLoader.h"
23
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Lin.h>
27 #include <GeomAPI_ShapeExplorer.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   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
101   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
102   aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
103   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
104   if (!anOldPath->isSame(aNewPath))
105     addMovedPath(anOldPath, aNewPath);
106
107   // Making pipe.
108   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
109   if(!aPipeBuilder) {
110     return;
111   }
112   aPipeBuilder->Build();
113
114   // Checking result.
115   if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
116     delete aPipeBuilder;
117     return;
118   }
119   this->initialize(aPipeBuilder);
120
121   // Setting naming.
122   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
123   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
124
125   // Setting result.
126   TopoDS_Shape aResult = aPipeBuilder->Shape();
127   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
128   GeomShapePtr aGeomSh(new GeomAPI_Shape());
129   aGeomSh->setImpl(new TopoDS_Shape(aResult));
130   this->setShape(aGeomSh);
131   this->setDone(true);
132 }
133
134 //==================================================================================================
135 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
136                              const GeomShapePtr thePathShape,
137                              const GeomShapePtr theBiNormal)
138 {
139   // Getting base shape and path.
140   TopoDS_Shape aBaseShape;
141   TopAbs_ShapeEnum aBaseShapeType;
142   TopoDS_Wire aPathWire;
143   if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
144       !getPath(aPathWire, thePathShape) ||
145       !theBiNormal.get()) {
146     return;
147   }
148
149   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
150   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
151   aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
152   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
153   if (!anOldPath->isSame(aNewPath))
154     addMovedPath(anOldPath, aNewPath);
155
156   // Getting Bi-Normal.
157   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
158   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
159     return;
160   }
161   TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
162   Standard_Real aFirst, aLast;
163   Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
164   Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
165   if(aBiNormalLine.IsNull()) {
166     return;
167   }
168   gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
169
170   // Making pipe.
171   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
172   if(!aPipeBuilder) {
173     return;
174   }
175   aPipeBuilder->Add(aBaseShape);
176   aPipeBuilder->SetMode(aBiNormalDir);
177   if(!buildPipe(aPipeBuilder)) {
178     delete aPipeBuilder;
179     return;
180   }
181   this->initialize(aPipeBuilder);
182
183   // Checking result.
184   if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
185     return;
186   }
187   TopoDS_Shape aResult = aPipeBuilder->Shape();
188   if(aResult.IsNull()) {
189     return;
190   }
191
192   // Setting naming.
193   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
194   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
195
196   // Setting result.
197   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
198   GeomShapePtr aGeomSh(new GeomAPI_Shape());
199   aGeomSh->setImpl(new TopoDS_Shape(aResult));
200   this->setShape(aGeomSh);
201   this->setDone(true);
202 }
203
204 //==================================================================================================
205 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
206                              const ListOfShape& theLocations,
207                              const GeomShapePtr thePathShape)
208 {
209   if(theBaseShapes.empty() ||
210      (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
211     return;
212   }
213
214   // Getting base shape and path.
215   TopoDS_Shape aBaseShape;
216   TopAbs_ShapeEnum aBaseShapeType;
217   TopoDS_Wire aPathWire;
218   if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
219       !getPath(aPathWire, thePathShape)) {
220     return;
221   }
222
223   TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
224   gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
225
226   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
227   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
228   aPathWire.Move(aTrsf);
229   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
230   if (!anOldPath->isSame(aNewPath))
231     addMovedPath(anOldPath, aNewPath);
232
233   // Get locations after moving path shape.
234   std::list<TopoDS_Vertex> aLocations;
235   for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
236        aLocIt != theLocations.cend();
237        ++aLocIt)
238   {
239     GeomShapePtr aLocation = *aLocIt;
240     if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
241       return;
242     }
243
244     TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
245     TopoDS_Vertex aMovedVertex;
246     for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
247       if (anExp.Current().IsPartner(aLocationVertex)) {
248         aMovedVertex = TopoDS::Vertex(anExp.Current());
249         aLocations.push_back(aMovedVertex);
250         break;
251       }
252     }
253     if (aMovedVertex.IsNull()) {
254       return;
255     }
256   }
257
258   if (theLocations.size() != aLocations.size()) {
259     return;
260   }
261
262   bool aHasLocations = !aLocations.empty();
263
264   // Making pipe.
265   Standard_Boolean isDone = Standard_False;
266   bool anIsSolidNeeded = false;
267   BRepOffsetAPI_MakePipeShell* aPipeBuilder;
268   for(int i = 0; i < 2; ++i) {
269     aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
270     if(!aPipeBuilder) {
271       return;
272     }
273     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
274     std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
275     while(aBaseIt != theBaseShapes.cend()) {
276       GeomShapePtr aBase = *aBaseIt;
277       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
278         delete aPipeBuilder;
279         return;
280       }
281       ++aBaseIt;
282       if(aBaseShapeType == TopAbs_FACE) {
283         anIsSolidNeeded = true;
284       }
285
286       if(aHasLocations) {
287         aPipeBuilder->Add(aBaseShape, *aLocationsIt);
288         ++aLocationsIt;
289       } else {
290         aPipeBuilder->Add(aBaseShape);
291       }
292     }
293
294     if(aPipeBuilder->IsReady() == Standard_False) {
295       delete aPipeBuilder;
296       return;
297     }
298
299     if (i == 1) {
300        // Try to use Descrete Trihedron mode.
301       aPipeBuilder->SetDiscreteMode();
302     }
303     aPipeBuilder->Build();
304     isDone = aPipeBuilder->IsDone();
305
306     if (isDone) {
307       break;
308     }
309
310     delete aPipeBuilder;
311   }
312
313   if (!isDone) {
314     return;
315   }
316
317   this->initialize(aPipeBuilder);
318
319   // Checking result.
320   if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
321     return;
322   }
323   TopoDS_Shape aResult = aPipeBuilder->Shape();
324
325   // Setting naming.
326   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
327   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
328   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
329   fixOrientation(aFromShape);
330   fixOrientation(aToShape);
331   this->addFromShape(aFromShape);
332   this->addToShape(aToShape);
333
334   // Setting result.
335   if(aResult.IsNull()) {
336     return;
337   }
338   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
339   GeomShapePtr aGeomSh(new GeomAPI_Shape());
340   aGeomSh->setImpl(new TopoDS_Shape(aResult));
341   this->setShape(aGeomSh);
342   this->setDone(true);
343 }
344
345 //==================================================================================================
346 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
347                                  ListOfShape& theHistory)
348 {
349   if (myMovedPath.isBound(theShape))
350     GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
351   else
352     GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
353 }
354
355 // Auxilary functions:
356 //==================================================================================================
357 bool getBase(TopoDS_Shape& theBaseOut,
358              TopAbs_ShapeEnum& theBaseTypeOut,
359              const GeomShapePtr theBaseShape)
360 {
361   if(!theBaseShape.get()) {
362     return false;
363   }
364
365   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
366   if(theBaseOut.IsNull()) {
367     return false;
368   }
369   theBaseTypeOut = theBaseOut.ShapeType();
370   if(theBaseTypeOut == TopAbs_VERTEX) {
371     // Do nothing.
372   } else if(theBaseTypeOut == TopAbs_EDGE) {
373     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
374   } else if(theBaseTypeOut == TopAbs_WIRE) {
375     // Do nothing.
376   } else if(theBaseTypeOut == TopAbs_FACE) {
377     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
378     theBaseOut = anExp.Current();
379   } else {
380     return false;
381   }
382
383   return true;
384 }
385
386 //==================================================================================================
387 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
388 {
389   if(!thePathShape.get()) {
390     return false;
391   }
392
393   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
394   if(aPathShape.IsNull()) {
395     return false;
396   }
397   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
398   if(aPathShapeType == TopAbs_EDGE) {
399     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
400     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
401   } else if(aPathShapeType == TopAbs_WIRE) {
402     thePathOut = TopoDS::Wire(aPathShape);
403   } else {
404     return false;
405   }
406
407   return true;
408 }
409
410 //==================================================================================================
411 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
412 {
413   gp_Trsf aTranslation;
414
415   BRepExtrema_DistShapeShape aDist(theBase, thePath);
416   aDist.Perform();
417   if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
418     gp_Pnt aPntBase = aDist.PointOnShape1(1);
419     gp_Pnt aPntPath = aDist.PointOnShape2(1);
420     aTranslation.SetTranslation(aPntPath, aPntBase);
421   }
422
423   return aTranslation;
424 }
425
426 //==================================================================================================
427 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
428 {
429   thePipeBuilder->Build();
430
431   Standard_Boolean isDone = thePipeBuilder->IsDone();
432
433   if (!isDone) {
434     // Try to use Descrete Trihedron mode.
435     thePipeBuilder->SetDiscreteMode();
436     thePipeBuilder->Build();
437     isDone = thePipeBuilder->IsDone();
438   }
439
440   return isDone == Standard_True;
441 }
442
443 //==================================================================================================
444 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
445 {
446   ListOfShape aList;
447
448   TopAbs_ShapeEnum aType = theShape.ShapeType();
449   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
450     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
451       GeomShapePtr aGeomShape(new GeomAPI_Shape());
452       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
453       aList.push_back(aGeomShape);
454     }
455   } else {
456     GeomShapePtr aGeomShape(new GeomAPI_Shape());
457     aGeomShape->setImpl(new TopoDS_Shape(theShape));
458     aList.push_back(aGeomShape);
459   }
460
461   return aList;
462 }
463
464 //==================================================================================================
465 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
466 {
467   myMovedPath.clear();
468   GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
469   GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
470   for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
471     myMovedPath.bind(anOldExp.current(), aNewExp.current());
472   }
473 }