Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
index 236f8e944d0efdc87925cc54a9a697170d8d55bb..210a8c4b04957695652e32c7cd64d9c7944e115f 100644 (file)
@@ -27,6 +27,7 @@
 #include <GeomAPI_Lin.h>
 
 #include <BRep_Tool.hxx>
+#include <BRepExtrema_DistShapeShape.hxx>
 #include <BRepOffsetAPI_MakePipe.hxx>
 #include <BRepOffsetAPI_MakePipeShell.hxx>
 #include <BRepBuilderAPI_MakeWire.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 GeomShapePtr theBaseShape);
 static bool getPath(TopoDS_Wire& thePathOut,
                     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);
 
@@ -93,6 +97,7 @@ void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
   if(!getPath(aPathWire, thePathShape)) {
     return;
   }
+  aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
 
   // Making pipe.
   BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
@@ -139,6 +144,8 @@ void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
     return;
   }
 
+  aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
+
   // Getting Bi-Normal.
   if(!theBiNormal.get()) {
     return;
@@ -202,17 +209,53 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
     return;
   }
 
-  bool aHasLocations = false;
-  if(!theLocations.empty()) {
-    aHasLocations = true;
+  // Getting path.
+  TopoDS_Shape aBaseShape;
+  TopAbs_ShapeEnum aBaseShapeType;
+  if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front())) {
+    return;
   }
 
-  // Getting path.
   TopoDS_Wire aPathWire;
   if(!getPath(aPathWire, thePathShape)) {
     return;
   }
 
+  TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
+  gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
+  aPathWire.Move(aTrsf);
+
+  // 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;
@@ -223,11 +266,9 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
       return;
     }
     ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
-    ListOfShape::const_iterator aLocIt = theLocations.cbegin();
+    std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
     while(aBaseIt != theBaseShapes.cend()) {
       GeomShapePtr aBase = *aBaseIt;
-      TopoDS_Shape aBaseShape;
-      TopAbs_ShapeEnum aBaseShapeType;
       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
         delete aPipeBuilder;
         return;
@@ -238,14 +279,8 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
       }
 
       if(aHasLocations) {
-        GeomShapePtr aLocation = *aLocIt;
-        if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
-          delete aPipeBuilder;
-          return;
-        }
-        TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
-        ++aLocIt;
-        aPipeBuilder->Add(aBaseShape, aLocationVertex);
+        aPipeBuilder->Add(aBaseShape, *aLocationsIt);
+        ++aLocationsIt;
       } else {
         aPipeBuilder->Add(aBaseShape);
       }
@@ -288,6 +323,8 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
   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);
 
@@ -341,8 +378,7 @@ bool getBase(TopoDS_Shape& theBaseOut,
 }
 
 //==================================================================================================
-bool getPath(TopoDS_Wire& thePathOut,
-             const GeomShapePtr thePathShape)
+bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
 {
   if(!thePathShape.get()) {
     return false;
@@ -365,6 +401,22 @@ 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);
+    aTranslation.SetTranslation(aPntPath, aPntBase);
+  }
+
+  return aTranslation;
+}
+
 //==================================================================================================
 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
 {