Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
index 7761a5b9d64085a01a5370002254edea84b4ab93..c56515314428fcb761de0583dda9aba565db0717 100644 (file)
@@ -1,65 +1,83 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_Pipe.cpp
-// Created:     16 March 2016
-// Author:      Dmitry Bobylev
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "GeomAlgoAPI_Pipe.h"
 
+#include "GeomAlgoAPI_DFLoader.h"
+
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
+#include <GeomAPI_ShapeExplorer.h>
 
 #include <BRep_Tool.hxx>
+#include <BRepExtrema_DistShapeShape.hxx>
 #include <BRepOffsetAPI_MakePipe.hxx>
 #include <BRepOffsetAPI_MakePipeShell.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
+#include <BOPAlgo_ArgumentAnalyzer.hxx>
 #include <Geom_Curve.hxx>
 #include <Geom_Line.hxx>
 #include <gp_Lin.hxx>
 #include <NCollection_List.hxx>
+#include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
+#include <Precision.hxx>
 
 static bool getBase(TopoDS_Shape& theBaseOut,
                     TopAbs_ShapeEnum& theBaseTypeOut,
-                    const std::shared_ptr<GeomAPI_Shape> theBaseShape);
+                    const GeomShapePtr theBaseShape);
 static bool getPath(TopoDS_Wire& thePathOut,
-                    const std::shared_ptr<GeomAPI_Shape> thePathShape);
+                    const GeomShapePtr thePathShape);
+static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
+                                        const TopoDS_Shape& thePath);
 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
+static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
 
-//=================================================================================================
-GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                                   const std::shared_ptr<GeomAPI_Shape> thePathShape)
-: /*myIsPipeShellUsed(false),*/
-  myBaseShape(theBaseShape),
-  myPathShape(thePathShape)
+//==================================================================================================
+GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
+                                   const GeomShapePtr thePathShape)
 {
   build(theBaseShape, thePathShape);
 }
 
-//=================================================================================================
-GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                                   const std::shared_ptr<GeomAPI_Shape> thePathShape,
-                                   const std::shared_ptr<GeomAPI_Shape> theBiNormal)
-//: myIsPipeShellUsed(true)
+//==================================================================================================
+GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
+                                   const GeomShapePtr thePathShape,
+                                   const GeomShapePtr theBiNormal)
 {
   build(theBaseShape, thePathShape, theBiNormal);
 }
 
-//=================================================================================================
+//==================================================================================================
 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
                                    const ListOfShape& theLocations,
-                                   const std::shared_ptr<GeomAPI_Shape> thePathShape)
-//: myIsPipeShellUsed(true)
+                                   const GeomShapePtr thePathShape)
 {
   build(theBaseShapes, theLocations, thePathShape);
 }
 
