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