Salome HOME
bos #29098 Merge branch 'CR29098'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Pipe.cpp
index 067a249bd592d27e11cf924b8923f6dcee1b48d2..38599fd00cb7873ef36f5b4071059785b8d7aa39 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #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>
@@ -112,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.
@@ -417,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);
   }