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