Salome HOME
Optimization of circle and arc creation
authormpv <mpv@opencascade.com>
Tue, 14 Jun 2016 09:25:37 +0000 (12:25 +0300)
committermpv <mpv@opencascade.com>
Tue, 14 Jun 2016 09:25:37 +0000 (12:25 +0300)
src/Model/Model_Data.cpp
src/Model/Model_Data.h
src/Model/Model_Update.cpp
src/ModelAPI/ModelAPI_Data.h
src/ParametersPlugin/ParametersPlugin_EvalListener.cpp
src/SketchPlugin/SketchPlugin_Arc.cpp
src/SketchPlugin/SketchPlugin_Arc.h
src/SketchPlugin/SketchPlugin_Circle.cpp

index 1e63282bb2690aebc2fb5c809bc7dc8c3c38574d..4d1b40b223f8b6b50f0b3655de35410a9b0672ba 100644 (file)
@@ -283,19 +283,31 @@ void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
         ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
       }
     } else {
-      myWasChangedButBlocked = true;
+      // to avoid too many duplications do not add the same like the last
+      if (myWasChangedButBlocked.empty() || *(myWasChangedButBlocked.rbegin()) != theAttr)
+        myWasChangedButBlocked.push_back(theAttr);
     }
   }
 }
 
-void Model_Data::blockSendAttributeUpdated(const bool theBlock)
+void Model_Data::blockSendAttributeUpdated(const bool theBlock, const bool theSendMessage)
 {
   if (mySendAttributeUpdated == theBlock) {
     mySendAttributeUpdated = !theBlock;
-    if (mySendAttributeUpdated && myWasChangedButBlocked) { // so, now it is ok to send the update signal
-      static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
-      ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
-      myWasChangedButBlocked = false;
+    if (mySendAttributeUpdated && !myWasChangedButBlocked.empty()) { // so, now it is ok to send the update signal
+      if (theSendMessage) {
+        // make a copy to avoid iteration on modified list (may be cleared by attribute changed call)
+        std::list<ModelAPI_Attribute*> aWasChangedButBlocked = myWasChangedButBlocked;
+        myWasChangedButBlocked.clear();
+        std::list<ModelAPI_Attribute*>::iterator aChangedIter = aWasChangedButBlocked.begin();
+        for(; aChangedIter != aWasChangedButBlocked.end(); aChangedIter++) {
+          myObject->attributeChanged((*aChangedIter)->id());
+        }
+        static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+        ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
+      } else {
+        myWasChangedButBlocked.clear();
+      }
     }
   }
 }
index 8f8cfe5151f3d22f073a2120d45b10f1553e4bfe..e505d375165731acd0fbd6b7ae75afd03132d667 100644 (file)
@@ -56,8 +56,8 @@ class Model_Data : public ModelAPI_Data
   std::set<AttributePtr> myRefsToMe;
   /// flag that may block the "attribute updated" sending
   bool mySendAttributeUpdated;
-  /// if some attribute was changed, but mySendAttributeUpdated was false, this flag stores this
-  bool myWasChangedButBlocked;
+  /// if some attribute was changed, but mySendAttributeUpdated was false, this stores this
+  std::list<ModelAPI_Attribute*> myWasChangedButBlocked;
 
   /// Returns label of this feature
   TDF_Label label()
@@ -155,7 +155,11 @@ class Model_Data : public ModelAPI_Data
   /// makes attribute initialized
   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
   /// Blocks sending "attribute updated" if theBlock is true
-  MODEL_EXPORT virtual void blockSendAttributeUpdated(const bool theBlock);
+  /// \param theBlock allows switching on/off the blocking state
+  /// \param theSendMessage if false, it does not send the update message even if something is changed
+  ///            (normally is it used in attributeChanged because this message will be sent anyway)
+  MODEL_EXPORT virtual void blockSendAttributeUpdated(
+    const bool theBlock, const bool theSendMessage = true);
 
   /// Puts feature to the document data sub-structure
   MODEL_EXPORT void setLabel(TDF_Label theLab);
index 2eb0e7dbe24b9532ae527ec2e36b08c79cdd8346..47c6db4b87aef32105d3a54171abe75a66662215 100755 (executable)
@@ -405,14 +405,26 @@ bool Model_Update::processFeature(FeaturePtr theFeature)
   // check all features this feature depended on (recursive call of updateFeature)
   std::set<std::shared_ptr<ModelAPI_Feature> >& aReasons = myModified[theFeature];
   if (aReasons.find(theFeature) == aReasons.end()) {
-    std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
-    for(; aReasonIter != aReasons.end(); aReasonIter++) {
-      if (*aReasonIter != theFeature && (*aReasonIter)->data()->isValid()) {
-        if (processFeature(*aReasonIter))
+    // take reasons one by one (they may be added during the feature process (circle by the radius of sketch)
+    std::set<FeaturePtr> aProcessedReasons;
+    FeaturePtr aReason = aReasons.empty() ? FeaturePtr() : *(aReasons.begin());
+    while(aReason.get()) {
+      if (aReason != theFeature && (aReason)->data()->isValid()) {
+        if (processFeature(aReason))
           aIsModified = true;
-        if ((*aReasonIter)->data()->execState() == ModelAPI_StateInvalidArgument)
+        if (aReason->data()->execState() == ModelAPI_StateInvalidArgument)
           isReferencedInvalid = true;
       }
+      // searching for the next not used reason
+      aProcessedReasons.insert(aReason);
+      aReason.reset();
+      std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasonIter = aReasons.begin();
+      for(; aReasonIter != aReasons.end(); aReasonIter++) {
+        if (aProcessedReasons.find(*aReasonIter) == aProcessedReasons.end()) {
+          aReason = *aReasonIter;
+          break;
+        }
+      }
     }
   } else { // check all features this feature depended on because here which one was modified is unknown
     std::list<std::pair<std::string, std::list<std::shared_ptr<ModelAPI_Object> > > > aDeps;
index 05dc2be644771e2b21e80311250421a2d14eb311..320dffb4035b640acc63fa219fa7731a2b34cd10 100644 (file)
@@ -118,7 +118,11 @@ class MODELAPI_EXPORT ModelAPI_Data
   /// makes attribute initialized
   virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr) = 0;
   /// Blocks sending "attribute updated" if theBlock is true
-  virtual void blockSendAttributeUpdated(const bool theBlock) = 0;
+  /// \param theID identifier of the attribute that can be referenced by this ID later
+  /// \param theAttrType type of the created attribute (received from the type method)
+  /// \returns the just created attribute
+  virtual void blockSendAttributeUpdated(
+    const bool theBlock, const bool theSendMessage = true) = 0;
 
   /// Erases all the data from the data model
   virtual void erase() = 0;
index fcd4b0dc808076245064ae35cd336edc6675e9e7..c2deeff2d25cce8c1a082e83d0ef892c18e7b5d2 100644 (file)
@@ -346,7 +346,7 @@ void setParameterName(ResultParameterPtr theResultParameter, const std::string&
 {
   theResultParameter->data()->blockSendAttributeUpdated(true);
   theResultParameter->data()->setName(theName);
-  theResultParameter->data()->blockSendAttributeUpdated(false);
+  theResultParameter->data()->blockSendAttributeUpdated(false, false);
 
   std::shared_ptr<ParametersPlugin_Parameter> aParameter = 
       std::dynamic_pointer_cast<ParametersPlugin_Parameter>(
index f0aaaef9f35148a8879d018424575d11424e1d30..b98a915bf9b7f7bc9c3f519eee17f8106bf6efb5 100644 (file)
@@ -135,8 +135,7 @@ void SketchPlugin_Arc::execute()
   // result for the arc is set only when all obligatory attributes are initialized,
   // otherwise AIS object is used to visualize the arc's preview
   if (aSketch && isFeatureValid()) {
-    ResultPtr aLastResult = lastResult();
-    bool hasResult = aLastResult && aLastResult.get();
+    bool hasResult = lastResult().get() != NULL;
 
     // compute a circle point in 3D view
     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
@@ -201,9 +200,6 @@ void SketchPlugin_Arc::execute()
       aConstr2->setIsInHistory(false);
       setResult(aConstr2, 1);
     }
-
-    // update radius and angle
-    updateDependentAttributes();
   }
 }
 
@@ -220,9 +216,6 @@ AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
       std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
       if (aCenterAttr->isInitialized()) {
         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
-        // make a visible point
-        std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
-        aShapes.push_back(aCenterPointShape);
 
         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
@@ -241,8 +234,22 @@ AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
             std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
             std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
             if (aTypeAttr && aTypeAttr->isInitialized() &&
-                aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized())
+                aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized() &&
+                aEndAttr->pnt()->distance(aStartAttr->pnt()) > tolerance) {
               aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
+              std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = 
+                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
+              if (!aPassedAttr->isInitialized()) { // calculate the appropriate center for the presentation
+                std::shared_ptr<GeomAPI_XY> aDir = 
+                  aEndAttr->pnt()->xy()->decreased(aStartAttr->pnt()->xy())->multiplied(0.5);
+                double x = aDir->x();
+                double y = aDir->y();
+                aDir->setX(x - y);
+                aDir->setY(y + x);
+                std::shared_ptr<GeomAPI_XY> aCenterXY = aStartAttr->pnt()->xy()->added(aDir);
+                aCenter = aSketch->to3D(aCenterXY->x(), aCenterXY->y());
+              }
+            }
 
             std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
                                                             aCenter, aStartPoint, aEndPoint, aNormal);
@@ -250,6 +257,9 @@ AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
               aShapes.push_back(aCircleShape);
           }
         }
+        // make a visible point
+        std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
+        aShapes.push_back(aCenterPointShape);
       }
       if (!aShapes.empty()) {
         std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
@@ -332,93 +342,6 @@ static inline void adjustPeriod(double& theParam)
   while (theParam >= PERIOD) theParam -= PERIOD;
 }
 
