Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
index 84a09ea1029e1ad2410ac0bbf9411c5a1fc16d55..c56515314428fcb761de0583dda9aba565db0717 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// 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
 //
 // 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
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "GeomAlgoAPI_Pipe.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>
@@ -97,7 +99,12 @@ void GeomAlgoAPI_Pipe::build(const GeomShapePtr 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);
@@ -107,10 +114,23 @@ void GeomAlgoAPI_Pipe::build(const GeomShapePtr 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.
@@ -131,25 +151,24 @@ 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;
   }
 
-  aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
+  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;
@@ -177,10 +196,8 @@ void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
   this->initialize(aPipeBuilder);
 
   // Checking result.
-  if(aBaseShapeType == TopAbs_FACE) {
-    if(aPipeBuilder->MakeSolid() == Standard_False) {
-      return;
-    }
+  if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
+    return;
   }
   TopoDS_Shape aResult = aPipeBuilder->Shape();
   if(aResult.IsNull()) {
@@ -209,25 +226,55 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
     return;
   }
 
-  bool aHasLocations = false;
-  if(!theLocations.empty()) {
-    aHasLocations = true;
-  }
-
-  // Getting path.
+  // Getting base shape and path.
   TopoDS_Shape aBaseShape;
   TopAbs_ShapeEnum aBaseShapeType;
-  if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front())) {
+  TopoDS_Wire aPathWire;
+  if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
+      !getPath(aPathWire, thePathShape)) {
     return;
   }
 
-  TopoDS_Wire aPathWire;
-  if(!getPath(aPathWire, thePathShape)) {
+  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;
   }
 
-  gp_Trsf aTrsf = getPathToBaseTranslation(aBaseShape, aPathWire);
-  aPathWire.Move(aTrsf);
+  bool aHasLocations = !aLocations.empty();
 
   // Making pipe.
   Standard_Boolean isDone = Standard_False;
@@ -239,7 +286,7 @@ 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;
       if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
@@ -252,15 +299,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>();
-        aLocationVertex.Move(aTrsf);
-        ++aLocIt;
-        aPipeBuilder->Add(aBaseShape, aLocationVertex);
+        aPipeBuilder->Add(aBaseShape, *aLocationsIt);
+        ++aLocationsIt;
       } else {
         aPipeBuilder->Add(aBaseShape);
       }
@@ -292,10 +332,8 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
   this->initialize(aPipeBuilder);
 
   // Checking result.
-  if(anIsSolidNeeded) {
-    if(aPipeBuilder->MakeSolid() == Standard_False) {
-      return;
-    }
+  if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
+    return;
   }
   TopoDS_Shape aResult = aPipeBuilder->Shape();
 
@@ -303,6 +341,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);
 
@@ -321,7 +361,10 @@ void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
                                  ListOfShape& theHistory)
 {
-  GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
+  if (myMovedPath.isBound(theShape))
+    GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
+  else
+    GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
 }
 
 // Auxilary functions:
@@ -389,6 +432,33 @@ gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape
   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);
   }
 
@@ -432,3 +502,14 @@ ListOfShape getListFromShape(const TopoDS_Shape& theShape)
 
   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());
+  }
+}