]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SolveSpaceSolver/SolveSpaceSolver_Storage.cpp
Salome HOME
Update processing Mirror constraint
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SolveSpaceSolver_Storage.h>
8 #include <SolveSpaceSolver_ConstraintWrapper.h>
9 #include <SolveSpaceSolver_EntityWrapper.h>
10 #include <SolveSpaceSolver_ParameterWrapper.h>
11 #include <SolveSpaceSolver_Builder.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_XY.h>
16 #include <math.h>
17 #include <assert.h>
18
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_ConstraintMiddle.h>
25
26 /** \brief Search the entity/parameter with specified ID in the list of elements
27  *  \param[in] theEntityID unique ID of the element
28  *  \param[in] theEntities list of elements
29  *  \return position of the found element or -1 if the element is not found
30  */
31 template<typename T>
32 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
33
34 /// \brief Compare two parameters to be different
35 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
36 /// \brief Compare two entities to be different
37 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
38 /// \brief Compare two constraints to be different
39 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
40
41
42 SolveSpaceSolver_Storage::SolveSpaceSolver_Storage(const GroupID& theGroup)
43   : SketchSolver_Storage(theGroup),
44     myWorkplaneID(SLVS_E_UNKNOWN),
45     myParamMaxID(SLVS_E_UNKNOWN),
46     myEntityMaxID(SLVS_E_UNKNOWN),
47     myConstrMaxID(SLVS_C_UNKNOWN),
48     myDuplicatedConstraint(false)
49 {
50 }
51
52 bool SolveSpaceSolver_Storage::update(ConstraintWrapperPtr theConstraint)
53 {
54   bool isUpdated = false;
55   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
56       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
57   Slvs_Constraint aSlvsConstr = getConstraint((Slvs_hConstraint)aConstraint->id());
58   if (aSlvsConstr.h == SLVS_C_UNKNOWN)
59     aSlvsConstr = aConstraint->constraint();
60
61   // update value of constraint if exist
62   double aCoeff = aSlvsConstr.type == SLVS_C_DIAMETER ? 2.0 : 1.0;
63   if (fabs(aSlvsConstr.valA - theConstraint->value() * aCoeff) > tolerance) {
64     aSlvsConstr.valA = theConstraint->value() * aCoeff;
65     isUpdated = true;
66   }
67
68   // update constrained entities
69   Slvs_hEntity* aPnts[2] = {&aSlvsConstr.ptA, &aSlvsConstr.ptB};
70   Slvs_hEntity* anEnts[4] = {&aSlvsConstr.entityA, &aSlvsConstr.entityB,
71                              &aSlvsConstr.entityC, &aSlvsConstr.entityD};
72   int aPtInd = 0;
73   int aEntInd = 0;
74
75   std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
76   std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
77   for (; anIt != anEntities.end(); ++anIt) {
78     isUpdated = update(*anIt) || isUpdated;
79     // do not update constrained entities for Multi constraints,
80     // and for middle point constraint translated to equal lines
81     if (aSlvsConstr.type == SLVS_C_MULTI_ROTATION || aSlvsConstr.type == SLVS_C_MULTI_TRANSLATION ||
82        (theConstraint->baseConstraint()->getKind() == SketchPlugin_ConstraintMiddle::ID() &&
83         aSlvsConstr.type != SLVS_C_AT_MIDPOINT))
84       continue;
85
86     Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
87     if ((*anIt)->type() == ENTITY_POINT) {
88       if (*(aPnts[aPtInd]) != anID) {
89         *(aPnts[aPtInd]) = anID;
90         isUpdated = true;
91       }
92       ++aPtInd;
93     } else {
94       if (*(anEnts[aEntInd]) != anID) {
95         *(anEnts[aEntInd]) = anID;
96         isUpdated = true;
97       }
98       ++aEntInd;
99     }
100   }
101
102   // update constraint itself (do not update constraints Multi)
103   if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
104     if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
105       aSlvsConstr.wrkpl = myWorkplaneID;
106     if (aSlvsConstr.group == SLVS_G_UNKNOWN)
107       aSlvsConstr.group = (Slvs_hGroup)myGroupID;
108     bool hasDupConstraints = myDuplicatedConstraint;
109     Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
110     if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
111       aConstraint->changeConstraint() = getConstraint(aConstrID);
112       isUpdated = true;
113       // check duplicated constraints based on different attributes
114       if (myDuplicatedConstraint && findSameConstraint(aConstraint) && !hasDupConstraints)
115         myDuplicatedConstraint = false;
116     }
117   }
118   return isUpdated;
119 }
120
121 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr theEntity)
122 {
123   bool isUpdated = false;
124   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
125       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
126   Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
127   if (aSlvsEnt.h == SLVS_E_UNKNOWN)
128     aSlvsEnt = anEntity->entity();
129
130   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
131   std::list<ParameterWrapperPtr>::iterator aPIt;
132   // if the entity is an attribute, need to update its coordinates
133   if (anEntity->baseAttribute()) {
134     BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
135     EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
136     if (anUpdAttr) {
137       std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
138       std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
139       for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
140           ++aPIt, ++anUpdIt) {
141         (*aPIt)->update(*anUpdIt);
142       }
143     }
144   }
145
146   // update parameters
147   int anInd = 0;
148   for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
149     assert(anInd < 4);
150     isUpdated = update(*aPIt) || isUpdated;
151     if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
152       isUpdated = true;
153       aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
154     }
155   }
156
157   // update sub-entities
158   std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
159   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
160   for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
161     assert(anInd < 4);
162     isUpdated = update(*aSIt) || isUpdated;
163
164     Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
165     if ((*aSIt)->type() == ENTITY_NORMAL)
166       aSlvsEnt.normal = anID;
167     else if ((*aSIt)->type() == ENTITY_SCALAR)
168       aSlvsEnt.distance = anID;
169     else if (aSlvsEnt.point[anInd] != anID) {
170       aSlvsEnt.point[anInd] = anID;
171       if ((*aSIt)->baseAttribute())
172         SketchSolver_Storage::addEntity((*aSIt)->baseAttribute(), *aSIt);
173       isUpdated = true;
174     }
175   }
176   if (theEntity->type() == ENTITY_POINT && aSubEntities.size() == 1) {
177     // theEntity is based on SketchPlugin_Point => need to substitute its attribute instead
178     bool isNew = (aSlvsEnt.h == SLVS_E_UNKNOWN);
179     aSlvsEnt = getEntity(aSlvsEnt.point[0]);
180     if (isNew) {
181       anEntity->changeEntity() = aSlvsEnt;
182       isUpdated = true;
183     }
184   }
185
186   // update entity itself
187   if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
188     aSlvsEnt.wrkpl = myWorkplaneID;
189   if (aSlvsEnt.group == SLVS_G_UNKNOWN)
190     aSlvsEnt.group = (Slvs_hGroup)myGroupID;
191   Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
192   if (aSlvsEnt.h == SLVS_E_UNKNOWN || isUpdated) {
193     anEntity->changeEntity() = getEntity(anEntID);
194     isUpdated = true;
195
196     if (anEntity->type() == ENTITY_SKETCH)
197       storeWorkplane(anEntity);
198   }
199
200   return isUpdated;
201 }
202
203 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr theParameter)
204 {
205   std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter = 
206       std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
207   const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
208   if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
209     return false;
210   Slvs_Param aParamToUpd = aParameter->parameter();
211   if (aParamToUpd.group == SLVS_G_UNKNOWN)
212     aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP : (Slvs_hGroup)myGroupID;
213   Slvs_hParam anID = updateParameter(aParamToUpd);
214   if (aParam.h == SLVS_E_UNKNOWN) // new parameter
215     aParameter->changeParameter() = getParameter(anID);
216   return true;
217 }
218
219 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
220 {
221   mySketchID = theSketch->id();
222   myWorkplaneID = (Slvs_hEntity)mySketchID;
223
224   // Update sub-entities of the sketch
225   std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
226   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
227   for (; aSIt != aSubEntities.end(); ++aSIt) {
228     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
229         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
230     aSub->changeEntity().wrkpl = myWorkplaneID;
231   }
232
233   // Update all stored entities
234   std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
235   for (; anIt != myEntities.end(); ++anIt)
236     anIt->wrkpl = myWorkplaneID;
237 }
238
239 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
240 {
241   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
242   std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
243   for (; aPIt != aParams.end(); ++aPIt)
244     changeGroup(*aPIt, theGroup);
245
246   std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
247   std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
248   for (; aSIt != aSubs.end(); ++aSIt)
249     changeGroup(*aSIt, theGroup);
250
251   theEntity->setGroup(theGroup);
252   int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
253   if (aPos >= 0 && aPos < (int)myEntities.size()) {
254     if (myEntities[aPos].group != (Slvs_hGroup)theGroup)
255       setNeedToResolve(true);
256     myEntities[aPos].group = (Slvs_hGroup)theGroup;
257   }
258 }
259
260 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
261 {
262   GroupID aGroup = theGroup;
263   if (theParam->isParametric())
264     aGroup = GID_OUTOFGROUP;
265   if (theParam->group() == aGroup)
266     return;
267
268   theParam->setGroup(aGroup);
269   int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
270   if (aPos >= 0 && aPos < (int)myParameters.size()) {
271     myParameters[aPos].group = (Slvs_hGroup)aGroup;
272     setNeedToResolve(true);
273   }
274 }
275
276 void SolveSpaceSolver_Storage::addCoincidentPoints(
277     EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
278 {
279   if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
280     return;
281   if (!theMaster->subEntities().empty() || !theSlave->subEntities().empty()) {
282     EntityWrapperPtr aSubMaster = theMaster->subEntities().empty() ?
283         theMaster : theMaster->subEntities().front();
284     EntityWrapperPtr aSubSlave = theSlave->subEntities().empty() ?
285         theSlave : theSlave->subEntities().front();
286     return addCoincidentPoints(aSubMaster, aSubSlave);
287   }
288
289   // Search available coincidence
290   CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
291   CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
292   if (aMasterFound == myCoincidentPoints.end() || aSlaveFound == myCoincidentPoints.end()) {
293     // try to find master and slave points in the lists of slaves of already existent coincidences
294     CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
295     for (; anIt != myCoincidentPoints.end(); ++anIt) {
296       if (anIt->second.find(theMaster) != anIt->second.end())
297         aMasterFound = anIt;
298       else if (anIt->second.find(theSlave) != anIt->second.end())
299         aSlaveFound = anIt;
300
301       if (aMasterFound != myCoincidentPoints.end() &&  aSlaveFound != myCoincidentPoints.end())
302         break;
303     }
304   }
305
306   if (aMasterFound == myCoincidentPoints.end()) {
307     // create new group
308     myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
309     aMasterFound = myCoincidentPoints.find(theMaster);
310   } else if (aMasterFound == aSlaveFound)
311     return; // already coincident
312
313   if (aSlaveFound != myCoincidentPoints.end()) {
314     // A slave has been found, we need to attach all points coincident with it to the new master
315     std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
316     aNewSlaves.insert(aSlaveFound->first);
317     myCoincidentPoints.erase(aSlaveFound);
318
319     std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
320     for (; aSlIt != aNewSlaves.end(); ++aSlIt)
321       addCoincidentPoints(theMaster, *aSlIt);
322   } else {
323     // Update the slave if it was used in constraints and features
324     replaceInFeatures(theSlave, theMaster);
325     replaceInConstraints(theSlave, theMaster);
326
327     // Remove slave entity (if the IDs are equal no need to remove slave entity, just update it)
328     if (theMaster->id() != theSlave->id())
329       removeEntity((Slvs_hEntity)theSlave->id());
330
331     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster = 
332         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
333     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave = 
334         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
335     aPointSlave->changeEntity() = aPointMaster->entity();
336     aPointSlave->setParameters(aPointMaster->parameters());
337
338     aMasterFound->second.insert(theSlave);
339   }
340 }
341
342 void SolveSpaceSolver_Storage::replaceInFeatures(
343     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
344 {
345   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
346   for (; anIt != myFeatureMap.end(); ++anIt) {
347     if (!anIt->second)
348       continue;
349     bool isUpdated = false;
350     std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
351     std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
352     for (; aSubIt != aSubs.end(); ++aSubIt)
353       if ((*aSubIt)->id() == theSource->id()) {
354         (*aSubIt)->update(theDest);
355         isUpdated = true;
356       }
357
358     if (!isUpdated)
359       continue;
360
361     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
362         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
363     // update SolveSpace entity
364     Slvs_Entity anEnt = aWrapper->entity();
365     for (int i = 0; i < 4; ++i)
366       if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
367         anEnt.point[i] = (Slvs_hEntity)theDest->id();
368     anEnt.h = updateEntity(anEnt);
369     aWrapper->changeEntity() = anEnt;
370
371     // update sub-entities
372     aWrapper->setSubEntities(aSubs);
373   }
374 }
375
376 void SolveSpaceSolver_Storage::replaceInConstraints(
377     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
378 {
379   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
380       anIt = myConstraintMap.begin();
381   std::list<ConstraintWrapperPtr>::const_iterator aCIt;
382   for (; anIt != myConstraintMap.end(); ++anIt)
383     for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
384       // Do not process coincidence between points and "multi" constraints
385       // (these constraints are stored to keep the structure of constraints).
386       if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
387           (*aCIt)->type() == CONSTRAINT_MULTI_ROTATION ||
388           (*aCIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
389         continue;
390
391       bool isUpdated = false;
392       std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
393       std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
394       for (; aSubIt != aSubs.end(); ++aSubIt)
395         if ((*aSubIt)->id() == theSource->id()) {
396           (*aSubIt)->update(theDest);
397           isUpdated = true;
398         }
399
400       if (!isUpdated)
401         continue;
402
403       std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
404           std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
405       if (theSource->id() == theDest->id()) {
406         // No need to update SolveSpace constraint if the entities are the same
407         aWrapper->changeConstraint() = getConstraint((Slvs_hConstraint)aWrapper->id());
408         aWrapper->setEntities(aSubs);
409         continue;
410       }
411
412       // change constraint entities
413       Slvs_Constraint aConstr = aWrapper->constraint();
414       if (aConstr.ptA == (Slvs_hEntity)theSource->id())
415         aConstr.ptA = (Slvs_hEntity)theDest->id();
416       if (aConstr.ptB == (Slvs_hEntity)theSource->id())
417         aConstr.ptB = (Slvs_hEntity)theDest->id();
418
419       // check the constraint is duplicated
420       std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
421       for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
422         if (aConstr.h != aSlvsCIt->h &&
423             aConstr.type == aSlvsCIt->type &&
424             aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
425             aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
426             aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
427           Slvs_hConstraint anIDToRemove = aConstr.h;
428           aConstr = *aSlvsCIt;
429           int aShift = (int)(aSlvsCIt - myConstraints.begin());
430           removeConstraint(anIDToRemove);
431           aSlvsCIt = myConstraints.begin() + aShift - 1;
432           for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
433             if (aSlvsCIt->h == aConstr.h)
434               break;
435           break;
436         }
437
438       if (aSlvsCIt != myConstraints.end()) {
439         // constraint is duplicated, search its wrapper to add the mapping
440         std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
441             anIt2 = myConstraintMap.begin();
442         std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
443         for (; anIt2 != myConstraintMap.end(); ++anIt2)
444           for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
445             if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
446               addSameConstraints(*aCIt2, aWrapper);
447               break;
448             }
449       } else 
450         aConstr.h = updateConstraint(aConstr);
451       aWrapper->changeConstraint() = aConstr;
452
453       // update sub-entities
454       aWrapper->setEntities(aSubs);
455     }
456 }
457
458 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
459                                                   ConstraintWrapperPtr theConstraint2)
460 {
461   SameConstraintMap::iterator anIt = myEqualConstraints.begin();
462   for (; anIt != myEqualConstraints.end(); ++anIt) {
463     if (anIt->find(theConstraint1) != anIt->end()) {
464       anIt->insert(theConstraint2);
465       return;
466     }
467     else if (anIt->find(theConstraint2) != anIt->end()) {
468       anIt->insert(theConstraint1);
469       return;
470     }
471   }
472   // group not found => create new one
473   std::set<ConstraintWrapperPtr> aNewGroup;
474   aNewGroup.insert(theConstraint1);
475   aNewGroup.insert(theConstraint2);
476   myEqualConstraints.push_back(aNewGroup);
477 }
478
479 bool SolveSpaceSolver_Storage::findSameConstraint(ConstraintWrapperPtr theConstraint)
480 {
481   if (theConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT ||
482       theConstraint->type() == CONSTRAINT_MULTI_ROTATION ||
483       theConstraint->type() == CONSTRAINT_MULTI_TRANSLATION)
484     return false;
485
486   const Slvs_Constraint& aCBase =
487       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint)->constraint();
488
489   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
490       aCIt = myConstraintMap.begin();
491   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
492   for (; aCIt != myConstraintMap.end(); ++aCIt)
493     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt) {
494       if ((*aCWIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
495           (*aCWIt)->type() == CONSTRAINT_MULTI_ROTATION ||
496           (*aCWIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
497         continue;
498       if ((*aCWIt)->type() == theConstraint->type()) {
499         const Slvs_Constraint& aCComp = getConstraint((Slvs_hConstraint)(*aCWIt)->id());
500
501         if (aCBase.ptA == aCComp.ptA && aCBase.ptB == aCComp.ptB &&
502             aCBase.entityA == aCComp.entityA && aCBase.entityB == aCComp.entityB &&
503             aCBase.entityC == aCComp.entityC && aCBase.entityD == aCComp.entityD &&
504             fabs(aCBase.valA -aCComp.valA) < tolerance) {
505           addSameConstraints(*aCWIt, theConstraint);
506           return true;
507         }
508       }
509     }
510   return false;
511 }
512
513
514 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
515     EntityWrapperPtr theBase, double theCoeff)
516 {
517   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
518
519   std::shared_ptr<GeomAPI_XY> aMidPoint;
520   if (theBase->type() == ENTITY_LINE) {
521     std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
522     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
523     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
524     for (int i = 0; i < 2; ++i, ++anIt)
525       aPoints[i] = aBuilder->point(*anIt);
526     aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
527         aPoints[1]->xy()->multiplied(theCoeff));
528   }
529   else if (theBase->type() == ENTITY_ARC) {
530     double theX, theY;
531     double anArcPoint[3][2];
532     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
533     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
534     for (int i = 0; i < 3; ++i, ++anIt) {
535       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
536       anArcPoint[i][0] = aPoint->x();
537       anArcPoint[i][1] = aPoint->y();
538     }
539     // project last point of arc on the arc
540     double x = anArcPoint[1][0] - anArcPoint[0][0];
541     double y = anArcPoint[1][1] - anArcPoint[0][1];
542     double aRad = sqrt(x*x + y*y);
543     x = anArcPoint[2][0] - anArcPoint[0][0];
544     y = anArcPoint[2][1] - anArcPoint[0][1];
545     double aNorm = sqrt(x*x + y*y);
546     if (aNorm >= tolerance) {
547       anArcPoint[2][0] = x * aRad / aNorm;
548       anArcPoint[2][1] = y * aRad / aNorm;
549     }
550     anArcPoint[1][0] -= anArcPoint[0][0];
551     anArcPoint[1][1] -= anArcPoint[0][1];
552     if (theCoeff < tolerance) {
553       theX = anArcPoint[0][0] + anArcPoint[1][0];
554       theY = anArcPoint[0][1] + anArcPoint[1][1];
555     } else if (1 - theCoeff < tolerance) {
556       theX = anArcPoint[0][0] + anArcPoint[2][0];
557       theY = anArcPoint[0][1] + anArcPoint[2][1];
558     } else {
559       std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
560       std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
561       double anAngle = aStartDir->angle(aEndDir);
562       if (anAngle < 0)
563         anAngle += 2.0 * PI;
564       anAngle *= theCoeff;
565       double aCos = cos(anAngle);
566       double aSin = sin(anAngle);
567       theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
568       theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
569     }
570     aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
571   }
572
573   if (!aMidPoint)
574     return EntityWrapperPtr();
575
576   std::list<ParameterWrapperPtr> aParameters;
577   Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
578   aParam1.h = addParameter(aParam1);
579   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
580   Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
581   aParam2.h = addParameter(aParam2);
582   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
583   // Create entity (parameters are not filled)
584   Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
585       (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
586   anEntity.h = addEntity(anEntity);
587
588   EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
589   aResult->setParameters(aParameters);
590   return aResult;
591 }
592
593
594
595
596
597
598 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
599 {
600   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
601     // parameter is already used, rewrite it
602     return updateParameter(theParam);
603   }
604
605   Slvs_Param aParam = theParam;
606   if (aParam.h > myParamMaxID)
607     myParamMaxID = aParam.h;
608   else
609     aParam.h = ++myParamMaxID;
610   myParameters.push_back(aParam);
611   myNeedToResolve = true;
612   return aParam.h;
613 }
614
615 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
616 {
617   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
618     // parameter already used, rewrite it
619     int aPos = Search(theParam.h, myParameters);
620     if (aPos >= 0 && aPos < (int)myParameters.size()) {
621       if (IsNotEqual(myParameters[aPos], theParam))
622         setNeedToResolve(true);
623       myParameters[aPos] = theParam;
624       return theParam.h;
625     }
626   }
627
628   // Parameter is not found, add new one
629   Slvs_Param aParam = theParam;
630   aParam.h = 0;
631   return addParameter(aParam);
632 }
633
634 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
635 {
636   int aPos = Search(theParamID, myParameters);
637   if (aPos >= 0 && aPos < (int)myParameters.size()) {
638     // Firstly, search the parameter is not used elsewhere
639     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
640     for (; anEntIter != myEntities.end(); anEntIter++) {
641       for (int i = 0; i < 4; i++)
642         if (anEntIter->param[i] == theParamID)
643           return false;
644     }
645     // Remove parameter
646     myParameters.erase(myParameters.begin() + aPos);
647     myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
648     myNeedToResolve = true;
649   }
650   return true;
651 }
652
653 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
654 {
655   int aPos = Search(theParamID, myParameters);
656   if (aPos >= 0 && aPos < (int)myParameters.size())
657     return myParameters[aPos];
658
659   // Parameter is not found, return empty object
660   static Slvs_Param aDummy;
661   aDummy.h = 0;
662   return aDummy;
663 }
664
665
666 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
667 {
668   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
669     // Entity is already used, rewrite it
670     return updateEntity(theEntity);
671   }
672
673   Slvs_Entity aEntity = theEntity;
674   if (aEntity.h > myEntityMaxID)
675     myEntityMaxID = aEntity.h;
676   else
677     aEntity.h = ++myEntityMaxID;
678   myEntities.push_back(aEntity);
679   myNeedToResolve = true;
680   return aEntity.h;
681 }
682
683 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
684 {
685   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
686     // Entity already used, rewrite it
687     int aPos = Search(theEntity.h, myEntities);
688     if (aPos >= 0 && aPos < (int)myEntities.size()) {
689       myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
690       myEntities[aPos] = theEntity;
691       return theEntity.h;
692     }
693   }
694
695   // Entity is not found, add new one
696   Slvs_Entity aEntity = theEntity;
697   aEntity.h = 0;
698   return addEntity(aEntity);
699 }
700
701 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
702 {
703   bool aResult = true;
704   int aPos = Search(theEntityID, myEntities);
705   if (aPos >= 0 && aPos < (int)myEntities.size()) {
706     // Firstly, check the entity and its attributes is not used elsewhere
707     std::set<Slvs_hEntity> anEntAndSubs;
708     anEntAndSubs.insert(theEntityID);
709     for (int i = 0; i < 4; i++)
710       if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
711         anEntAndSubs.insert(myEntities[aPos].point[i]);
712
713     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
714     for (; anEntIter != myEntities.end(); anEntIter++) {
715       if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
716         continue;
717       for (int i = 0; i < 4; i++)
718         if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
719           return false;
720       if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
721         return false;
722     }
723     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
724     for (; aConstrIter != myConstraints.end(); aConstrIter++) {
725       Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
726           aConstrIter->entityA, aConstrIter->entityB,
727           aConstrIter->entityC, aConstrIter->entityD};
728       for (int i = 0; i < 6; i++)
729         if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
730           return false;
731     }
732     // The entity is not used, remove it and its parameters
733     Slvs_Entity anEntity = myEntities[aPos];
734     myEntities.erase(myEntities.begin() + aPos);
735     myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
736     if (anEntity.distance != SLVS_E_UNKNOWN)
737       aResult = aResult && removeEntity(anEntity.distance);
738     for (int i = 0; i < 4; i++)
739       if (anEntity.param[i] != SLVS_E_UNKNOWN)
740         aResult = removeParameter(anEntity.param[i]) && aResult;
741     myNeedToResolve = true;
742   }
743   return aResult;
744 }
745
746 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
747 {
748   int aPos = Search(theEntityID, myEntities);
749   if (aPos >= 0 && aPos < (int)myEntities.size())
750     return myEntities[aPos];
751
752   // Entity is not found, return empty object
753   static Slvs_Entity aDummy;
754   aDummy.h = SLVS_E_UNKNOWN;
755   return aDummy;
756 }
757
758
759 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
760 {
761   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
762     // Constraint is already used, rewrite it
763     return updateConstraint(theConstraint);
764   }
765
766   Slvs_Constraint aConstraint = theConstraint;
767
768   // Find a constraint with same type uses same arguments to show user overconstraint situation
769   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
770   for (; aCIt != myConstraints.end(); aCIt++) {
771     if (aConstraint.type != aCIt->type)
772       continue;
773     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
774         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
775         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
776       myDuplicatedConstraint = true;
777       break;
778     }
779   }
780
781   if (aConstraint.h > myConstrMaxID)
782     myConstrMaxID = aConstraint.h;
783   else
784     aConstraint.h = ++myConstrMaxID;
785   myConstraints.push_back(aConstraint);
786   myNeedToResolve = true;
787   return aConstraint.h;
788 }
789
790 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
791 {
792   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
793     // Constraint already used, rewrite it
794     int aPos = Search(theConstraint.h, myConstraints);
795     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
796       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
797       myConstraints[aPos] = theConstraint;
798       return theConstraint.h;
799     }
800   }
801
802   // Constraint is not found, add new one
803   Slvs_Constraint aConstraint = theConstraint;
804   aConstraint.h = 0;
805
806   // Firstly, check middle-point constraint conflicts with point-on-line
807   if (aConstraint.type == SLVS_C_AT_MIDPOINT) {
808     std::vector<Slvs_Constraint>::const_iterator anIt = myConstraints.begin();
809     for (; anIt != myConstraints.end(); ++anIt)
810       if (anIt->type == SLVS_C_PT_ON_LINE &&
811           anIt->ptA == aConstraint.ptA &&
812           anIt->entityA == aConstraint.entityA)
813         break;
814     if (anIt != myConstraints.end()) {
815       // change the constraint to the lengths equality to avoid conflicts
816       Slvs_Entity aLine = getEntity(aConstraint.entityA);
817       Slvs_Entity aNewLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
818           myWorkplaneID, aLine.point[0], aConstraint.ptA);
819       aNewLine1.h = addEntity(aNewLine1);
820       Slvs_Entity aNewLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
821           myWorkplaneID, aLine.point[1], aConstraint.ptA);
822       aNewLine2.h = addEntity(aNewLine2);
823       aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, SLVS_C_EQUAL_LENGTH_LINES,
824           myWorkplaneID, 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aNewLine1.h, aNewLine2.h);
825     }
826   }
827
828   return addConstraint(aConstraint);
829 }
830
831 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
832 {
833   int aPos = Search(theConstraintID, myConstraints);
834   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
835     Slvs_Constraint aConstraint = myConstraints[aPos];
836     myConstraints.erase(myConstraints.begin() + aPos);
837     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
838     myNeedToResolve = true;
839
840     if (myDuplicatedConstraint) {
841       // Find a constraint with same type uses same arguments
842       std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
843       for (; aCIt != myConstraints.end(); aCIt++) {
844         if (aConstraint.type != aCIt->type)
845           continue;
846         if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
847             aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
848             aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
849           myDuplicatedConstraint = false;
850           break;
851         }
852       }
853     }
854   }
855   return true;
856 }
857
858 const Slvs_Constraint& SolveSpaceSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
859 {
860   int aPos = Search(theConstraintID, myConstraints);
861   if (aPos >= 0 && aPos < (int)myConstraints.size())
862     return myConstraints[aPos];
863
864   // Constraint is not found, return empty object
865   static Slvs_Constraint aDummy;
866   aDummy.h = 0;
867   return aDummy;
868 }
869
870
871 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
872 {
873   std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
874       std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
875   if (!aSolver)
876     return;
877
878   if (myExistArc)
879     processArcs();
880
881   if (myConstraints.empty()) {
882     // Adjust all arc to place their points correctly
883     std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
884     for (; anEntIt != myEntities.end(); ++anEntIt)
885       if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
886         adjustArc(*anEntIt);
887   }
888
889   aSolver->setParameters(myParameters.data(), (int)myParameters.size());
890   aSolver->setEntities(myEntities.data(), (int)myEntities.size());
891   aSolver->setConstraints(myConstraints.data(), (int)myConstraints.size());
892 }
893
894
895 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
896 {
897   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
898       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
899
900   // verify whether the constraint has duplicated
901   bool hasSameID = false;
902   SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
903   for (; anEqIt != myEqualConstraints.end(); ++anEqIt) {
904     std::set<ConstraintWrapperPtr>::const_iterator aFound = anEqIt->find(aConstraint);
905     if (aFound != anEqIt->end()) {
906       // verify there is a constraint with same ID
907       std::set<ConstraintWrapperPtr>::const_iterator anIt = anEqIt->begin();
908       ConstraintID anID = (*aFound)->id();
909       for (++anIt; anIt != anEqIt->end() && !hasSameID; ++anIt)
910         if ((*anIt)->id() == anID && aFound != anIt)
911           hasSameID = true;
912       // erase constraint
913       anEqIt->erase(aConstraint);
914       break;
915     }
916   }
917   if (anEqIt != myEqualConstraints.end() && hasSameID)
918     return true;
919
920   bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
921   // remove point-point coincidence
922   if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
923     isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
924   return SketchSolver_Storage::remove(theConstraint) && isFullyRemoved;
925 }
926
927 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
928 {
929   if (!theEntity)
930     return false;
931
932   // Additional check for entity to be used in point-point coincidence
933   bool isCoincide = false;
934   if (theEntity->type() == ENTITY_POINT) {
935     CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
936     std::set<EntityWrapperPtr>::const_iterator aCIt;
937     for (; anIt != myCoincidentPoints.end(); ++anIt) {
938       if (anIt->first == theEntity)
939         break;
940       for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt)
941         if (*aCIt == theEntity)
942           break;
943       if (aCIt != anIt->second.end())
944         break;
945     }
946     if (anIt != myCoincidentPoints.end()) {
947       if (anIt->first != theEntity && isUsed(anIt->first->baseAttribute()))
948         isCoincide = true;
949       for (aCIt = anIt->second.begin(); !isCoincide && aCIt != anIt->second.end(); ++aCIt)
950         if (*aCIt != theEntity && isUsed((*aCIt)->baseAttribute()))
951           isCoincide = true;
952     }
953   }
954
955   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
956         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
957   bool isFullyRemoved = isCoincide ? true : removeEntity((Slvs_hEntity)anEntity->id());
958   return (SketchSolver_Storage::remove(theEntity) || isCoincide) && isFullyRemoved;
959 }
960
961 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
962 {
963   return removeParameter((Slvs_hParam)theParameter->id());
964 }
965
966
967 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
968 {
969   //blockEvents(true);
970
971   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
972   std::list<ParameterWrapperPtr> aParams;
973   std::list<ParameterWrapperPtr>::const_iterator aParIt;
974   for (; anIt != myAttributeMap.end(); ++anIt) {
975     if (!anIt->second)
976       continue;
977     // the external feature always should keep the up to date values, so, 
978     // refresh from the solver is never needed
979     if (anIt->first.get()) {
980       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
981         std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
982       if (aSketchFeature.get() && aSketchFeature->isExternal())
983         continue;
984       // not need to refresh here sketch's origin and normal vector
985       CompositeFeaturePtr aSketch =
986           std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anIt->first->owner());
987       if (aSketch)
988         continue;
989     }
990
991     // update parameter wrappers and obtain values of attributes
992     aParams = anIt->second->parameters();
993     double aCoords[3];
994     bool isUpd[3] = {false};
995     int i = 0;
996     for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
997       std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper = 
998           std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
999       if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
1000         aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
1001         aCoords[i] = aWrapper->value();
1002         isUpd[i] = true;
1003       }
1004     }
1005     if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1006       continue; // nothing is updated
1007
1008     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1009         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1010     if (aPoint2D) {
1011       if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1012           (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1013         if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1014         if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1015         aPoint2D->setValue(aCoords[0], aCoords[1]);
1016         // Find points coincident with this one (probably not in GID_OUTOFGROUP)
1017         std::map<AttributePtr, EntityWrapperPtr>::const_iterator aLocIt;
1018         if (theFixedOnly) 
1019           aLocIt = myAttributeMap.begin();
1020         else {
1021           aLocIt = anIt;
1022           ++aLocIt;
1023         }
1024         for (; aLocIt != myAttributeMap.end(); ++aLocIt) {
1025           if (!aLocIt->second)
1026             continue;
1027           std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
1028             std::dynamic_pointer_cast<SketchPlugin_Feature>(aLocIt->first->owner());
1029           if (aSketchFeature && aSketchFeature->isExternal())
1030             continue;
1031           if (anIt->second->id() == aLocIt->second->id()) {
1032             aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLocIt->first);
1033             aPoint2D->setValue(aCoords[0], aCoords[1]);
1034           }
1035         }
1036       }
1037       continue;
1038     }
1039     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1040     if (aScalar) {
1041       if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1042         aScalar->setValue(aCoords[0]);
1043       continue;
1044     }
1045     std::shared_ptr<GeomDataAPI_Point> aPoint =
1046         std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1047     if (aPoint) {
1048       if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1049           (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1050           (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1051         if (!isUpd[0]) aCoords[0] = aPoint->x();
1052         if (!isUpd[1]) aCoords[1] = aPoint->y();
1053         if (!isUpd[2]) aCoords[2] = aPoint->z();
1054         aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1055       continue;
1056     }
1057   }
1058
1059   //blockEvents(false);
1060 }
1061
1062 void SolveSpaceSolver_Storage::verifyFixed()
1063 {
1064   std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1065   for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1066     if (!anAttrIt->second)
1067       continue;
1068     if (anAttrIt->second->group() == GID_OUTOFGROUP) {
1069       Slvs_Entity anEnt = getEntity((Slvs_hEntity)anAttrIt->second->id());
1070       if (anEnt.group != (Slvs_hEntity)GID_OUTOFGROUP)
1071         anEnt.group = (Slvs_hEntity)GID_OUTOFGROUP;
1072       updateEntity(anEnt);
1073     }
1074
1075     const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1076     std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1077     for (; aParIt != aParameters.end(); ++aParIt)
1078       if (anAttrIt->second->group() == GID_OUTOFGROUP || (*aParIt)->group() == GID_OUTOFGROUP) {
1079         Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1080         if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1081           aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1082           updateParameter(aParam);
1083         }
1084       }
1085   }
1086 }
1087
1088
1089 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1090 {
1091   double anArcPoints[3][2];
1092   double aDist[3] = {0.0};
1093   bool isFixed[3] = {false};
1094   for (int i = 0; i < 3; ++i) {
1095     Slvs_Entity aPoint = getEntity(theArc.point[i]);
1096     isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1097     anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1098     anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1099     if (i > 0) {
1100       anArcPoints[i][0] -= anArcPoints[0][0];
1101       anArcPoints[i][1] -= anArcPoints[0][1];
1102       aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] + 
1103                       anArcPoints[i][1] * anArcPoints[i][1]);
1104     }
1105   }
1106
1107   if (fabs(aDist[1] - aDist[2]) < tolerance)
1108     return;
1109
1110   int anInd = 2;
1111   while (anInd > 0 && isFixed[anInd])
1112     --anInd;
1113   if (anInd < 1)
1114     return; // adjust only start or end point of the arc
1115
1116   anArcPoints[anInd][0] /= aDist[anInd];
1117   anArcPoints[anInd][1] /= aDist[anInd];
1118
1119   Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1120   for (int i = 0; i < 2; ++i) {
1121     Slvs_Param aParam = getParameter(aPoint.param[i]);
1122     aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1123     updateParameter(aParam);
1124   }
1125 }
1126
1127
1128
1129
1130
1131
1132
1133 // ========================================================
1134 // =========      Auxiliary functions       ===============
1135 // ========================================================
1136
1137 template<typename T>
1138 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1139 {
1140   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1141   int aVecSize = (int)theEntities.size();
1142   if (theEntities.empty())
1143     return 1;
1144   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1145     aResIndex--;
1146   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1147     aResIndex++;
1148   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1149     aResIndex = aVecSize;
1150   return aResIndex;
1151 }
1152
1153 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1154 {
1155   return fabs(theParam1.val - theParam2.val) > tolerance;
1156 }
1157
1158 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1159 {
1160   int i = 0;
1161   for (; theEntity1.param[i] != 0 && i < 4; i++)
1162     if (theEntity1.param[i] != theEntity2.param[i])
1163       return true;
1164   i = 0;
1165   for (; theEntity1.point[i] != 0 && i < 4; i++)
1166     if (theEntity1.point[i] != theEntity2.point[i])
1167       return true;
1168   return false;
1169 }
1170
1171 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1172 {
1173   return theConstraint1.ptA != theConstraint2.ptA ||
1174          theConstraint1.ptB != theConstraint2.ptB ||
1175          theConstraint1.entityA != theConstraint2.entityA ||
1176          theConstraint1.entityB != theConstraint2.entityB ||
1177          theConstraint1.entityC != theConstraint2.entityC ||
1178          theConstraint1.entityD != theConstraint2.entityD ||
1179          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
1180 }