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