]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintGroup.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintGroup.cpp
1 // File:    SketchSolver_ConstraintGroup.cpp
2 // Created: 27 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_ConstraintGroup.h"
6
7 #include <SketchSolver_Constraint.h>
8
9 #include <Events_Error.h>
10 #include <Events_Loop.h>
11 #include <GeomDataAPI_Dir.h>
12 #include <GeomDataAPI_Point.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <Model_Events.h>
17
18 #include <SketchPlugin_Constraint.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
21
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_Line.h>
25 #include <SketchPlugin_Point.h>
26 #include <SketchPlugin_Sketch.h>
27
28 #include <math.h>
29 #include <assert.h>
30
31 /// Tolerance for value of parameters
32 const double tolerance = 1.e-10;
33
34 const std::string ERROR_SOLVE_CONSTRAINTS = "Conflicting constraints";
35
36 /// This value is used to give unique index to the groups
37 static Slvs_hGroup myGroupIndexer = 0;
38
39 /** \brief Search the entity/parameter with specified ID in the list of elements
40  *  \param[in] theEntityID unique ID of the element
41  *  \param[in] theEntities list of elements
42  *  \return position of the found element or -1 if the element is not found
43  */
44 template <typename T>
45 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
46
47
48 // ========================================================
49 // =========  SketchSolver_ConstraintGroup  ===============
50 // ========================================================
51
52 SketchSolver_ConstraintGroup::
53   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
54   : myID(++myGroupIndexer),
55     myParamMaxID(0),
56     myEntityMaxID(0),
57     myConstrMaxID(0),
58     myConstraintMap(),
59     myNeedToSolve(false),
60     myConstrSolver()
61 {
62   myParams.clear();
63   myEntities.clear();
64   myConstraints.clear();
65   
66   myTempConstraints.clear();
67   myTempPointWhereDragged.clear();
68   myTempPointWDrgdID = 0;
69
70   // Initialize workplane
71   myWorkplane.h = SLVS_E_UNKNOWN;
72 #ifndef NDEBUG
73   assert(addWorkplane(theWorkplane));
74 #else
75   addWorkplane(theWorkplane);
76 #endif
77 }
78
79 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
80 {
81   myParams.clear();
82   myEntities.clear();
83   myConstraints.clear();
84   myConstraintMap.clear();
85   myTempConstraints.clear();
86   myTempPointWhereDragged.clear();
87
88   // If the group with maximal identifier is deleted, decrease the indexer
89   if (myID == myGroupIndexer)
90     myGroupIndexer--;
91 }
92
93 // ============================================================================
94 //  Function: isBaseWorkplane
95 //  Class:    SketchSolver_ConstraintGroup
96 //  Purpose:  verify the group is based on the given workplane
97 // ============================================================================
98 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
99                 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
100 {
101   return theWorkplane == mySketch;
102 }
103
104 // ============================================================================
105 //  Function: isInteract
106 //  Class:    SketchSolver_ConstraintGroup
107 //  Purpose:  verify are there any entities in the group used by given constraint
108 // ============================================================================
109 bool SketchSolver_ConstraintGroup::isInteract(
110                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
111 {
112   // Check the group is empty
113   if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
114     return true;
115
116   // Go through constraint entities and verify if some of them already in the group
117   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
118   {
119     boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
120       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
121         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
122       );
123     if (!aCAttrRef) continue;
124     if (!aCAttrRef->isFeature() && 
125         myEntityAttrMap.find(aCAttrRef->attr()) != myEntityAttrMap.end())
126       return true;
127     if (aCAttrRef->isFeature() && 
128         myEntityFeatMap.find(aCAttrRef->feature()) != myEntityFeatMap.end())
129       return true;
130   }
131
132   // Entities did not found
133   return false;
134 }
135
136 // ============================================================================
137 //  Function: changeConstraint
138 //  Class:    SketchSolver_ConstraintGroup
139 //  Purpose:  create/update the constraint in the group
140 // ============================================================================
141 bool SketchSolver_ConstraintGroup::changeConstraint(
142                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
143 {
144   // There is no workplane yet, something wrong
145   if (myWorkplane.h == SLVS_E_UNKNOWN)
146     return false;
147
148   // Search this constraint in the current group to update it
149   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
150     aConstrMapIter = myConstraintMap.find(theConstraint);
151   std::vector<Slvs_Constraint>::iterator aConstrIter;
152   if (aConstrMapIter != myConstraintMap.end())
153   {
154     int aConstrPos = Search(aConstrMapIter->second, myConstraints);
155     aConstrIter = myConstraints.begin() + aConstrPos;
156   }
157
158   // Get constraint type and verify the constraint parameters are correct
159   SketchSolver_Constraint aConstraint(theConstraint);
160   int aConstrType = aConstraint.getType();
161   if (aConstrType == SLVS_C_UNKNOWN ||
162      (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
163     return false;
164   const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
165
166   // Create constraint parameters
167   double aDistance = 0.0; // scalar value of the constraint
168   AttributeDoublePtr aDistAttr =
169     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
170   if (aDistAttr)
171   {
172     aDistance = aDistAttr->value();
173     // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
174     if (aConstrType == SLVS_C_DIAMETER)
175       aDistance *= 2.0;
176     if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
177     {
178       myNeedToSolve = true;
179       aConstrIter->valA = aDistance;
180     }
181   }
182
183   Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
184   for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
185   {
186     aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
187     boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
188       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
189         theConstraint->data()->attribute(aConstraintAttributes[indAttr])
190       );
191     if (!aConstrAttr) continue;
192
193     // For the length constraint the start and end points of the line should be added to the entities list instead of line
194     if (aConstrType == SLVS_C_PT_PT_DISTANCE && theConstraint->getKind().compare(SKETCH_CONSTRAINT_LENGTH_KIND) == 0)
195     {
196       boost::shared_ptr<ModelAPI_Data> aData = aConstrAttr->feature()->data();
197       aConstrEnt[indAttr]   = changeEntity(aData->attribute(LINE_ATTR_START));
198       aConstrEnt[indAttr+1] = changeEntity(aData->attribute(LINE_ATTR_END));
199       myEntityFeatMap[aConstrAttr->feature()] = 0; // measured object is added into the map of objects to avoid problems with interaction betwee constraint and group
200       break; // there should be no other entities
201     }
202     else if (aConstrAttr->isFeature())
203       aConstrEnt[indAttr] = changeEntity(aConstrAttr->feature());
204     else
205       aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
206   }
207
208   if (aConstrMapIter == myConstraintMap.end())
209   {
210     // Several points may be coincident, it is not necessary to store all constraints between them.
211     // Try to find sequence of coincident points which connects the points of new constraint
212     if (aConstrType == SLVS_C_POINTS_COINCIDENT &&
213         !addCoincidentPoints(aConstrEnt[0], aConstrEnt[1]))
214     {
215       myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
216       return false;
217     }
218
219     // Create SolveSpace constraint structure
220     Slvs_Constraint aConstraint =
221       Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
222                           aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
223     myConstraints.push_back(aConstraint);
224     myConstraintMap[theConstraint] = aConstraint.h;
225   }
226   return true;
227 }
228
229 // ============================================================================
230 //  Function: changeEntity
231 //  Class:    SketchSolver_ConstraintGroup
232 //  Purpose:  create/update the element affected by any constraint
233 // ============================================================================
234 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
235                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
236 {
237   // If the entity is already in the group, try to find it
238   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
239     aEntIter = myEntityAttrMap.find(theEntity);
240   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
241   if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
242     aParamIter = myParams.end();
243   else
244   { // the entity already exists
245     int aEntPos = Search(aEntIter->second, myEntities);
246     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
247     aParamIter = myParams.begin() + aParamPos;
248   }
249   const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
250
251   // Look over supported types of entities
252
253   // Point in 3D
254   boost::shared_ptr<GeomDataAPI_Point> aPoint =
255     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
256   if (aPoint)
257   {
258     Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
259     Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
260     Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
261
262     if (isEntExists)
263       return aEntIter->second;
264
265     // New entity
266     Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
267     myEntities.push_back(aPtEntity);
268     myEntityAttrMap[theEntity] = aPtEntity.h;
269     return aPtEntity.h;
270   }
271
272   // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
273   if (myWorkplane.h == SLVS_E_UNKNOWN)
274     return SLVS_E_UNKNOWN;
275
276   // Point in 2D
277   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
278     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
279   if (aPoint2D)
280   {
281     Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
282     Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
283
284     if (isEntExists)
285       return aEntIter->second;
286
287     // New entity
288     Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
289     myEntities.push_back(aPt2DEntity);
290     myEntityAttrMap[theEntity] = aPt2DEntity.h;
291     return aPt2DEntity.h;
292   }
293
294   // Scalar value (used for the distance entities)
295   AttributeDoublePtr aScalar = 
296     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
297   if (aScalar)
298   {
299     Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
300
301     if (isEntExists)
302       return aEntIter->second;
303
304     // New entity
305     Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
306     myEntities.push_back(aDistance);
307     myEntityAttrMap[theEntity] = aDistance.h;
308     return aDistance.h;
309   }
310
311   /// \todo Other types of entities
312
313   // Unsupported or wrong entity type
314   return SLVS_E_UNKNOWN;
315 }
316
317
318 // ============================================================================
319 //  Function: changeEntity
320 //  Class:    SketchSolver_ConstraintGroup
321 //  Purpose:  create/update the element defined by the feature affected by any constraint
322 // ============================================================================
323 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
324                 FeaturePtr theEntity)
325 {
326   // If the entity is already in the group, try to find it
327   std::map<FeaturePtr, Slvs_hEntity>::const_iterator
328     aEntIter = myEntityFeatMap.find(theEntity);
329   // defines that the entity already exists
330   const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
331
332   // SketchPlugin features
333   boost::shared_ptr<SketchPlugin_Feature> aFeature =
334     boost::dynamic_pointer_cast<SketchPlugin_Feature>(theEntity);
335   if (aFeature)
336   { // Verify the feature by its kind
337     const std::string& aFeatureKind = aFeature->getKind();
338
339     // Line
340     if (aFeatureKind.compare(SKETCH_LINE_KIND) == 0)
341     {
342       Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(LINE_ATTR_START));
343       Slvs_hEntity aEnd   = changeEntity(aFeature->data()->attribute(LINE_ATTR_END));
344
345       if (isEntExists)
346         return aEntIter->second;
347
348       // New entity
349       Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
350       myEntities.push_back(aLineEntity);
351       myEntityFeatMap[theEntity] = aLineEntity.h;
352       return aLineEntity.h;
353     }
354     // Circle
355     else if (aFeatureKind.compare(SKETCH_CIRCLE_KIND) == 0)
356     {
357       Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_CENTER));
358       Slvs_hEntity aRadius = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
359
360       if (isEntExists)
361         return aEntIter->second;
362
363       // New entity
364       Slvs_Entity aCircleEntity = 
365         Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter, myWorkplane.normal, aRadius);
366       myEntities.push_back(aCircleEntity);
367       myEntityFeatMap[theEntity] = aCircleEntity.h;
368       return aCircleEntity.h;
369     }
370     // Arc
371     else if (aFeatureKind.compare(SKETCH_ARC_KIND) == 0)
372     {
373       Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(ARC_ATTR_CENTER));
374       Slvs_hEntity aStart  = changeEntity(aFeature->data()->attribute(ARC_ATTR_START));
375       Slvs_hEntity aEnd    = changeEntity(aFeature->data()->attribute(ARC_ATTR_END));
376
377       if (isEntExists)
378         return aEntIter->second;
379
380       Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, 
381                                   myWorkplane.h, myWorkplane.normal, aCenter, aStart, aEnd);
382       myEntities.push_back(anArcEntity);
383       myEntityFeatMap[theEntity] = anArcEntity.h;
384       return anArcEntity.h;
385     }
386     // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
387     else if (aFeatureKind.compare(SKETCH_POINT_KIND) == 0)
388     {
389       Slvs_hEntity aPoint = changeEntity(aFeature->data()->attribute(POINT_ATTR_COORD));
390
391       if (isEntExists)
392         return aEntIter->second;
393
394       // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
395       myEntityFeatMap[theEntity] = aPoint;
396       return aPoint;
397     }
398   }
399
400   /// \todo Other types of features
401
402   // Unsupported or wrong entity type
403   return SLVS_E_UNKNOWN;
404 }
405
406 // ============================================================================
407 //  Function: changeNormal
408 //  Class:    SketchSolver_ConstraintGroup
409 //  Purpose:  create/update the normal of workplane
410 // ============================================================================
411 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
412                 boost::shared_ptr<ModelAPI_Attribute> theDirX,
413                 boost::shared_ptr<ModelAPI_Attribute> theDirY,
414                 boost::shared_ptr<ModelAPI_Attribute> theNorm)
415 {
416   boost::shared_ptr<GeomDataAPI_Dir> aDirX =
417     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
418   boost::shared_ptr<GeomDataAPI_Dir> aDirY =
419     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
420   if (!aDirX || !aDirY ||
421      (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
422      (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
423     return SLVS_E_UNKNOWN;
424
425   // quaternion parameters of normal vector
426   double qw, qx, qy, qz;
427   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
428                       aDirY->x(), aDirY->y(), aDirY->z(),
429                       &qw, &qx, &qy, &qz);
430   double aNormCoord[4] = {qw, qx, qy, qz};
431
432   // Try to find existent normal
433   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
434     aEntIter = myEntityAttrMap.find(theNorm);
435   std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
436   if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
437     aParamIter = myParams.end();
438   else
439   { // the entity already exists, update it
440     int aEntPos = Search(aEntIter->second, myEntities);
441     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
442     aParamIter = myParams.begin() + aParamPos;
443   }
444
445   // Change parameters of the normal
446   Slvs_hParam aNormParams[4];
447   for (int i = 0; i < 4; i++)
448     aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
449
450   if (aEntIter != myEntityAttrMap.end()) // the entity already exists
451     return aEntIter->second;
452
453   // Create a normal
454   Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
455                 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
456   myEntities.push_back(aNormal);
457   myEntityAttrMap[theNorm] = aNormal.h;
458   return aNormal.h;
459 }
460
461
462 // ============================================================================
463 //  Function: addWorkplane
464 //  Class:    SketchSolver_ConstraintGroup
465 //  Purpose:  create workplane for the group
466 // ============================================================================
467 bool SketchSolver_ConstraintGroup::addWorkplane(
468                 boost::shared_ptr<SketchPlugin_Feature> theSketch)
469 {
470   if (myWorkplane.h || theSketch->getKind().compare(SKETCH_KIND) != 0)
471     return false; // the workplane already exists or the function parameter is not Sketch
472
473   mySketch = theSketch;
474   updateWorkplane();
475   return true;
476 }
477
478 // ============================================================================
479 //  Function: updateWorkplane
480 //  Class:    SketchSolver_ConstraintGroup
481 //  Purpose:  update parameters of workplane
482 // ============================================================================
483 bool SketchSolver_ConstraintGroup::updateWorkplane()
484 {
485   // Get parameters of workplane
486   boost::shared_ptr<ModelAPI_Attribute> aDirX    = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
487   boost::shared_ptr<ModelAPI_Attribute> aDirY    = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
488   boost::shared_ptr<ModelAPI_Attribute> aNorm    = mySketch->data()->attribute(SKETCH_ATTR_NORM);
489   boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
490   // Transform them into SolveSpace format
491   Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
492   if (!aNormalWP) return false;
493   Slvs_hEntity anOriginWP = changeEntity(anOrigin);
494   if (!anOriginWP) return false;
495
496   if (!myWorkplane.h)
497   {
498     // Create workplane
499     myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
500     // Workplane should be added to the list of entities
501     myEntities.push_back(myWorkplane);
502   }
503   return true;
504 }
505
506 // ============================================================================
507 //  Function: changeParameter
508 //  Class:    SketchSolver_ConstraintGroup
509 //  Purpose:  create/update value of parameter
510 // ============================================================================
511 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
512                 const double&                            theParam,
513                 std::vector<Slvs_Param>::const_iterator& thePrmIter)
514 {
515   if (thePrmIter != myParams.end())
516   { // Parameter should be updated
517     int aParamPos = thePrmIter - myParams.begin();
518     if (fabs(thePrmIter->val - theParam) > tolerance)
519     {
520       myNeedToSolve = true; // parameter is changed, need to resolve constraints
521       myParams[aParamPos].val = theParam;
522     }
523     thePrmIter++;
524     return myParams[aParamPos].h;
525   }
526
527   // Newly created parameter
528   Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
529   myParams.push_back(aParam);
530   myNeedToSolve = true;
531   // The list of parameters is changed, move iterator to the end of the list to avoid problems
532   thePrmIter = myParams.end();
533   return aParam.h;
534 }
535
536 // ============================================================================
537 //  Function: resolveConstraints
538 //  Class:    SketchSolver_ConstraintGroup
539 //  Purpose:  solve the set of constraints for the current group
540 // ============================================================================
541 void SketchSolver_ConstraintGroup::resolveConstraints()
542 {
543   if (!myNeedToSolve)
544     return;
545
546   myConstrSolver.setGroupID(myID);
547   myConstrSolver.setParameters(myParams);
548   myConstrSolver.setEntities(myEntities);
549   myConstrSolver.setConstraints(myConstraints);
550   myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
551
552   int aResult = myConstrSolver.solve();
553   if (aResult == SLVS_RESULT_OKAY)
554   { // solution succeeded, store results into correspondent attributes
555     // Obtain result into the same list of parameters
556     if (!myConstrSolver.getResult(myParams))
557       return;
558
559     // We should go through the attributes map, because only attributes have valued parameters
560     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
561       anEntIter = myEntityAttrMap.begin();
562     for ( ; anEntIter != myEntityAttrMap.end(); anEntIter++)
563       updateAttribute(anEntIter->first, anEntIter->second);
564   }
565   else if (!myConstraints.empty())
566     Events_Error::send(ERROR_SOLVE_CONSTRAINTS, this);
567
568   removeTemporaryConstraints();
569   myNeedToSolve = false;
570 }
571
572 // ============================================================================
573 //  Function: mergeGroups
574 //  Class:    SketchSolver_ConstraintGroup
575 //  Purpose:  append specified group to the current group
576 // ============================================================================
577 void SketchSolver_ConstraintGroup::mergeGroups(
578                 const SketchSolver_ConstraintGroup& theGroup)
579 {
580   // If specified group is empty, no need to merge
581   if (theGroup.myConstraintMap.empty())
582     return ;
583
584   // Map between old and new indexes of SolveSpace constraints
585   std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
586
587   // Add all constraints from theGroup to the current group
588   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
589     aConstrIter = theGroup.myConstraintMap.begin();
590   for ( ; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
591     if (changeConstraint(aConstrIter->first))
592       aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
593
594   // Add temporary constraints from theGroup
595   std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
596   for ( ; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++)
597   {
598     std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(*aTempConstrIter);
599     if (aFind != aConstrMap.end())
600       myTempConstraints.push_back(aFind->second);
601   }
602
603   if (myTempPointWhereDragged.empty())
604     myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
605   else if (!theGroup.myTempPointWhereDragged.empty())
606   { // Need to create additional transient constraint
607     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
608       aFeatureIter = theGroup.myEntityAttrMap.begin();
609     for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
610       if (aFeatureIter->second == myTempPointWDrgdID)
611       {
612         addTemporaryConstraintWhereDragged(aFeatureIter->first);
613         break;
614       }
615   }
616
617   myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
618 }
619
620 // ============================================================================
621 //  Function: splitGroup
622 //  Class:    SketchSolver_ConstraintGroup
623 //  Purpose:  divide the group into several subgroups
624 // ============================================================================
625 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
626 {
627   // Divide constraints and entities into several groups
628   std::vector< std::set<Slvs_hEntity> >     aGroupsEntities;
629   std::vector< std::set<Slvs_hConstraint> > aGroupsConstr;
630   int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
631   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
632   for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
633   {
634     Slvs_hEntity aConstrEnt[] = {
635       aConstrIter->ptA,     aConstrIter->ptB,
636       aConstrIter->entityA, aConstrIter->entityB};
637     std::vector<int> anIndexes;
638     // Go through the groupped entities and find even one of entities of current constraint
639     std::vector< std::set<Slvs_hEntity> >::iterator aGrEntIter;
640     for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++)
641     {
642       bool isFound = false;
643       for (int i = 0; i < 4 && !isFound; i++)
644         if (aConstrEnt[i] != 0)
645           isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
646       if (isFound)
647         anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
648     }
649     // Add new group if no one is found
650     if (anIndexes.empty())
651     {
652       std::set<Slvs_hEntity> aNewGrEnt;
653       for (int i = 0; i < 4; i++)
654         if (aConstrEnt[i] != 0)
655           aNewGrEnt.insert(aConstrEnt[i]);
656       std::set<Slvs_hConstraint> aNewGrConstr;
657       aNewGrConstr.insert(aConstrIter->h);
658
659       aGroupsEntities.push_back(aNewGrEnt);
660       aGroupsConstr.push_back(aNewGrConstr);
661       if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
662         aMaxNbEntities = aGroupsEntities.size() - 1;
663     }
664     else if (anIndexes.size() == 1)
665     { // Add entities indexes into the found group
666       aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
667       for (int i = 0; i < 4; i++)
668         if (aConstrEnt[i] != 0)
669           aGrEntIter->insert(aConstrEnt[i]);
670       aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
671       if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
672         aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
673     }
674     else 
675     { // There are found several connected groups, merge them
676       std::vector< std::set<Slvs_hEntity> >::iterator aFirstGroup = 
677         aGroupsEntities.begin() + anIndexes.front();
678       std::vector< std::set<Slvs_hConstraint> >::iterator aFirstConstr = 
679         aGroupsConstr.begin() + anIndexes.front();
680       std::vector<int>::iterator anInd = anIndexes.begin();
681       for (++anInd; anInd != anIndexes.end(); anInd++)
682       {
683         aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
684         aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
685       }
686       if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
687         aMaxNbEntities = anIndexes.front();
688       // Remove merged groups
689       for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--)
690       {
691         aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
692         aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
693       }
694     }
695   }
696
697   if (aGroupsEntities.size() <= 1)
698     return ;
699
700   // Remove the group with maximum elements as it will be left in the current group
701   aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
702   aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
703
704   // Add new groups of constraints and divide current group
705   std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
706   for (int i = aGroupsEntities.size(); i > 0; i--)
707   {
708     SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
709     aNewGroups.push_back(aG);
710   }
711   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
712     aConstrMapIter = myConstraintMap.begin();
713   int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
714   while (aConstrMapIter != myConstraintMap.end())
715   {
716     std::vector< std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
717     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
718     for ( ; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
719       if (aGIter->find(aConstrMapIter->second) != aGIter->end())
720       {
721         (*aGroup)->changeConstraint(aConstrMapIter->first);
722         removeConstraint(aConstrMapIter->first);
723         // restore iterator
724         aConstrMapIter = myConstraintMap.begin();
725         for (int i = 0; i < aConstrMapPos; i++)
726           aConstrMapIter++;
727         break;
728       }
729     if (aGIter == aGroupsConstr.end())
730     {
731       aConstrMapIter++;
732       aConstrMapPos++;
733     }
734   }
735
736   theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
737 }
738
739 // ============================================================================
740 //  Function: updateGroup
741 //  Class:    SketchSolver_ConstraintGroup
742 //  Purpose:  search removed entities and constraints
743 // ============================================================================
744 bool SketchSolver_ConstraintGroup::updateGroup()
745 {
746   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
747     aConstrIter = myConstraintMap.rbegin();
748   bool isAllValid = true;
749   bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
750   while (isAllValid && aConstrIter != myConstraintMap.rend())
751   {
752     if (!aConstrIter->first->data()->isValid())
753     {
754       if (aConstrIter->first->getKind().compare(SKETCH_CONSTRAINT_COINCIDENCE_KIND) == 0)
755         isCCRemoved = true;
756       std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
757         aCopyIter = aConstrIter++;
758       removeConstraint(aCopyIter->first);
759       isAllValid = false;
760     }
761     else aConstrIter++;
762   }
763
764   // Probably, need to update coincidence constraints
765   if (isCCRemoved && !myExtraCoincidence.empty())
766   {
767     // Make a copy, because the new list of unused constrtaints will be generated
768     std::set< boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
769     myExtraCoincidence.clear();
770
771     std::set< boost::shared_ptr<SketchPlugin_Constraint> >::iterator
772       aCIter = anExtraCopy.begin();
773     for ( ; aCIter != anExtraCopy.end(); aCIter++)
774       if ((*aCIter)->data()->isValid())
775         changeConstraint(*aCIter);
776   }
777
778   return !isAllValid;
779 }
780
781 // ============================================================================
782 //  Function: updateAttribute
783 //  Class:    SketchSolver_ConstraintGroup
784 //  Purpose:  update features of sketch after resolving constraints
785 // ============================================================================
786 void SketchSolver_ConstraintGroup::updateAttribute(
787                 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
788                 const Slvs_hEntity&                   theEntityID)
789 {
790   // Search the position of the first parameter of the entity
791   int anEntPos = Search(theEntityID, myEntities);
792   int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
793
794   // Look over supported types of entities
795
796   // Point in 3D
797   boost::shared_ptr<GeomDataAPI_Point> aPoint =
798     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
799   if (aPoint)
800   {
801     aPoint->setValue(myParams[aFirstParamPos].val,
802                      myParams[aFirstParamPos+1].val,
803                      myParams[aFirstParamPos+2].val);
804     return ;
805   }
806
807   // Point in 2D
808   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
809     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
810   if (aPoint2D)
811   {
812     aPoint2D->setValue(myParams[aFirstParamPos].val,
813                        myParams[aFirstParamPos+1].val);
814     return ;
815   }
816
817   // Scalar value
818   AttributeDoublePtr aScalar = 
819     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
820   if (aScalar)
821   {
822     aScalar->setValue(myParams[aFirstParamPos].val);
823     return ;
824   }
825
826   /// \todo Support other types of entities
827 }
828
829 // ============================================================================
830 //  Function: updateEntityIfPossible
831 //  Class:    SketchSolver_ConstraintGroup
832 //  Purpose:  search the entity in this group and update it
833 // ============================================================================
834 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
835                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
836 {
837   if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end())
838   {
839     // If the attribute is a point and it is changed (the group needs to rebuild),
840     // probably user has dragged this point into this position,
841     // so it is necessary to add constraint which will guarantee the point will not change
842
843     // Store myNeedToSolve flag to verify the entity is really changed
844     bool aNeedToSolveCopy = myNeedToSolve;
845     myNeedToSolve = false;
846
847     changeEntity(theEntity);
848
849     if (myNeedToSolve) // the entity is changed
850     {
851       // Verify the entity is a point and add temporary constraint of permanency
852       boost::shared_ptr<GeomDataAPI_Point> aPoint =
853         boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
854       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
855         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
856       if (aPoint || aPoint2D)
857         addTemporaryConstraintWhereDragged(theEntity);
858     }
859
860     // Restore flag of changes
861     myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
862
863     updateRelatedConstraints(theEntity);
864   }
865 }
866
867 // ============================================================================
868 //  Function: addTemporaryConstraintWhereDragged
869 //  Class:    SketchSolver_ConstraintGroup
870 //  Purpose:  add transient constraint SLVS_C_WHERE_DRAGGED for the entity, 
871 //            which was moved by user
872 // ============================================================================
873 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
874                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
875 {
876   // Find identifier of the entity
877   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
878     anEntIter = myEntityAttrMap.find(theEntity);
879   if (anEntIter == myEntityAttrMap.end())
880     return ;
881
882   // If this is a first dragged point, its parameters should be placed 
883   // into Slvs_System::dragged field to avoid system inconsistense
884   if (myTempPointWhereDragged.empty())
885   {
886     int anEntPos = Search(anEntIter->second, myEntities);
887     Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
888     for (int i = 0; i < 4; i++, aDraggedParam++)
889       if (*aDraggedParam != 0)
890         myTempPointWhereDragged.push_back(*aDraggedParam);
891     myTempPointWDrgdID = myEntities[anEntPos].h;
892     return ;
893   }
894
895   // Get identifiers of all dragged points
896   std::set<Slvs_hEntity> aDraggedPntID;
897   aDraggedPntID.insert(myTempPointWDrgdID);
898   std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
899   for ( ; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++)
900   {
901     unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
902     if (aConstrPos < myConstraints.size())
903       aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
904   }
905   // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
906   std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
907   for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
908   {
909     if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
910       continue; // the entity was not found in current set
911
912     // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
913     std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
914     for ( ; aDrgIter != aDraggedPntID.end(); aDrgIter++)
915       if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
916         return ; // the SLVS_C_WHERE_DRAGGED constraint already exists
917   }
918
919   // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
920   Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
921                                                   myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
922   myConstraints.push_back(aWDConstr);
923   myTempConstraints.push_back(aWDConstr.h);
924 }
925
926 // ============================================================================
927 //  Function: removeTemporaryConstraints
928 //  Class:    SketchSolver_ConstraintGroup
929 //  Purpose:  remove all transient SLVS_C_WHERE_DRAGGED constraints after
930 //            resolving the set of constraints
931 // ============================================================================
932 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
933 {
934   std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
935   for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
936   {
937     unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
938     if (aConstrPos >= myConstraints.size())
939       continue;
940     myConstraints.erase(myConstraints.begin() + aConstrPos);
941
942     // If the removing constraint has higher index, decrease the indexer
943     if (*aTmpConstrIter == myConstrMaxID)
944       myConstrMaxID--;
945   }
946   myTempConstraints.clear();
947
948   // Clear basic dragged point
949   myTempPointWhereDragged.clear();
950 }
951
952 // ============================================================================
953 //  Function: removeConstraint
954 //  Class:    SketchSolver_ConstraintGroup
955 //  Purpose:  remove constraint and all unused entities
956 // ============================================================================
957 void SketchSolver_ConstraintGroup::removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
958 {
959   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator
960     anIterToRemove = myConstraintMap.find(theConstraint);
961   if (anIterToRemove == myConstraintMap.end())
962      return ;
963
964   Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
965   // Remove constraint from the map
966   myConstraintMap.erase(anIterToRemove);
967
968   // Find unused entities
969   int aConstrPos = Search(aCnstrToRemove, myConstraints);
970   std::set<Slvs_hEntity> anEntToRemove;
971   Slvs_hEntity aCnstEnt[] = {myConstraints[aConstrPos].ptA,     myConstraints[aConstrPos].ptB, 
972                              myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
973   for (int i = 0; i < 4; i++)
974     if (aCnstEnt[i] != 0)
975       anEntToRemove.insert(aCnstEnt[i]);
976   myConstraints.erase(myConstraints.begin() + aConstrPos);
977   if (aCnstrToRemove == myConstrMaxID)
978     myConstrMaxID--;
979   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
980   for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
981   {
982     Slvs_hEntity aEnts[] = {aConstrIter->ptA,     aConstrIter->ptB, 
983                             aConstrIter->entityA, aConstrIter->entityB};
984     for (int i = 0; i < 4; i++)
985       if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
986         anEntToRemove.erase(aEnts[i]);
987   }
988
989   if (anEntToRemove.empty())
990     return ;
991
992   // Remove unused entities
993   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
994     anEntAttrIter = myEntityAttrMap.begin();
995   while (anEntAttrIter != myEntityAttrMap.end())
996   {
997     if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end())
998     {
999       std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1000         aRemovedIter = anEntAttrIter;
1001       anEntAttrIter++;
1002       myEntityAttrMap.erase(aRemovedIter);
1003     }
1004     else anEntAttrIter++;
1005   }
1006   std::map<FeaturePtr, Slvs_hEntity>::iterator
1007     anEntFeatIter = myEntityFeatMap.begin();
1008   while (anEntFeatIter != myEntityFeatMap.end())
1009   {
1010     if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end())
1011     {
1012       std::map<FeaturePtr, Slvs_hEntity>::iterator
1013         aRemovedIter = anEntFeatIter;
1014       anEntFeatIter++;
1015       myEntityFeatMap.erase(aRemovedIter);
1016     }
1017     else anEntFeatIter++;
1018   }
1019   std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1020   for ( ; aRemIter != anEntToRemove.rend(); aRemIter++)
1021   {
1022     unsigned int anEntPos = Search(*aRemIter, myEntities);
1023     if (anEntPos >= myEntities.size())
1024       continue;
1025     unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1026     if (aParamPos >= myParams.size())
1027       continue;
1028     int aNbParams = 0;
1029     while (myEntities[anEntPos].param[aNbParams] != 0) 
1030       aNbParams++;
1031     if (myEntities[anEntPos].param[aNbParams-1] == myParamMaxID)
1032       myParamMaxID -= aNbParams;
1033     myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1034     if (*aRemIter == myEntityMaxID)
1035       myEntityMaxID--;
1036     myEntities.erase(myEntities.begin() + anEntPos);
1037
1038     // Remove entity's ID from the lists of conincident points
1039     std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1040     for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1041       aCoPtIter->erase(*aRemIter);
1042   }
1043   if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1044     myCoincidentPoints.clear();
1045 }
1046
1047
1048 // ============================================================================
1049 //  Function: addCoincidentPoints
1050 //  Class:    SketchSolver_ConstraintGroup
1051 //  Purpose:  add coincident point the appropriate list of such points
1052 // ============================================================================
1053 bool SketchSolver_ConstraintGroup::addCoincidentPoints(
1054                 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1055 {
1056   std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1057   std::vector< std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1058   while (aCoPtIter != myCoincidentPoints.end())
1059   {
1060     bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1061       aCoPtIter->find(thePoint1) != aCoPtIter->end(),
1062       aCoPtIter->find(thePoint2) != aCoPtIter->end(),
1063     };
1064     if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1065       return false;
1066     if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1]))
1067     {
1068       if (aFirstFound != myCoincidentPoints.end())
1069       { // there are two groups of coincident points connected by created constraint => merge them
1070         int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1071         int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1072         aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1073         myCoincidentPoints.erase(aCoPtIter);
1074         aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1075         aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1076         continue;
1077       }
1078       else
1079       {
1080         aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1081         aFirstFound = aCoPtIter;
1082       }
1083     }
1084     aCoPtIter++;
1085   }
1086   // No points were found, need to create new set
1087   if (aFirstFound == myCoincidentPoints.end())
1088   {
1089     std::set<Slvs_hEntity> aNewSet;
1090     aNewSet.insert(thePoint1);
1091     aNewSet.insert(thePoint2);
1092     myCoincidentPoints.push_back(aNewSet);
1093   }
1094
1095   return true;
1096 }
1097
1098
1099 // ============================================================================
1100 //  Function: updateRelatedConstraints
1101 //  Class:    SketchSolver_ConstraintGroup
1102 //  Purpose:  emit the signal to update constraints
1103 // ============================================================================
1104 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1105                     boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1106 {
1107   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
1108     aConstrIter = myConstraintMap.begin();
1109   for ( ; aConstrIter != myConstraintMap.end(); aConstrIter++)
1110   {
1111     std::list< boost::shared_ptr<ModelAPI_Attribute> > anAttributes = 
1112       aConstrIter->first->data()->attributes(theEntity->attributeType());
1113
1114     std::list< boost::shared_ptr<ModelAPI_Attribute> >::iterator
1115       anAttrIter = anAttributes.begin();
1116     for ( ; anAttrIter != anAttributes.end(); anAttrIter++)
1117       if (*anAttrIter == theEntity)
1118       {
1119         static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
1120         Model_FeatureUpdatedMessage aMsg(aConstrIter->first, anEvent);
1121         Events_Loop::loop()->send(aMsg, true);
1122         break;
1123       }
1124   }
1125 }
1126
1127 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1128                     boost::shared_ptr<ModelAPI_Feature> theFeature) const
1129 {
1130   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
1131     aConstrIter = myConstraintMap.begin();
1132   for ( ; aConstrIter != myConstraintMap.end(); aConstrIter++)
1133   {
1134     std::list< boost::shared_ptr<ModelAPI_Attribute> > anAttributes = 
1135       aConstrIter->first->data()->attributes(std::string());
1136
1137     std::list< boost::shared_ptr<ModelAPI_Attribute> >::iterator
1138       anAttrIter = anAttributes.begin();
1139     for ( ; anAttrIter != anAttributes.end(); anAttrIter++)
1140     {
1141       boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = 
1142         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
1143       if (aRefAttr && aRefAttr->isFeature() && aRefAttr->feature() == theFeature)
1144       {
1145         static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_UPDATED);
1146         Model_FeatureUpdatedMessage aMsg(aConstrIter->first, anEvent);
1147         Events_Loop::loop()->send(aMsg, true);
1148         break;
1149       }
1150     }
1151   }
1152 }
1153
1154
1155
1156 // ========================================================
1157 // =========      Auxiliary functions       ===============
1158 // ========================================================
1159
1160 template <typename T>
1161 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1162 {
1163   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1164   int aVecSize = theEntities.size();
1165   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1166     aResIndex--;
1167   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1168     aResIndex++;
1169   if (aResIndex == -1)
1170     aResIndex = aVecSize;
1171   return aResIndex;
1172 }