Salome HOME
09f8dbcb6030459c7fdfccf8c25259259ecc7ccd
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintSplit.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintSplit.cpp
4 // Created: 17 Jul 2016
5 // Author:  Natalia ERMOLAEVA
6
7 #include "SketchPlugin_ConstraintSplit.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_AttributeReference.h>
17 //#include <ModelAPI_AttributeRefList.h>
18 //#include <ModelAPI_AttributeRefAttrList.h>
19 //#include <ModelAPI_Data.h>
20 //#include <ModelAPI_Events.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_Point.h>
27 //#include <SketchPlugin_Sketch.h>
28 //#include <SketchPlugin_ConstraintCoincidence.h>
29 //#include <SketchPlugin_ConstraintTangent.h>
30 //#include <SketchPlugin_ConstraintRadius.h>
31 //#include <SketchPlugin_Tools.h>
32 //
33 //#include <Events_Loop.h>
34 //
35 //#include <math.h>
36 //
37 //const double tolerance = 1.e-7;
38 //const double paramTolerance = 1.e-4;
39
40 ///// \brief Attract specified point on theNewArc to the attribute of theFeature
41 //static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
42 //  FeaturePtr theFeature, const std::string& theFeatureAttribute);
43 //
44 ///// \brief Calculates center of fillet arc and coordinates of tangency points
45 //static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
46 //                                  double theRadius, bool theNotInversed[2],
47 //                                  std::shared_ptr<GeomAPI_XY>& theCenter,
48 //                                  std::shared_ptr<GeomAPI_XY>& theTangentA,
49 //                                  std::shared_ptr<GeomAPI_XY>& theTangentB);
50 //
51 ///// Get point on 1/3 length of edge from fillet point
52 //static void getPointOnEdge(const FeaturePtr theFeature,
53 //                           const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
54 //                           std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
55 //
56 ///// Get distance from point to feature
57 //static double getProjectionDistance(const FeaturePtr theFeature,
58 //                             const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
59 //
60 ///// Get coincide edges for fillet
61 //static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
62
63 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
64 //: myListOfPointsChangedInCode(false),
65 //  myRadiusChangedByUser(false),
66 //  myRadiusChangedInCode(false),
67 //  myRadiusInitialized(false)
68 {
69 }
70
71 void SketchPlugin_ConstraintSplit::initAttributes()
72 {
73   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
74   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), GeomDataAPI_Point2D::typeId());
75   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), GeomDataAPI_Point2D::typeId());
76
77   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
78   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
79 }
80
81 void SketchPlugin_ConstraintSplit::execute()
82 {
83 /*  std::shared_ptr<ModelAPI_Data> aData = data();
84
85   // Check the base objects are initialized.
86   AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
87     aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
88   if(!aPointsRefList->isInitialized()) {
89     setError("Error: List of points is not initialized.");
90     return;
91   }
92
93   // Get fillet radius.
94   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
95     aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
96
97   // Wait all constraints being created, then send update events
98   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
99   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
100   if (isUpdateFlushed)
101     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
102
103   for(std::set<AttributePtr>::iterator aPointsIter = myNewPoints.begin();
104       aPointsIter != myNewPoints.end();
105       ++aPointsIter) {
106     AttributePtr aPointAttr = *aPointsIter;
107     std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2d = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aPointAttr);
108     if(!aFilletPoint2d.get()) {
109       setError("Error: One of the selected points is empty.");
110       return;
111     }
112     std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2d->pnt();
113
114     // Obtain base lines for fillet.
115     bool anIsNeedNewObjects = true;
116     FilletFeatures aFilletFeatures;
117     std::map<AttributePtr, FilletFeatures>::iterator aPrevPointsIter = myPointFeaturesMap.find(aPointAttr);
118     if(aPrevPointsIter != myPointFeaturesMap.end()) {
119       anIsNeedNewObjects = false;
120       aFilletFeatures = aPrevPointsIter->second;
121     }
122     FeaturePtr aBaseEdgeA, aBaseEdgeB;
123     if(!anIsNeedNewObjects) {
124       aBaseEdgeA = aFilletFeatures.baseEdgesState.front().first;
125       aBaseEdgeB = aFilletFeatures.baseEdgesState.back().first;
126     } else {
127       // Obtain constraint coincidence for the fillet point.
128       FeaturePtr aConstraintCoincidence;
129       const std::set<AttributePtr>& aRefsList = aFilletPoint2d->owner()->data()->refsToMe();
130       for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
131         std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
132         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
133         if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
134           AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
135             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
136           AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
137             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
138           if(anAttrRefA.get() && !anAttrRefA->isObject()) {
139             AttributePtr anAttrA = anAttrRefA->attr();
140             if(aFilletPoint2d == anAttrA) {
141               aConstraintCoincidence = aConstrFeature;
142               break;
143             }
144           }
145           if(anAttrRefB.get() && !anAttrRefB->isObject()) {
146             AttributePtr anAttrB = anAttrRefB->attr();
147             if(aFilletPoint2d == anAttrB) {
148               aConstraintCoincidence = aConstrFeature;
149               break;
150             }
151           }
152         }
153       }
154
155       if(!aConstraintCoincidence.get()) {
156         setError("Error: No coincident edges at one of the selected points.");
157         return;
158       }
159
160       // Get coincide edges.
161       std::set<FeaturePtr> aCoincides = getCoincides(aConstraintCoincidence);
162       if(aCoincides.size() != 2) {
163         setError("Error: One of the selected points does not have two suitable edges for fillet.");
164         return;
165       }
166
167       std::set<FeaturePtr>::iterator aLinesIt = aCoincides.begin();
168       aBaseEdgeA = *aLinesIt++;
169       aBaseEdgeB = *aLinesIt;
170
171       std::pair<FeaturePtr, bool> aBasePairA = std::make_pair(aBaseEdgeA, aBaseEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
172       std::pair<FeaturePtr, bool> aBasePairB = std::make_pair(aBaseEdgeB, aBaseEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value());
173       aFilletFeatures.baseEdgesState.push_back(aBasePairA);
174       aFilletFeatures.baseEdgesState.push_back(aBasePairB);
175     }
176
177     if(!aBaseEdgeA.get() || !aBaseEdgeB.get()) {
178       setError("Error: One of the base edges is empty.");
179       return;
180     }
181
182     // Create new edges and arc if needed.
183     FeaturePtr aResultEdgeA, aResultEdgeB, aResultArc;
184     if(!anIsNeedNewObjects) {
185       // Obtain features from the list.
186       std::list<FeaturePtr>::iterator aResultEdgesIt = aFilletFeatures.resultEdges.begin();
187       aResultEdgeA = *aResultEdgesIt++;
188       aResultEdgeB = *aResultEdgesIt++;
189       aResultArc = *aResultEdgesIt;
190     } else {
191       // Copy edges and create arc.
192       aResultEdgeA = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aBaseEdgeA, sketch());
193       aResultEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
194       aResultEdgeB = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aBaseEdgeB, sketch());
195       aResultEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(false);
196       aResultArc = sketch()->addFeature(SketchPlugin_Arc::ID());
197
198       aFilletFeatures.resultEdges.push_back(aResultEdgeA);
199       aFilletFeatures.resultEdges.push_back(aResultEdgeB);
200       aFilletFeatures.resultEdges.push_back(aResultArc);
201     }
202
203     // Calculate arc attributes
204     static const int aNbFeatures = 2;
205     FeaturePtr aBaseFeatures[aNbFeatures] = {aBaseEdgeA, aBaseEdgeB};
206     FeaturePtr aResultFeatures[aNbFeatures] = {aResultEdgeA, aResultEdgeB};
207     std::shared_ptr<GeomAPI_Dir2d> aTangentDir[aNbFeatures]; // tangent directions of the features in coincident point
208     bool isStart[aNbFeatures]; // indicates which point the features share
209     std::shared_ptr<GeomAPI_Pnt2d> aStartEndPnt[aNbFeatures * 2]; // first pair of points relate to first feature, second pair -  to second
210     std::string aFeatAttributes[aNbFeatures * 2]; // attributes of features
211     for (int i = 0; i < aNbFeatures; i++) {
212       std::string aStartAttr, aEndAttr;
213       if (aResultFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
214         aStartAttr = SketchPlugin_Line::START_ID();
215         aEndAttr = SketchPlugin_Line::END_ID();
216       } else if (aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
217         aStartAttr = SketchPlugin_Arc::START_ID();
218         aEndAttr = SketchPlugin_Arc::END_ID();
219       } else { // wrong argument
220         setError("Error: One of the points has wrong coincide feature");
221         return;
222       }
223       aFeatAttributes[2*i] = aStartAttr;
224       aStartEndPnt[2*i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
225         aBaseFeatures[i]->attribute(aStartAttr))->pnt();
226       aFeatAttributes[2*i+1] = aEndAttr;
227       aStartEndPnt[2*i+1] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
228         aBaseFeatures[i]->attribute(aEndAttr))->pnt();
229     }
230     for (int aFeatInd = 0; aFeatInd < aNbFeatures; aFeatInd++) {
231       for (int j = 0; j < 2; j++) // loop on start-end of each feature
232         if (aStartEndPnt[aFeatInd * aNbFeatures + j]->distance(aFilletPnt2d) < 1.e-10) {
233           isStart[aFeatInd] = (j==0);
234           break;
235         }
236     }
237     // tangent directions of the features
238     for (int i = 0; i < aNbFeatures; i++) {
239       std::shared_ptr<GeomAPI_XY> aDir;
240       if (aResultFeatures[i]->getKind() == SketchPlugin_Line::ID()) {
241         aDir = aStartEndPnt[2*i+1]->xy()->decreased(aStartEndPnt[2*i]->xy());
242         if (!isStart[i])
243           aDir = aDir->multiplied(-1.0);
244       } else if (aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID()) {
245         std::shared_ptr<GeomAPI_Pnt2d> aCenterPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
246           aResultFeatures[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
247         aDir = isStart[i] ? aStartEndPnt[2*i]->xy() : aStartEndPnt[2*i+1]->xy();
248         aDir = aDir->decreased(aCenterPoint->xy());
249
250         double x = aDir->x();
251         double y = aDir->y();
252         aDir->setX(-y);
253         aDir->setY(x);
254         if (isStart[i] == std::dynamic_pointer_cast<SketchPlugin_Arc>(aBaseFeatures[i])->isReversed())
255           aDir = aDir->multiplied(-1.0);
256       }
257       aTangentDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aDir));
258     }
259
260     // By default, the start point of fillet arc is connected to FeatureA,
261     // and the end point - to FeatureB. But when the angle between TangentDirA and
262     // TangentDirB greater 180 degree, the sequaence of features need to be reversed.
263     double cosBA = aTangentDir[0]->cross(aTangentDir[1]); // cos(B-A), where A and B - angles between corresponding tanget direction and the X axis
264     bool isReversed = cosBA > 0.0;
265
266     // Calculate fillet arc parameters
267     std::shared_ptr<GeomAPI_XY> aCenter, aTangentPntA, aTangentPntB;
268     calculateFilletCenter(aBaseEdgeA, aBaseEdgeB, aFilletRadius, isStart, aCenter, aTangentPntA, aTangentPntB);
269     if(!aCenter.get() || !aTangentPntA.get() || !aTangentPntB.get()) {
270       setError("Can not create fillet with the specified parameters.");
271       return;
272     }
273     // update features
274     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
275       aResultEdgeA->attribute(aFeatAttributes[isStart[0] ? 0 : 1]))->setValue(aTangentPntA->x(), aTangentPntA->y());
276     aResultEdgeA->execute();
277     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
278       aResultEdgeB->attribute(aFeatAttributes[2 + (isStart[1] ? 0 : 1)]))->setValue(aTangentPntB->x(), aTangentPntB->y());
279     aResultEdgeB->execute();
280     // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
281     // by arc; moreover, it may cause cyclicity in hte mechanism of updater
282     aResultArc->data()->blockSendAttributeUpdated(true);
283     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
284       aResultArc->attribute(SketchPlugin_Arc::CENTER_ID()))->setValue(aCenter->x(), aCenter->y());
285     if(isReversed) {
286       std::shared_ptr<GeomAPI_XY> aTmp = aTangentPntA;
287       aTangentPntA = aTangentPntB;
288       aTangentPntB = aTmp;
289     }
290     std::shared_ptr<GeomDataAPI_Point2D> aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
291       aResultArc->attribute(SketchPlugin_Arc::START_ID()));
292     std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
293       aResultArc->attribute(SketchPlugin_Arc::END_ID()));
294     if(aStartPoint->isInitialized() && aEndPoint->isInitialized() &&
295       (aStartPoint->pnt()->xy()->distance(aTangentPntA) > tolerance ||
296        aEndPoint->pnt()->xy()->distance(aTangentPntB) > tolerance)) {
297       std::dynamic_pointer_cast<SketchPlugin_Arc>(aResultArc)->setReversed(false);
298     }
299     aStartPoint->setValue(aTangentPntA->x(), aTangentPntA->y());
300     aEndPoint->setValue(aTangentPntB->x(), aTangentPntB->y());
301     aResultArc->data()->blockSendAttributeUpdated(false);
302     aResultArc->execute();
303
304     if(anIsNeedNewObjects) {
305       // Create list of additional constraints:
306       // 1. Coincidence of boundary points of features (copied lines/arcs) and fillet arc
307       // 1.1. coincidence
308       FeaturePtr aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
309       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
310           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
311       aRefAttr->setAttr(aResultArc->attribute(SketchPlugin_Arc::START_ID()));
312       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
313           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
314       int aFeatInd = isReversed ? 1 : 0;
315       int anAttrInd = (isReversed ? 2 : 0) + (isStart[isReversed ? 1 : 0] ? 0 : 1);
316       aRefAttr->setAttr(aResultFeatures[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
317       recalculateAttributes(aResultArc, SketchPlugin_Arc::START_ID(), aResultFeatures[aFeatInd], aFeatAttributes[anAttrInd]);
318       aConstraint->execute();
319       aFilletFeatures.resultConstraints.push_back(aConstraint);
320       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
321       // 1.2. coincidence
322       aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
323       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
324           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
325       aRefAttr->setAttr(aResultArc->attribute(SketchPlugin_Arc::END_ID()));
326       aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
327           aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
328       aFeatInd = isReversed ? 0 : 1;
329       anAttrInd = (isReversed ? 0 : 2) + (isStart[isReversed ? 0 : 1] ? 0 : 1);
330       aRefAttr->setAttr(aResultFeatures[aFeatInd]->attribute(aFeatAttributes[anAttrInd]));
331       recalculateAttributes(aResultArc, SketchPlugin_Arc::END_ID(), aResultFeatures[aFeatInd], aFeatAttributes[anAttrInd]);
332       aConstraint->execute();
333       aFilletFeatures.resultConstraints.push_back(aConstraint);
334       ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
335       // 2. Fillet arc radius
336       //aConstraint = sketch()->addFeature(SketchPlugin_ConstraintRadius::ID());
337       //aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
338       //    aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
339       //aRefAttr->setObject(aNewArc->lastResult());
340       //std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
341       //    aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->setValue(aFilletRadius);
342       //std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
343       //    aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->setValue(
344       //    isStart[0] ? aStartEndPnt[0] : aStartEndPnt[1]);
345       //aConstraint->execute();
346       //myProducedFeatures.push_back(aConstraint);
347       //ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
348       // 3. Tangency of fillet arc and features
349       for (int i = 0; i < aNbFeatures; i++) {
350         aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
351         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
352             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
353         aRefAttr->setObject(aResultArc->lastResult());
354         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
355             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
356         bool isArc = aResultFeatures[i]->getKind() == SketchPlugin_Arc::ID();
357         aRefAttr->setObject(isArc ? aResultFeatures[i]->lastResult() : aResultFeatures[i]->firstResult());
358         aConstraint->execute();
359         aFilletFeatures.resultConstraints.push_back(aConstraint);
360         ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
361       }
362       // 4. Coincidence of free boundaries of base and copied features
363       for (int i = 0; i < aNbFeatures; i++) {
364         anAttrInd = 2*i + (isStart[i] ? 1 : 0);
365         aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
366         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
367             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
368         aRefAttr->setAttr(aBaseFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
369         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
370             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
371         aRefAttr->setAttr(aResultFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
372         aFilletFeatures.resultConstraints.push_back(aConstraint);
373       }
374       // 4.1. Additional tangency constraints when the fillet is based on arcs.
375       //      It is used to verify the created arc will be placed on a source.
376       for (int i = 0; i < aNbFeatures; ++i) {
377         if (aResultFeatures[i]->getKind() != SketchPlugin_Arc::ID())
378           continue;
379         aConstraint = sketch()->addFeature(SketchPlugin_ConstraintTangent::ID());
380         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
381             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
382         aRefAttr->setObject(aBaseFeatures[i]->lastResult());
383         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
384             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
385         aRefAttr->setObject(aResultFeatures[i]->lastResult());
386         aConstraint->execute();
387         aFilletFeatures.resultConstraints.push_back(aConstraint);
388         ModelAPI_EventCreator::get()->sendUpdated(aConstraint, anUpdateEvent);
389       }
390       // 5. Tangent points should be placed on the base features
391       for (int i = 0; i < aNbFeatures; i++) {
392         anAttrInd = 2*i + (isStart[i] ? 0 : 1);
393         aConstraint = sketch()->addFeature(SketchPlugin_ConstraintCoincidence::ID());
394         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
395             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
396         aRefAttr->setAttr(aResultFeatures[i]->attribute(aFeatAttributes[anAttrInd]));
397         aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
398             aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
399         aRefAttr->setObject(aBaseFeatures[i]->lastResult());
400         aFilletFeatures.resultConstraints.push_back(aConstraint);
401       }
402       // make base features auxiliary
403       aBaseEdgeA->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
404       aBaseEdgeB->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(true);
405
406       // exchange the naming IDs of newly created and old line that become auxiliary
407       sketch()->exchangeIDs(aBaseEdgeA, aResultEdgeA);
408       sketch()->exchangeIDs(aBaseEdgeB, aResultEdgeB);
409
410       // store point and features in the map.
411       myPointFeaturesMap[aPointAttr] = aFilletFeatures;
412     } else {
413       // Update radius value
414       int aNbSubs = sketch()->numberOfSubs();
415       FeaturePtr aSubFeature;
416       for (int aSub = 0; aSub < aNbSubs; aSub++) {
417         aSubFeature = sketch()->subFeature(aSub);
418         if (aSubFeature->getKind() != SketchPlugin_ConstraintRadius::ID())
419           continue;
420         AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
421             aSubFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
422         if (!aRefAttr || !aRefAttr->isObject())
423           continue;
424         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
425         if (aFeature == aResultArc) {
426           AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
427             aSubFeature->attribute(SketchPlugin_Constraint::VALUE()));
428           aRadius->setValue(aFilletRadius);
429           break;
430         }
431       }
432     }
433   }
434
435   // Send events to update the sub-features by the solver.
436   if(isUpdateFlushed) {
437     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
438   }
439 */
440 }
441
442 void SketchPlugin_ConstraintSplit::attributeChanged(const std::string& theID)
443 {
444 /*  if(theID == SketchPlugin_Constraint::ENTITY_A()) {
445     if(myListOfPointsChangedInCode) {
446       return;
447     }
448
449     // Clear results.
450     clearResults();
451
452     // Clear list of new points.
453     myNewPoints.clear();
454
455     // Get list of points for fillets and current radius.
456     AttributeRefAttrListPtr aRefListOfFilletPoints = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
457       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
458     AttributeDoublePtr aRadiusAttribute = real(VALUE());
459     int aListSize = aRefListOfFilletPoints->size();
460     if(aListSize == 0 && !myRadiusChangedByUser) {
461       // If list is empty reset radius to zero (if it was not changed by user).
462       myRadiusChangedInCode = true;
463       aRadiusAttribute->setValue(0);
464       myRadiusChangedInCode = false;
465       return;
466     }
467
468     // Iterate over points to get base lines an calculate radius for fillets.
469     double aMinimumRadius = 0;
470     std::list<std::pair<ObjectPtr, AttributePtr>> aSelectedPointsList = aRefListOfFilletPoints->list();
471     std::list<std::pair<ObjectPtr, AttributePtr>>::iterator anIter = aSelectedPointsList.begin();
472     std::set<AttributePtr> aPointsToSkeep;
473     for(int anIndex = 0; anIndex < aListSize; anIndex++, anIter++) {
474       AttributePtr aFilletPointAttr = (*anIter).second;
475       std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
476         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFilletPointAttr);
477       if(!aFilletPoint2D.get()) {
478         myNewPoints.clear();
479         setError("Error: One of the selected points is invalid.");
480         return;
481       }
482
483       // If point or coincident point is already in list remove it from attribute.
484       if(aPointsToSkeep.find(aFilletPointAttr) != aPointsToSkeep.end()) {
485         myListOfPointsChangedInCode = true;
486         aRefListOfFilletPoints->remove(aFilletPointAttr);
487         myListOfPointsChangedInCode = false;
488         continue;
489       }
490
491       // Obtain constraint coincidence for the fillet point.
492       FeaturePtr aConstraintCoincidence;
493       const std::set<AttributePtr>& aRefsList = aFilletPointAttr->owner()->data()->refsToMe();
494       for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
495         std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
496         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
497         if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
498           AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
499             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
500           AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
501             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
502           if(anAttrRefA.get()) {
503             AttributePtr anAttrA = anAttrRefA->attr();
504             if(aFilletPointAttr == anAttrA) {
505               aConstraintCoincidence = aConstrFeature;
506               break;
507             }
508           }
509           if(anAttrRefB.get()) {
510             AttributePtr anAttrB = anAttrRefB->attr();
511             if(aFilletPointAttr == anAttrB) {
512               aConstraintCoincidence = aConstrFeature;
513               break;
514             }
515           }
516         }
517       }
518
519       if(!aConstraintCoincidence.get()) {
520         myNewPoints.clear();
521         setError("Error: No coincident edges at one of the selected points.");
522         return;
523       }
524
525       // Get coincides from constraint.
526       std::set<FeaturePtr> aCoincides;
527
528
529       SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
530                                            SketchPlugin_ConstraintCoincidence::ENTITY_A(),
531                                            aCoincides);
532       SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
533                                            SketchPlugin_ConstraintCoincidence::ENTITY_B(),
534                                            aCoincides);
535
536       // Remove points from set of coincides. Also get all attributes which is equal to this point to exclude it.
537       std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
538       std::set<FeaturePtr> aNewSetOfCoincides;
539       for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
540         std::string aFeatureKind = (*anIt)->getKind();
541         if(aFeatureKind == SketchPlugin_Point::ID()) {
542           AttributePtr anAttr = (*anIt)->attribute(SketchPlugin_Point::COORD_ID());
543           std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
544             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
545           if(aPoint2D.get() && aFilletPnt2d->isEqual(aPoint2D->pnt())) {
546             aPointsToSkeep.insert(anAttr);
547           }
548         } else if(aFeatureKind == SketchPlugin_Line::ID()) {
549           AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Line::START_ID());
550           std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
551             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
552           if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
553             aPointsToSkeep.insert(anAttrStart);
554           }
555           AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Line::END_ID());
556           std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
557             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
558           if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
559             aPointsToSkeep.insert(anAttrEnd);
560           }
561           aNewSetOfCoincides.insert(*anIt);
562         } else if(aFeatureKind == SketchPlugin_Arc::ID() ) {
563           AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
564           std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
565             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
566           if(aPointCenter2D.get() && aFilletPnt2d->isEqual(aPointCenter2D->pnt())) {
567             aPointsToSkeep.insert(anAttrCenter);
568             continue;
569           }
570           AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Arc::START_ID());
571           std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
572             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
573           if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
574             aPointsToSkeep.insert(anAttrStart);
575           }
576           AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Arc::END_ID());
577           std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
578             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
579           if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
580             aPointsToSkeep.insert(anAttrEnd);
581           }
582           aNewSetOfCoincides.insert(*anIt);
583         }
584       }
585       aCoincides = aNewSetOfCoincides;
586
587       // If we still have more than two coincides remove auxilary entities from set of coincides.
588       if(aCoincides.size() > 2) {
589         aNewSetOfCoincides.clear();
590         for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
591           if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
592             aNewSetOfCoincides.insert(*anIt);
593           }
594         }
595         aCoincides = aNewSetOfCoincides;
596       }
597
598       if(aCoincides.size() != 2) {
599         myNewPoints.clear();
600         setError("Error: One of the selected points does not have two suitable edges for fillet.");
601         return;
602       }
603
604       // Store base point for fillet.
605       aPointsToSkeep.insert(aFilletPointAttr);
606       myNewPoints.insert(aFilletPointAttr);
607
608       // Get base lines for fillet.
609       FeaturePtr anOldFeatureA, anOldFeatureB;
610       std::set<FeaturePtr>::iterator aLinesIt = aCoincides.begin();
611       anOldFeatureA = *aLinesIt++;
612       anOldFeatureB = *aLinesIt;
613
614       // Getting radius value if it was not changed by user.
615       if(!myRadiusChangedByUser) {
616         // Getting points located at 1/3 of edge length from fillet point.
617         std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
618         std::shared_ptr<GeomAPI_Pnt2d> aPntA, aPntB;
619         getPointOnEdge(anOldFeatureA, aFilletPnt2d, aPntA);
620         getPointOnEdge(anOldFeatureB, aFilletPnt2d, aPntB);
621
622         /// Getting distances.
623         double aDistanceA = getProjectionDistance(anOldFeatureB, aPntA);
624         double aDistanceB = getProjectionDistance(anOldFeatureA, aPntB);
625         double aRadius = aDistanceA < aDistanceB ? aDistanceA / 2.0 : aDistanceB / 2.0;
626         aMinimumRadius = aMinimumRadius == 0 ? aRadius : aRadius < aMinimumRadius ? aRadius : aMinimumRadius;
627       }
628     }
629
630     // Set new default radius if it was not changed by user.
631     if(!myRadiusChangedByUser) {
632       myRadiusChangedInCode = true;
633       aRadiusAttribute->setValue(aMinimumRadius);
634       myRadiusChangedInCode = false;
635     }
636
637   } else if(theID == SketchPlugin_Constraint::VALUE()) {
638     if(myRadiusInitialized && !myRadiusChangedInCode) {
639       myRadiusChangedByUser = true;
640     }
641     if(!myRadiusInitialized) {
642       myRadiusInitialized = true;
643     }
644   }
645 */
646 }
647
648 //AISObjectPtr SketchPlugin_ConstraintSplit::getAISObject(AISObjectPtr thePrevious)
649 //{
650 //  if (!sketch())
651 //    return thePrevious;
652 //
653 //  AISObjectPtr anAIS = thePrevious;
654 //  /// TODO: Equal constraint presentation should be put here
655 //  return anAIS;
656 //}
657
658 bool SketchPlugin_ConstraintSplit::isMacro() const
659 {
660   return true;
661 }
662
663 //void SketchPlugin_ConstraintSplit::clearResults()
664 //{
665 ///*  // Clear auxiliary flag on initial objects.
666 //  for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
667 //      aPointsIter != myPointFeaturesMap.end();) {
668 //    const FilletFeatures& aFilletFeatures = aPointsIter->second;
669 //    std::list<std::pair<FeaturePtr, bool>>::const_iterator aFeatureIt;
670 //    for(aFeatureIt = aFilletFeatures.baseEdgesState.cbegin();
671 //        aFeatureIt != aFilletFeatures.baseEdgesState.cend();
672 //        ++aFeatureIt) {
673 //      aFeatureIt->first->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(aFeatureIt->second);
674 //    }
675 //    ++aPointsIter;
676 //  }
677 //
678 //  // And remove all produced features.
679 //  DocumentPtr aDoc = sketch()->document();
680 //  for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
681 //      aPointsIter != myPointFeaturesMap.end();) {
682 //    // Remove all produced constraints.
683 //    const FilletFeatures& aFilletFeatures = aPointsIter->second;
684 //    std::list<FeaturePtr>::const_iterator aFeatureIt;
685 //    for(aFeatureIt = aFilletFeatures.resultConstraints.cbegin();
686 //        aFeatureIt != aFilletFeatures.resultConstraints.cend();
687 //        ++aFeatureIt) {
688 //      aDoc->removeFeature(*aFeatureIt);
689 //    }
690 //
691 //    // Remove all result edges.
692 //    for(aFeatureIt = aFilletFeatures.resultEdges.cbegin();
693 //        aFeatureIt != aFilletFeatures.resultEdges.cend();
694 //        ++aFeatureIt) {
695 //      aDoc->removeFeature(*aFeatureIt);
696 //    }
697 //
698 //    // Remove point from map.
699 //    myPointFeaturesMap.erase(aPointsIter++);
700 //  }
701 //*/
702 //};
703 //
704 //
705 //// =========   Auxiliary functions   =================
706 //void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
707 //                           FeaturePtr theFeature, const std::string& theFeatureAttribute)
708 //{
709 //  std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
710 //      theNewArc->attribute(theNewArcAttribute))->pnt();
711 //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
712 //      theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
713 //}
714
715 /*/// \brief Find intersections of lines shifted along normal direction
716 void possibleFilletCenterLineLine(
717     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
718     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
719     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
720 {
721   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
722   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
723   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
724   double aDet = theDirA->cross(theDirB);
725   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
726     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
727     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
728       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
729       double aVX = aDirAT->xy()->dot(aPntA);
730       double aVY = aDirBT->xy()->dot(aPntB);
731       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
732           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
733           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
734       theCenters.push_back(aPoint);
735     }
736   }
737 }
738
739 /// \brief Find intersections of line shifted along normal direction in both sides
740 ///        and a circle with extended radius
741 void possibleFilletCenterLineArc(
742     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
743     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
744     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
745 {
746   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
747   std::shared_ptr<GeomAPI_XY> aPnt;
748   double aDirNorm2 = theDirLine->dot(theDirLine);
749   double aRad = 0.0;
750   double aDirX = theDirLine->x();
751   double aDirX2 = theDirLine->x() * theDirLine->x();
752   double aDirY2 = theDirLine->y() * theDirLine->y();
753   double aDirXY = theDirLine->x() * theDirLine->y();
754   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
755     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
756     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
757     double aCoeff2 = aCoeff * aCoeff;
758     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
759       aRad = theRadiusArc + aStepB * theRadius;
760       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
761       if (aD < 0.0)
762         continue;
763       double aDs = sqrt(aD);
764       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
765       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
766       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
767           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
768       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
769           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
770
771       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
772       theCenters.push_back(aPoint1);
773       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
774       theCenters.push_back(aPoint2);
775     }
776   }
777 }
778
779 /// \brief Find intersections of two circles with extended radii
780 void possibleFilletCenterArcArc(
781     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
782     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
783     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
784 {
785   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
786   double aCenterDist2 = aCenterDir->dot(aCenterDir);
787   double aCenterDist = sqrt(aCenterDist2);
788
789   double aRadA, aRadB;
790   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
791     aRadA = theRadiusA + aStepA * theRadius;
792     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
793       aRadB = theRadiusB + aStepB * theRadius;
794       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
795         continue; // there is no intersections
796
797       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
798       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
799
800       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
801       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
802
803       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
804       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
805
806       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
807       theCenters.push_back(aPoint1);
808       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
809       theCenters.push_back(aPoint2);
810     }
811   }
812 }
813
814 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
815                            double theRadius, bool theNotInversed[2],
816                            std::shared_ptr<GeomAPI_XY>& theCenter,
817                            std::shared_ptr<GeomAPI_XY>& theTangentA,
818                            std::shared_ptr<GeomAPI_XY>& theTangentB)
819 {
820   static const int aNbFeatures = 2;
821   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
822   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
823   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
824
825   for (int i = 0; i < aNbFeatures; i++) {
826     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
827       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
828           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
829       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
830           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
831     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
832       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
833           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
834       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
835           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
836       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
837           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
838     } else
839       return;
840     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
841         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
842         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
843     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
844         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
845         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
846   }
847
848   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
849       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
850     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
851     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
852     for (int i = 0; i < aNbFeatures; i++) {
853       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
854       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
855     }
856
857     // get and filter possible centers
858     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
859     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
860     double aDot = 0.0;
861     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
862     for (; anIt != aSuspectCenters.end(); anIt++) {
863       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
864       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
865       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
866         continue; // incorrect position
867       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
868       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
869       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
870         continue; // incorrect position
871       // the center is found, stop searching
872       theCenter = *anIt;
873       return;
874     }
875   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
876       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
877       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
878       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
879     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
880     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
881     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
882         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
883     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
884         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
885
886     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
887         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
888     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
889         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
890     double anArcAngle = aEndArcDir->angle(aStartArcDir);
891
892     // get possible centers and filter them
893     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
894     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
895     double aDot = 0.0;
896     // the line is forward into the arc
897     double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
898     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
899     // The possible centers are ranged by their positions.
900     // If the point is not satisfy one of criteria, the weight is decreased with penalty.
901     int aBestWeight = 0;
902     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
903     for (; anIt != aSuspectCenters.end(); anIt++) {
904       int aWeight = 2;
905       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
906       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
907       // Check the point is placed on the correct arc (penalty if false)
908       if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
909         aWeight -= 1;
910       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
911           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
912       double aCurAngle = aCurDir->angle(aStartArcDir);
913       if (anArcAngle < 0.0) aCurAngle *= -1.0;
914       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
915         continue;
916       if (aWeight > aBestWeight)
917         aBestWeight = aWeight;
918       else if (aWeight < aBestWeight ||
919                aStart[aLineInd]->distance(*anIt) >
920                aStart[aLineInd]->distance(theCenter)) // <-- take closer point
921         continue;
922       // the center is found, stop searching
923       theCenter = *anIt;
924       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
925       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
926         theTangentA = aLineTgPoint;
927         theTangentB = anArcTgPoint;
928       } else {
929         theTangentA = anArcTgPoint;
930         theTangentB = aLineTgPoint;
931       }
932       //return;
933     }
934   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
935       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
936     double anArcRadius[aNbFeatures];
937     double anArcAngle[aNbFeatures];
938     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
939     for (int i = 0; i < aNbFeatures; i++) {
940       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
941       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
942           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
943       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
944           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
945       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
946     }
947
948     // get and filter possible centers
949     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
950     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
951     double aDot = 0.0;
952     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
953     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
954     for (; anIt != aSuspectCenters.end(); anIt++) {
955       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
956           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
957       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
958       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
959       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
960         continue; // incorrect position
961       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
962
963       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
964       aCurAngle = aCurDir->angle(aStartArcDir[1]);
965       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
966       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
967         continue; // incorrect position
968       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
969
970       // the center is found, stop searching
971       theCenter = *anIt;
972       return;
973     }
974   }
975 }
976
977 void getPointOnEdge(const FeaturePtr theFeature,
978                     const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
979                     std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
980   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
981     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
982       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
983     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
984       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
985     if(aPntStart->distance(theFilletPoint) > 1.e-7) {
986       aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
987         theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
988       aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
989         theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
990     }
991     thePoint.reset( new GeomAPI_Pnt2d(aPntStart->xy()->added( aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
992   } else {
993     std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
994     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
995       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
996     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
997       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
998     if(theFeature->attribute(SketchPlugin_Arc::INVERSED_ID())) {
999       aPntTemp = aPntStart;
1000       aPntStart = aPntEnd;
1001       aPntEnd = aPntTemp;
1002     }
1003     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1004       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1005     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
1006     double aStartParameter(0), anEndParameter(0);
1007     aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
1008     aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
1009     if(aPntStart->distance(theFilletPoint) > tolerance) {
1010       double aTmpParameter = aStartParameter;
1011       aStartParameter = anEndParameter;
1012       anEndParameter = aTmpParameter;
1013     }
1014     double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
1015     aCirc->D0(aPntParameter, thePoint);
1016   }
1017 }
1018
1019 double getProjectionDistance(const FeaturePtr theFeature,
1020                              const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
1021 {
1022   std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
1023   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
1024     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1025       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
1026     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1027       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
1028     std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
1029     aProjectPnt = aLin->project(thePoint);
1030   } else {
1031     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1032       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
1033     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1034       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
1035     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1036       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1037     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
1038     aProjectPnt = aCirc->project(thePoint);
1039   }
1040   if(aProjectPnt.get()) {
1041     return aProjectPnt->distance(thePoint);
1042   }
1043   return -1;
1044 }
1045
1046 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
1047 {
1048   std::set<FeaturePtr> aCoincides;
1049
1050   std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt = SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
1051
1052   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
1053                                        SketchPlugin_ConstraintCoincidence::ENTITY_A(),
1054                                        aCoincides);
1055   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
1056                                        SketchPlugin_ConstraintCoincidence::ENTITY_B(),
1057                                        aCoincides);
1058
1059   // Remove points from set of coincides.
1060   std::set<FeaturePtr> aNewSetOfCoincides;
1061   for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
1062     if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
1063       aNewSetOfCoincides.insert(*anIt);
1064     } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
1065       AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
1066       std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
1067         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
1068       if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
1069         continue;
1070       }
1071       aNewSetOfCoincides.insert(*anIt);
1072     }
1073   }
1074   aCoincides = aNewSetOfCoincides;
1075
1076   // If we still have more than two coincides remove auxilary entities from set of coincides.
1077   if(aCoincides.size() > 2) {
1078     aNewSetOfCoincides.clear();
1079     for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
1080       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
1081         aNewSetOfCoincides.insert(*anIt);
1082       }
1083     }
1084     aCoincides = aNewSetOfCoincides;
1085   }
1086
1087   return aCoincides;
1088 }
1089 */