Salome HOME
Merge branch 'master' of salome:modules/shaper
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
index 2c55b1ddf2376bf2287dca697162e1993e8bbda5..e295aa8f44e762a3a757beaa9a35123598ea1b73 100644 (file)
@@ -19,6 +19,7 @@
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
 
 #include <GeomAPI_Ax2.h>
 #include <GeomAPI_Circ2d.h>
@@ -28,6 +29,7 @@
 #include <GeomAPI_Lin2d.h>
 #include <GeomAPI_Lin.h>
 #include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_Vertex.h>
 #include <GeomAPI_XY.h>
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Dir.h>
 
 const double tolerance = 1e-7;
 const double paramTolerance = 1.e-4;
-const double PI =3.141592653589793238463;
+const double PI = 3.141592653589793238463;
 
 namespace {
-  static const std::string& ARC_TYPE_CENTER_START_END()
-  {
-    static const std::string TYPE("CenterStartEnd");
-    return TYPE;
-  }
-  static const std::string& ARC_TYPE_THREE_POINTS()
-  {
-    static const std::string TYPE("ThreePoints");
-    return TYPE;
-  }
-
-  static const std::string& PASSED_POINT_ID()
-  {
-    static const std::string PASSED_PNT("ArcPassedPoint");
-    return PASSED_PNT;
-  }
-  static const std::string& RADIUS_ID()
-  {
-    static const std::string RADIUS("ArcRadius");
-    return RADIUS;
-  }
-  static const std::string& ANGLE_ID()
-  {
-    static const std::string ANGLE("ArcAngle");
-    return ANGLE;
-  }
-
   static const std::string& POINT_ID(int theIndex)
   {
     switch (theIndex) {
     case 1: return SketchPlugin_Arc::START_ID();
     case 2: return SketchPlugin_Arc::END_ID();
-    case 3: return PASSED_POINT_ID();
+    case 3: return SketchPlugin_Arc::PASSED_POINT_ID();
     }
 
     static const std::string DUMMY;
@@ -93,7 +68,7 @@ SketchPlugin_Arc::SketchPlugin_Arc()
   myXEndBefore = 0;
   myYEndBefore = 0;
 
-  myParamBefore = 0;
+  myParamBefore = PI * 2.0;
 }
 
 void SketchPlugin_Arc::initDerivedClassAttributes()
@@ -105,11 +80,8 @@ void SketchPlugin_Arc::initDerivedClassAttributes()
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
 
-  data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
-  AttributeBooleanPtr isInversed =
-      std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
-  if (!isInversed->isInitialized())
-    isInversed->setValue(false);
+  AttributeBooleanPtr isInversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
+    data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
 
   // get the initial values
   if (anEndAttr->isInitialized()) {
@@ -117,14 +89,18 @@ void SketchPlugin_Arc::initDerivedClassAttributes()
     myYEndBefore = anEndAttr->y();
   }
 
-  data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId());
-  std::dynamic_pointer_cast<ModelAPI_AttributeString>(
-      data()->attribute(ARC_TYPE()))->setValue(ARC_TYPE_CENTER_START_END());
+  AttributeStringPtr anArcType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
+    data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId()));
 
   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
+
+  // set after all to avoid in attributeChanged reference to not existing attributes
+  if (!isInversed->isInitialized())
+    isInversed->setValue(false);
+  anArcType->setValue(ARC_TYPE_CENTER_START_END());
 }
 
 void SketchPlugin_Arc::execute()
@@ -133,8 +109,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<
@@ -145,7 +120,7 @@ void SketchPlugin_Arc::execute()
 
     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);
+    std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
     std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
         data(), 0);
     aConstr1->setShape(aCenterPointShape);
@@ -170,21 +145,6 @@ void SketchPlugin_Arc::execute()
     AttributeBooleanPtr isInversed =
         std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
 
-    std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
-    std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
-    std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
-    double aParameterNew = 0.0;
-    if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
-      if(0 <= myParamBefore && myParamBefore <= PI / 2.0
-        && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
-          isInversed->setValue(true);
-      } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
-        && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
-          isInversed->setValue(false);
-      }
-    }
-    myParamBefore = aParameterNew;
-
     std::shared_ptr<GeomAPI_Shape> aCircleShape;
     if(!isInversed->value()) {
       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
@@ -199,11 +159,6 @@ void SketchPlugin_Arc::execute()
       aConstr2->setIsInHistory(false);
       setResult(aConstr2, 1);
     }
