Salome HOME
4d2e186fa172670201cf4734a560d0e8b9f272c3
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
1 // Copyright (C) 2014-2023  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 <BOPAlgo_ArgumentAnalyzer.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_Line.hxx>
37 #include <gp_Lin.hxx>
38 #include <NCollection_List.hxx>
39 #include <TopExp.hxx>
40 #include <TopExp_Explorer.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <Precision.hxx>
44
45 static bool getBase(TopoDS_Shape& theBaseOut,
46                     TopAbs_ShapeEnum& theBaseTypeOut,
47                     const GeomShapePtr theBaseShape);
48 static bool getPath(TopoDS_Wire& thePathOut,
49                     const GeomShapePtr thePathShape);
50 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
51                                         const TopoDS_Shape& thePath);
52 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
53 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
54
55 //==================================================================================================
56 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
57                                    const GeomShapePtr thePathShape)
58 {
59   build(theBaseShape, thePathShape);
60 }
61
62 //==================================================================================================
63 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
64                                    const GeomShapePtr thePathShape,
65                                    const GeomShapePtr theBiNormal)
66 {
67   build(theBaseShape, thePathShape, theBiNormal);
68 }
69
70 //==================================================================================================
71 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
72                                    const ListOfShape& theLocations,
73                                    const GeomShapePtr thePathShape)
74 {
75   build(theBaseShapes, theLocations, thePathShape);
76 }
77
78 //==================================================================================================
79 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
80                              const GeomShapePtr thePathShape)
81 {
82   // Getting base shape.
83   if(!theBaseShape.get()) {
84     return;
85   }
86   TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
87   if(aBaseShape.IsNull()) {
88     return;
89   }
90   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
91   if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
92      aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
93      aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
94     return;
95   }
96
97   // Getting path.
98   TopoDS_Wire aPathWire;
99   if(!getPath(aPathWire, thePathShape)) {
100     return;
101   }
102   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
103   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
104   aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
105   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
106   if (!anOldPath->isSame(aNewPath))
107     addMovedPath(anOldPath, aNewPath);
108
109   // Making pipe.
110   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
111   if(!aPipeBuilder) {
112     return;
113   }
114   aPipeBuilder->Build();
115
116   // Checking result.
117   if (!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
118     delete aPipeBuilder;
119     return;
120   }
121
122   // Check for self-interfering result
123   BOPAlgo_ArgumentAnalyzer aChecker;
124   aChecker.SetShape1(aPipeBuilder->Shape());
125   aChecker.SelfInterMode() = Standard_True;
126   aChecker.StopOnFirstFaulty() = Standard_True;
127   aChecker.Perform();
128   if (aChecker.HasFaulty()) {
129     myError = "Self-interfering result.";
130     delete aPipeBuilder;
131     return;
132   }
133
134   this->initialize(aPipeBuilder);
135
136   // Setting naming.
137   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
138   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
139
140   // Setting result.
141   TopoDS_Shape aResult = aPipeBuilder->Shape();
142   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
143   GeomShapePtr aGeomSh(new GeomAPI_Shape());
144   aGeomSh->setImpl(new TopoDS_Shape(aResult));
145   this->setShape(aGeomSh);
146   this->setDone(true);
147 }
148
149 //==================================================================================================
150 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
151                              const GeomShapePtr thePathShape,
152                              const GeomShapePtr theBiNormal)
153 {
154   // Getting base shape and path.
155   TopoDS_Shape aBaseShape;
156   TopAbs_ShapeEnum aBaseShapeType;
157   TopoDS_Wire aPathWire;
158   if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
159       !getPath(aPathWire, thePathShape) ||
160       !theBiNormal.get()) {
161     return;
162   }
163
164   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
165   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
166   aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
167   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
168   if (!anOldPath->isSame(aNewPath))
169     addMovedPath(anOldPath, aNewPath);
170
171   // Getting Bi-Normal.
172   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
173   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
174     return;
175   }
176   TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
177   Standard_Real aFirst, aLast;
178   Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
179   Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
180   if(aBiNormalLine.IsNull()) {
181     return;
182   }
183   gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
184
185   // Making pipe.
186   BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
187   if(!aPipeBuilder) {
188     return;
189   }
190   aPipeBuilder->Add(aBaseShape);
191   aPipeBuilder->SetMode(aBiNormalDir);
192   if(!buildPipe(aPipeBuilder)) {
193     delete aPipeBuilder;
194     return;
195   }
196   this->initialize(aPipeBuilder);
197
198   // Checking result.
199   if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
200     return;
201   }
202   TopoDS_Shape aResult = aPipeBuilder->Shape();
203   if(aResult.IsNull()) {
204     return;
205   }
206
207   // Setting naming.
208   this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
209   this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
210
211   // Setting result.
212   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
213   GeomShapePtr aGeomSh(new GeomAPI_Shape());
214   aGeomSh->setImpl(new TopoDS_Shape(aResult));
215   this->setShape(aGeomSh);
216   this->setDone(true);
217 }
218
219 //==================================================================================================
220 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
221                              const ListOfShape& theLocations,
222                              const GeomShapePtr thePathShape)
223 {
224   if(theBaseShapes.empty() ||
225      (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
226     return;
227   }
228
229   // Getting base shape and path.
230   TopoDS_Shape aBaseShape;
231   TopAbs_ShapeEnum aBaseShapeType;
232   TopoDS_Wire aPathWire;
233   if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
234       !getPath(aPathWire, thePathShape)) {
235     return;
236   }
237
238   TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
239   gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
240
241   GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
242   anOldPath->setImpl(new TopoDS_Shape(aPathWire));
243   aPathWire.Move(aTrsf);
244   aNewPath->setImpl(new TopoDS_Shape(aPathWire));
245   if (!anOldPath->isSame(aNewPath))
246     addMovedPath(anOldPath, aNewPath);
247
248   // Get locations after moving path shape.
249   std::list<TopoDS_Vertex> aLocations;
250   for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
251        aLocIt != theLocations.cend();
252        ++aLocIt)
253   {
254     GeomShapePtr aLocation = *aLocIt;
255     if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
256       return;
257     }
258
259     TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
260     TopoDS_Vertex aMovedVertex;
261     for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
262       if (anExp.Current().IsPartner(aLocationVertex)) {
263         aMovedVertex = TopoDS::Vertex(anExp.Current());
264         aLocations.push_back(aMovedVertex);
265         break;
266       }
267     }
268     if (aMovedVertex.IsNull()) {
269       return;
270     }
271   }
272
273   if (theLocations.size() != aLocations.size()) {
274     return;
275   }
276
277   bool aHasLocations = !aLocations.empty();
278
279   // Making pipe.
280   Standard_Boolean isDone = Standard_False;
281   bool anIsSolidNeeded = false;
282   BRepOffsetAPI_MakePipeShell* aPipeBuilder;
283   for(int i = 0; i < 2; ++i) {
284     aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
285     if(!aPipeBuilder) {
286       return;
287     }
288     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
289     std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
290     while(aBaseIt != theBaseShapes.cend()) {
291       GeomShapePtr aBase = *aBaseIt;
292       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
293         delete aPipeBuilder;
294         return;
295       }
296       ++aBaseIt;
297       if(aBaseShapeType == TopAbs_FACE) {
298         anIsSolidNeeded = true;
299       }
300
301       if(aHasLocations) {
302         aPipeBuilder->Add(aBaseShape, *aLocationsIt);
303         ++aLocationsIt;
304       } else {
305         aPipeBuilder->Add(aBaseShape);
306       }
307     }
308
309     if(aPipeBuilder->IsReady() == Standard_False) {
310       delete aPipeBuilder;
311       return;
312     }
313
314     if (i == 1) {
315        // Try to use Descrete Trihedron mode.
316       aPipeBuilder->SetDiscreteMode();
317     }
318     aPipeBuilder->Build();
319     isDone = aPipeBuilder->IsDone();
320
321     if (isDone) {
322       break;
323     }
324
325     delete aPipeBuilder;
326   }
327
328   if (!isDone) {
329     return;
330   }
331
332   this->initialize(aPipeBuilder);
333
334   // Checking result.
335   if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
336     return;
337   }
338   TopoDS_Shape aResult = aPipeBuilder->Shape();
339
340   // Setting naming.
341   GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
342   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
343   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
344   fixOrientation(aFromShape);
345   fixOrientation(aToShape);
346   this->addFromShape(aFromShape);
347   this->addToShape(aToShape);
348
349   // Setting result.
350   if(aResult.IsNull()) {
351     return;
352   }
353   aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
354   GeomShapePtr aGeomSh(new GeomAPI_Shape());
355   aGeomSh->setImpl(new TopoDS_Shape(aResult));
356   this->setShape(aGeomSh);
357   this->setDone(true);
358 }
359
360 //==================================================================================================
361 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
362                                  ListOfShape& theHistory)
363 {
364   if (myMovedPath.isBound(theShape))
365     GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
366   else
367     GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
368 }
369
370 // Auxilary functions:
371 //==================================================================================================
372 bool getBase(TopoDS_Shape& theBaseOut,
373              TopAbs_ShapeEnum& theBaseTypeOut,
374              const GeomShapePtr theBaseShape)
375 {
376   if(!theBaseShape.get()) {
377     return false;
378   }
379
380   theBaseOut = theBaseShape->impl<TopoDS_Shape>();
381   if(theBaseOut.IsNull()) {
382     return false;
383   }
384   theBaseTypeOut = theBaseOut.ShapeType();
385   if(theBaseTypeOut == TopAbs_VERTEX) {
386     // Do nothing.
387   } else if(theBaseTypeOut == TopAbs_EDGE) {
388     theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
389   } else if(theBaseTypeOut == TopAbs_WIRE) {
390     // Do nothing.
391   } else if(theBaseTypeOut == TopAbs_FACE) {
392     TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
393     theBaseOut = anExp.Current();
394   } else {
395     return false;
396   }
397
398   return true;
399 }
400
401 //==================================================================================================
402 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
403 {
404   if(!thePathShape.get()) {
405     return false;
406   }
407
408   TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
409   if(aPathShape.IsNull()) {
410     return false;
411   }
412   TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
413   if(aPathShapeType == TopAbs_EDGE) {
414     TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
415     thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
416   } else if(aPathShapeType == TopAbs_WIRE) {
417     thePathOut = TopoDS::Wire(aPathShape);
418   } else {
419     return false;
420   }
421
422   return true;
423 }
424
425 //==================================================================================================
426 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
427 {
428   gp_Trsf aTranslation;
429
430   BRepExtrema_DistShapeShape aDist(theBase, thePath);
431   aDist.Perform();
432   if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
433     gp_Pnt aPntBase = aDist.PointOnShape1(1);
434     gp_Pnt aPntPath = aDist.PointOnShape2(1);
435     // get the end point on the path nearest to the found point,
436     // because the path is almost usually started from the one of the sections
437     if (!BRep_Tool::IsClosed(thePath)) {
438       TopoDS_Wire aPathWire = TopoDS::Wire(thePath);
439       if (!aPathWire.IsNull()) {
440         TopoDS_Vertex aV1, aV2;
441         TopExp::Vertices(aPathWire, aV1, aV2);
442
443         gp_Pnt aStart = BRep_Tool::Pnt(aV1);
444         gp_Pnt anEnd = BRep_Tool::Pnt(aV2);
445
446         // compare 3 distances
447         double aDistToStart = aPntBase.Distance(aStart);
448         double aDistToEnd = aPntBase.Distance(anEnd);
449         double aMinDist = aPntBase.Distance(aPntPath);
450
451         static const double THE_THRESHOLD = 0.01; // threshold for distance ratio
452         double aDeltaStart = Abs(aDistToStart - aMinDist);
453         double aDeltaEnd = Abs(aDistToEnd - aMinDist);
454         if (aDeltaStart < THE_THRESHOLD * aDeltaEnd)
455           aPntPath = aStart;
456         else if (aDeltaEnd < THE_THRESHOLD * aDeltaStart)
457           aPntPath = anEnd;
458         else
459           aPntPath = aPntBase; // no translation
460       }
461     }
462     aTranslation.SetTranslation(aPntPath, aPntBase);
463   }
464
465   return aTranslation;
466 }
467
468 //==================================================================================================
469 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
470 {
471   thePipeBuilder->Build();
472
473   Standard_Boolean isDone = thePipeBuilder->IsDone();
474
475   if (!isDone) {
476     // Try to use Descrete Trihedron mode.
477     thePipeBuilder->SetDiscreteMode();
478     thePipeBuilder->Build();
479     isDone = thePipeBuilder->IsDone();
480   }
481
482   return isDone == Standard_True;
483 }
484
485 //==================================================================================================
486 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
487 {
488   ListOfShape aList;
489
490   TopAbs_ShapeEnum aType = theShape.ShapeType();
491   if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
492     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
493       GeomShapePtr aGeomShape(new GeomAPI_Shape());
494       aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
495       aList.push_back(aGeomShape);
496     }
497   } else {
498     GeomShapePtr aGeomShape(new GeomAPI_Shape());
499     aGeomShape->setImpl(new TopoDS_Shape(theShape));
500     aList.push_back(aGeomShape);
501   }
502
503   return aList;
504 }
505
506 //==================================================================================================
507 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
508 {
509   myMovedPath.clear();
510   GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
511   GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
512   for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
513     myMovedPath.bind(anOldExp.current(), aNewExp.current());
514   }
515 }