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