]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Arc.cpp
Salome HOME
Fixed validators for Boolean and Intersection
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Arc.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Arc.cpp
4 // Created:     26 Apr 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "SketchPlugin_Arc.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintTangent.h>
11
12 #include <Events_Loop.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Tools.h>
23
24 #include <GeomAPI_Ax2.h>
25 #include <GeomAPI_Circ2d.h>
26 #include <GeomAPI_Circ.h>
27 #include <GeomAPI_Dir2d.h>
28 #include <GeomAPI_Dir.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_XY.h>
33 #include <GeomDataAPI_Point2D.h>
34 #include <GeomDataAPI_Dir.h>
35 #include <GeomAlgoAPI_PointBuilder.h>
36 #include <GeomAlgoAPI_EdgeBuilder.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
38 // for sqrt on Linux
39 #include <math.h>
40
41 const double tolerance = 1e-7;
42 const double paramTolerance = 1.e-4;
43 const double PI =3.141592653589793238463;
44
45 namespace {
46   static const std::string& POINT_ID(int theIndex)
47   {
48     switch (theIndex) {
49     case 1: return SketchPlugin_Arc::START_ID();
50     case 2: return SketchPlugin_Arc::END_ID();
51     case 3: return SketchPlugin_Arc::PASSED_POINT_ID();
52     }
53
54     static const std::string DUMMY;
55     return DUMMY;
56   }
57 }
58
59
60
61 SketchPlugin_Arc::SketchPlugin_Arc()
62     : SketchPlugin_SketchEntity()
63 {
64   myStartUpdate = false;
65   myEndUpdate = false;
66   // default values
67   myXEndBefore = 0;
68   myYEndBefore = 0;
69
70   myParamBefore = 0;
71 }
72
73 void SketchPlugin_Arc::initDerivedClassAttributes()
74 {
75   data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
76   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
77   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
78     GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
79   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
80   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
81
82   AttributeBooleanPtr isInversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
83     data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
84
85   // get the initial values
86   if (anEndAttr->isInitialized()) {
87     myXEndBefore = anEndAttr->x();
88     myYEndBefore = anEndAttr->y();
89   }
90
91   AttributeStringPtr anArcType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
92     data()->addAttribute(ARC_TYPE(), ModelAPI_AttributeString::typeId()));
93
94   data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
95   data()->addAttribute(TANGENT_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
96   data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
97   data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
98
99   // set after all to avoid in attributeChanged reference to not existing attributes
100   if (!isInversed->isInitialized())
101     isInversed->setValue(false);
102   anArcType->setValue(ARC_TYPE_CENTER_START_END());
103 }
104
105 void SketchPlugin_Arc::execute()
106 {
107   SketchPlugin_Sketch* aSketch = sketch();
108   // result for the arc is set only when all obligatory attributes are initialized,
109   // otherwise AIS object is used to visualize the arc's preview
110   if (aSketch && isFeatureValid()) {
111     bool hasResult = lastResult().get() != NULL;
112
113     // compute a circle point in 3D view
114     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
115         GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
116     // compute the arc start point
117     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
118         GeomDataAPI_Point2D>(data()->attribute(START_ID()));
119
120     std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
121     // make a visible point
122     std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
123     std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
124         data(), 0);
125     aConstr1->setShape(aCenterPointShape);
126     aConstr1->setIsInHistory(false);
127     setResult(aConstr1, 0);
128
129     // make a visible circle
130     std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
131         aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
132     std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
133     std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
134
135     // compute and change the arc end point
136     std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
137         GeomDataAPI_Point2D>(data()->attribute(END_ID()));
138     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
139         new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
140     std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
141     if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
142       anEndAttr->setValue(aProjection);
143     std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
144     AttributeBooleanPtr isInversed =
145         std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
146
147     std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
148     std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
149     std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
150     double aParameterNew = 0.0;
151     if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
152       if(0 <= myParamBefore && myParamBefore <= PI / 2.0
153         && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
154           isInversed->setValue(true);
155       } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
156         && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
157           isInversed->setValue(false);
158       }
159     }
160     myParamBefore = aParameterNew;
161
162     std::shared_ptr<GeomAPI_Shape> aCircleShape;
163     if(!isInversed->value()) {
164       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
165     } else {
166       aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
167     }
168
169     if (aCircleShape) {
170       std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
171           data(), 1);
172       aConstr2->setShape(aCircleShape);
173       aConstr2->setIsInHistory(false);
174       setResult(aConstr2, 1);
175     }
176   }
177 }
178
179 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
180 {
181   SketchPlugin_Sketch* aSketch = sketch();
182   if (aSketch) {
183     // if the feature is valid, the execute() method should be performed, AIS object is empty
184     if (!isFeatureValid()) {
185       // compute a circle point in 3D view
186       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
187           GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
188
189       std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
190       if (aCenterAttr->isInitialized()) {
191         std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
192
193         std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
194             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
195         std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<
196             GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
197         AttributeStringPtr aTypeAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
198             data()->attribute(ARC_TYPE()));
199
200         if (aStartAttr->isInitialized()) {
201           // make a visible circle
202           std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
203               aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
204           bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
205           if (aHasPlane) {
206             std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
207             std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
208             std::shared_ptr<GeomAPI_Pnt> aEndPoint = aStartPoint;
209             if (aTypeAttr && aTypeAttr->isInitialized() &&
210                 aTypeAttr->value() == ARC_TYPE_THREE_POINTS() && aEndAttr->isInitialized() &&
211                 aEndAttr->pnt()->distance(aStartAttr->pnt()) > tolerance) {
212               aEndPoint = aSketch->to3D(aEndAttr->x(), aEndAttr->y());
213               std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = 
214                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
215               if (!aPassedAttr->isInitialized()) { // calculate the appropriate center for the presentation
216                 // check that center is bad for the current start and end and must be recomputed
217                 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(
218                   aCenterAttr->pnt(), aStartAttr->pnt()));
219                 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aEndAttr->pnt());
220                 if (!aProjection.get() || aEndAttr->pnt()->distance(aProjection) > tolerance) {
221                   std::shared_ptr<GeomAPI_XY> aDir = 
222                     aEndAttr->pnt()->xy()->decreased(aStartAttr->pnt()->xy())->multiplied(0.5);
223                   double x = aDir->x();
224                   double y = aDir->y();
225                   aDir->setX(x - y);
226                   aDir->setY(y + x);
227                   std::shared_ptr<GeomAPI_XY> aCenterXY = aStartAttr->pnt()->xy()->added(aDir);
228                   aCenter = aSketch->to3D(aCenterXY->x(), aCenterXY->y());
229                 }
230               }
231             }
232             AttributeBooleanPtr isInversed =
233               std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
234
235             std::shared_ptr<GeomAPI_Shape> aCircleShape = 
236               (isInversed->isInitialized() && isInversed->value()) ?
237                 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal) :
238                 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
239
240             if (aCircleShape)
241               aShapes.push_back(aCircleShape);
242           }
243         }
244         // make a visible point
245         std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
246         aShapes.push_back(aCenterPointShape);
247       }
248       if (!aShapes.empty()) {
249         std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
250         AISObjectPtr anAIS = thePrevious;
251         if (!anAIS)
252           anAIS = AISObjectPtr(new GeomAPI_AISObject);
253         anAIS->createShape(aCompound);
254         anAIS->setWidth(3);
255         return anAIS;
256       }
257     }
258   }
259   return AISObjectPtr();
260 }
261
262 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
263 {
264   std::shared_ptr<ModelAPI_Data> aData = data();
265   if (!aData->isValid())
266     return;
267
268   aData->blockSendAttributeUpdated(true);
269
270   myStartUpdate = true;
271   myEndUpdate = true;
272   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273       aData->attribute(SketchPlugin_Arc::START_ID()));
274   aPoint2->move(theDeltaX, theDeltaY);
275
276   std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
277       aData->attribute(SketchPlugin_Arc::END_ID()));
278   aPoint3->move(theDeltaX, theDeltaY);
279   myStartUpdate = false;
280   myEndUpdate = false;
281
282   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
283       aData->attribute(SketchPlugin_Arc::CENTER_ID()));
284   aPoint1->move(theDeltaX, theDeltaY);
285
286   std::shared_ptr<GeomDataAPI_Point2D> aPassedPoint =
287       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(PASSED_POINT_ID()));
288   aPassedPoint->move(theDeltaX, theDeltaY);
289   aData->blockSendAttributeUpdated(false);
290 }
291
292 bool SketchPlugin_Arc::isFixed() {
293   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
294 }
295
296 bool SketchPlugin_Arc::isFeatureValid()
297 {
298   AttributeStringPtr anArcTypeAttr =
299       std::dynamic_pointer_cast<ModelAPI_AttributeString>(data()->attribute(ARC_TYPE()));
300   if (!anArcTypeAttr)
301     return false;
302   std::string anArcType = anArcTypeAttr->value();
303
304   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
305       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
306   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
307       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
308   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
309       GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
310   std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr = std::dynamic_pointer_cast<
311       GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
312
313   bool isValid = false;
314   if (anArcType == ARC_TYPE_THREE_POINTS())
315     isValid = aStartAttr->isInitialized() && anEndAttr->isInitialized() && aPassedAttr->isInitialized();
316   else
317     isValid = aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
318
319   return isValid;
320 }
321
322 static inline void adjustPeriod(double& theParam)
323 {
324   static const double PERIOD = 2.0 * PI;
325   while (theParam < 0.0) theParam += PERIOD;
326   while (theParam >= PERIOD) theParam -= PERIOD;
327 }
328
329 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
330 {
331   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
332       GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
333   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
334       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
335   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
336       GeomDataAPI_Point2D>(data()->attribute(END_ID()));
337   // the second condition for unability to move external segments anywhere
338   if (theID == EXTERNAL_ID() || isFixed()) {
339     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
340     // update arguments due to the selection value
341     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
342       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
343       std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
344       if (aCirc.get()) {
345         aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
346         anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
347         aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
348
349         data()->real(RADIUS_ID())->setValue(aCirc->radius());
350         double aStartAngle, aEndAngle;
351         anEdge->getRange(aStartAngle, aEndAngle);
352         data()->real(ANGLE_ID())->setValue(aEndAngle - aStartAngle);
353       }
354     }
355     return;
356   }
357   AttributeStringPtr aTypeAttr =
358       std::dynamic_pointer_cast<ModelAPI_AttributeString>(attribute(ARC_TYPE()));
359
360   // this is before others since here end attribute may be changed, but with the special behavior
361   if (aTypeAttr->value() == ARC_TYPE_TANGENT() && (theID == TANGENT_POINT_ID() || theID == END_ID())) {
362     SketchPlugin_Sketch* aSketch = sketch();
363     AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
364         data()->attribute(TANGENT_POINT_ID()));
365
366     if (aTangPtAttr->isInitialized() && anEndAttr->isInitialized()) {
367       // compute orthogonal direction
368       std::shared_ptr<GeomAPI_Dir2d> anOrthoDir;
369       std::shared_ptr<GeomDataAPI_Point2D> aTangentPoint =
370           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aTangPtAttr->attr());
371       std::shared_ptr<GeomAPI_Pnt2d> aTangPnt2d = aTangentPoint->pnt();
372       FeaturePtr aTangFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
373       std::shared_ptr<GeomAPI_Edge> aTangEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
374           aTangFeature->lastResult()->shape());
375       if (aTangEdge->isLine()) {
376         std::shared_ptr<GeomAPI_Dir> aDir = aTangEdge->line()->direction();
377         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aDir->x(), aDir->y(), aDir->z()));
378         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aSketch->to2D(aPnt);
379         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aPnt2d->y(), aPnt2d->x()));
380       }
381       else if (aTangEdge->isArc()) {
382         std::shared_ptr<GeomAPI_Pnt> aCenter = aTangEdge->circle()->center();
383         std::shared_ptr<GeomAPI_Pnt2d> aCenter2d = aSketch->to2D(aCenter);
384         anOrthoDir = std::shared_ptr<GeomAPI_Dir2d>(
385             new GeomAPI_Dir2d(aTangPnt2d->xy()->decreased(aCenter2d->xy())));
386       }
387
388       // compute parameters of the middle perpendicular
389       std::shared_ptr<GeomAPI_XY> aEndPntCoord = anEndAttr->pnt()->xy();
390       std::shared_ptr<GeomAPI_XY> aTempDir = aEndPntCoord->decreased(aTangPnt2d->xy());
391       std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(-aTempDir->y(), aTempDir->x()));
392       std::shared_ptr<GeomAPI_Pnt2d> aMidPnt(
393           new GeomAPI_Pnt2d(aEndPntCoord->added(aTangPnt2d->xy())->multiplied(0.5)));
394
395       // compute center of arc by calculating intersection of orthogonal line and middle perpendicular
396       std::shared_ptr<GeomAPI_Lin2d> anOrthoLine(new GeomAPI_Lin2d(aTangPnt2d, anOrthoDir));
397       std::shared_ptr<GeomAPI_Lin2d> aMiddleLine(new GeomAPI_Lin2d(aMidPnt, aMidDir));
398       std::shared_ptr<GeomAPI_Pnt2d> aCenter = anOrthoLine->intersect(aMiddleLine);
399       if (aCenter) {
400         data()->blockSendAttributeUpdated(true);
401         aCenterAttr->setValue(aCenter);
402         aStartAttr->setValue(aTangPnt2d);
403         data()->blockSendAttributeUpdated(false);
404       }
405
406       tangencyArcConstraints();
407     }
408     return;
409   }
410
411   // if changed the base attributes, update all other (is necessary) without recursion
412   if (theID == CENTER_ID() || theID == START_ID() || theID == END_ID() || theID == ARC_TYPE()) {
413     if (!isFeatureValid())
414       return;
415     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
416     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
417     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
418     double aRadius = aCenter->distance(aStart);
419     if (aRadius < tolerance)
420       return;
421     std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
422
423     data()->blockSendAttributeUpdated(true);
424     if (theID == END_ID() && isStable()) {
425       // The arc is under construction, so its end point projected
426       // on the circle formed by center and start points
427       std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
428       if (aProjection && anEnd->distance(aProjection) > tolerance) {
429         anEndAttr->setValue(aProjection);
430         anEnd = aProjection;
431       }
432     }
433     // update all other attributes due to the base attributes values
434     if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // update passed point due to start, end and center
435       if (aCenter->distance(aStart) > tolerance && aCenter->distance(anEnd) > tolerance) {
436         // project passed point t othe circle
437         std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
438           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
439         if (aPassedAttr->isInitialized()) {
440           std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aPassedAttr->pnt());
441           if (aProjection && aPassedAttr->pnt()->distance(aProjection) > tolerance) {
442             aPassedAttr->setValue(aProjection);
443           }
444         } else { // initialize it by some middle - value
445           std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
446             aStart->xy()->decreased(aCenter->xy())));
447           std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
448             anEnd->xy()->decreased(aCenter->xy())));
449           std::shared_ptr<GeomAPI_XY> aMidDirXY = aStartDir->xy()->added(aEndDir->xy());
450           if (aMidDirXY->dot(aMidDirXY) < tolerance * tolerance) {
451             // start and end directions are opposite, so middle direction will be orthogonal
452             aMidDirXY->setX(-aStartDir->y());
453             aMidDirXY->setY(aStartDir->x());
454           }
455           std::shared_ptr<GeomAPI_Dir2d> aMidDir(new GeomAPI_Dir2d(aMidDirXY));
456           if ((aStartDir->cross(aMidDir) > 0) ^ !isReversed())
457             aMidDir->reverse();
458           std::shared_ptr<GeomAPI_XY> aPassedPnt = 
459             aCenter->xy()->added(aMidDir->xy()->multiplied(aCenter->distance(aStart)));
460           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()))->
461             setValue(aPassedPnt->x(), aPassedPnt->y());
462         }
463       }
464     }
465     // update radius and angle
466     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
467         data()->attribute(RADIUS_ID()));
468     aRadiusAttr->setValue(aRadius);
469     AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
470         data()->attribute(ANGLE_ID()));
471     std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aStart));
472     double aStartParam, aEndParam;
473     aCircle->parameter(aStart, paramTolerance, aStartParam);
474     aCircle->parameter(anEnd, paramTolerance, aEndParam);
475     adjustPeriod(aStartParam);
476     adjustPeriod(aEndParam);
477     if (aTypeAttr->value() == ARC_TYPE_THREE_POINTS()) { // use the passed point for the angle calculation
478       std::shared_ptr<GeomDataAPI_Point2D> aPassedAttr =
479         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(PASSED_POINT_ID()));
480       double aPassedParam;
481       aCircle->parameter(aPassedAttr->pnt(), paramTolerance, aPassedParam);
482       adjustPeriod(aPassedParam);
483       double aNewAngle = aPassedParam >= aStartParam && aPassedParam <= aEndParam ?
484         ((aEndParam - aStartParam) * 180.0 / PI) :
485         ((aEndParam - aStartParam - 2.0 * PI) * 180.0 / PI);
486       if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
487         anAngleAttr->setValue(aNewAngle);
488     } else {
489       double aNewAngle = (aEndParam - aStartParam) * 180.0 / PI;
490       if (fabs(aNewAngle - anAngleAttr->value()) > tolerance)
491         anAngleAttr->setValue(aNewAngle);
492     }
493     // do not need to inform that other parameters were changed in this basis mode: these arguments
494     // change is enough
495     data()->blockSendAttributeUpdated(false, false);
496     return;
497   }
498
499   if (theID == PASSED_POINT_ID()) {
500     data()->blockSendAttributeUpdated(true);
501
502     std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
503     int aNbInitialized = 0;
504     for (int i = 1; i <= 3; ++i) {
505       std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
506           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
507       if (aCurPnt->isInitialized())
508         aPoints[aNbInitialized++] = aCurPnt->pnt();
509     }
510
511     if (aNbInitialized == 3) {
512       std::shared_ptr<GeomAPI_Circ2d> aCircle(
513           new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
514
515       std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
516       if (aCenter) {
517         aCenterAttr->setValue(aCenter);
518       }
519     }
520     data()->blockSendAttributeUpdated(false);
521     return;
522   }
523
524   if (theID == RADIUS_ID()) {
525     if (!aStartAttr->isInitialized() || !anEndAttr->isInitialized() || !aCenterAttr->isInitialized())
526       return;
527     // move center and passed point
528     std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
529     std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
530     std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
531     if (aStart->distance(aCenter) < tolerance || anEnd->distance(aCenter) < tolerance)
532       return;
533     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
534         data()->attribute(RADIUS_ID()));
535     double aRadius = aRadiusAttr->value();
536
537     data()->blockSendAttributeUpdated(true);
538     std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
539     std::shared_ptr<GeomAPI_XY> aNewStart = aStartDir->xy()->multiplied(aRadius)->added(aCenter->xy());
540     aStartAttr->setValue(aNewStart->x(), aNewStart->y());
541     std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEnd->xy()->decreased(aCenter->xy())));
542     std::shared_ptr<GeomAPI_XY> aNewEnd = anEndDir->xy()->multiplied(aRadius)->added(aCenter->xy());
543     anEndAttr->setValue(aNewEnd->x(), aNewEnd->y());
544     data()->blockSendAttributeUpdated(false);
545     return;
546   }
547   if (theID == ANGLE_ID()) {
548     if (!aStartAttr->isInitialized() || !aCenterAttr->isInitialized())
549       return;
550     AttributeDoublePtr anAngleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
551         data()->attribute(ANGLE_ID()));
552     data()->blockSendAttributeUpdated(true);
553     // move end point and passed point
554     std::shared_ptr<GeomAPI_XY> aCenter = aCenterAttr->pnt()->xy();
555     double anAngle = anAngleAttr->value() * PI / 180.0;
556     double sinA = sin(anAngle);
557     double cosA = cos(anAngle);
558     std::shared_ptr<GeomAPI_XY> aStartDir = aStartAttr->pnt()->xy()->decreased(aCenter);
559     std::shared_ptr<GeomAPI_XY> aDir(new GeomAPI_XY(
560         aStartDir->x() * cosA - aStartDir->y() * sinA,
561         aStartDir->x() * sinA + aStartDir->y() * cosA));
562     anEndAttr->setValue(aCenter->x() + aDir->x(), aCenter->y() + aDir->y());
563     data()->blockSendAttributeUpdated(false);
564     return;
565   }
566 }
567
568 void SketchPlugin_Arc::setReversed(bool isReversed)
569 {
570   std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
571   myParamBefore = 0.0;
572 }
573
574 bool SketchPlugin_Arc::isReversed()
575 {
576   return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();
577 }
578
579 void SketchPlugin_Arc::tangencyArcConstraints()
580 {
581   if (!lastResult())
582     return;
583
584   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
585       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
586   AttributeRefAttrPtr aTangPtAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
587       attribute(TANGENT_POINT_ID()));
588   if (!aTangPtAttr->attr())
589     return;
590
591   FeaturePtr aFeature = ModelAPI_Feature::feature(aStartAttr->owner());
592   ObjectPtr aThisArc = aFeature->lastResult();
593   aFeature = ModelAPI_Feature::feature(aTangPtAttr->attr()->owner());
594   ObjectPtr aTangFeature = aFeature->lastResult();
595
596   // trying to find constraints to fix the tangency of the arc
597   std::set<FeaturePtr> aCoincidence;
598   std::set<FeaturePtr> aTangency;
599
600   AttributeRefAttrPtr aRefAttrA, aRefAttrB;
601   std::set<AttributePtr> aRefs = data()->refsToMe();
602   const std::set<AttributePtr>& aRefsToResult = lastResult()->data()->refsToMe();
603   aRefs.insert(aRefsToResult.begin(), aRefsToResult.end());
604   std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
605   for (; aRefIt != aRefs.end(); ++aRefIt) {
606     FeaturePtr aConstrFeature = ModelAPI_Feature::feature((*aRefIt)->owner());
607     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
608       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
609           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
610       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
611           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
612       if ((aRefAttrA && aRefAttrA->attr() == aStartAttr) ||
613           (aRefAttrB && aRefAttrB->attr() == aStartAttr))
614         aCoincidence.insert(aConstrFeature);
615     }
616     else if (aConstrFeature->getKind() == SketchPlugin_ConstraintTangent::ID()) {
617       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
618           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
619       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
620           aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
621       if ((aRefAttrA && aRefAttrA->object() == aThisArc) ||
622           (aRefAttrB && aRefAttrB->object() == aThisArc))
623         aTangency.insert(aConstrFeature);
624     }
625   }
626   // search applicable pair of constraints
627   bool isFound = false;
628   FeaturePtr aPrevCoincidence, aPrevTangency;
629   std::set<FeaturePtr>::const_iterator aCIt, aTIt;
630   for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end() && !isFound; ++aCIt) {
631     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
632         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
633     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
634         (*aCIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
635     AttributePtr anOtherPoint =
636         aRefAttrA->attr() == aStartAttr ? aRefAttrB->attr() : aRefAttrA->attr();
637     for (aTIt = aTangency.begin(); aTIt != aTangency.end() && !isFound; ++aTIt) {
638       aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
639           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_A()));
640       aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
641           (*aTIt)->attribute(SketchPlugin_Constraint::ENTITY_B()));
642       ObjectPtr anOtherObject = aRefAttrA->object() == aThisArc ?
643           aRefAttrB->object() : aRefAttrA->object();
644       FeaturePtr anOtherFeature = ModelAPI_Feature::feature(anOtherObject);
645       if (anOtherPoint->owner() == anOtherFeature) {
646         isFound = true;
647         aPrevCoincidence = *aCIt;
648         aPrevTangency = *aTIt;
649       }
650     }
651   }
652
653   if (isFound) {
654     // update previous constraints
655     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
656         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_A()));
657     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
658         aPrevCoincidence->attribute(SketchPlugin_Constraint::ENTITY_B()));
659     if (aRefAttrA->attr() == aStartAttr)
660       aRefAttrB->setAttr(aTangPtAttr->attr());
661     else
662       aRefAttrA->setAttr(aTangPtAttr->attr());
663
664     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
665         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_A()));
666     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
667         aPrevTangency->attribute(SketchPlugin_Constraint::ENTITY_B()));
668     if (aRefAttrA->object() == aThisArc)
669       aRefAttrB->setObject(aTangFeature);
670     else
671       aRefAttrA->setObject(aTangFeature);
672   } else {
673     // Wait all constraints being removed, then send update events
674     static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
675     bool isDeleteFlushed = Events_Loop::loop()->isFlushed(aDeleteEvent);
676     if (isDeleteFlushed)
677       Events_Loop::loop()->setFlushed(aDeleteEvent, false);
678     // Remove all obtained constraints which use current arc, because
679     // there is no information which of them were used to build tangency arc.
680     DocumentPtr aDoc = sketch()->document();
681     std::set<FeaturePtr> aFeaturesToBeRemoved;
682     for (aCIt = aCoincidence.begin(); aCIt != aCoincidence.end(); ++aCIt)
683       aFeaturesToBeRemoved.insert(*aCIt);
684     for (aTIt = aTangency.begin(); aTIt != aTangency.end(); ++aTIt)
685       aFeaturesToBeRemoved.insert(*aTIt);
686     ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
687     // Send events to update the sub-features by the solver.
688     if (isDeleteFlushed)
689       Events_Loop::loop()->setFlushed(aDeleteEvent, true);
690     else
691       Events_Loop::loop()->flush(aDeleteEvent);
692
693     // Wait all constraints being created, then send update events
694     static Events_ID aCreateEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
695     bool isCreateFlushed = Events_Loop::loop()->isFlushed(aCreateEvent);
696     if (isCreateFlushed)
697       Events_Loop::loop()->setFlushed(aCreateEvent, false);
698
699     // Create new constraints
700     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
701     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
702         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
703     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
704         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
705     aRefAttrA->setAttr(aStartAttr);
706     aRefAttrB->setAttr(aTangPtAttr->attr());
707     aConstraint->execute();
708     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
709
710     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
711     aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
712         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
713     aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
714         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
715     aRefAttrA->setObject(aThisArc);
716     aRefAttrB->setObject(aTangFeature);
717     aConstraint->execute();
718     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, aCreateEvent);
719
720     // Send events to update the sub-features by the solver.
721     if(isCreateFlushed)
722       Events_Loop::loop()->setFlushed(aCreateEvent, true);
723     else
724       Events_Loop::loop()->flush(aCreateEvent);
725   }
726 }