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