X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPartSet%2FPartSet_Tools.cpp;h=2c7a93b069bab6d65eaaab70c0fd1612d4444d42;hb=1d7a043abfadf964bf38802e8adb5a4773fec900;hp=3caf619cac56abe93aec6f184246c39c315b0f8b;hpb=277db1718bb7a3226515e4cc2272d83b463b3434;p=modules%2Fshaper.git diff --git a/src/PartSet/PartSet_Tools.cpp b/src/PartSet/PartSet_Tools.cpp index 3caf619ca..2c7a93b06 100644 --- a/src/PartSet/PartSet_Tools.cpp +++ b/src/PartSet/PartSet_Tools.cpp @@ -9,11 +9,15 @@ #include #include +#include #include #include #include +#include + +#include #include #include @@ -139,3 +143,53 @@ void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2, theY = aPoint.Y(); } } + +boost::shared_ptr PartSet_Tools::NearestFeature(QPoint thePoint, + Handle_V3d_View theView, + boost::shared_ptr theSketch, + const std::list& theFeatures) +{ + double aX, anY; + gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, theView); + PartSet_Tools::ConvertTo2D(aPoint, theSketch, theView, aX, anY); + + boost::shared_ptr aFeature; + std::list::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end(); + + boost::shared_ptr 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(boost::shared_ptr theFeature, + double theX, double theY) +{ + double aDelta = 0; + if (theFeature->getKind() != "SketchLine") + return aDelta; + + boost::shared_ptr aData = theFeature->data(); + + boost::shared_ptr aPoint1 = + boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_START)); + boost::shared_ptr aPoint2 = + boost::dynamic_pointer_cast(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; +}