Salome HOME
Clean Property panel before starting of a new operation.
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index 3caf619cac56abe93aec6f184246c39c315b0f8b..ae51d7b20a6d788ba94f9861169852ba8fe787e1 100644 (file)
@@ -9,11 +9,15 @@
 
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Dir.h>
+#include <GeomDataAPI_Point2D.h>
 
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_XYZ.h>
 
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_Line.h>
+
+#include <XGUI_ViewerPrs.h>
 
 #include <V3d_View.hxx>
 #include <gp_Pln.hxx>
@@ -28,7 +32,7 @@
 
 const double PRECISION_TOLERANCE = 0.000001;
 
-gp_Pnt PartSet_Tools::ConvertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
+gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
 {
   if (theView.IsNull())
     return gp_Pnt();
@@ -53,7 +57,7 @@ gp_Pnt PartSet_Tools::ConvertClickToPoint(QPoint thePoint, Handle(V3d_View) theV
   return ResultPoint;
 }
 
-void PartSet_Tools::ConvertTo2D(const gp_Pnt& thePoint, boost::shared_ptr<ModelAPI_Feature> theSketch,
+void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
                                 Handle(V3d_View) theView, double& theX, double& theY)
 {
   if (!theSketch)
@@ -99,7 +103,30 @@ void PartSet_Tools::ConvertTo2D(const gp_Pnt& thePoint, boost::shared_ptr<ModelA
   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
 }
 
-void PartSet_Tools::IntersectLines(double theX0, double theY0, double theX1, double theY1,
+void PartSet_Tools::convertTo3D(const double theX, const double theY,
+                                FeaturePtr theSketch,
+                                gp_Pnt& thePoint)
+{
+  if (!theSketch)
+    return;
+
+  boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
+
+  boost::shared_ptr<GeomDataAPI_Point> aC = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
+  boost::shared_ptr<GeomDataAPI_Dir> aX = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
+  boost::shared_ptr<GeomDataAPI_Dir> aY = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
+
+  boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
+    aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
+
+  boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
+  thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
+}
+
+void PartSet_Tools::intersectLines(double theX0, double theY0, double theX1, double theY1,
                                    double theX2, double theY2, double theX3, double theY3,
                                    double& theX, double& theY)
 {
@@ -119,11 +146,11 @@ void PartSet_Tools::IntersectLines(double theX0, double theY0, double theX1, dou
   //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
   Standard_Real aPrec = PRECISION_TOLERANCE;
   if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) {
-    ProjectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);    
+    projectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);    
   }
 }
 
-void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2, double theY2,
+void PartSet_Tools::projectPointOnLine(double theX1, double theY1, double theX2, double theY2,
                                        double thePointX, double thePointY, double& theX, double& theY)
 {
   theX = theY = 0;
@@ -139,3 +166,53 @@ void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2,
     theY = aPoint.Y();
   }
 }
+
+FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint,
+                                                   Handle_V3d_View theView,
+                                                   FeaturePtr theSketch,
+                                                   const std::list<XGUI_ViewerPrs>& theFeatures)
+{
+  double aX, anY;
+  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
+  PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
+
+  FeaturePtr aFeature;
+  std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
+
+  FeaturePtr aDeltaFeature;   
+  double aMinDelta = -1;
+  XGUI_ViewerPrs aPrs;
+  for (; anIt != aLast; anIt++) {
+    aPrs = *anIt;
+    if (!aPrs.feature())
+      continue;
+    double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
+    if (aMinDelta < 0 || aMinDelta > aDelta) {
+      aMinDelta = aDelta;
+      aDeltaFeature = aPrs.feature();
+    }
+  }
+  return aDeltaFeature;
+}
+
+double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
+                                      double theX, double theY)
+{
+  double aDelta = 0;
+  if (theFeature->getKind() != SKETCH_LINE_KIND)
+    return aDelta;
+
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
+
+  double aX, anY;
+  PartSet_Tools::projectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
+
+  aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));
+
+  return aDelta;
+}