-static inline void calculateArcAngleRadius(
-    const std::shared_ptr<GeomAPI_Circ2d>& theCircle,
-    const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
-    const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
-    const std::shared_ptr<GeomAPI_Pnt2d>& thePassedPoint,
-    AttributeDoublePtr theAngleAttr,
-    AttributeDoublePtr theRadiusAttr)
-{
-  double aStartParam, aEndParam, aPassedParam;
-  theCircle->parameter(theStartPoint, paramTolerance, aStartParam);
-  theCircle->parameter(theEndPoint, paramTolerance, aEndParam);
-  theCircle->parameter(thePassedPoint, paramTolerance, aPassedParam);
-  adjustPeriod(aStartParam);
-  adjustPeriod(aEndParam);
-  adjustPeriod(aPassedParam);
-
-  if (aPassedParam >= aStartParam && aPassedParam <= aEndParam)
-    theAngleAttr->setValue((aEndParam - aStartParam) * 180.0 / PI);
-  else
-    theAngleAttr->setValue((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
-  theRadiusAttr->setValue(theCircle->radius());
-}
-
-static inline bool calculatePassedPoint(
-    const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
-    const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
-    const std::shared_ptr<GeomAPI_Pnt2d>& theEndPoint,
-    bool theArcReversed,
-    std::shared_ptr<GeomDataAPI_Point2D> thePassedPoint)
-{
-  if (theCenter->distance(theStartPoint) < tolerance ||
-      theCenter->distance(theEndPoint) < tolerance)
-    return false;
-
-  std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
-      theStartPoint->xy()->decreased(theCenter->xy())));
-  std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
-      theEndPoint->xy()->decreased(theCenter->xy())));
-  std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
-  if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
-    // start and end directions are opposite, so middle direction will be orthogonal
-    aMidDirXY->setX(-aStartDir->y());
-    aMidDirXY->setY(aStartDir->x());
-  }
-  std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
-  if ((aStartDir->cross(aMidDir) > 0) ^ !theArcReversed)
-    aMidDir->reverse();
-
-  double aRadius = theCenter->distance(theStartPoint);
-  std::shared_ptr<GeomAPI_XY> aPassedPnt = theCenter->xy()->added( aMidDir->xy()->multiplied(aRadius) );
-  thePassedPoint->setValue(aPassedPnt->x(), aPassedPnt->y());
-  return true;
-}
-
-void SketchPlugin_Arc::updateDependentAttributes()
-{
-  std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(START_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(END_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
-      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
-  AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      data()->attribute(RADIUS_ID()));
-  AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      data()->attribute(ANGLE_ID()));
-
-  if (!aPassedPoint)
-    return;
-
-  data()->blockSendAttributeUpdated(true);
-
-  bool isOk = calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
-                       isReversed(), aPassedPoint);
-  if (isOk && aRadiusAttr && anAngleAttr) {
-    std::shared_ptr<GeomAPI_Circ2d> aCircle(
-        new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
-    if (aCircle->implPtr<void*>())
-      calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
-                              anAngleAttr, aRadiusAttr);
-  }
-  data()->blockSendAttributeUpdated(false);
-}
-
-
 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
 {
   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
@@ -442,161 +365,16 @@ void SketchPlugin_Arc::attributeChanged(const std::string& theID)
     }
     return;
   }
