Salome HOME
c311ec712475eb96f82d3c591535c20108a447b5
[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     // 5. Tangent points should be placed on the base features
347     for (int i = 0; i < aNbFeatures; i++) {
348       anAttrInd = 2*i + (isStart[i] ? 0 : 1);
349       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
350       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
351           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
352       aRefAttr->setAttr(aNewFeature[i]->attribute(aFeatAttributes[anAttrInd]));
353       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
354           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
355       aRefAttr->setObject(aFeature[i]->lastResult());
356       myProducedFeatures.push_back(aConstraint);
357     }
358     // make base features auxiliary
359     anOldFeatureA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
360     anOldFeatureB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
361     myBaseObjects.clear();
362     myBaseObjects.push_back(anOldFeatureA);
363     myBaseObjects.push_back(anOldFeatureB);
364     // exchange the naming IDs of newly created and old line that become auxiliary
365     sketch()->exchangeIDs(anOldFeatureA, aNewFeatureA);
366     sketch()->exchangeIDs(anOldFeatureB, aNewFeatureB);
367   } else {
368     // Update radius value
369     int aNbSubs = sketch()->numberOfSubs();
370     FeaturePtr aSubFeature;
371     for (int aSub = 0; aSub < aNbSubs; aSub++) {
372       aSubFeature = sketch()->subFeature(aSub);
373       if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
374         continue;
375       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
376           aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
377       if (!aRefAttr || !aRefAttr->isObject())
378         continue;
379       FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
380       if (aFeature == aNewArc) {
381         AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
382           aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
383         aRadius->setValue(aFilletRadius);
384         break;
385       }
386     }
387   }
388
389   // send events to update the sub-features by the solver
390   if (isUpdateFlushed)
391     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
392
393   // the viewer update should be unblocked in order after the fillet features
394   // are processed by the solver
395   //aMsg = std::shared_ptr<Events_Message>(
396   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
397   //Events_Loop::loop()->send(aMsg);
398 }
399
400 void SketchPlugin_ConstraintFillet::attributeChanged(const std::string& theID)
401 {
402   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
403     // clear the list of fillet entities
404     AttributeRefListPtr aRefListOfFillet = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
405         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
406     aRefListOfFillet->clear();
407
408     // clear the list of base features
409     AttributeRefListPtr aRefListOfBaseLines = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
410         data()->attribute(SketchPlugin_Constraint::ENTITY_C()));
411       aRefListOfBaseLines->clear();
412
413     // remove all produced objects and constraints
414     DocumentPtr aDoc = sketch()->document();
415     std::list<FeaturePtr>::iterator aCIt = myProducedFeatures.begin();
416     for (; aCIt != myProducedFeatures.end(); ++aCIt)
417       aDoc->removeFeature(*aCIt);
418     myProducedFeatures.clear();
419
420     // clear auxiliary flag on initial objects
421     for (aCIt = myBaseObjects.begin(); aCIt != myBaseObjects.end(); ++aCIt)
422       (*aCIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
423     myBaseObjects.clear();
424
425     // Obtain fillet point
426     AttributeRefAttrPtr aBaseA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
427         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
428     if(!aBaseA->isInitialized() || aBaseA->isObject()) {
429       return;
430     }
431     AttributePtr anAttrBaseA = aBaseA->attr();
432     std::shared_ptr<GeomDataAPI_Point2D> aBasePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrBaseA);
433     if (!aBasePoint) {
434       return;
435     }
436     std::shared_ptr<GeomAPI_Pnt2d> aFilletPoint = aBasePoint->pnt();
437
438     // Obtain conicident edges
439     const std::set<AttributePtr>& aRefsList = anAttrBaseA->owner()->data()->refsToMe();
440     std::set<AttributePtr>::const_iterator aIt;
441     FeaturePtr aCoincident;
442     for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
443       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
444       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
445       if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
446         AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
447           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
448         AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
449           aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
450         if(anAttrRefA.get() && !anAttrRefA->isObject()) {
451           AttributePtr anAttrA = anAttrRefA->attr();
452           if(anAttrBaseA == anAttrA) {
453             aCoincident = aConstrFeature;
454             break;
455           }
456         }
457         if(anAttrRefA.get() && !anAttrRefB->isObject()) {
458           AttributePtr anAttrB = anAttrRefB->attr();
459           if(anAttrBaseA == anAttrB) {
460             aCoincident = aConstrFeature;
461             break;
462           }
463         }
464       }
465     }
466
467     if(!aCoincident.get()) {
468       setError("No coincident edges at selected vertex");
469       return;
470     }
471
472     std::set<FeaturePtr> aCoinsides;
473     SketchPlugin_Tools::findCoincidences(aCoincident,
474                                          SketchPlugin_ConstraintCoincidence::ENTITY_A(),
475                                          aCoinsides);
476     SketchPlugin_Tools::findCoincidences(aCoincident,
477                                          SketchPlugin_ConstraintCoincidence::ENTITY_B(),
478                                          aCoinsides);
479
480     // Remove points
481     std::set<FeaturePtr> aNewLines;
482     for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
483       if((*anIt)->getKind() != SketchPlugin_Point::ID()) {
484         aNewLines.insert(*anIt);
485       }
486     }
487     aCoinsides = aNewLines;
488
489     // Remove auxilary lines
490     if(aCoinsides.size() > 2) {
491       aNewLines.clear();
492       for(std::set<FeaturePtr>::iterator anIt = aCoinsides.begin(); anIt != aCoinsides.end(); ++anIt) {
493         if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
494           aNewLines.insert(*anIt);
495         }
496       }
497       aCoinsides = aNewLines;
498     }
499
500     if(aCoinsides.size() != 2) {
501       setError("At selected vertex should be two coincident lines");
502       return;
503     }
504
505     // Store base lines
506     FeaturePtr anOldFeatureA, anOldFeatureB;
507     std::set<FeaturePtr>::iterator aLinesIt = aCoinsides.begin();
508     anOldFeatureA = *aLinesIt++;
509     anOldFeatureB = *aLinesIt;
510     aRefListOfBaseLines->append(anOldFeatureA);
511     aRefListOfBaseLines->append(anOldFeatureB);
512
513     // Getting points located at 1/3 of edge length from fillet point
514     std::shared_ptr<GeomAPI_Pnt2d> aPntA, aPntB;
515     getPointOnEdge(anOldFeatureA, aFilletPoint, aPntA);
516     getPointOnEdge(anOldFeatureB, aFilletPoint, aPntB);
517
518     /// Getting distances
519     double aRadius = 1;
520     double aDistanceA = getProjectionDistance(anOldFeatureB, aPntA);
521     double aDistanceB = getProjectionDistance(anOldFeatureA, aPntB);
522     aRadius = aDistanceA < aDistanceB ? aDistanceA / 2.0 : aDistanceB / 2.0;
523
524     std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aRadius);
525   }
526 }
527
528 AISObjectPtr SketchPlugin_ConstraintFillet::getAISObject(AISObjectPtr thePrevious)
529 {
530   if (!sketch())
531     return thePrevious;
532
533   AISObjectPtr anAIS = thePrevious;
534   /// TODO: Equal constraint presentation should be put here
535   return anAIS;
536 }
537
538 bool SketchPlugin_ConstraintFillet::isMacro() const
539 {
540   return true;
541 }
542
543
544 // =========   Auxiliary functions   =================
545 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
546                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
547 {
548   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
549       theNewArc->attribute(theNewArcAttribute))->pnt();
550   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
551       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
552 }
553
554 /// \brief Find intersections of lines shifted along normal direction
555 void possibleFilletCenterLineLine(
556     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
557     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
558     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
559 {
560   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
561   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
562   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
563   double aDet = theDirA->cross(theDirB);
564   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
565     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
566     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
567       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
568       double aVX = aDirAT->xy()->dot(aPntA);
569       double aVY = aDirBT->xy()->dot(aPntB);
570       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
571           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
572           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
573       theCenters.push_back(aPoint);
574     }
575   }
576 }
577
578 /// \brief Find intersections of line shifted along normal direction in both sides
579 ///        and a circle with extended radius
580 void possibleFilletCenterLineArc(
581     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
582     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
583     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
584 {
585   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
586   std::shared_ptr<GeomAPI_XY> aPnt;
587   double aDirNorm2 = theDirLine->dot(theDirLine);
588   double aRad = 0.0;
589   double aDirX = theDirLine->x();
590   double aDirX2 = theDirLine->x() * theDirLine->x();
591   double aDirY2 = theDirLine->y() * theDirLine->y();
592   double aDirXY = theDirLine->x() * theDirLine->y();
593   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
594     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
595     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
596     double aCoeff2 = aCoeff * aCoeff;
597     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
598       aRad = theRadiusArc + aStepB * theRadius;
599       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
600       if (aD < 0.0)
601         continue;
602       double aDs = sqrt(aD);
603       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
604       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
605       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
606           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
607       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
608           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
609
610       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
611       theCenters.push_back(aPoint1);
612       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
613       theCenters.push_back(aPoint2);
614     }
615   }
616 }
617
618 /// \brief Find intersections of two circles with extended radii
619 void possibleFilletCenterArcArc(
620     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
621     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
622     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
623 {
624   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
625   double aCenterDist2 = aCenterDir->dot(aCenterDir);
626   double aCenterDist = sqrt(aCenterDist2);
627
628   double aRadA, aRadB;
629   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
630     aRadA = theRadiusA + aStepA * theRadius;
631     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
632       aRadB = theRadiusB + aStepB * theRadius;
633       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
634         continue; // there is no intersections
635
636       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
637       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
638
639       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
640       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
641
642       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
643       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
644
645       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
646       theCenters.push_back(aPoint1);
647       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
648       theCenters.push_back(aPoint2);
649     }
650   }
651 }
652
653 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
654                            double theRadius, bool theNotInversed[2],
655                            std::shared_ptr<GeomAPI_XY>& theCenter,
656                            std::shared_ptr<GeomAPI_XY>& theTangentA,
657                            std::shared_ptr<GeomAPI_XY>& theTangentB)
658 {
659   static const int aNbFeatures = 2;
660   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
661   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
662   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
663
664   for (int i = 0; i < aNbFeatures; i++) {
665     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
666       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
667           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
668       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
669           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
670     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
671       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
672           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
673       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
674           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
675       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
676           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
677     } else
678       return;
679     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
680         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
681         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
682     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
683         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
684         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
685   }
686
687   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
688       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
689     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
690     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
691     for (int i = 0; i < aNbFeatures; i++) {
692       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
693       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
694     }
695
696     // get and filter possible centers
697     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
698     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
699     double aDot = 0.0;
700     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
701     for (; anIt != aSuspectCenters.end(); anIt++) {
702       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
703       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
704       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
705         continue; // incorrect position
706       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
707       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
708       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
709         continue; // incorrect position
710       // the center is found, stop searching
711       theCenter = *anIt;
712       return;
713     }
714   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
715       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
716       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
717       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
718     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
719     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
720     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
721         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
722     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
723         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
724
725     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
726         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
727     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
728         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
729     double anArcAngle = aEndArcDir->angle(aStartArcDir);
730
731     // get possible centers and filter them
732     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
733     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
734     double aDot = 0.0;
735     // the line is forward into the arc
736     double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
737     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
738     // The possible centers are ranged by their positions.
739     // If the point is not satisfy one of criteria, the weight is decreased with penalty.
740     int aBestWeight = 0;
741     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
742     for (; anIt != aSuspectCenters.end(); anIt++) {
743       int aWeight = 2;
744       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
745       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
746       // Check the point is placed on the correct arc (penalty if false)
747       if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
748         aWeight -= 1;
749       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
750           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
751       double aCurAngle = aCurDir->angle(aStartArcDir);
752       if (anArcAngle < 0.0) aCurAngle *= -1.0;
753       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
754         continue;
755       if (aWeight > aBestWeight)
756         aBestWeight = aWeight;
757       else if (aWeight < aBestWeight ||
758                aStart[aLineInd]->distance(*anIt) >
759                aStart[aLineInd]->distance(theCenter)) // <-- take closer point
760         continue;
761       // the center is found, stop searching
762       theCenter = *anIt;
763       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
764       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
765         theTangentA = aLineTgPoint;
766         theTangentB = anArcTgPoint;
767       } else {
768         theTangentA = anArcTgPoint;
769         theTangentB = aLineTgPoint;
770       }
771       //return;
772     }
773   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
774       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
775     double anArcRadius[aNbFeatures];
776     double anArcAngle[aNbFeatures];
777     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
778     for (int i = 0; i < aNbFeatures; i++) {
779       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
780       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
781           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
782       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
783           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
784       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
785     }
786
787     // get and filter possible centers
788     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
789     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
790     double aDot = 0.0;
791     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
792     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
793     for (; anIt != aSuspectCenters.end(); anIt++) {
794       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
795           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
796       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
797       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
798       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
799         continue; // incorrect position
800       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
801
802       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
803       aCurAngle = aCurDir->angle(aStartArcDir[1]);
804       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
805       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
806         continue; // incorrect position
807       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
808
809       // the center is found, stop searching
810       theCenter = *anIt;
811       return;
812     }
813   }
814 }
815
816 void getPointOnEdge(const FeaturePtr theFeature,
817                     const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
818                     std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
819   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
820     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
821       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
822     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
823       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
824     if(aPntStart->distance(theFilletPoint) > 1.e-7) {
825       aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
826         theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
827       aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
828         theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
829     }
830     thePoint.reset( new GeomAPI_Pnt2d(aPntStart->xy()->added( aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
831   } else {
832     std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
833     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
834       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
835     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
836       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
837     if(theFeature->attribute(SketchPlugin_Arc::INVERSED_ID())) {
838       aPntTemp = aPntStart;
839       aPntStart = aPntEnd;
840       aPntEnd = aPntTemp;
841     }
842     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
843       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
844     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
845     double aStartParameter(0), anEndParameter(0);
846     aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
847     aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
848     if(aPntStart->distance(theFilletPoint) > tolerance) {
849       double aTmpParameter = aStartParameter;
850       aStartParameter = anEndParameter;
851       anEndParameter = aTmpParameter;
852     }
853     double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
854     aCirc->D0(aPntParameter, thePoint);
855   }
856 }
857
858 double getProjectionDistance(const FeaturePtr theFeature,
859                              const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
860 {
861   std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
862   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
863     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
864       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
865     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
866       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
867     std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
868     aProjectPnt = aLin->project(thePoint);
869   } else {
870     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
871       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
872     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
873       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
874     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
875       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
876     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
877     aProjectPnt = aCirc->project(thePoint);
878   }
879   if(aProjectPnt.get()) {
880     return aProjectPnt->distance(thePoint);
881   }
882   return -1;
883 }