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