Salome HOME
Issue #1467: pipe problem.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
index fb5f6e8b8016dbb7f76821cce0e8d193b9f9d7df..84a09ea1029e1ad2410ac0bbf9411c5a1fc16d55 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(aBaseShape, aPathWire));
+
   // Getting Bi-Normal.
   if(!theBiNormal.get()) {
     return;
@@ -208,55 +215,80 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
   }
 
   // Getting path.
+  TopoDS_Shape aBaseShape;
+  TopAbs_ShapeEnum aBaseShapeType;
+  if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front())) {
+    return;
+  }
+
   TopoDS_Wire aPathWire;
   if(!getPath(aPathWire, thePathShape)) {
     return;
   }
 
+  gp_Trsf aTrsf = getPathToBaseTranslation(aBaseShape, aPathWire);
+  aPathWire.Move(aTrsf);
+
   // Making pipe.
-  BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
-  if(!aPipeBuilder) {
-    return;
-  }
+  Standard_Boolean isDone = Standard_False;
   bool anIsSolidNeeded = false;
-  ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
-  ListOfShape::const_iterator aLocIt = theLocations.cbegin();
-  while(aBaseIt != theBaseShapes.cend()) {
-    GeomShapePtr 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) {
-      GeomShapePtr aLocation = *aLocIt;
-      if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
+    ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
+    ListOfShape::const_iterator aLocIt = theLocations.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) {
+        GeomShapePtr aLocation = *aLocIt;
+        if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
+          delete aPipeBuilder;
+          return;
+        }
+        TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
+        aLocationVertex.Move(aTrsf);
+        ++aLocIt;
+        aPipeBuilder->Add(aBaseShape, aLocationVertex);
+      } 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.
@@ -324,8 +356,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;
@@ -348,6 +379,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)
 {