From 0e158cb76c7797d04c2090d9283c3fb907d8b4bb Mon Sep 17 00:00:00 2001 From: vsv Date: Fri, 21 Aug 2015 17:30:40 +0300 Subject: [PATCH] Icon for angle constraint is added, execute method is reviewed for working with pre-selection --- src/PartSet/PartSet_icons.qrc | 1 + src/PartSet/icons/angle_constr.png | Bin 0 -> 476 bytes .../SketchPlugin_ConstraintAngle.cpp | 79 +++++++++++++++--- .../SketchPlugin_ConstraintAngle.h | 5 ++ src/SketchPlugin/plugin-Sketch.xml | 40 +++++---- 5 files changed, 92 insertions(+), 33 deletions(-) create mode 100644 src/PartSet/icons/angle_constr.png diff --git a/src/PartSet/PartSet_icons.qrc b/src/PartSet/PartSet_icons.qrc index 2e7928ace..c8e47dcd1 100644 --- a/src/PartSet/PartSet_icons.qrc +++ b/src/PartSet/PartSet_icons.qrc @@ -60,5 +60,6 @@ icons/movement.png icons/extrusion_cut.png icons/extrusion_fuse.png + icons/angle_constr.png diff --git a/src/PartSet/icons/angle_constr.png b/src/PartSet/icons/angle_constr.png new file mode 100644 index 0000000000000000000000000000000000000000..1c403b8132635cb31879cbb60f474d5604a60ac9 GIT binary patch literal 476 zcmV<20VDp2P)N2bZe?^J zG%heMHvEiZ-v9ssT1iAfR5(wClfOy?F%ZU6SXhajV0Dc_1bqU9tF6RN8!bGGMZrfn zJGsvOiYpZD?giwu6D%wg1QEo-La;ba@$Yet-)tty?3%;k7m}IzzGNpGLqq{%=?uJr ztvr^QuFj_@E-du`9WJlzL;M0pbUsC#Sejrl=tKAkI^Y4cz@!#w;>6Of7@X%ZK0coy zGuz-6tom9_oLIUPgL)|NJ;=-yxJa4AiKQ aData = data(); - AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast( - aData->attribute(SketchPlugin_Constraint::VALUE())); - if(anAttrValue->isInitialized()) + std::shared_ptr anAttrA = aData->refattr(SketchPlugin_Constraint::ENTITY_A()); + std::shared_ptr anAttrB = aData->refattr(SketchPlugin_Constraint::ENTITY_B()); + if (!anAttrA->isInitialized() || !anAttrB->isInitialized()) return; - double anAngle = calculateAngle(); - anAttrValue->setValue(anAngle); + AttributeDoublePtr anAttrValue = std::dynamic_pointer_cast( + aData->attribute(SketchPlugin_Constraint::VALUE())); + + if (!anAttrValue->isInitialized()) { + double anAngle = calculateAngle(); + anAttrValue->setValue(anAngle); + } + // the value should to be computed here, not in the getAISObject in order to change the model value + // inside the object transaction. This is important for creating a constraint by preselection. + // The display of the presentation in this case happens after the transaction commit + std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + if(!aFlyOutAttr->isInitialized()) + compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()); } AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious) @@ -67,16 +79,16 @@ AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID) { - if (theID == SketchPlugin_Constraint::ENTITY_A() || - theID == SketchPlugin_Constraint::ENTITY_B()) { - std::shared_ptr aData = data(); - if (!aData) - return; + std::shared_ptr aData = data(); + if (!aData) + return; FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A()); FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B()); - if (!aLineA || !aLineB) - return; + if (!aLineA || !aLineB) + return; + if (theID == SketchPlugin_Constraint::ENTITY_A() || + theID == SketchPlugin_Constraint::ENTITY_B()) { std::shared_ptr aValueAttr = std::dynamic_pointer_cast< ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE())); if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value @@ -85,6 +97,7 @@ void SketchPlugin_ConstraintAngle::attributeChanged(const std::string& theID) } } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) { // Recalculate flyout point in local coordinates + // coordinates are calculated according to the center of shapes intersection std::shared_ptr aFlyoutAttr = std::dynamic_pointer_cast(attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); } @@ -148,3 +161,45 @@ void SketchPlugin_ConstraintAngle::move(double theDeltaX, double theDeltaY) aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY); myFlyoutUpdate = false; } + + +bool SketchPlugin_ConstraintAngle::compute(const std::string& theAttributeId) +{ + if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT()) + return false; + if (!sketch()) + return false; + + std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(attribute(theAttributeId)); + if (fabs(aFlyOutAttr->x()) >= tolerance || fabs(aFlyOutAttr->y()) >= tolerance) + return false; + + DataPtr 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()); + + if ((aLineA.get() == NULL) || (aLineB.get() == NULL)) + return false; + + // Start and end points of lines + std::shared_ptr aPointA1 = std::dynamic_pointer_cast( + aLineA->attribute(SketchPlugin_Line::START_ID())); + std::shared_ptr aPointA2 = std::dynamic_pointer_cast( + aLineA->attribute(SketchPlugin_Line::END_ID())); + + std::shared_ptr aStartA = aPointA1->pnt(); + std::shared_ptr aEndA = aPointA2->pnt(); + if (aStartA->distance(aEndA) < tolerance) + return false; + + myFlyoutUpdate = true; + double aX = (aStartA->x() + aEndA->x()) / 2.; + double aY = (aStartA->y() + aEndA->y()) / 2.; + + aFlyOutAttr->setValue(aX, aY); + myFlyoutUpdate = false; + + return true; +} \ No newline at end of file diff --git a/src/SketchPlugin/SketchPlugin_ConstraintAngle.h b/src/SketchPlugin/SketchPlugin_ConstraintAngle.h index 29f37f82f..ef3c478be 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintAngle.h +++ b/src/SketchPlugin/SketchPlugin_ConstraintAngle.h @@ -37,6 +37,11 @@ class SketchPlugin_ConstraintAngle : public SketchPlugin_ConstraintBase /// \brief Creates a new part document if needed SKETCHPLUGIN_EXPORT virtual void execute(); + /// Computes the attribute value on the base of other attributes if the value can be computed + /// \param theAttributeId an attribute index to be computed + /// \return a boolean value about it is computed + SKETCHPLUGIN_EXPORT virtual bool compute(const std::string& theAttributeId); + /// \brief Request for initialization of data model of the feature: adding all attributes SKETCHPLUGIN_EXPORT virtual void initAttributes(); diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 8418520a8..8468ea0eb 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -101,8 +101,25 @@ - - + + + + + + + + + + + + + + + + + + + @@ -219,25 +236,6 @@ - - - - - - - - - - - - - - - - - - - -- 2.39.2