X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPartSet%2FPartSet_FeatureLinePrs.cpp;h=ac173167f1e4c0e50df38abc4cd98b5010ad31f7;hb=23b712002a59e78673d8fce54c24aa4a7ab9ee51;hp=5e67aaeeef6ade656ab6f1eb7a19e28fe453d626;hpb=fd30229f7f40d6cc60670a4d9367324431964961;p=modules%2Fshaper.git diff --git a/src/PartSet/PartSet_FeatureLinePrs.cpp b/src/PartSet/PartSet_FeatureLinePrs.cpp index 5e67aaeee..ac173167f 100644 --- a/src/PartSet/PartSet_FeatureLinePrs.cpp +++ b/src/PartSet/PartSet_FeatureLinePrs.cpp @@ -12,6 +12,8 @@ #include #include +#include +#include #include #include @@ -19,6 +21,7 @@ #include #include +#include using namespace std; @@ -27,22 +30,9 @@ PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch) { } -void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature) +std::string PartSet_FeatureLinePrs::getKind() { - if (feature() && theFeature) - { - // use the last point of the previous feature as the first of the new one - boost::shared_ptr aData = theFeature->data(); - boost::shared_ptr anInitPoint = boost::dynamic_pointer_cast - (aData->attribute(LINE_ATTR_END)); - PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START); - PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END); - - aData = feature()->data(); - boost::shared_ptr aPoint = boost::dynamic_pointer_cast - (aData->attribute(LINE_ATTR_START)); - PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint); - } + return SKETCH_LINE_KIND; } PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY, @@ -68,6 +58,27 @@ PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY, return aMode; } +PartSet_SelectionMode PartSet_FeatureLinePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode) +{ + PartSet_SelectionMode aMode = theMode; + if (feature() && theFeature && theMode == SM_FirstPoint) + { + // use the last point of the previous feature as the first of the new one + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr anInitPoint = boost::dynamic_pointer_cast + (aData->attribute(LINE_ATTR_END)); + PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END); + + aData = feature()->data(); + boost::shared_ptr aPoint = boost::dynamic_pointer_cast + (aData->attribute(LINE_ATTR_START)); + PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint); + aMode = SM_SecondPoint; + } + return aMode; +} + std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const { std::string aAttribute; @@ -96,6 +107,111 @@ PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& the return aMode; } +void PartSet_FeatureLinePrs::projectPointOnLine(FeaturePtr theFeature, + const PartSet_SelectionMode& theMode, + const gp_Pnt& thePoint, Handle(V3d_View) theView, + double& theX, double& theY) +{ + if (theFeature && theFeature->getKind() == getKind()) { + double X0, X1; + double Y0, Y1; + + PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1); + boost::shared_ptr aPoint = boost::shared_ptr(new GeomAPI_Pnt2d(X1, Y1)); + boost::shared_ptr aFeatureLin = PartSet_FeatureLinePrs::createLin2d(theFeature); + + switch (theMode) { + case SM_FirstPoint: { + boost::shared_ptr aResult = aFeatureLin->project(aPoint); + theX = aResult->x(); + theY = aResult->y(); + } + break; + case SM_SecondPoint: { + getLinePoint(feature(), LINE_ATTR_START, X0, Y0); + boost::shared_ptr aCurrentLin = boost::shared_ptr + (new GeomAPI_Lin2d(X0, Y0, X1, Y1)); + boost::shared_ptr aResult = aFeatureLin->intersect(aCurrentLin); + boost::shared_ptr aPoint0 = boost::shared_ptr(new GeomAPI_Pnt2d(X0, Y0)); + if (aResult->distance(aPoint0) < Precision::Confusion()) { // the start point is nearest to the line + // if the first point of a line belongs to the given line + // we need to project the second point on the same line + aResult = aFeatureLin->project(aPoint); + } + theX = aResult->x(); + theY = aResult->y(); + } + break; + default: + break; + } + } +} + +double PartSet_FeatureLinePrs::distanceToPoint(FeaturePtr theFeature, + double theX, double theY) +{ + double aDelta = 0; + if (!theFeature || theFeature->getKind() != getKind()) + 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)); + + GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y()); + boost::shared_ptr aPoint = boost::shared_ptr(new GeomAPI_Pnt2d(theX, theY)); + + if (false/*projection*/) { // TODO: if it has not been necessary, remove this block + boost::shared_ptr aResult = aLin2d.project(aPoint); + aDelta = aResult->distance(aPoint); + } + else { // distance + aDelta = aLin2d.distance(aPoint); + } + + return aDelta; +} + +boost::shared_ptr PartSet_FeatureLinePrs::findPoint(FeaturePtr theFeature, + double theX, double theY) +{ + boost::shared_ptr aPoint2D; + if (!theFeature || theFeature->getKind() != getKind()) + return aPoint2D; + + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_START)); + if (fabs(aPoint->x() - theX) < Precision::Confusion() && + fabs(aPoint->y() - theY) < Precision::Confusion()) + aPoint2D = aPoint; + else { + aPoint = boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_END)); + if (fabs(aPoint->x() - theX) < Precision::Confusion() && + fabs(aPoint->y() - theY) < Precision::Confusion()) + aPoint2D = aPoint; + } + return aPoint2D; +} + +boost::shared_ptr PartSet_FeatureLinePrs::createLin2d(FeaturePtr theFeature) +{ + boost::shared_ptr aFeatureLin; + if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind()) + return aFeatureLin; + + double aStartX, aStartY, anEndX, anEndY; + getLinePoint(theFeature, LINE_ATTR_START, aStartX, aStartY); + getLinePoint(theFeature, LINE_ATTR_END, anEndX, anEndY); + + aFeatureLin = boost::shared_ptr + (new GeomAPI_Lin2d(aStartX, aStartY, anEndX, anEndY)); + return aFeatureLin; +} + boost::shared_ptr PartSet_FeatureLinePrs::featurePoint (const PartSet_SelectionMode& theMode) { @@ -116,3 +232,15 @@ boost::shared_ptr PartSet_FeatureLinePrs::featurePoint (aData->attribute(aPointArg)); return aPoint; } + +void PartSet_FeatureLinePrs::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute, + double& theX, double& theY) +{ + if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind()) + return; + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(aData->attribute(theAttribute)); + theX = aPoint->x(); + theY = aPoint->y(); +}