Salome HOME
Issue #2811: Update content of Object node on creation moment
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_PointBuilder.cpp
index 657a1ef68f848a6a54286170b5061d488a7c131a..ce7be932c4557724520697e7176ff191817a03d1 100644 (file)
@@ -14,7 +14,8 @@
 // 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<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
 //
 
 #include "GeomAlgoAPI_PointBuilder.h"
 #include <Geom_Line.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <GeomAPI_ExtremaCurveCurve.hxx>
+#include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <gp_Pln.hxx>
 #include <gp_Pnt.hxx>
 #include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
 #include <TopoDS_Vertex.hxx>
 
 //==================================================================================================
@@ -121,19 +124,54 @@ std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexOnEdge(
   return aVertex;
 }
 
+//==================================================================================================
+std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
+  const std::shared_ptr<GeomAPI_Vertex> theVertex,
+  const std::shared_ptr<GeomAPI_Edge> theEdge)
+{
+  std::shared_ptr<GeomAPI_Vertex> aVertex;
+
+  if (!theVertex.get() || !theEdge.get()) {
+    return aVertex;
+  }
+
+  std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
+  gp_Pnt aPnt(aProjPnt->x(), aProjPnt->y(), aProjPnt->z());
+
+  TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
+  double aFirstOnCurve, aLastOnCurve;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
+
+  if (aCurve.IsNull()) {
+    return aVertex;
+  }
+
+  GeomAPI_ProjectPointOnCurve aProjection(aPnt, aCurve);
+
+  if (aProjection.NbPoints() == 0) {
+    return aVertex;
+  }
+
+  gp_Pnt aNearestPoint = aProjection.NearestPoint();
+
+  aVertex.reset(new GeomAPI_Vertex(aNearestPoint.X(), aNearestPoint.Y(), aNearestPoint.Z()));
+
+  return aVertex;
+}
+
 //==================================================================================================
 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_PointBuilder::vertexByProjection(
     const std::shared_ptr<GeomAPI_Vertex> theVertex,
-    const std::shared_ptr<GeomAPI_Face> thePlane)
+    const std::shared_ptr<GeomAPI_Face> theFace)
 {
   std::shared_ptr<GeomAPI_Vertex> aVertex;
 
-  if(!theVertex.get() || !thePlane.get() || !thePlane->isPlanar()) {
+  if(!theVertex.get() || !theFace.get() || !theFace->isPlanar()) {
     return aVertex;
   }
 
   std::shared_ptr<GeomAPI_Pnt> aProjPnt = theVertex->point();
-  std::shared_ptr<GeomAPI_Pln> aProjPln = thePlane->getPlane();
+  std::shared_ptr<GeomAPI_Pln> aProjPln = theFace->getPlane();
 
   std::shared_ptr<GeomAPI_Pnt> aPnt = aProjPln->project(aProjPnt);