X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchPlugin%2FSketchPlugin_ConstraintAngle.cpp;h=c73ae7d175e339714dcab36fb25f1eeefc3deb8c;hb=c7a5ff20294ae8270bfb9120b8887f0c9959d658;hp=03c840b270a051812c87d0ca455c8427797d05e8;hpb=b39df335d5faba8a9d578f7d28f9105fd581b783;p=modules%2Fshaper.git diff --git a/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp b/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp index 03c840b27..c73ae7d17 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintAngle.cpp @@ -23,6 +23,9 @@ const double tolerance = 1.e-7; #define PI 3.1415926535897932 +/// \brief Calculate intersection point of two lines +static std::shared_ptr intersect(FeaturePtr theLine1, FeaturePtr theLine2); + SketchPlugin_ConstraintAngle::SketchPlugin_ConstraintAngle() { @@ -102,6 +105,22 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID) // coordinates are calculated according to the center of shapes intersection std::shared_ptr aFlyoutAttr = std::dynamic_pointer_cast(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + + std::shared_ptr aData = data(); + std::shared_ptr aPlane = SketchPlugin_Sketch::plane(sketch()); + FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); + FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); + + // Intersection of lines + std::shared_ptr anInter = intersect(aLineA, aLineB); + if (!anInter) + return; + + myFlyoutUpdate = true; + std::shared_ptr aFlyoutDir = aFlyoutAttr->pnt()->xy()->decreased(anInter->xy()); + if (aFlyoutDir->dot(aFlyoutDir) < tolerance * tolerance) + aFlyoutAttr->setValue(aFlyoutAttr->x() + tolerance, aFlyoutAttr->y()); + myFlyoutUpdate = false; } } @@ -114,6 +133,11 @@ double SketchPlugin_ConstraintAngle::calculateAngle() FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); + // Intersection of lines + std::shared_ptr anInter = intersect(aLineA, aLineB); + if (!anInter) + return anAngle; + // Start and end points of lines std::shared_ptr aPointA1 = std::dynamic_pointer_cast( aLineA->attribute(SketchPlugin_Line::START_ID())); @@ -129,25 +153,21 @@ double SketchPlugin_ConstraintAngle::calculateAngle() std::shared_ptr aEndA = aPointA2->pnt(); std::shared_ptr aStartB = aPointB1->pnt(); std::shared_ptr aEndB = aPointB2->pnt(); - if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance) - return anAngle; - // Lines and their intersection point - std::shared_ptr aLA(new GeomAPI_Lin2d(aStartA, aEndA)); - std::shared_ptr aLB(new GeomAPI_Lin2d(aStartB, aEndB)); - std::shared_ptr anInter = aLA->intersect(aLB); - if (!anInter) - return anAngle; + double aDist[2][2] = { + { anInter->distance(aStartA), anInter->distance(aEndA) }, + { anInter->distance(aStartB), anInter->distance(aEndB) } + }; // Directions of lines - if (anInter->distance(aEndA) < tolerance) + if (aDist[0][0] > aDist[0][1]) aEndA = aStartA; - if (anInter->distance(aEndB) < tolerance) + if (aDist[1][0] > aDist[1][1]) aEndB = aStartB; std::shared_ptr aDirA(new GeomAPI_Dir2d(aEndA->xy()->decreased(anInter->xy()))); std::shared_ptr aDirB(new GeomAPI_Dir2d(aEndB->xy()->decreased(anInter->xy()))); - anAngle = aDirA->angle(aDirB) * 180.0 / PI; + anAngle = fabs(aDirA->angle(aDirB)) * 180.0 / PI; return anAngle; } @@ -204,4 +224,32 @@ bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId) myFlyoutUpdate = false; return true; -} \ No newline at end of file +} + + +// =============== Auxiliary functions ================================== +std::shared_ptr intersect(FeaturePtr theLine1, FeaturePtr theLine2) +{ + // Start and end points of lines + std::shared_ptr aPointA1 = std::dynamic_pointer_cast( + theLine1->attribute(SketchPlugin_Line::START_ID())); + std::shared_ptr aPointA2 = std::dynamic_pointer_cast( + theLine1->attribute(SketchPlugin_Line::END_ID())); + + std::shared_ptr aPointB1 = std::dynamic_pointer_cast( + theLine2->attribute(SketchPlugin_Line::START_ID())); + std::shared_ptr aPointB2 = std::dynamic_pointer_cast( + theLine2->attribute(SketchPlugin_Line::END_ID())); + + std::shared_ptr aStartA = aPointA1->pnt(); + std::shared_ptr aEndA = aPointA2->pnt(); + std::shared_ptr aStartB = aPointB1->pnt(); + std::shared_ptr aEndB = aPointB2->pnt(); + if (aStartA->distance(aEndA) < tolerance || aStartB->distance(aEndB) < tolerance) + std::shared_ptr(); + + // Lines and their intersection point + std::shared_ptr aLA(new GeomAPI_Lin2d(aStartA, aEndA)); + std::shared_ptr aLB(new GeomAPI_Lin2d(aStartB, aEndB)); + return aLA->intersect(aLB); +}