-
-  AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      data()->attribute(RADIUS_ID()));
-  AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      data()->attribute(ANGLE_ID()));
-
-  if (theID == RADIUS_ID()) {
-    if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized())
-      return;
-    // move center and passed point
-    std::shared_ptr<GeomAPI_XY> aStartPnt = aStartAttr->pnt()->xy();
-    std::shared_ptr<GeomAPI_XY> aEndPnt = anEndAttr->pnt()->xy();
-    double aDist = aStartPnt->distance(aEndPnt);
-    if (fabs(aDist) < tolerance)
-      return;
-    std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
-    std::shared_ptr<GeomAPI_Dir2d> aMidPerpDir(new GeomAPI_Dir2d(-aDir->y(), aDir->x()));
-    std::shared_ptr<GeomAPI_XY> aMidPnt = aStartPnt->added(aEndPnt)->multiplied(0.5);
-
-    double anAngle = anAngleAttr->value() * PI / 180.0;
-    adjustPeriod(anAngle);
-    if (anAngle > PI)
-      aMidPerpDir->reverse();
-
-    double aRadius = aRadiusAttr->value();
-    // The center is placed on a perpendicular bisector of a start-end points segment.
-    // If the radius is smaller that necessary, start and end points are moved too.
-    double aDist2 = aRadius * aRadius - aDist * aDist / 4.0;
-    aDist = aDist2 > 0.0 ? sqrt(aDist2) : 0.0;
-    // distance between middle point and start point (does not changed if the arc diameter is greater than start-end distance)
-    aDist2 = sqrt(aRadius * aRadius - aDist * aDist);
-
-    std::shared_ptr<GeomAPI_XY> aCenter = aMidPnt->added(aMidPerpDir->xy()->multiplied(aDist));
-    aStartPnt = aMidPnt->added(aDir->xy()->multiplied(-aDist2));
-    aEndPnt = aMidPnt->added(aDir->xy()->multiplied(aDist2));
-
-    data()->blockSendAttributeUpdated(true);
-    aCenterAttr->setValue(aCenter->x(), aCenter->y());
-    aStartAttr->setValue(aStartPnt->x(), aStartPnt->y());
-    anEndAttr->setValue(aEndPnt->x(), aEndPnt->y());
-    updateDependentAttributes();
-    data()->blockSendAttributeUpdated(false);
-    return;
-  }
-  if (theID == ANGLE_ID()) {
-    if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
-      return;
-    data()->blockSendAttributeUpdated(true);
-    // move end point and passed point
-    std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
-    double anAngle = anAngleAttr->value() * PI / 180.0;
-    double sinA = sin(anAngle);
-    double cosA = cos(anAngle);
-    std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
-    std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
-        aStartDir->x() * cosA - aStartDir->y() * sinA,
-        aStartDir->x() * sinA + aStartDir->y() * cosA));
-    anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
-
-    anAngle /= 2.0;
-    sinA = sin(anAngle);
-    cosA = cos(anAngle);
-    aDir = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(
-        aStartDir->x() * cosA - aStartDir->y() * sinA,
-        aStartDir->x() * sinA + aStartDir->y() * cosA));
-    std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
-        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(PASSED_POINT_ID()));
-    aPassedPoint->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
-
-    std::shared_ptr<GeomAPI_Circ2d> aCircle(
-        new GeomAPI_Circ2d(aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt()));
-    calculateArcAngleRadius(aCircle, aStartAttr->pnt(), anEndAttr->pnt(), aPassedPoint->pnt(),
-                            anAngleAttr, aRadiusAttr);
-    data()->blockSendAttributeUpdated(false);
-    return;
-  }
-
-  if (theID == CENTER_ID()) {
-    if (isFeatureValid())
-      projectEndPoint();
-    return;
-  }
-
   AttributeStringPtr aTypeAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
