From d4a163b94d27eddb09d6bbe1cec96c9c25042282 Mon Sep 17 00:00:00 2001 From: mpv Date: Thu, 27 Nov 2014 15:12:36 +0300 Subject: [PATCH] Values of constraint (length, distance, radius) now initialized on reference definition --- src/Model/Model_Data.cpp | 3 +- src/ModelAPI/ModelAPI_Attribute.h | 20 +++- src/ModelAPI/ModelAPI_Object.h | 3 +- src/SketchPlugin/SketchPlugin_Circle.cpp | 25 +++-- src/SketchPlugin/SketchPlugin_Circle.h | 2 +- .../SketchPlugin_ConstraintDistance.cpp | 22 +++-- .../SketchPlugin_ConstraintDistance.h | 4 + .../SketchPlugin_ConstraintLength.cpp | 55 ++++++++--- .../SketchPlugin_ConstraintLength.h | 13 +++ .../SketchPlugin_ConstraintRadius.cpp | 82 ++++++++++------ .../SketchPlugin_ConstraintRadius.h | 9 ++ src/SketchPlugin/SketchPlugin_Line.cpp | 27 +++-- src/SketchPlugin/SketchPlugin_Line.h | 2 +- src/SketchPlugin/SketchPlugin_Sketch.cpp | 98 ++++++++----------- src/SketchPlugin/SketchPlugin_Sketch.h | 2 +- src/XGUI/XGUI_PropertyPanel.cpp | 1 + 16 files changed, 223 insertions(+), 145 deletions(-) diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 5dcc138de..0ae7a75aa 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -100,6 +100,7 @@ void Model_Data::addAttribute(const std::string& theID, const std::string theAtt if (anAttr) { myAttrs[theID] = std::shared_ptr(anAttr); anAttr->setObject(myObject); + anAttr->setID(theID); } else { Events_Error::send("Can not create unknown type of attribute " + theAttrType); } @@ -195,7 +196,7 @@ void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr) static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED); ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent); if (myObject) { - myObject->attributeChanged(); + myObject->attributeChanged(theAttr->id()); } } } diff --git a/src/ModelAPI/ModelAPI_Attribute.h b/src/ModelAPI/ModelAPI_Attribute.h index 27d65eb9b..041e27b7c 100644 --- a/src/ModelAPI/ModelAPI_Attribute.h +++ b/src/ModelAPI/ModelAPI_Attribute.h @@ -19,11 +19,12 @@ class ModelAPI_Attribute { ///< needed here to emit signal that feature changed on change of the attribute std::shared_ptr myObject; + std::string myID; ///< identifier of this attribute in Data class protected: // accessible from the attributes - bool myIsInitialized; - bool myIsArgument; - bool myIsImmutable; + bool myIsInitialized; ///< is some value assigned to this attribute + bool myIsArgument; ///< is this attribute used as an argument for execution + bool myIsImmutable; ///< is this attribute can be changed programmatically (e.g. by constraint) public: @@ -88,6 +89,12 @@ class ModelAPI_Attribute return myIsImmutable; } + /// ID of the attribute in Data + MODELAPI_EXPORT const std::string& id() const + { + return myID; + } + protected: /// Objects are created for features automatically ModelAPI_Attribute() @@ -97,6 +104,13 @@ class ModelAPI_Attribute myIsImmutable = false; } + /// Sets the ID of the attribute in Data (called from Data) + MODELAPI_EXPORT void setID(const std::string theID) + { + myID = theID; + } + + friend class Model_Data; }; //! Pointer on attribute object diff --git a/src/ModelAPI/ModelAPI_Object.h b/src/ModelAPI/ModelAPI_Object.h index be4880540..3c8e6a6e8 100644 --- a/src/ModelAPI/ModelAPI_Object.h +++ b/src/ModelAPI/ModelAPI_Object.h @@ -55,7 +55,8 @@ class ModelAPI_Object virtual std::string groupName() = 0; /// Called on change of any argument-attribute of this object - MODELAPI_EXPORT virtual void attributeChanged() + /// \param theID identifier of changed attribute + MODELAPI_EXPORT virtual void attributeChanged(const std::string& theID) {} /// To use virtuality for destructors diff --git a/src/SketchPlugin/SketchPlugin_Circle.cpp b/src/SketchPlugin/SketchPlugin_Circle.cpp index 209f9744a..eeb77874d 100644 --- a/src/SketchPlugin/SketchPlugin_Circle.cpp +++ b/src/SketchPlugin/SketchPlugin_Circle.cpp @@ -100,18 +100,17 @@ bool SketchPlugin_Circle::isFixed() { return data()->selection(EXTERNAL_ID())->context(); } -void SketchPlugin_Circle::attributeChanged() { - static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change - std::shared_ptr aSelection = data()->selection(EXTERNAL_ID())->value(); - // update arguments due to the selection value - if (aSelection && !aSelection->isNull() && aSelection->isEdge() && !myIsUpdated) { - myIsUpdated = true; - std::shared_ptr anEdge( new GeomAPI_Edge(aSelection)); - std::shared_ptr aCirc = anEdge->circle(); - std::shared_ptr aCenterAttr = - std::dynamic_pointer_cast(attribute(CENTER_ID())); - aCenterAttr->setValue(sketch()->to2D(aCirc->center())); - real(RADIUS_ID())->setValue(aCirc->radius()); - myIsUpdated = false; +void SketchPlugin_Circle::attributeChanged(const std::string& theID) { + if (theID == EXTERNAL_ID()) { + std::shared_ptr aSelection = data()->selection(EXTERNAL_ID())->value(); + // update arguments due to the selection value + if (aSelection && !aSelection->isNull() && aSelection->isEdge()) { + std::shared_ptr anEdge( new GeomAPI_Edge(aSelection)); + std::shared_ptr aCirc = anEdge->circle(); + std::shared_ptr aCenterAttr = + std::dynamic_pointer_cast(attribute(CENTER_ID())); + aCenterAttr->setValue(sketch()->to2D(aCirc->center())); + real(RADIUS_ID())->setValue(aCirc->radius()); + } } } diff --git a/src/SketchPlugin/SketchPlugin_Circle.h b/src/SketchPlugin/SketchPlugin_Circle.h index 8ceef34eb..08263db8c 100644 --- a/src/SketchPlugin/SketchPlugin_Circle.h +++ b/src/SketchPlugin/SketchPlugin_Circle.h @@ -77,7 +77,7 @@ class SketchPlugin_Circle : public SketchPlugin_Feature //, public GeomAPI_IPre virtual double distanceToPoint(const std::shared_ptr& thePoint); /// Called on change of any argument-attribute of this object - SKETCHPLUGIN_EXPORT virtual void attributeChanged(); + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); /// Use plugin manager for features creation SketchPlugin_Circle(); diff --git a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp index 97d41154c..092d3c74c 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp @@ -101,18 +101,12 @@ AISObjectPtr SketchPlugin_ConstraintDistance::getAISObject(AISObjectPtr thePrevi // value calculation std::shared_ptr aValueAttr = std::dynamic_pointer_cast< ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE())); - // TODO: has to be calculated on definition of reference object - double aValue; + double aValue = 0; // Issue #196: checking the positivity of the distance constraint // there is a validator for a distance constraint, that the value should be positive // in case if an invalid value is set, the current distance value is shown if (aValueAttr->isInitialized()) aValue = aValueAttr->value(); - else { - aValue = calculateCurrentDistance(); - aValueAttr->setValue(aValue); - } - // End TODO AISObjectPtr anAIS = thePrevious; if (!anAIS) @@ -168,6 +162,20 @@ double SketchPlugin_ConstraintDistance::calculateCurrentDistance() const return aDistance; } +void SketchPlugin_ConstraintDistance::attributeChanged(const std::string& theID) { + 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 + double aDistance = calculateCurrentDistance(); + if (aDistance > 0) { // set as value the length of updated references + aValueAttr->setValue(aDistance); + } + } + } +} //************************************************************************************* std::shared_ptr getFeaturePoint(DataPtr theData, diff --git a/src/SketchPlugin/SketchPlugin_ConstraintDistance.h b/src/SketchPlugin/SketchPlugin_ConstraintDistance.h index 8ac80c313..ef58b2453 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintDistance.h +++ b/src/SketchPlugin/SketchPlugin_ConstraintDistance.h @@ -56,6 +56,10 @@ class SketchPlugin_ConstraintDistance : public SketchPlugin_ConstraintBase /// \param theDeltaY the delta for Y coordinate is moved SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY); + /// Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + /// Returns the current distance between the feature attributes double calculateCurrentDistance() const; diff --git a/src/SketchPlugin/SketchPlugin_ConstraintLength.cpp b/src/SketchPlugin/SketchPlugin_ConstraintLength.cpp index 5799a3a12..b5431fddf 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintLength.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintLength.cpp @@ -51,32 +51,41 @@ void SketchPlugin_ConstraintLength::execute() } } -AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious) +bool SketchPlugin_ConstraintLength::getPoints( + std::shared_ptr& thePoint1, std::shared_ptr& thePoint2, + std::shared_ptr& theStartPoint, + std::shared_ptr& theEndPoint) { if (!sketch()) - return thePrevious; - + return false; std::shared_ptr aPlane = sketch()->plane(); - std::shared_ptr anAttr = std::dynamic_pointer_cast< ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A())); if (!anAttr) - return thePrevious; + return false; FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object()); if (!aFeature || aFeature->getKind() != SketchPlugin_Line::ID()) - return thePrevious; - - std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< - GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); - + return false; DataPtr aData = aFeature->data(); - std::shared_ptr aStartPoint = std::dynamic_pointer_cast< + theStartPoint = std::dynamic_pointer_cast< GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID())); - std::shared_ptr anEndPoint = std::dynamic_pointer_cast< + theEndPoint = std::dynamic_pointer_cast< GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID())); + thePoint1 = sketch()->to3D(theStartPoint->x(), theStartPoint->y()); + thePoint2 = sketch()->to3D(theEndPoint->x(), theEndPoint->y()); + return true; +} - std::shared_ptr aPoint1 = sketch()->to3D(aStartPoint->x(), aStartPoint->y()); - std::shared_ptr aPoint2 = sketch()->to3D(anEndPoint->x(), anEndPoint->y()); +AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePrevious) +{ + std::shared_ptr aPoint1, aPoint2; + std::shared_ptr aStartPoint, anEndPoint; + if (!getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) + return thePrevious; // not possible to show length because points are not defined + + std::shared_ptr aPlane = sketch()->plane(); + std::shared_ptr aFlyOutAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); std::shared_ptr aFlyoutPnt = std::shared_ptr(); if (aFlyOutAttr->isInitialized()) { aFlyoutPnt = sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()); @@ -106,7 +115,8 @@ AISObjectPtr SketchPlugin_ConstraintLength::getAISObject(AISObjectPtr thePreviou anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue); // Set color from preferences - std::vector aRGB = Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR); + std::vector aRGB = + Config_PropManager::color("Visualization", "length_color", LENGTH_COLOR); anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]); return anAIS; } @@ -122,3 +132,18 @@ void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY) aPoint->move(theDeltaX, theDeltaY); } +void SketchPlugin_ConstraintLength::attributeChanged(const std::string& theID) { + if (theID == SketchPlugin_Constraint::ENTITY_A()) + { + 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 + std::shared_ptr aPoint1, aPoint2; + std::shared_ptr aStartPoint, anEndPoint; + if (getPoints(aPoint1, aPoint2, aStartPoint, anEndPoint)) { + double aLength = aPoint1->distance(aPoint2); + aValueAttr->setValue(aLength); + } + } + } +} diff --git a/src/SketchPlugin/SketchPlugin_ConstraintLength.h b/src/SketchPlugin/SketchPlugin_ConstraintLength.h index 5f3ae9aae..9cdd7e153 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintLength.h +++ b/src/SketchPlugin/SketchPlugin_ConstraintLength.h @@ -10,6 +10,8 @@ #include #include +class GeomDataAPI_Point2D; + #define LENGTH_COLOR "#ff00ff" /** \class SketchPlugin_ConstraintLength @@ -50,8 +52,19 @@ class SketchPlugin_ConstraintLength : public SketchPlugin_ConstraintBase /// \param theDeltaY the delta for Y coordinate is moved SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY); + /// Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + /// \brief Use plugin manager for features creation SketchPlugin_ConstraintLength(); + +private: + // retrns the points-base of length, returns false if it is not possible + bool getPoints( + std::shared_ptr& thePoint1, std::shared_ptr& thePoint2, + std::shared_ptr& theStartPoint, + std::shared_ptr& theEndPoint); }; #endif diff --git a/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp b/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp index 3aa6cba13..77fff3184 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp +++ b/src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp @@ -61,39 +61,58 @@ void SketchPlugin_ConstraintRadius::execute() } } -AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious) +double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr& theCirc) { + static const double kErrorResult = -1.; if (!sketch()) - return thePrevious; + return kErrorResult; std::shared_ptr aData = data(); std::shared_ptr anAttr = std::dynamic_pointer_cast< ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A())); if (!anAttr) - return thePrevious; - FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object()); - std::string aKind = aFeature ? aFeature->getKind() : ""; + return kErrorResult; + theCirc = ModelAPI_Feature::feature(anAttr->object()); + std::string aKind = theCirc ? theCirc->getKind() : ""; if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID()) - return thePrevious; + return kErrorResult; + + DataPtr aCircData = theCirc->data(); + std::shared_ptr aCenterAttr; + if (aKind == SketchPlugin_Circle::ID()) { + AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast( + aCircData->attribute(SketchPlugin_Circle::RADIUS_ID())); + return aCircRadius->value(); + } else { + aCenterAttr = std::dynamic_pointer_cast( + aCircData->attribute(SketchPlugin_Arc::CENTER_ID())); + std::shared_ptr aStartAttr = std::dynamic_pointer_cast< + GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID())); + return aCenterAttr->pnt()->distance(aStartAttr->pnt()); + } + return kErrorResult; +} + +AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious) +{ + std::shared_ptr aCyrcFeature; + double aRadius = circleRadius(aCyrcFeature); + if (aRadius < 0) + return thePrevious; // can not create a good presentation // Flyout point std::shared_ptr aFlyoutAttr = std::dynamic_pointer_cast< - GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); + GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT())); std::shared_ptr aFlyoutPnt; if (aFlyoutAttr->isInitialized()) { aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y()); } // Prepare a circle - std::shared_ptr aCyrcData = aFeature->data(); std::shared_ptr aCenterAttr; - double aRadius; - if (aKind == SketchPlugin_Circle::ID()) { + if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle aCenterAttr = std::dynamic_pointer_cast( - aCyrcData->attribute(SketchPlugin_Circle::CENTER_ID())); - AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast( - aCyrcData->attribute(SketchPlugin_Circle::RADIUS_ID())); - aRadius = aCircRadius->value(); + aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID())); if (!aFlyoutPnt) { double aShift = aRadius * 1.1; std::shared_ptr aPnt = aCenterAttr->pnt(); @@ -102,15 +121,12 @@ AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePreviou aFlyoutAttr->setValue(aFPnt); aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y()); } - } else if (aKind == SketchPlugin_Arc::ID()) { + } else { // arc aCenterAttr = std::dynamic_pointer_cast( - aCyrcData->attribute(SketchPlugin_Arc::CENTER_ID())); - std::shared_ptr aStartAttr = std::dynamic_pointer_cast< - GeomDataAPI_Point2D>(aCyrcData->attribute(SketchPlugin_Arc::START_ID())); - aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt()); + aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID())); if (!aFlyoutPnt) { std::shared_ptr aStartAttr = std::dynamic_pointer_cast< - GeomDataAPI_Point2D>(aCyrcData->attribute(SketchPlugin_Arc::START_ID())); + GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID())); aFlyoutAttr->setValue(aStartAttr->pnt()); aFlyoutPnt = sketch()->to3D(aStartAttr->pnt()->x(), aStartAttr->pnt()->y()); } @@ -123,20 +139,14 @@ AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePreviou std::shared_ptr aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius)); // Value - // TODO: has to be calculated on definition of reference object std::shared_ptr aValueAttr = std::dynamic_pointer_cast< - ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE())); - double aValue = aRadius; + ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE())); if (aValueAttr->isInitialized()) - aValue = aValueAttr->value(); - else { - aValueAttr->setValue(aValue); - } - // End TODO + aRadius = aValueAttr->value(); AISObjectPtr anAIS = thePrevious; if (!anAIS) anAIS = AISObjectPtr(new GeomAPI_AISObject); - anAIS->createRadius(aCircle, aFlyoutPnt, aValue); + anAIS->createRadius(aCircle, aFlyoutPnt, aRadius); // Set color from preferences std::vector aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR); @@ -181,3 +191,17 @@ void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY) aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y()); } + +void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) { + if (theID == SketchPlugin_Constraint::ENTITY_A()) { + 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 + std::shared_ptr aCyrcFeature; + double aRadius = circleRadius(aCyrcFeature); + if (aRadius > 0) { // set as value the radius of updated reference to another circle + aValueAttr->setValue(aRadius); + } + } + } +} diff --git a/src/SketchPlugin/SketchPlugin_ConstraintRadius.h b/src/SketchPlugin/SketchPlugin_ConstraintRadius.h index 96e295e1b..7aa5ec6fb 100644 --- a/src/SketchPlugin/SketchPlugin_ConstraintRadius.h +++ b/src/SketchPlugin/SketchPlugin_ConstraintRadius.h @@ -49,8 +49,17 @@ class SketchPlugin_ConstraintRadius : public SketchPlugin_ConstraintBase /// \param theDeltaY the delta for Y coordinate is moved SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY); + /// Called on change of any argument-attribute of this object + /// \param theID identifier of changed attribute + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + /// \brief Use plugin manager for features creation SketchPlugin_ConstraintRadius(); + +private: + /// Checks and gets the radius of referenced circle (or arc) otherwise returns -1. + /// \param theCircData the found referenced circle returned by this method + double circleRadius(std::shared_ptr& theCirc); }; #endif diff --git a/src/SketchPlugin/SketchPlugin_Line.cpp b/src/SketchPlugin/SketchPlugin_Line.cpp index 2cba979ce..83ee21659 100644 --- a/src/SketchPlugin/SketchPlugin_Line.cpp +++ b/src/SketchPlugin/SketchPlugin_Line.cpp @@ -99,19 +99,18 @@ bool SketchPlugin_Line::isFixed() { return data()->selection(EXTERNAL_ID())->context(); } -void SketchPlugin_Line::attributeChanged() { - static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change - std::shared_ptr aSelection = data()->selection(EXTERNAL_ID())->value(); - // update arguments due to the selection value - if (aSelection && !aSelection->isNull() && aSelection->isEdge() && !myIsUpdated) { - myIsUpdated = true; - std::shared_ptr anEdge( new GeomAPI_Edge(aSelection)); - std::shared_ptr aStartAttr = - std::dynamic_pointer_cast(attribute(START_ID())); - aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint())); - std::shared_ptr anEndAttr = - std::dynamic_pointer_cast(attribute(END_ID())); - anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint())); - myIsUpdated = false; +void SketchPlugin_Line::attributeChanged(const std::string& theID) { + if (theID == EXTERNAL_ID()) { + std::shared_ptr aSelection = data()->selection(EXTERNAL_ID())->value(); + // update arguments due to the selection value + if (aSelection && !aSelection->isNull() && aSelection->isEdge()) { + std::shared_ptr anEdge( new GeomAPI_Edge(aSelection)); + std::shared_ptr aStartAttr = + std::dynamic_pointer_cast(attribute(START_ID())); + aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint())); + std::shared_ptr anEndAttr = + std::dynamic_pointer_cast(attribute(END_ID())); + anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint())); + } } } diff --git a/src/SketchPlugin/SketchPlugin_Line.h b/src/SketchPlugin/SketchPlugin_Line.h index a9e3e606c..850d17e4a 100644 --- a/src/SketchPlugin/SketchPlugin_Line.h +++ b/src/SketchPlugin/SketchPlugin_Line.h @@ -68,7 +68,7 @@ class SketchPlugin_Line : public SketchPlugin_Feature virtual double distanceToPoint(const std::shared_ptr& thePoint); /// Called on change of any argument-attribute of this object - SKETCHPLUGIN_EXPORT virtual void attributeChanged(); + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); /// Use plugin manager for features creation SketchPlugin_Line(); diff --git a/src/SketchPlugin/SketchPlugin_Sketch.cpp b/src/SketchPlugin/SketchPlugin_Sketch.cpp index 213d38502..1793ff16a 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.cpp +++ b/src/SketchPlugin/SketchPlugin_Sketch.cpp @@ -271,66 +271,46 @@ void SketchPlugin_Sketch::erase() ModelAPI_CompositeFeature::erase(); } -void SketchPlugin_Sketch::attributeChanged() { - static bool kIsUpdated = false; // to avoid infinitive cycle on attrubtes change - static bool kIsAttrChanged = false; - std::shared_ptr aSelection = - data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value(); - if (aSelection && !kIsUpdated) { // update arguments due to the selection value - kIsUpdated = true; - // update the sketch plane - std::shared_ptr aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection); - if (aPlane) { - double anA, aB, aC, aD; - aPlane->coefficients(anA, aB, aC, aD); - - // calculate attributes of the sketch - std::shared_ptr aNormDir(new GeomAPI_Dir(anA, aB, aC)); - std::shared_ptr aCoords = aNormDir->xyz(); - std::shared_ptr aZero(new GeomAPI_XYZ(0, 0, 0)); - aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero)); - std::shared_ptr anOrigPnt(new GeomAPI_Pnt(aCoords)); - // X axis is preferable to be dirX on the sketch - static const double tol = 1.e-7; - bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol; - std::shared_ptr aTempDir( - isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0)); - std::shared_ptr aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir))); - std::shared_ptr aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir))); - - kIsAttrChanged = false; // track the attributes were really changed during the plane update - std::shared_ptr anOrigin = std::dynamic_pointer_cast - (data()->attribute(SketchPlugin_Sketch::ORIGIN_ID())); - anOrigin->setValue(anOrigPnt); - std::shared_ptr aNormal = std::dynamic_pointer_cast( - data()->attribute(SketchPlugin_Sketch::NORM_ID())); - aNormal->setValue(aNormDir); - std::shared_ptr aDirX = std::dynamic_pointer_cast( - data()->attribute(SketchPlugin_Sketch::DIRX_ID())); - aDirX->setValue(aXDir); - std::shared_ptr aDirY = std::dynamic_pointer_cast( - data()->attribute(SketchPlugin_Sketch::DIRY_ID())); - aDirY->setValue(aYDir); - std::shared_ptr aDir = aPlane->direction(); - - if (kIsAttrChanged) { - /* now it is in updater - // the plane was changed, so reexecute sub-elements to update shapes (located in new plane) - ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators(); - list aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list(); - for(list::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) { - std::shared_ptr aFeature = - std::dynamic_pointer_cast(*aSubIt); - if (aFeature && aFactory->validate(aFeature)) { - aFeature->execute(); - } - } - */ - kIsAttrChanged = false; +void SketchPlugin_Sketch::attributeChanged(const std::string& theID) { + if (theID == SketchPlugin_Feature::EXTERNAL_ID()) { + std::shared_ptr aSelection = + data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value(); + if (aSelection) { // update arguments due to the selection value + // update the sketch plane + std::shared_ptr aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection); + if (aPlane) { + double anA, aB, aC, aD; + aPlane->coefficients(anA, aB, aC, aD); + + // calculate attributes of the sketch + std::shared_ptr aNormDir(new GeomAPI_Dir(anA, aB, aC)); + std::shared_ptr aCoords = aNormDir->xyz(); + std::shared_ptr aZero(new GeomAPI_XYZ(0, 0, 0)); + aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero)); + std::shared_ptr anOrigPnt(new GeomAPI_Pnt(aCoords)); + // X axis is preferable to be dirX on the sketch + static const double tol = 1.e-7; + bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol; + std::shared_ptr aTempDir( + isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0)); + std::shared_ptr aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir))); + std::shared_ptr aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir))); + + // update position of the sketch + std::shared_ptr anOrigin = std::dynamic_pointer_cast + (data()->attribute(SketchPlugin_Sketch::ORIGIN_ID())); + anOrigin->setValue(anOrigPnt); + std::shared_ptr aNormal = std::dynamic_pointer_cast( + data()->attribute(SketchPlugin_Sketch::NORM_ID())); + aNormal->setValue(aNormDir); + std::shared_ptr aDirX = std::dynamic_pointer_cast( + data()->attribute(SketchPlugin_Sketch::DIRX_ID())); + aDirX->setValue(aXDir); + std::shared_ptr aDirY = std::dynamic_pointer_cast( + data()->attribute(SketchPlugin_Sketch::DIRY_ID())); + aDirY->setValue(aYDir); + std::shared_ptr aDir = aPlane->direction(); } } - kIsUpdated = false; - } else if (kIsUpdated) { // other attributes are updated during the selection comupation - kIsAttrChanged = true; } } diff --git a/src/SketchPlugin/SketchPlugin_Sketch.h b/src/SketchPlugin/SketchPlugin_Sketch.h index 66795f7bc..0b4abaa44 100644 --- a/src/SketchPlugin/SketchPlugin_Sketch.h +++ b/src/SketchPlugin/SketchPlugin_Sketch.h @@ -135,7 +135,7 @@ class SketchPlugin_Sketch : public ModelAPI_CompositeFeature//, public GeomAPI_I /// Returns the point projected into the sketch plane std::shared_ptr to2D(const std::shared_ptr& thePnt); - SKETCHPLUGIN_EXPORT virtual void attributeChanged(); + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); protected: /// Creates a plane and append it to the list /// \param theX the X normal value diff --git a/src/XGUI/XGUI_PropertyPanel.cpp b/src/XGUI/XGUI_PropertyPanel.cpp index c5b1a50d7..04655869a 100644 --- a/src/XGUI/XGUI_PropertyPanel.cpp +++ b/src/XGUI/XGUI_PropertyPanel.cpp @@ -97,6 +97,7 @@ void XGUI_PropertyPanel::setModelWidgets(const QList& t //if (aPointWidget) // connect(aPointWidget, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this, // SIGNAL(storedPoint2D(ObjectPtr, const std::string&))) + //} if (!isEnableStretch) continue; foreach(QWidget* eachWidget, (*anIt)->getControls()) { -- 2.39.2