-
-    AttributeRefAttrPtr aTangentPoint = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-        attribute(TANGENT_POINT_ID()));
-    if (!hasResult && aTangentPoint->attr())
-      tangencyArcConstraints();
   }
 }
 
@@ -216,6 +171,8 @@ AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
       // compute a circle point in 3D view
       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
           GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+
+      std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
       if (aCenterAttr->isInitialized()) {
         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
 
@@ -236,30 +193,56 @@ 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())
-              aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
-            std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
-                                                            aCenter, aStartPoint, aEndPoint, aNormal);
-            if (aCircleShape) {
-              std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
-              // make a visible point
-              std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
-              aShapes.push_back(aCenterPointShape);
-
-              aShapes.push_back(aCircleShape);
-              if (!aShapes.empty())
-              {
-                std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
-                AISObjectPtr anAIS = thePrevious;
-                if (!anAIS)
-                  anAIS = AISObjectPtr(new GeomAPI_AISObject);
-                anAIS->createShape(aCompound);
-                anAIS->setWidth(3);
-                return anAIS;
+                aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) {
+              if (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
+                  // check that center is bad for the current start and end and must be recomputed
+                  std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(
+                    aCenterAttr->pnt(), aStartAttr->pnt()));
+                  std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aEndAttr->pnt());
+                  if (!aProjection.get() || aEndAttr->pnt()->distance(aProjection) > tolerance) {
+                    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());
+                  }
+                }
+              } else { // issue #1695: don't display circle if initialized only start point
+                return AISObjectPtr();
               }
             }
+            AttributeBooleanPtr isInversed =
+              std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
+
+            std::shared_ptr<GeomAPI_Shape> aCircleShape = 
+              (isInversed->isInitialized() && isInversed->value()) ?
+                GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal) :
+                GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
+
+            if (aCircleShape)
+              aShapes.push_back(aCircleShape);
           }
         }
+        // make a visible point
+        std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
+        aShapes.push_back(aCenterPointShape);
+      }
+      if (!aShapes.empty()) {
+        std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
+        AISObjectPtr anAIS = thePrevious;
+        if (!anAIS)
+          anAIS = AISObjectPtr(new GeomAPI_AISObject);
+        anAIS->createShape(aCompound);
+        anAIS->setWidth(3);
+        return anAIS;
       }
     }
   }
@@ -278,21 +261,25 @@ void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
   myEndUpdate = true;
   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(SketchPlugin_Arc::START_ID()));
-  aPoint2->move(theDeltaX, theDeltaY);
+  if (aPoint2->isInitialized())
+    aPoint2->move(theDeltaX, theDeltaY);
 
   std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(SketchPlugin_Arc::END_ID()));
-  aPoint3->move(theDeltaX, theDeltaY);
+  if (aPoint3->isInitialized())
+    aPoint3->move(theDeltaX, theDeltaY);
   myStartUpdate = false;
   myEndUpdate = false;
 
   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
-  aPoint1->move(theDeltaX, theDeltaY);
+  if (aPoint1->isInitialized())
+    aPoint1->move(theDeltaX, theDeltaY);
 
   std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
-  aPassedPoint->move(theDeltaX, theDeltaY);
+  if (aPassedPoint->isInitialized())
+    aPassedPoint->move(theDeltaX, theDeltaY);
   aData->blockSendAttributeUpdated(false);
 }
 
@@ -333,92 +320,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 void 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;
-
-  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());
-}
-
-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);
-
-  calculatePassedPoint(aCenterAttr->pnt(), aStartAttr->pnt(), anEndAttr->pnt(),
-                       isReversed(), aPassedPoint);
-  if (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<
@@ -430,6 +331,12 @@ void SketchPlugin_Arc::attributeChanged(const std::string& theID)
   // the second condition for unability to move external segments anywhere
   if (theID == EXTERNAL_ID() || isFixed()) {
     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+    if (!aSelection) {
+      // empty shape in selection shows that the shape is equal to context
+      ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
+      if (anExtRes)
+        aSelection = anExtRes->shape();
+    }
     // update arguments due to the selection value
     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
@@ -438,170 +345,34 @@ void SketchPlugin_Arc::attributeChanged(const std::string& theID)
         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
+
+        data()->real(RADIUS_ID())->setValue(aCirc->radius());
+        double aStartAngle, aEndAngle;
+        anEdge->getRange(aStartAngle, aEndAngle);
+        data()->real(ANGLE_ID())->setValue(aEndAngle - aStartAngle);
+        myParamBefore = aEndAngle;
+        adjustPeriod(myParamBefore);
       }
     }
     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 =
           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
       std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