-  if (!aTypeAttr)
-    return;
-  std::string anArcType = aTypeAttr->value();
-
-  // update the points in accordance to the changed point changes
-  if (anArcType == ARC_TYPE_CENTER_START_END()) {
-    if (!isFeatureValid())
-      return;
-    if (theID == END_ID() && isStable()) {
-      // The arc is under construction, so its end point projected
-      // on the circle formed by center and start points
-      projectEndPoint();
-    }
-    updateDependentAttributes();
-  }
-  else if (anArcType == ARC_TYPE_THREE_POINTS() &&
-          (theID == START_ID() || theID == END_ID() || theID == PASSED_POINT_ID())) {
-    data()->blockSendAttributeUpdated(true);
 
-    std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
-    int aNbInitialized = 0;
-    for (int i = 1; i <= 3; ++i) {
-      std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
-          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
-      if (aCurPnt->isInitialized())
-        aPoints[aNbInitialized++] = aCurPnt->pnt();
-    }
-
-    if (aNbInitialized == 1)
-      aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
-    else if (aNbInitialized == 2) {
-      // calculate center point, which gives a quarter of circle for the given start and end points
-      std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aPoints[0];
-      std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aPoints[1];
-      std::shared_ptr<GeomAPI_XY> aDir = aEndPnt->xy()->decreased(aStartPnt->xy())->multiplied(0.5);
-      double x = aDir->x();
-      double y = aDir->y();
-      aDir->setX(x - y);
-      aDir->setY(y + x);
-      std::shared_ptr<GeomAPI_XY> aCenter = aStartPnt->xy()->added(aDir);
-      double aRadius = sqrt(aDir->dot(aDir));
-
-      aCenterAttr->setValue(aCenter->x(), aCenter->y());
-      aRadiusAttr->setValue(aRadius);
-      anAngleAttr->setValue(90.0);
-    }
-    else {
-      std::shared_ptr<GeomAPI_Circ2d> aCircle(
-          new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
-
-      std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
-      if (aCenter) {
-        aCenterAttr->setValue(aCenter);
-        if (theID == START_ID() || theID == END_ID())
-          updateDependentAttributes();
-        else
-          calculateArcAngleRadius(aCircle, aPoints[0], aPoints[1], aPoints[2],
-                                  anAngleAttr, aRadiusAttr);
-      }
-    }
-
-    data()->blockSendAttributeUpdated(false);
-  }
-  else if (anArcType == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
+  // this is before others since here end attribute may be changed, but with the special behavior
+  if (aTypeAttr->value() == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
     SketchPlugin_Sketch* aSketch = sketch();
     AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
         data()->attribute(TANGENT_POINT_ID()));
 
     if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
-      data()->blockSendAttributeUpdated(true);
       // compute orthogonal direction
       std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
       std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
@@ -630,14 +408,171 @@ void SketchPlugin_Arc::attributeChanged(const std::string& theID)
       std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
       std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
       if (aCenter) {
+        data()->blockSendAttributeUpdated(true);
         aCenterAttr->setValue(aCenter);
         aStartAttr->setValue(aTangPnt2d);
-        updateDependentAttributes();
+        data()->blockSendAttributeUpdated(false);
       }
 
-      data()->blockSendAttributeUpdated(false);
       tangencyArcConstraints();
     }
+    return;
+  }
+
+  // if changed the base attributes, update all other (is necessary) without recursion
+  if (theID == CENTER_ID() || theID == START_ID() || theID == END_ID() || theID == ARC_TYPE()) {
+    if (!isFeatureValid())
+      return;
+    std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
+    std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
+    std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
+    double aRadius = aCenter->distance(aStart);
+    if (aRadius < tolerance)
+      return;
+    std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
+
+    data()->blockSendAttributeUpdated(true);
+    if (theID == END_ID() && isStable()) {
+      // The arc is under construction, so its end point projected
+      // on the circle formed by center and start points
+      std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
+      if (aProjection && anEnd->distance(aProjection) > tolerance) {
+        anEndAttr->setValue(aProjection);
+        anEnd = aProjection;
+      }
+    }
+    // update all other attributes due to the base attributes values
+    if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // update passed point due to start, end and center
+      if (aCenter->distance(aStart) > tolerance && aCenter->distance(anEnd) > tolerance) {
+        // project passed point t othe circle
+        std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
+        if (aPassedAttr->isInitialized()) {
+          std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aPassedAttr->pnt());
+          if (aProjection && aPassedAttr->pnt()->distance(aProjection) > tolerance) {
+            aPassedAttr->setValue(aProjection);
+          }
+        } else { // initialize it by some middle - value
+          std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
+            aStart->xy()->decreased(aCenter->xy())));
+          std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
+            anEnd->xy()->decreased(aCenter->xy())));
+          std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
+          if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
+            // start and end directions are opposite, so middle direction will be orthogonal
+            aMidDirXY->setX(-aStartDir->y());
+            aMidDirXY->setY(aStartDir->x());
+          }
+          std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
+          if ((aStartDir->cross(aMidDir) > 0) ^ !isReversed())
+            aMidDir->reverse();
+          std::shared_ptr<GeomAPI_XY> aPassedPnt = 
+            aCenter->xy()->added(aMidDir->xy()->multiplied(aCenter->distance(aStart)));
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()))->
+            setValue(aPassedPnt->x(), aPassedPnt->y());
+        }
+      }
+    }
+    // update radius and angle
+    AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+        data()->attribute(RADIUS_ID()));
+    aRadiusAttr->setValue(aRadius);
+    AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+        data()->attribute(ANGLE_ID()));
+    std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aStart));
+    double aStartParam, aEndParam;
+    aCircle->parameter(aStart, paramTolerance, aStartParam);
+    aCircle->parameter(anEnd, paramTolerance, aEndParam);
+    adjustPeriod(aStartParam);
+    adjustPeriod(aEndParam);
+    if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // use the passed point for the angle calculation
+      std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
+      double aPassedParam;
+      aCircle->parameter(aPassedAttr->pnt(), paramTolerance, aPassedParam);
+      adjustPeriod(aPassedParam);
+      double aNewAngle = aPassedParam >= aStartParam && aPassedParam <= aEndParam ?
+        ((aEndParam - aStartParam) * 180.0 / PI) :
+        ((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
+      if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
+        anAngleAttr->setValue(aNewAngle);
+    } else {
+      double aNewAngle = (aEndParam - aStartParam) * 180.0 / PI;
+      if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
+        anAngleAttr->setValue(aNewAngle);
+    }
+    // do not need to inform that other parameters were changed in this basis mode: these arguments
+    // change is enough
+    data()->blockSendAttributeUpdated(false, false);
+    return;
+  }
+
+  if (theID == PASSED_POINT_ID()) {
+    data()->blockSendAttributeUpdated(true);
+
+    std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
+    int aNbInitialized = 0;
+    for (int i = 1; i <= 3; ++i) {
+      std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
+      if (aCurPnt->isInitialized())
+        aPoints[aNbInitialized++] = aCurPnt->pnt();
+    }
+
+    if (aNbInitialized == 3) {
+      std::shared_ptr<GeomAPI_Circ2d> aCircle(
+          new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
+
+      std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
+      if (aCenter) {
+        aCenterAttr->setValue(aCenter);
+      }
+    }
+    data()->blockSendAttributeUpdated(false);
+    return;
+  }
+
+  if (theID == RADIUS_ID()) {
+    if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized() || !aCenterAttr->isInitialized())
+      return;
+    // move center and passed point
+    std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
+    std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
+    std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
+    if (aStart->distance(aCenter) < tolerance || anEnd->distance(aCenter) < tolerance)
+      return;
+    AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+        data()->attribute(RADIUS_ID()));
+    double aRadius = aRadiusAttr->value();
+
+    data()->blockSendAttributeUpdated(true);
+    std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
+    std::shared_ptr<GeomAPI_XY> aNewStart = aStartDir->xy()->multiplied(aRadius)->added(aCenter->xy());
+    aStartAttr->setValue(aNewStart->x(), aNewStart->y());
+    std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEnd->xy()->decreased(aCenter->xy())));
+    std::shared_ptr<GeomAPI_XY> aNewEnd = anEndDir->xy()->multiplied(aRadius)->added(aCenter->xy());
+    anEndAttr->setValue(aNewEnd->x(), aNewEnd->y());
+    data()->blockSendAttributeUpdated(false);
+    return;
+  }
+  if (theID == ANGLE_ID()) {
+    if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
+      return;
+    AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+        data()->attribute(ANGLE_ID()));
+    data()->blockSendAttributeUpdated(true);
+    // move end point and passed point
+    std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
+    double anAngle = anAngleAttr->value() * PI / 180.0;
+    double sinA = sin(anAngle);
+    double cosA = cos(anAngle);
+    std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
+    std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
+        aStartDir->x() * cosA - aStartDir->y() * sinA,
+        aStartDir->x() * sinA + aStartDir->y() * cosA));
+    anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
+    data()->blockSendAttributeUpdated(false);
+    return;
   }
 }
 
