Salome HOME
Issue 1974: Incorrect fillet after sketch modification
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Fillet.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_Fillet.cpp
4 // Created: 19 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_Fillet.h"
8
9 #include "SketchPlugin_Arc.h"
10 #include "SketchPlugin_Line.h"
11 #include "SketchPlugin_Point.h"
12 #include "SketchPlugin_Sketch.h"
13 #include "SketchPlugin_ConstraintEqual.h"
14 #include "SketchPlugin_ConstraintCoincidence.h"
15 #include "SketchPlugin_ConstraintLength.h"
16 #include "SketchPlugin_ConstraintMiddle.h"
17 #include "SketchPlugin_ConstraintTangent.h"
18 #include "SketchPlugin_ConstraintRadius.h"
19 #include "SketchPlugin_Tools.h"
20
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_Validator.h>
27
28 #include <GeomAlgoAPI_Circ2dBuilder.h>
29 #include <GeomAlgoAPI_EdgeBuilder.h>
30
31 #include <GeomAPI_Circ2d.h>
32 #include <GeomAPI_Dir2d.h>
33 #include <GeomAPI_Lin2d.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_XY.h>
36
37 #include <GeomDataAPI_Point2D.h>
38
39 #include <Events_Loop.h>
40
41 #include <math.h>
42
43 const double tolerance = 1.e-7;
44 const double paramTolerance = 1.e-4;
45 const double PI = 3.141592653589793238463;
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
52 /// \brief Calculate radius of a fillet.
53 ///        It should not be greater than 1/3 of shortest edge length.
54 static double calculateFilletRadius(FeaturePtr theFilletFeatures[2]);
55
56 /// \brief Calculates center of fillet arc and coordinates of tangency points
57 static void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
58                                   double theFilletRadius,
59                                   const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
60                                   std::shared_ptr<GeomAPI_XY>& theCenter,
61                                   std::shared_ptr<GeomAPI_XY>& theTangentA,
62                                   std::shared_ptr<GeomAPI_XY>& theTangentB);
63
64 /// Get coincide edges for fillet
65 static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
66
67 static std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
68                                                  const AttributePtr theAttribute);
69
70 SketchPlugin_Fillet::SketchPlugin_Fillet()
71 : myFilletCreated(false)
72 {
73 }
74
75 void SketchPlugin_Fillet::initAttributes()
76 {
77   data()->addAttribute(FILLET_POINT_ID(), ModelAPI_AttributeRefAttr::typeId());
78 }
79
80 void SketchPlugin_Fillet::execute()
81 {
82   // Wait all constraints being created, then send update events
83   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
84   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
85   if (isUpdateFlushed)
86     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
87
88   // set flag here to avoid building Fillet presentation if "Redisplay" event appears
89   myFilletCreated = true;
90
91   // Calculate Fillet parameters if does not yet
92   if (!myBaseFeatures[0] || !myBaseFeatures[1])
93     calculateFilletParameters();
94
95   // Create arc feature.
96   FeaturePtr aFilletArc = sketch()->addFeature(SketchPlugin_Arc::ID());
97
98   // Set arc attributes.
99   bool aWasBlocked = aFilletArc->data()->blockSendAttributeUpdated(true);
100   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101       aFilletArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(myCenterXY->x(),
102                                                                       myCenterXY->y());
103   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
104       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105           aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
106   std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
107       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108           aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
109   if(aStartPoint->isInitialized() && aEndPoint->isInitialized()
110       && (aStartPoint->pnt()->xy()->distance(myTangentXY1) > tolerance
111       || aEndPoint->pnt()->xy()->distance(myTangentXY2) > tolerance)) {
112     std::dynamic_pointer_cast<SketchPlugin_Arc>(aFilletArc)->setReversed(false);
113   }
114   aStartPoint->setValue(myTangentXY1->x(), myTangentXY1->y());
115   aEndPoint->setValue(myTangentXY2->x(), myTangentXY2->y());
116   aFilletArc->data()->blockSendAttributeUpdated(aWasBlocked);
117   aFilletArc->execute();
118
119   // Delete features with refs to points of edges.
120   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint1;
121   int aFeatInd1 = myIsReversed ? 1 : 0;
122   int anAttrInd1 = (myIsReversed ? 2 : 0) + (myIsNotInversed[aFeatInd1] ? 0 : 1);
123   aStartPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
124       myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
125   std::set<FeaturePtr> aFeaturesToBeRemoved1 =
126     findFeaturesToRemove(myBaseFeatures[aFeatInd1], aStartPoint1);
127
128   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint2;
129   int aFeatInd2 = myIsReversed ? 0 : 1;
130   int anAttrInd2 = (myIsReversed ? 0 : 2) + (myIsNotInversed[aFeatInd2] ? 0 : 1);
131   aStartPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132       myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
133   std::set<FeaturePtr> aFeaturesToBeRemoved2 =
134     findFeaturesToRemove(myBaseFeatures[aFeatInd2], aStartPoint2);
135
136   aFeaturesToBeRemoved1.insert(aFeaturesToBeRemoved2.begin(), aFeaturesToBeRemoved2.end());
137   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved1);
138   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
139
140   // Update fillet edges.
141   recalculateAttributes(aFilletArc, SketchPlugin_Arc::START_ID(),
142                         myBaseFeatures[aFeatInd1], myFeatAttributes[anAttrInd1]);
143   recalculateAttributes(aFilletArc, SketchPlugin_Arc::END_ID(),
144                         myBaseFeatures[aFeatInd2], myFeatAttributes[anAttrInd2]);
145
146   // Create coincidence features.
147   FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
148   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
149       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
150   aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::START_ID()));
151   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
152       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
153   aRefAttr->setAttr(myBaseFeatures[aFeatInd1]->attribute(myFeatAttributes[anAttrInd1]));
154   aConstraint->execute();
155   ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
156   aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
157   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
158       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
159   aRefAttr->setAttr(aFilletArc->attribute(SketchPlugin_Arc::END_ID()));
160   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
161       aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
162   aRefAttr->setAttr(myBaseFeatures[aFeatInd2]->attribute(myFeatAttributes[anAttrInd2]));
163   aConstraint->execute();
164   ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
165
166   // Create tangent features.
167   for (int i = 0; i < 2; i++) {
168     aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
169     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
170         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
171     aRefAttr->setObject(aFilletArc->lastResult());
172     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
173         aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
174     bool isArc = myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID();
175     aRefAttr->setObject(isArc ? myBaseFeatures[i]->lastResult() :
176                                 myBaseFeatures[i]->firstResult());
177     aConstraint->execute();
178     ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
179   }
180
181   // Send events to update the sub-features by the solver.
182   if(isUpdateFlushed) {
183     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
184   }
185 }
186
187 AISObjectPtr SketchPlugin_Fillet::getAISObject(AISObjectPtr thePrevious)
188 {
189   if(myFilletCreated) {
190     return AISObjectPtr();
191   }
192
193   SketchPlugin_Sketch* aSketch = sketch();
194   if(!aSketch) {
195     return AISObjectPtr();
196   }
197
198   if (!calculateFilletParameters())
199     return AISObjectPtr();
200
201   // Create arc for presentation.
202   std::shared_ptr<GeomAPI_Pnt> aCenterPnt(aSketch->to3D(myCenterXY->x(), myCenterXY->y()));
203   std::shared_ptr<GeomAPI_Pnt> aTangentPnt1(aSketch->to3D(myTangentXY1->x(),
204                                                           myTangentXY1->y()));
205   std::shared_ptr<GeomAPI_Pnt> aTangentPnt2(aSketch->to3D(myTangentXY2->x(),
206                                                           myTangentXY2->y()));
207   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
208     aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
209   std::shared_ptr<GeomAPI_Shape> anArcShape =
210       GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenterPnt, aTangentPnt1, aTangentPnt2, aNDir->dir());
211
212   AISObjectPtr anAISObject = thePrevious;
213   if(!anAISObject.get()) {
214     anAISObject = AISObjectPtr(new GeomAPI_AISObject);
215   }
216   anAISObject->createShape(anArcShape);
217   return anAISObject;
218 }
219
220 bool SketchPlugin_Fillet::calculateFilletParameters()
221 {
222   // Get fillet point.
223   AttributeRefAttrPtr aPointRefAttr = refattr(FILLET_POINT_ID());
224   if (!aPointRefAttr->isInitialized() || aPointRefAttr->isObject())
225     return false;
226   std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
227     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointRefAttr->attr());
228   if (!aFilletPoint2D.get())
229     return false;
230
231   if (!findFeaturesContainingFilletPoint(aFilletPoint2D)) {
232     setError("Error: Selected point does not have two suitable edges for fillet.");
233     return false;
234   }
235
236   std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
237   double aRadius = calculateFilletRadius(myBaseFeatures);
238
239   // Calculate arc attributes.
240   static const int aNbFeatures = 2;
241   // First pair of points relate to first feature, second pair -  to second.
242   std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2];
243   for (int i = 0; i < aNbFeatures; i++) {
244     std::string aStartAttr, aEndAttr;
245     if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
246       aStartAttr = SketchPlugin_Line::START_ID();
247       aEndAttr = SketchPlugin_Line::END_ID();
248     } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
249       aStartAttr = SketchPlugin_Arc::START_ID();
250       aEndAttr = SketchPlugin_Arc::END_ID();
251     } else { // Wrong argument.
252       setError("Error: One of the points has wrong coincide feature");
253       return false;
254     }
255     myFeatAttributes[2*i] = aStartAttr;
256     aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
257       myBaseFeatures[i]->attribute(aStartAttr))->pnt();
258     myFeatAttributes[2*i+1] = aEndAttr;
259     aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260       myBaseFeatures[i]->attribute(aEndAttr))->pnt();
261   }
262   for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
263     for (int j = 0; j < 2; j++) // loop on start-end of each feature
264       if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
265         myIsNotInversed[aFeatInd] = (j==0);
266         break;
267       }
268   }
269
270   std::shared_ptr<GeomAPI_Ax3> aSketchPlane = SketchPlugin_Sketch::plane(sketch());
271   calculateFilletCenter(myBaseFeatures, aRadius, aSketchPlane,
272                         myCenterXY, myTangentXY1, myTangentXY2);
273
274   // Tangent directions of the features in coincident point.
275   std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures];
276   for (int i = 0; i < aNbFeatures; i++) {
277     std::shared_ptr<GeomAPI_XY> aDir;
278     if (myBaseFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
279       aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
280       if (!myIsNotInversed[i])
281         aDir = aDir->multiplied(-1.0);
282     } else if (myBaseFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
283       std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint =
284         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
285         myBaseFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
286       aDir = myIsNotInversed[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
287       aDir = aDir->decreased(aCenterPoint->xy());
288
289       double x = aDir->x();
290       double y = aDir->y();
291       aDir->setX(-y);
292       aDir->setY(x);
293       if (myIsNotInversed[i] ==
294           std::dynamic_pointer_cast<SketchPlugin_Arc>(myBaseFeatures[i])->isReversed())
295         aDir = aDir->multiplied(-1.0);
296     }
297     aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
298   }
299
300   // By default, the start point of fillet arc is connected to FeatureA,
301   // and the end point - to FeatureB. But when the angle between TangentDirA and
302   // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
303   double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A),
304   // where A and B - angles between corresponding tanget direction and the X axis
305   myIsReversed = cosBA > 0.0;
306
307   if(myIsReversed) {
308     std::shared_ptr<GeomAPI_XY> aTmp = myTangentXY1;
309     myTangentXY1 = myTangentXY2;
310     myTangentXY2 = aTmp;
311   }
312   return true;
313 }
314
315 bool SketchPlugin_Fillet::findFeaturesContainingFilletPoint(
316     std::shared_ptr<GeomDataAPI_Point2D> theFilletPoint)
317 {
318   // Obtain constraint coincidence for the fillet point.
319   FeaturePtr aConstraintCoincidence;
320   const std::set<AttributePtr>& aRefsList = theFilletPoint->owner()->data()->refsToMe();
321   for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin();
322       anIt != aRefsList.cend();
323       ++anIt) {
324     std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
325     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
326     if(aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
327       AttributeRefAttrPtr anAttrRefA =
328           aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A());
329       AttributeRefAttrPtr anAttrRefB =
330           aFeature->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B());
331       if(anAttrRefA.get() && !anAttrRefA->isObject()) {
332         AttributePtr anAttrA = anAttrRefA->attr();
333         if(theFilletPoint == anAttrA) {
334           aConstraintCoincidence = aFeature;
335           break;
336         }
337       }
338       if(anAttrRefB.get() && !anAttrRefB->isObject()) {
339         AttributePtr anAttrB = anAttrRefB->attr();
340         if(theFilletPoint == anAttrB) {
341           aConstraintCoincidence = aFeature;
342           break;
343         }
344       }
345     }
346   }
347
348   if(!aConstraintCoincidence.get())
349     return false;
350
351   // Get coincide edges.
352   std::set<FeaturePtr> anEdgeFeatures = getCoincides(aConstraintCoincidence);
353   std::set<FeaturePtr>::iterator aLinesIt = anEdgeFeatures.begin();
354   for (int i = 0; aLinesIt != anEdgeFeatures.end() && i < 2; ++aLinesIt, ++i)
355     myBaseFeatures[i] = *aLinesIt;
356
357   return myBaseFeatures[0] && myBaseFeatures[1];
358 }
359
360 // =========   Auxiliary functions   =================
361 void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
362                            FeaturePtr theFeature, const std::string& theFeatureAttribute)
363 {
364   std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
365       theNewArc->attribute(theNewArcAttribute))->pnt();
366   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
367       theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
368 }
369
370 static std::shared_ptr<GeomAPI_Pnt2d> toPoint(const AttributePtr& theAttribute)
371 {
372   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
373   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
374   if (aPointAttr)
375     aPoint = aPointAttr->pnt();
376   return aPoint;
377 }
378
379 static std::shared_ptr<GeomAPI_Lin2d> toLine(const FeaturePtr& theFeature)
380 {
381   std::shared_ptr<GeomAPI_Lin2d> aLine;
382   if (theFeature->getKind() == SketchPlugin_Line::ID()) {
383     std::shared_ptr<GeomAPI_Pnt2d> aStart =
384         toPoint( theFeature->attribute(SketchPlugin_Line::START_ID()) );
385     std::shared_ptr<GeomAPI_Pnt2d> aEnd =
386         toPoint( theFeature->attribute(SketchPlugin_Line::END_ID()) );
387     aLine = std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aStart, aEnd));
388   }
389   return aLine;
390 }
391
392 static std::shared_ptr<GeomAPI_Circ2d> toCircle(const FeaturePtr& theFeature)
393 {
394   std::shared_ptr<GeomAPI_Circ2d> aCircle;
395   if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
396     std::shared_ptr<GeomAPI_Pnt2d> aCenter =
397         toPoint( theFeature->attribute(SketchPlugin_Arc::CENTER_ID()) );
398     std::shared_ptr<GeomAPI_Pnt2d> aStart =
399         toPoint( theFeature->attribute(SketchPlugin_Arc::START_ID()) );
400     aCircle = std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenter, aStart));
401   }
402   return aCircle;
403 }
404
405
406 void calculateFilletCenter(FeaturePtr theFilletFeatures[2],
407                            double theFilletRadius,
408                            const std::shared_ptr<GeomAPI_Ax3>& theSketchPlane,
409                            std::shared_ptr<GeomAPI_XY>& theCenter,
410                            std::shared_ptr<GeomAPI_XY>& theTangentA,
411                            std::shared_ptr<GeomAPI_XY>& theTangentB)
412 {
413   GeomShapePtr aShapeA = theFilletFeatures[0]->lastResult()->shape();
414   GeomShapePtr aShapeB = theFilletFeatures[1]->lastResult()->shape();
415
416   GeomAlgoAPI_Circ2dBuilder aCircBuilder(theSketchPlane);
417   aCircBuilder.addTangentCurve(aShapeA);
418   aCircBuilder.addTangentCurve(aShapeB);
419   aCircBuilder.setRadius(theFilletRadius);
420
421   std::shared_ptr<GeomAPI_Circ2d> aFilletCircle = aCircBuilder.circle();
422   if (!aFilletCircle)
423     return;
424
425   theCenter = aFilletCircle->center()->xy();
426   // tangent points
427   std::shared_ptr<GeomAPI_Pnt2d> aTgPoints[2];
428   for (int i = 0; i < 2; ++i) {
429     std::shared_ptr<GeomAPI_Circ2d> aCircle = toCircle(theFilletFeatures[i]);
430     if (aCircle)
431       aTgPoints[i] = aCircle->project(aFilletCircle->center());
432     else {
433       std::shared_ptr<GeomAPI_Lin2d> aLine = toLine(theFilletFeatures[i]);
434       if (aLine)
435         aTgPoints[i] = aLine->project(aFilletCircle->center());
436     }
437   }
438   theTangentA = aTgPoints[0]->xy();
439   theTangentB = aTgPoints[1]->xy();
440 }
441
442 double calculateFilletRadius(FeaturePtr theFilletFeatures[2])
443 {
444   double aLengths[2] = { 0, 0 };
445   for (int i = 0; i < 2; ++i) {
446     GeomShapePtr aShape = theFilletFeatures[i]->lastResult()->shape();
447     std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(aShape);
448     if (anEdge)
449       aLengths[i] = anEdge->length();
450   }
451   return std::min(aLengths[0], aLengths[1]) / 6.0;
452 }
453
454 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
455 {
456   std::set<FeaturePtr> aCoincides;
457
458   std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt =
459     SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
460
461   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
462                                        SketchPlugin_ConstraintCoincidence::ENTITY_A(),
463                                        aCoincides);
464   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
465                                        SketchPlugin_ConstraintCoincidence::ENTITY_B(),
466                                        aCoincides);
467
468   // Remove points from set of coincides.
469   std::set<FeaturePtr> aNewSetOfCoincides;
470   for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
471     std::shared_ptr<SketchPlugin_SketchEntity> aSketchEntity =
472       std::dynamic_pointer_cast<SketchPlugin_SketchEntity>(*anIt);
473     if(aSketchEntity.get() && aSketchEntity->isCopy()) {
474       continue;
475     }
476     if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
477       aNewSetOfCoincides.insert(*anIt);
478     } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
479       AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
480       std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
481         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
482       if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
483         continue;
484       }
485       aNewSetOfCoincides.insert(*anIt);
486     }
487   }
488   aCoincides = aNewSetOfCoincides;
489
490   // If we still have more than two coincides remove auxilary entities from set of coincides.
491   if(aCoincides.size() > 2) {
492     aNewSetOfCoincides.clear();
493     for(std::set<FeaturePtr>::iterator
494         anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
495       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
496         aNewSetOfCoincides.insert(*anIt);
497       }
498     }
499     aCoincides = aNewSetOfCoincides;
500   }
501
502   return aCoincides;
503 }
504
505 std::set<FeaturePtr> findFeaturesToRemove(const FeaturePtr theFeature,
506                                           const AttributePtr theAttribute) {
507   std::set<FeaturePtr> aFeaturesToBeRemoved;
508   std::set<AttributePtr> aRefs = theFeature->data()->refsToMe();
509   std::list<ResultPtr> aResults = theFeature->results();
510   for(std::list<ResultPtr>::const_iterator aResultsIt = aResults.cbegin();
511       aResultsIt != aResults.cend();
512       ++aResultsIt) {
513     ResultPtr aResult = *aResultsIt;
514     std::set<AttributePtr> aResultRefs = aResult->data()->refsToMe();
515     aRefs.insert(aResultRefs.begin(), aResultRefs.end());
516   }
517   for(std::set<AttributePtr>::const_iterator anIt = aRefs.cbegin();
518     anIt != aRefs.cend();
519     ++anIt) {
520     std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
521     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
522     if(aFeature->getKind() == SketchPlugin_Fillet::ID()) {
523       continue;
524     }
525     if(aFeature->getKind() == SketchPlugin_ConstraintLength::ID()
526         || aFeature->getKind() == SketchPlugin_ConstraintEqual::ID()
527         || aFeature->getKind() == SketchPlugin_ConstraintMiddle::ID()) {
528       aFeaturesToBeRemoved.insert(aFeature);
529     } else {
530       std::list<AttributePtr> anAttrs =
531           aFeature->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
532       for(std::list<AttributePtr>::const_iterator aRefAttrsIt = anAttrs.cbegin();
533           aRefAttrsIt != anAttrs.cend();
534           ++aRefAttrsIt) {
535         AttributeRefAttrPtr anAttrRefAttr =
536           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefAttrsIt);
537         if(anAttrRefAttr.get() && anAttrRefAttr->attr() == theAttribute) {
538           aFeaturesToBeRemoved.insert(aFeature);
539         }
540       }
541     }
542   }
543   return aFeaturesToBeRemoved;
544 }