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