@@ -800,25 +735,3 @@ void SketchPlugin_Arc::tangencyArcConstraints()
       Events_Loop::loop()->flush(aCreateEvent);
   }
 }
-
-void SketchPlugin_Arc::projectEndPoint()
-{
-  std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(START_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
-      GeomDataAPI_Point2D>(data()->attribute(END_ID()));
-
-  if (aCenterAttr->pnt()->distance(aStartAttr->pnt()) < tolerance)
-    return;
-  data()->blockSendAttributeUpdated(true);
-  // compute and change the arc end point
-  std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
-      new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
-  std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
-  if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
-    anEndAttr->setValue(aProjection);
-  updateDependentAttributes();
-  data()->blockSendAttributeUpdated(false);
-}
index 37d1d8ff63a9bd62f22352da08949a847ab8d03a..0fd638d933bdf881fa5ac5ca9fd45c07802a070f 100644 (file)
@@ -124,14 +124,9 @@ private:
   /// Returns true if all obligatory attributes are initialized
   bool isFeatureValid();
 
-  /// Update attributes like passed point, radius and angle of the arc
-  void updateDependentAttributes();
-
   /// Compose constraints to build tangency arc
   void tangencyArcConstraints();
 
-  /// Project end point of arc to the circle
-  void projectEndPoint();
 };
 
 #endif