-//=================================================================================================
-void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                             const std::shared_ptr<GeomAPI_Shape> thePathShape)
+//==================================================================================================
+void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
+                             const GeomShapePtr thePathShape)
 {
   // Getting base shape.
   if(!theBaseShape.get()) {
@@ -72,7 +90,7 @@ void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
   TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
   if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
      aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
-     aBaseShapeType != TopAbs_SHELL) {
+     aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
     return;
   }
 
@@ -81,6 +99,12 @@ void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
   if(!getPath(aPathWire, thePathShape)) {
     return;
   }
+  GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
+  anOldPath->setImpl(new TopoDS_Shape(aPathWire));
+  aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
+  aNewPath->setImpl(new TopoDS_Shape(aPathWire));
+  if (!anOldPath->isSame(aNewPath))
+    addMovedPath(anOldPath, aNewPath);
 
   // Making pipe.
   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
@@ -90,49 +114,61 @@ void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
   aPipeBuilder->Build();
 
   // Checking result.
-  if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
+  if (!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
+    delete aPipeBuilder;
+    return;
+  }
+
+  // Check for self-interfering result
+  BOPAlgo_ArgumentAnalyzer aChecker;
+  aChecker.SetShape1(aPipeBuilder->Shape());
+  aChecker.SelfInterMode() = Standard_True;
+  aChecker.StopOnFirstFaulty() = Standard_True;
+  aChecker.Perform();
+  if (aChecker.HasFaulty()) {
+    myError = "Self-interfering result.";
     delete aPipeBuilder;
     return;
   }
+
   this->initialize(aPipeBuilder);
 
   // Setting naming.
-  std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
-  aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
-  aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
-  this->addFromShape(aFromShape);
-  this->addToShape(aToShape);
+  this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
+  this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
 
   // Setting result.
-  TopoDS_Shape aResultShape = aPipeBuilder->Shape();
-  std::shared_ptr<GeomAPI_Shape> aResultGeomShape(new GeomAPI_Shape());
-  aResultGeomShape->setImpl(new TopoDS_Shape(aResultShape));
-  this->setShape(aResultGeomShape);
+  TopoDS_Shape aResult = aPipeBuilder->Shape();
+  aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+  GeomShapePtr aGeomSh(new GeomAPI_Shape());
+  aGeomSh->setImpl(new TopoDS_Shape(aResult));
+  this->setShape(aGeomSh);
   this->setDone(true);
 }
 