+      if (aTangPnt2d->isEqual(anEndAttr->pnt()))
+        return;
       FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
       std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
           aTangFeature->lastResult()->shape());
@@ -630,16 +401,184 @@ 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));
 
-      if (theID == TANGENT_POINT_ID())
-        tangencyArcConstraints();
+    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 (!anAngleAttr->isInitialized() || fabs(aNewAngle - anAngleAttr->value()) > tolerance)
+        anAngleAttr->setValue(aNewAngle);
+    } else {
+      double aNewAngle = (aEndParam - aStartParam) * 180.0 / PI;
+      if (!anAngleAttr->isInitialized() || fabs(aNewAngle - anAngleAttr->value()) > tolerance)
+        anAngleAttr->setValue(aNewAngle);
+    }
+
+    // calculate arc aperture and change the Inversed flag if needed
+    AttributeBooleanPtr isInversed =
+        std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
+    double aParameterNew = aEndParam - aStartParam;
+    if (0 <= myParamBefore && myParamBefore <= PI / 2.0 &&
+        PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0)
+      isInversed->setValue(true);
+    else if (PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0 &&
+             0 <= aParameterNew && aParameterNew <= PI / 2.0)
+      isInversed->setValue(false);
+    myParamBefore = aParameterNew;
+
+    // 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;
   }
 }
 
@@ -719,7 +658,8 @@ void SketchPlugin_Arc::tangencyArcConstraints()
           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
       ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
           aRefAttrB->object() : aRefAttrA->object();
-      if (anOtherPoint->owner() == anOtherObject) {
+      FeaturePtr anOtherFeature = ModelAPI_Feature::feature(anOtherObject);
+      if (anOtherPoint->owner() == anOtherFeature) {
         isFound = true;
         aPrevCoincidence = *aCIt;
         aPrevTangency = *aTIt;
@@ -755,10 +695,12 @@ void SketchPlugin_Arc::tangencyArcConstraints()
     // Remove all obtained constraints which use current arc, because
     // there is no information which of them were used to build tangency arc.
     DocumentPtr aDoc = sketch()->document();
+    std::set<FeaturePtr> aFeaturesToBeRemoved;
     for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
-      aDoc->removeFeature(*aCIt);
+      aFeaturesToBeRemoved.insert(*aCIt);
     for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
-      aDoc->removeFeature(*aTIt);
+      aFeaturesToBeRemoved.insert(*aTIt);
+    ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
     // Send events to update the sub-features by the solver.
     if (isDeleteFlushed)
       Events_Loop::loop()->setFlushed(aDeleteEvent, true);
@@ -766,10 +708,10 @@ void SketchPlugin_Arc::tangencyArcConstraints()
       Events_Loop::loop()->flush(aDeleteEvent);
 
     // Wait all constraints being created, then send update events
-    static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
-    bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
-    if (isUpdateFlushed)
-      Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+    static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
+    bool isCreateFlushed = Events_Loop::loop()->isFlushed(aCreateEvent);
+    if (isCreateFlushed)
+      Events_Loop::loop()->setFlushed(aCreateEvent, false);
 
     // Create new constraints
     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
@@ -780,7 +722,7 @@ void SketchPlugin_Arc::tangencyArcConstraints()
     aRefAttrA->setAttr(aStartAttr);
     aRefAttrB->setAttr(aTangPtAttr->attr());
     aConstraint->execute();
-    ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+    ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
 
     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
@@ -790,32 +732,12 @@ void SketchPlugin_Arc::tangencyArcConstraints()
     aRefAttrA->setObject(aThisArc);
     aRefAttrB->setObject(aTangFeature);
     aConstraint->execute();
-    ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
+    ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
 
     // Send events to update the sub-features by the solver.
-    if(isUpdateFlushed)
-      Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+    if(isCreateFlushed)
+      Events_Loop::loop()->setFlushed(aCreateEvent, true);
+    else
+      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);
-}