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