-//=================================================================================================
-void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
-                             const std::shared_ptr<GeomAPI_Shape> thePathShape,
-                             const std::shared_ptr<GeomAPI_Shape> theBiNormal)
+//==================================================================================================
+void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
+                             const GeomShapePtr thePathShape,
+                             const GeomShapePtr theBiNormal)
 {
-  // Getting base shape.
+  // Getting base shape and path.
   TopoDS_Shape aBaseShape;
   TopAbs_ShapeEnum aBaseShapeType;
-  if(!getBase(aBaseShape, aBaseShapeType, theBaseShape)) {
-    return;
-  }
-
-  // Getting path.
   TopoDS_Wire aPathWire;
-  if(!getPath(aPathWire, thePathShape)) {
+  if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
+      !getPath(aPathWire, thePathShape) ||
+      !theBiNormal.get()) {
     return;
   }
 
+  GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
+  anOldPath->setImpl(new TopoDS_Shape(aPathWire));
+  aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
+  aNewPath->setImpl(new TopoDS_Shape(aPathWire));
+  if (!anOldPath->isSame(aNewPath))
+    addMovedPath(anOldPath, aNewPath);
+
   // Getting Bi-Normal.
-  if(!theBiNormal.get()) {
-    return;
-  }
   TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
   if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
     return;
@@ -160,168 +196,182 @@ void GeomAlgoAPI_Pipe::build(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
   this->initialize(aPipeBuilder);
 
   // Checking result.
-  if(aBaseShapeType == TopAbs_FACE) {
-    if(aPipeBuilder->MakeSolid() == Standard_False) {
-      return;
-    }
+  if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
+    return;
   }
-  if(aPipeBuilder->Shape().IsNull()) {
+  TopoDS_Shape aResult = aPipeBuilder->Shape();
+  if(aResult.IsNull()) {
     return;
   }
 
   // Setting naming.
-  std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
-  aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
-  aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
-  this->addFromShape(aFromShape);
-  this->addToShape(aToShape);
+  this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
+  this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
 
   // Setting result.
-  TopoDS_Shape aResultShape = aPipeBuilder->Shape();
-  std::shared_ptr<GeomAPI_Shape> aResultGeomShape(new GeomAPI_Shape());
-  aResultGeomShape->setImpl(new TopoDS_Shape(aResultShape));
-  this->setShape(aResultGeomShape);
+  aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+  GeomShapePtr aGeomSh(new GeomAPI_Shape());
+  aGeomSh->setImpl(new TopoDS_Shape(aResult));
+  this->setShape(aGeomSh);
   this->setDone(true);
 }
 
-//=================================================================================================
+//==================================================================================================
 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
                              const ListOfShape& theLocations,
-                             const std::shared_ptr<GeomAPI_Shape> thePathShape)
+                             const GeomShapePtr thePathShape)
 {
-  if(theBaseShapes.empty() || (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
+  if(theBaseShapes.empty() ||
+     (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
     return;
   }
 
-  bool aHasLocations = false;
-  if(!theLocations.empty()) {
-    aHasLocations = true;
-  }
-
-  // Getting path.
+  // Getting base shape and path.
+  TopoDS_Shape aBaseShape;
+  TopAbs_ShapeEnum aBaseShapeType;
   TopoDS_Wire aPathWire;
-  if(!getPath(aPathWire, thePathShape)) {
+  if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
+      !getPath(aPathWire, thePathShape)) {
     return;
   }
 
-  // Making pipe.
-  BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
-  if(!aPipeBuilder) {
+  TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
+  gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
+
+  GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
+  anOldPath->setImpl(new TopoDS_Shape(aPathWire));
+  aPathWire.Move(aTrsf);
+  aNewPath->setImpl(new TopoDS_Shape(aPathWire));
+  if (!anOldPath->isSame(aNewPath))
+    addMovedPath(anOldPath, aNewPath);
+
+  // Get locations after moving path shape.
+  std::list<TopoDS_Vertex> aLocations;
+  for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
+       aLocIt != theLocations.cend();
+       ++aLocIt)
+  {
+    GeomShapePtr aLocation = *aLocIt;
+    if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
+      return;
+    }
+
+    TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
+    TopoDS_Vertex aMovedVertex;
+    for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
+      if (anExp.Current().IsPartner(aLocationVertex)) {
+        aMovedVertex = TopoDS::Vertex(anExp.Current());
+        aLocations.push_back(aMovedVertex);
+        break;
+      }
+    }
+    if (aMovedVertex.IsNull()) {
+      return;
+    }
+  }
+
+  if (theLocations.size() != aLocations.size()) {
     return;
   }
+
+  bool aHasLocations = !aLocations.empty();
+
+  // Making pipe.
+  Standard_Boolean isDone = Standard_False;
   bool anIsSolidNeeded = false;
-  ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
-  ListOfShape::const_iterator aLocIt = theLocations.cbegin();
-  while(aBaseIt != theBaseShapes.cend()) {
-    std::shared_ptr<GeomAPI_Shape> aBase = *aBaseIt;
-    TopoDS_Shape aBaseShape;
-    TopAbs_ShapeEnum aBaseShapeType;
-    if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
-      delete aPipeBuilder;
+  BRepOffsetAPI_MakePipeShell* aPipeBuilder;
+  for(int i = 0; i < 2; ++i) {
+    aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
+    if(!aPipeBuilder) {
       return;
     }
-    ++aBaseIt;
-    if(aBaseShapeType == TopAbs_FACE) {
-      anIsSolidNeeded = true;
-    }
-
-    if(aHasLocations) {
-      std::shared_ptr<GeomAPI_Shape> aLocation = *aLocIt;
-      if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
+    ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
+    std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
+    while(aBaseIt != theBaseShapes.cend()) {
+      GeomShapePtr aBase = *aBaseIt;
+      if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
         delete aPipeBuilder;
         return;
       }
-      TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
-      ++aLocIt;
-      aPipeBuilder->Add(aBaseShape, aLocationVertex);
-    } else {
-      aPipeBuilder->Add(aBaseShape);
+      ++aBaseIt;
+      if(aBaseShapeType == TopAbs_FACE) {
+        anIsSolidNeeded = true;
+      }
+
+      if(aHasLocations) {
+        aPipeBuilder->Add(aBaseShape, *aLocationsIt);
+        ++aLocationsIt;
+      } else {
+        aPipeBuilder->Add(aBaseShape);
+      }
+    }
+
+    if(aPipeBuilder->IsReady() == Standard_False) {
+      delete aPipeBuilder;
+      return;
+    }
+
+    if (i == 1) {
+       // Try to use Descrete Trihedron mode.
+      aPipeBuilder->SetDiscreteMode();
+    }
+    aPipeBuilder->Build();
+    isDone = aPipeBuilder->IsDone();
+
+    if (isDone) {
+      break;
     }
-  }
 
-  if(aPipeBuilder->IsReady() == Standard_False) {
     delete aPipeBuilder;
-    return;
   }
 
-  if(!buildPipe(aPipeBuilder)) {
-    delete aPipeBuilder;
+  if (!isDone) {
     return;
   }
+
   this->initialize(aPipeBuilder);
 
   // Checking result.
-  if(anIsSolidNeeded) {
-    if(aPipeBuilder->MakeSolid() == Standard_False) {
-      return;
-    }
-  }
-  if(aPipeBuilder->Shape().IsNull()) {
+  if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
     return;
   }
+  TopoDS_Shape aResult = aPipeBuilder->Shape();
 
   // Setting naming.
-  std::shared_ptr<GeomAPI_Shape> aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
+  GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
   aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
   aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
+  fixOrientation(aFromShape);
+  fixOrientation(aToShape);
   this->addFromShape(aFromShape);
   this->addToShape(aToShape);
 
   // Setting result.
-  TopoDS_Shape aResultShape = aPipeBuilder->Shape();
-  std::shared_ptr<GeomAPI_Shape> aResultGeomShape(new GeomAPI_Shape());
-  aResultGeomShape->setImpl(new TopoDS_Shape(aResultShape));
-  this->setShape(aResultGeomShape);
+  if(aResult.IsNull()) {
+    return;
+  }
+  aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+  GeomShapePtr aGeomSh(new GeomAPI_Shape());
+  aGeomSh->setImpl(new TopoDS_Shape(aResult));
+  this->setShape(aGeomSh);
   this->setDone(true);
 }
 
-//=================================================================================================
-void GeomAlgoAPI_Pipe::generated(const std::shared_ptr<GeomAPI_Shape> theShape,
+//==================================================================================================
+void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
                                  ListOfShape& theHistory)
 {
-  GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
-
-  //if(myIsPipeShellUsed) {
-  //  GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
-  //  return;
-  //}
-
-  //BRepOffsetAPI_MakePipe* aMakePipe = implPtr<BRepOffsetAPI_MakePipe>();
-  //const TopoDS_Shape& aProfile = theShape->impl<TopoDS_Shape>();
-  //const TopAbs_ShapeEnum aProfileShapeType = aProfile.ShapeType();
-  //if(aProfileShapeType != TopAbs_VERTEX && aProfileShapeType != TopAbs_EDGE) {
-  //  return;
-  //}
-  //const TopoDS_Shape& aBaseShape = myBaseShape->impl<TopoDS_Shape>();
-  //TopExp_Explorer anExp(aBaseShape, aProfileShapeType);
-  //Standard_Boolean ahasShape = Standard_False;
-  //for(; anExp.More(); anExp.Next()) {
-  //  if(anExp.Current().IsSame(aProfile)) {
-  //    ahasShape = Standard_True;
-  //    break;
-  //  }
-  //}
-  //if(!ahasShape) {
-  //  return;
-  //}
-  //TopExp_Explorer aShapeExplorer(myPathShape->impl<TopoDS_Shape>(), TopAbs_EDGE);
-  //for(; aShapeExplorer.More(); aShapeExplorer.Next ()) {
-  //  const TopoDS_Shape& aSpine = aShapeExplorer.Current();
-  //  const TopoDS_Shape& aGeneratedShape = aMakePipe->Generated(aSpine, aProfile);
-  //  if(aGeneratedShape.IsNull()) {
-  //    continue;
-  //  }
-  //  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
-  //  aShape->setImpl(new TopoDS_Shape(aGeneratedShape));
-  //  theHistory.push_back(aShape);
-  //}
+  if (myMovedPath.isBound(theShape))
+    GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
+  else
+    GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
 }
 
 // Auxilary functions:
-//=================================================================================================
+//==================================================================================================
 bool getBase(TopoDS_Shape& theBaseOut,
              TopAbs_ShapeEnum& theBaseTypeOut,
-             const std::shared_ptr<GeomAPI_Shape> theBaseShape)
+             const GeomShapePtr theBaseShape)
 {
   if(!theBaseShape.get()) {
     return false;
@@ -348,9 +398,8 @@ bool getBase(TopoDS_Shape& theBaseOut,
   return true;
 }
 
-//=================================================================================================
-bool getPath(TopoDS_Wire& thePathOut,
-             const std::shared_ptr<GeomAPI_Shape> thePathShape)
+//==================================================================================================
+bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
 {
   if(!thePathShape.get()) {
     return false;
@@ -373,7 +422,50 @@ bool getPath(TopoDS_Wire& thePathOut,
   return true;
 }
 
-//=================================================================================================
+//==================================================================================================
+gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
+{
+  gp_Trsf aTranslation;
+
+  BRepExtrema_DistShapeShape aDist(theBase, thePath);
+  aDist.Perform();
+  if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
+    gp_Pnt aPntBase = aDist.PointOnShape1(1);
+    gp_Pnt aPntPath = aDist.PointOnShape2(1);
+    // get the end point on the path nearest to the found point,
+    // because the path is almost usually started from the one of the sections
+    if (!BRep_Tool::IsClosed(thePath)) {
+      TopoDS_Wire aPathWire = TopoDS::Wire(thePath);
+      if (!aPathWire.IsNull()) {
+        TopoDS_Vertex aV1, aV2;
+        TopExp::Vertices(aPathWire, aV1, aV2);
+
+        gp_Pnt aStart = BRep_Tool::Pnt(aV1);
+        gp_Pnt anEnd = BRep_Tool::Pnt(aV2);
+
+        // compare 3 distances
+        double aDistToStart = aPntBase.Distance(aStart);
+        double aDistToEnd = aPntBase.Distance(anEnd);
+        double aMinDist = aPntBase.Distance(aPntPath);
+
+        static const double THE_THRESHOLD = 0.01; // threshold for distance ratio
+        double aDeltaStart = Abs(aDistToStart - aMinDist);
+        double aDeltaEnd = Abs(aDistToEnd - aMinDist);
+        if (aDeltaStart < THE_THRESHOLD * aDeltaEnd)
+          aPntPath = aStart;
+        else if (aDeltaEnd < THE_THRESHOLD * aDeltaStart)
+          aPntPath = anEnd;
+        else
+          aPntPath = aPntBase; // no translation
+      }
+    }
+    aTranslation.SetTranslation(aPntPath, aPntBase);
+  }
+
+  return aTranslation;
+}
+
+//==================================================================================================
 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
 {
   thePipeBuilder->Build();
@@ -388,4 +480,36 @@ bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
   }
 
   return isDone == Standard_True;
-}
\ No newline at end of file
+}
+
+//==================================================================================================
+ListOfShape getListFromShape(const TopoDS_Shape& theShape)
+{
+  ListOfShape aList;
+
+  TopAbs_ShapeEnum aType = theShape.ShapeType();
+  if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
+    for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
+      GeomShapePtr aGeomShape(new GeomAPI_Shape());
+      aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
+      aList.push_back(aGeomShape);
+    }
+  } else {
+    GeomShapePtr aGeomShape(new GeomAPI_Shape());
+    aGeomShape->setImpl(new TopoDS_Shape(theShape));
+    aList.push_back(aGeomShape);
+  }
+
+  return aList;
+}
+
+//==================================================================================================
+void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
+{
+  myMovedPath.clear();
+  GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
+  GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
+  for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
+    myMovedPath.bind(anOldExp.current(), aNewExp.current());
+  }
+}