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