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