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