Salome HOME
Issue #1084: parameter cyclic dependence
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintFillet.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintFillet.cpp
4 // Created: 19 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintFillet.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Line.h>
24 #include <SketchPlugin_Sketch.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintRadius.h>
28 #include <SketchPlugin_Tools.h>
29
30 #include <SketcherPrs_Factory.h>
31 #include <SketcherPrs_Tools.h>
32
33 #include <Config_PropManager.h>
34 #include <Events_Loop.h>
35
36 #define _USE_MATH_DEFINES
37 #include <math.h>
38
39 static const std::string PREVIOUS_VALUE("FilletPreviousRadius");
40
41 /// \brief Attract specified point on theNewArc to the attribute of theFeature
42 static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
43   FeaturePtr theFeature, const std::string& theFeatureAttribute);
44
45 /// \brief Calculates center of fillet arc and coordinates of tangency points
46 static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
47                                   double theRadius, bool theNotInversed[2],
48                                   std::shared_ptr<GeomAPI_XY>& theCenter,
49                                   std::shared_ptr<GeomAPI_XY>& theTangentA,
50                                   std::shared_ptr<GeomAPI_XY>& theTangentB);
51
52 SketchPlugin_ConstraintFillet::SketchPlugin_ConstraintFillet()
53 {
54 }
55
56 void SketchPlugin_ConstraintFillet::initAttributes()
57 {
58   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
59   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
60   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
61   // This attribute used to store base edges
62   data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
63   data()->addAttribute(PREVIOUS_VALUE, ModelAPI_AttributeDouble::typeId());
64   // initialize attribute not applicable for user
65   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
66   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
67   data()->attribute(PREVIOUS_VALUE)->setInitialized();
68   std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(PREVIOUS_VALUE))->setValue(0.0);
69 }
70
71 void SketchPlugin_ConstraintFillet::execute()
72 {
73   // the viewer update should be blocked in order to avoid the temporaty fillet sub-features visualization
74   // before they are processed by the solver
75   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
76   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
77   //Events_Loop::loop()->send(aMsg);
78
79   std::shared_ptr<ModelAPI_Data> aData = data();
80   ResultConstructionPtr aRC;
81   // Check the base objects are initialized
82   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83       aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
84   AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
85       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
86   if (!aBaseA->isInitialized() || aBaseA->isObject()) {
87     setError("Bad vertex selected");
88     return;
89   }
90
91   // Obtain fillet point
92   std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseA->attr());
93   if (!aBasePoint) {
94     setError("Bad vertex selected");
95     return;
96   }
97   std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
98
99   // Check the fillet shapes is not initialized yet
100   AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
101       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
102   bool needNewObjects = aRefListOfFillet->size() == 0;
103
104   AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
105       aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
106
107   // Obtain base features
108   FeaturePtr anOldFeatureA, anOldFeatureB;
109   std::list<ObjectPtr> anOldFeatList = aRefListOfBaseLines->list();
110   std::list<ObjectPtr>::iterator aFeatIt = anOldFeatList.begin();
111   anOldFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
112   anOldFeatureB = ModelAPI_Feature::feature(*aFeatIt);
113
114   if(!anOldFeatureA.get() || !anOldFeatureB.get()) {
115     setError("One of the edges is empty");
116     return;
117   }
118
119   FeaturePtr aNewFeatureA, aNewFeatureB, aNewArc;
120   if (needNewObjects) {
121     // Create list of objects composing a fillet
122     // copy aFeatureA
123     aNewFeatureA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureA, sketch());
124     // copy aFeatureB
125     aNewFeatureB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(anOldFeatureB, sketch());
126     // create filleting arc (it will be attached to the list later)
127     aNewArc = sketch()->addFeature(SketchPlugin_Arc::ID());
128   } else {
129     // Obtain features from the list
130     std::list<ObjectPtr> aNewFeatList = aRefListOfFillet->list();
131     std::list<ObjectPtr>::iterator aFeatIt = aNewFeatList.begin();
132     aNewFeatureA = ModelAPI_Feature::feature(*aFeatIt++);
133     aNewFeatureB = ModelAPI_Feature::feature(*aFeatIt++);
134     aNewArc = ModelAPI_Feature::feature(*aFeatIt);
135   }
136
137   // Calculate arc attributes
138   static const int aNbFeatures = 2;
139   FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
140   FeaturePtr aNewFeature[aNbFeatures] = {aNewFeatureA, aNewFeatureB};
141   std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
142   bool isStart[aNbFeatures]; // indicates which point the features share
143   std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair -  to second
144   std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
145   for (int i = 0; i < aNbFeatures; i++) {
146     std::string aStartAttr, aEndAttr;
147     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
148       aStartAttr = SketchPlugin_Line::START_ID();
149       aEndAttr = SketchPlugin_Line::END_ID();
150     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
151       aStartAttr = SketchPlugin_Arc::START_ID();
152       aEndAttr = SketchPlugin_Arc::END_ID();
153     } else { // wrong argument
154       aRefListOfFillet->remove(aNewFeatureA);
155       aRefListOfFillet->remove(aNewFeatureB);
156       aRefListOfFillet->remove(aNewArc);
157       aRefListOfBaseLines->clear();
158       return;
159     }
160     aFeatAttributes[2*i] = aStartAttr;
161     aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
162         aFeature[i]->attribute(aStartAttr))->pnt();
163     aFeatAttributes[2*i+1] = aEndAttr;
164     aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165         aFeature[i]->attribute(aEndAttr))->pnt();
166   }
167   for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
168     for (int j = 0; j < 2; j++) // loop on start-end of each feature
169       if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPoint) < 1.e-10) {
170         isStart[aFeatInd] = (j==0);
171         break;
172       }
173   }
174   // tangent directions of the features
175   for (int i = 0; i < aNbFeatures; i++) {
176     std::shared_ptr<GeomAPI_XY> aDir;
177     if (aNewFeature[i]->getKind() == SketchPlugin_Line::ID()) {
178       aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
179       if (!isStart[i])
180         aDir = aDir->multiplied(-1.0);
181     } else if (aNewFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
182       std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
183           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184           aNewFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
185       aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
186       aDir = aDir->decreased(aCenterPoint->xy());
187
188       double x = aDir->x();
189       double y = aDir->y();
190       aDir->setX(-y);
191       aDir->setY(x);
192       if (!isStart[i])
193         aDir = aDir->multiplied(-1.0);
194     }
195     aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
196   }
197
198   // Wait all constraints being created, then send update events
199   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
200   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
201   if (isUpdateFlushed)
202     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
203
204   // By default, the start point of fillet arc is connected to FeatureA,
205   // and the end point - to FeatureB. But when the angle between TangentDirA and
206   // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
207   double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
208   bool isReversed = cosBA > 0.0;
209
210   // Calculate fillet arc parameters
211   std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
212   calculateFilletCenter(anOldFeatureA, anOldFeatureB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
213   // update features
214   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
215       aNewFeatureA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(
216       aTangentPntA->x(), aTangentPntA->y());
217   aNewFeatureA->execute();
218   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
219       aNewFeatureB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(
220       aTangentPntB->x(), aTangentPntB->y());
221   aNewFeatureB->execute();
222   // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
223   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
224   aNewArc->data()->blockSendAttributeUpdated(true);
225   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
226       aNewArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(
227       aCenter->x(), aCenter->y());
228   if (isReversed) {
229     std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
230     aTangentPntA = aTangentPntB;
231     aTangentPntB = aTmp;
232   }
233   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
234       aNewArc->attribute(SketchPlugin_Arc::START_ID()))->setValue(aTangentPntA->x(), aTangentPntA->y());
235   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236       aNewArc->attribute(SketchPlugin_Arc::END_ID()))->setValue(aTangentPntB->x(), aTangentPntB->y());
237   aNewArc->data()->blockSendAttributeUpdated(false);
238   aNewArc->execute();
239
240   if (needNewObjects) {
241     // attach new arc to the list
242     aRefListOfFillet->append(aNewFeatureA->lastResult());
243     aRefListOfFillet->append(aNewFeatureB->lastResult());
244     aRefListOfFillet->append(aNewArc->lastResult());
245
246     myProducedFeatures.push_back(aNewFeatureA);
247     myProducedFeatures.push_back(aNewFeatureB);
248     myProducedFeatures.push_back(aNewArc);
249
250     // Create list of additional constraints:
251     // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
252     // 1.1. coincidence
253     FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
254     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
255         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
256     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::START_ID()));
257     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
258         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
259     int aFeatInd = isReversed ? 1 : 0;
260     int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
261     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
262     recalculateAttributes(aNewArc, SketchPlugin_Arc::START_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
263     aConstraint->execute();
264     myProducedFeatures.push_back(aConstraint);
265     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
266     // 1.2. coincidence
267     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
268     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
269         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
270     aRefAttr->setAttr(aNewArc->attribute(SketchPlugin_Arc::END_ID()));
271     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
272         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
273     aFeatInd = isReversed ? 0 : 1;
274     anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
275     aRefAttr->setAttr(aNewFeature[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
276     recalculateAttributes(aNewArc, SketchPlugin_Arc::END_ID(), aNewFeature[aFeatInd], aFeatAttributes[anAttrInd]);
277     aConstraint->execute();
278     myProducedFeatures.push_back(aConstraint);
279     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
280     // 2. Fillet arc radius
281     //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
282     //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
283     //    aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
284     //aRefAttr->setObject(aNewArc->lastResult());
285     //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
286     //    aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
287     //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
288     //    aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
289     //    isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
290     //aConstraint->execute();
291     //myProducedFeatures.push_back(aConstraint);
292     //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
293     // 3. Tangency of fillet arc and features
294     for (int i = 0; i < aNbFeatures; i++) {
295       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
296       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
297           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
298       aRefAttr->setObject(aNewArc->lastResult());
299       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
300           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
301       bool isArc = aNewFeature[i]->getKind() == SketchPlugin_Arc::ID();
302       aRefAttr->setObject(isArc ? aNewFeature[i]->lastResult() : aNewFeature[i]->firstResult());
303       aConstraint->execute();
304       myProducedFeatures.push_back(aConstraint);
305       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
306     }
307     // 4. Coincidence of free boundaries of base and copied features
308     for (int i = 0; i < aNbFeatures; i++) {
309       anAttrInd = 2*i + (isStart[i] ? 1 : 0);
310       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
311       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
312           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
313       aRefAttr->setAttr(aFeature[i]->attribute(aFeatAttributes[anAttrInd]));
314       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
315           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
316       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
317       myProducedFeatures.push_back(aConstraint);
318     }
319     // 5. Tangent points should be placed on the base features
320     for (int i = 0; i < aNbFeatures; i++) {
321       anAttrInd = 2*i + (isStart[i] ? 0 : 1);
322       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
323       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
324           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
325       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
326       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
327           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
328       aRefAttr->setObject(aFeature[i]->lastResult());
329       myProducedFeatures.push_back(aConstraint);
330     }
331     // make base features auxiliary
332     anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
333     anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
334     myBaseObjects.clear();
335     myBaseObjects.push_back(anOldFeatureA);
336     myBaseObjects.push_back(anOldFeatureB);
337     // exchange the naming IDs of newly created and old line that become auxiliary
338     sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
339     sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
340   } else {
341     // Update radius value
342     int aNbSubs = sketch()->numberOfSubs();
343     FeaturePtr aSubFeature;
344     for (int aSub = 0; aSub < aNbSubs; aSub++) {
345       aSubFeature = sketch()->subFeature(aSub);
346       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
347         continue;
348       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
349           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
350       if (!aRefAttr || !aRefAttr->isObject())
351         continue;
352       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
353       if (aFeature == aNewArc) {
354         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
355           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
356         aRadius->setValue(aFilletRadius);
357         break;
358       }
359     }
360   }
361
362   // send events to update the sub-features by the solver
363   if (isUpdateFlushed)
364     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
365
366   // the viewer update should be unblocked in order after the fillet features
367   // are processed by the solver
368   //aMsg = std::shared_ptr<Events_Message>(
369   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
370   //Events_Loop::loop()->send(aMsg);
371 }
372
373 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
374 {
375   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
376     // clear the list of fillet entities
377     AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
378         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
379     aRefListOfFillet->clear();
380
381     // clear the list of base features
382     AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
383         data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
384       aRefListOfBaseLines->clear();
385
386     // remove all produced objects and constraints
387     DocumentPtr aDoc = sketch()->document();
388     std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
389     for (; aCIt != myProducedFeatures.end(); ++aCIt)
390       aDoc->removeFeature(*aCIt);
391     myProducedFeatures.clear();
392
393     // clear auxiliary flag on initial objects
394     for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
395       (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
396     myBaseObjects.clear();
397
398     AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
399         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
400     if(!aBaseA->isInitialized() || aBaseA->isObject()) {
401       return;
402     }
403
404     AttributePtr anAttrBase = aBaseA->attr();
405     const std::set<AttributePtr>& aRefsList = anAttrBase->owner()->data()->refsToMe();
406     std::set<AttributePtr>::const_iterator aIt;
407     FeaturePtr aCoincident;
408     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
409       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
410       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
411       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
412         AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
413           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
414         AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
415           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
416         if(anAttrRefA.get() && !anAttrRefA->isObject()) {
417           AttributePtr anAttrA = anAttrRefA->attr();
418           if(anAttrBase == anAttrA) {
419             aCoincident = aConstrFeature;
420             break;
421           }
422         }
423         if(anAttrRefA.get() && !anAttrRefB->isObject()) {
424           AttributePtr anAttrB = anAttrRefB->attr();
425           if(anAttrBase == anAttrB) {
426             aCoincident = aConstrFeature;
427             break;
428           }
429         }
430       }
431     }
432
433     if(!aCoincident.get()) {
434       setError("No coincident edges at selected vertex");
435       return;
436     }
437
438     std::set<FeaturePtr> aCoinsideLines;
439     SketchPlugin_Tools::findCoincidences(aCoincident,
440                                          SketchPlugin_ConstraintCoincidence::ENTITY_A(),
441                                          aCoinsideLines);
442     SketchPlugin_Tools::findCoincidences(aCoincident,
443                                          SketchPlugin_ConstraintCoincidence::ENTITY_B(),
444                                          aCoinsideLines);
445
446     // Remove auxilary lines
447     if(aCoinsideLines.size() > 2) {
448       std::set<FeaturePtr> aNewLines;
449       for(std::set<FeaturePtr>::iterator anIt = aCoinsideLines.begin(); anIt != aCoinsideLines.end(); ++anIt) {
450         if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
451           aNewLines.insert(*anIt);
452         }
453       }
454       aCoinsideLines = aNewLines;
455     }
456
457
458     if(aCoinsideLines.size() != 2) {
459       setError("At selected vertex should be two coincident lines");
460       return;
461     }
462
463     FeaturePtr anOldFeatureA, anOldFeatureB;
464     std::set<FeaturePtr>::iterator aLinesIt = aCoinsideLines.begin();
465     anOldFeatureA = *aLinesIt++;
466     anOldFeatureB = *aLinesIt;
467     aRefListOfBaseLines->append(anOldFeatureA);
468     aRefListOfBaseLines->append(anOldFeatureB);
469
470
471     // Set default value equal to 1/3 of the smallest line sharing the point.
472     static const int aNbFeatures = 2;
473     FeaturePtr aFeature[aNbFeatures] = {anOldFeatureA, anOldFeatureB};
474     double aLength = 0;
475
476     double aLengths[aNbFeatures];
477     for (int i = 0; i < aNbFeatures; i++) {
478       std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
479         aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::START_ID() : SketchPlugin_Arc::START_ID()))->pnt();
480       std::shared_ptr<GeomAPI_Pnt2d> anEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
481         aFeature[i]->getKind() == SketchPlugin_Line::ID() ? SketchPlugin_Line::END_ID() : SketchPlugin_Arc::END_ID()))->pnt();
482       if(aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
483         aLengths[i] = aStartPnt->distance(anEndPnt);
484       } else {
485         std::shared_ptr<GeomAPI_Pnt2d> anArcCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature[i]->attribute(
486           SketchPlugin_Arc::CENTER_ID()))->pnt();
487         std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(aStartPnt->xy()->decreased(anArcCenter->xy())));
488         std::shared_ptr<GeomAPI_Dir2d> anEndDir(new GeomAPI_Dir2d(anEndPnt->xy()->decreased(anArcCenter->xy())));
489         double aRadius = aStartPnt->distance(anArcCenter);
490         double anAngle = aStartDir->angle(anEndDir);
491         aLengths[i] = aRadius * abs(anAngle);
492       }
493     }
494     aLength = aLengths[0];
495     for(int i = 1; i < aNbFeatures; i++) {
496       if(aLengths[i] < aLength) aLength = aLengths[i];
497     }
498     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aLength / 3.0);
499   }
500 }
501
502 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
503 {
504   if (!sketch())
505     return thePrevious;
506
507   AISObjectPtr anAIS = thePrevious;
508   /// TODO: Equal constraint presentation should be put here
509   return anAIS;
510 }
511
512 bool SketchPlugin_ConstraintFillet::isMacro() const
513 {
514   return true;
515 }
516
517
518 // =========   Auxiliary functions   =================
519 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
520                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
521 {
522   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523       theNewArc->attribute(theNewArcAttribute))->pnt();
524   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
525       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
526 }
527
528 /// \brief Find intersections of lines shifted along normal direction
529 void possibleFilletCenterLineLine(
530     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
531     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
532     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
533 {
534   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
535   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
536   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
537   double aDet = theDirA->cross(theDirB);
538   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
539     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
540     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
541       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
542       double aVX = aDirAT->xy()->dot(aPntA);
543       double aVY = aDirBT->xy()->dot(aPntB);
544       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
545           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
546           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
547       theCenters.push_back(aPoint);
548     }
549   }
550 }
551
552 /// \brief Find intersections of line shifted along normal direction in both sides
553 ///        and a circle with extended radius
554 void possibleFilletCenterLineArc(
555     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
556     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
557     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
558 {
559   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
560   std::shared_ptr<GeomAPI_XY> aPnt;
561   double aDirNorm2 = theDirLine->dot(theDirLine);
562   double aRad = 0.0;
563   double aDirX = theDirLine->x();
564   double aDirX2 = theDirLine->x() * theDirLine->x();
565   double aDirY2 = theDirLine->y() * theDirLine->y();
566   double aDirXY = theDirLine->x() * theDirLine->y();
567   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
568     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
569     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
570     double aCoeff2 = aCoeff * aCoeff;
571     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
572       aRad = theRadiusArc + aStepB * theRadius;
573       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
574       if (aD < 0.0)
575         continue;
576       double aDs = sqrt(aD);
577       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
578       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
579       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
580           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
581       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
582           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
583
584       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
585       theCenters.push_back(aPoint1);
586       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
587       theCenters.push_back(aPoint2);
588     }
589   }
590 }
591
592 /// \brief Find intersections of two circles with extended radii
593 void possibleFilletCenterArcArc(
594     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
595     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
596     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
597 {
598   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
599   double aCenterDist2 = aCenterDir->dot(aCenterDir);
600   double aCenterDist = sqrt(aCenterDist2);
601
602   double aRadA, aRadB;
603   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
604     aRadA = theRadiusA + aStepA * theRadius;
605     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
606       aRadB = theRadiusB + aStepB * theRadius;
607       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
608         continue; // there is no intersections
609
610       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
611       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
612
613       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
614       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
615
616       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
617       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
618
619       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
620       theCenters.push_back(aPoint1);
621       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
622       theCenters.push_back(aPoint2);
623     }
624   }
625 }
626
627 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
628                            double theRadius, bool theNotInversed[2],
629                            std::shared_ptr<GeomAPI_XY>& theCenter,
630                            std::shared_ptr<GeomAPI_XY>& theTangentA,
631                            std::shared_ptr<GeomAPI_XY>& theTangentB)
632 {
633   static const int aNbFeatures = 2;
634   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
635   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
636   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
637
638   for (int i = 0; i < aNbFeatures; i++) {
639     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
640       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
641           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
642       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
643           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
644     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
645       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
646           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
647       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
648           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
649       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
650           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
651     } else
652       return;
653     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
654         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
655         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
656     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
657         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
658         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
659   }
660
661   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
662       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
663     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
664     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
665     for (int i = 0; i < aNbFeatures; i++) {
666       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
667       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
668     }
669
670     // get and filter possible centers
671     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
672     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
673     double aDot = 0.0;
674     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
675     for (; anIt != aSuspectCenters.end(); anIt++) {
676       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
677       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
678       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
679         continue; // incorrect position
680       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
681       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
682       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
683         continue; // incorrect position
684       // the center is found, stop searching
685       theCenter = *anIt;
686       return;
687     }
688   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
689       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
690       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
691       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
692     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
693     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
694     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
695         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
696     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
697         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
698
699     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
700         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
701     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
702         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
703     double anArcAngle = aEndArcDir->angle(aStartArcDir);
704
705     // get possible centers and filter them
706     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
707     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
708     double aDot = 0.0;
709     // the line is forward into the arc
710     double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
711     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
712     // The possible centers are ranged by their positions.
713     // If the point is not satisfy one of criteria, the weight is decreased with penalty.
714     int aBestWeight = 0;
715     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
716     for (; anIt != aSuspectCenters.end(); anIt++) {
717       int aWeight = 2;
718       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
719       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
720       // Check the point is placed on the correct arc (penalty if false)
721       if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
722         aWeight -= 1;
723       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
724           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
725       double aCurAngle = aCurDir->angle(aStartArcDir);
726       if (anArcAngle < 0.0) aCurAngle *= -1.0;
727       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
728         continue;
729       if (aWeight > aBestWeight)
730         aBestWeight = aWeight;
731       else if (aWeight < aBestWeight ||
732                aStart[aLineInd]->distance(*anIt) >
733                aStart[aLineInd]->distance(theCenter)) // <-- take closer point
734         continue;
735       // the center is found, stop searching
736       theCenter = *anIt;
737       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
738       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
739         theTangentA = aLineTgPoint;
740         theTangentB = anArcTgPoint;
741       } else {
742         theTangentA = anArcTgPoint;
743         theTangentB = aLineTgPoint;
744       }
745       //return;
746     }
747   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
748       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
749     double anArcRadius[aNbFeatures];
750     double anArcAngle[aNbFeatures];
751     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
752     for (int i = 0; i < aNbFeatures; i++) {
753       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
754       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
755           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
756       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
757           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
758       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
759     }
760
761     // get and filter possible centers
762     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
763     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
764     double aDot = 0.0;
765     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
766     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
767     for (; anIt != aSuspectCenters.end(); anIt++) {
768       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
769           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
770       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
771       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
772       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
773         continue; // incorrect position
774       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
775
776       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
777       aCurAngle = aCurDir->angle(aStartArcDir[1]);
778       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
779       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
780         continue; // incorrect position
781       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
782
783       // the center is found, stop searching
784       theCenter = *anIt;
785       return;
786     }
787   }
788 }