index bd3efaf41082ec3a99d02e072935efb1e08e8788..ac719a70896d09a32dd73c589f808b71e8391c46 100644 (file)
@@ -121,8 +121,6 @@ void SketchPlugin_Circle::execute()
       aConstr2->setShape(aCircleShape);
       aConstr2->setIsInHistory(false);
       setResult(aConstr2, 1);
-
-      adjustThreePoints();
     }
   }
 }
@@ -221,23 +219,15 @@ void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
   else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
       data()->attribute(CIRCLE_TYPE()))->value();
-    if (aType == CIRCLE_TYPE_THREE_POINTS())
-      return;
-
-    // check the execute() was called and the shape was built
-    if (!lastResult())
-      return;
-
-    adjustThreePoints();
-  }
-  else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
+    if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult())  // adjust data from the solver
+      adjustThreePoints();
+  } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
+    // support the center and radius attributes enev in other mode: solver uses them
     std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
       data()->attribute(CIRCLE_TYPE()))->value();
     if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
       return;
-
-    data()->blockSendAttributeUpdated(true);
-
+    data()->blockSendAttributeUpdated(true); // to modify two attributes at once
     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
     int aNbInitialized = 0;
     for (int i = 1; i <= 3; ++i) {
@@ -271,8 +261,14 @@ void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
         aRadiusAttr->setValue(aRadius);
       }
     }
+    data()->blockSendAttributeUpdated(false, false);
 
-    data()->blockSendAttributeUpdated(false);
+  } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
+    std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
+      data()->attribute(CIRCLE_TYPE()))->value();
+    if (aType == CIRCLE_TYPE_THREE_POINTS()) {
+      adjustThreePoints();
+    }
   }
 }
 
@@ -303,5 +299,5 @@ void SketchPlugin_Circle::adjustThreePoints()
     aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
     aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
   }
-  data()->blockSendAttributeUpdated(false);
+  data()->blockSendAttributeUpdated(false, false);
 }