Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
index 5e72160c5af8b0ce65abf43ac988ae4c9c455325..01c98124c438de5cc5fd096a8412f0a68d49526d 100644 (file)
 #include <TopoDS.hxx>
 #include <BRep_Builder.hxx>
 #include <BRep_Tool.hxx>
+#include <ElCLib.hxx>
+#include <GCPnts_UniformAbscissa.hxx>
 #include <Geom_Curve.hxx>
 #include <Geom_Line.hxx>
 #include <Geom_Circle.hxx>
 #include <Geom_Ellipse.hxx>
+#include <Geom_Plane.hxx>
+#include <GeomAPI_IntCS.hxx>
 #include <GeomAdaptor_Curve.hxx>
 #include <gp_Ax1.hxx>
 #include <gp_Pln.hxx>
 #include <gp_Elips.hxx>
+#include <TopExp.hxx>
 
 #include <GCPnts_AbscissaPoint.hxx>
 
@@ -212,16 +217,16 @@ bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
   double aInStart, aInEnd;
   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
 
+  // Check that end point parameters are the same
+  if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
+    return false;
+
   // Check that curves a the same type
   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
   GeomAdaptor_Curve aInAdaptor(aInCurve);
   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
     return false;
 
-  // Check that end point parameters are the same
-  if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
-    return false;
-
   // Check that end points are equal
   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
@@ -279,6 +284,39 @@ bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
   return inPlane;
 }
 
+void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
+                                      std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
+{
+  double aFirst, aLast;
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (!aCurve.IsNull()) {
+    double A, B, C, D;
+    thePlane->coefficients(A, B, C, D);
+    gp_Pln aPln(A, B, C, D);
+
+    Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
+    GeomAPI_IntCS aIntersect;
+    aIntersect.Perform(aCurve, aPlane);
+    if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
+      gp_Pnt aPnt;
+      for (int i = 1; i <= aIntersect.NbPoints(); i++) {
+        // check the parameter of intersection in the edge range
+        aIntersect.Parameters(i, A, B, C);
+        if (aCurve->IsPeriodic())
+          C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
+        if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
+          continue;
+
+        // obtain intersection point
+        aPnt = aIntersect.Point(i);
+        std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
+        theResult.push_back(aPntPtr);
+      }
+    }
+  }
+}
+
 double GeomAPI_Edge::length() const
 {
   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
@@ -308,3 +346,41 @@ bool GeomAPI_Edge::isDegenerated() const
     return false;
   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
 }
+
+void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
+{
+  TopoDS_Edge anEdge = impl<TopoDS_Edge>();
+  TopoDS_Vertex aVFirst, aVLast;
+  TopExp::Vertices(anEdge, aVFirst, aVLast);
+  BRep_Builder().UpdateVertex(aVFirst, theTolerance);
+}
+
+void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
+{
+  TopoDS_Edge anEdge = impl<TopoDS_Edge>();
+  TopoDS_Vertex aVFirst, aVLast;
+  TopExp::Vertices(anEdge, aVFirst, aVLast);
+  BRep_Builder().UpdateVertex(aVLast, theTolerance);
+}
+
+GeomPointPtr GeomAPI_Edge::middlePoint() const
+{
+  GeomPointPtr aMiddlePoint;
+
+  const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
+  if (anEdge.IsNull())
+    return aMiddlePoint;
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
+  if (aCurve.IsNull())
+    return aMiddlePoint;
+
+  static const int NB_POINTS = 3;
+  GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
+  GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
+  if (anAlgo.IsDone()) {
+    gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
+    aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
+  }
+  return aMiddlePoint;
+}