]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Storage.cpp
Salome HOME
3e9b00b7588fbdb3fee5610dc6a964e1ba0fa721
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Storage.cpp
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_Storage.h>
8 #include <PlaneGCSSolver_Builder.h>
9 #include <PlaneGCSSolver_Solver.h>
10 #include <PlaneGCSSolver_ConstraintWrapper.h>
11 #include <PlaneGCSSolver_EntityWrapper.h>
12 #include <PlaneGCSSolver_PointWrapper.h>
13 #include <PlaneGCSSolver_ScalarWrapper.h>
14 #include <PlaneGCSSolver_ParameterWrapper.h>
15
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <SketchPlugin_Arc.h>
22
23 #include <cmath>
24
25
26 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const GroupID& theGroup)
27   : SketchSolver_Storage(theGroup),
28     myEntityLastID(EID_SKETCH),
29     myConstraintLastID(CID_UNKNOWN)
30 {
31 }
32
33 void PlaneGCSSolver_Storage::addConstraint(
34     ConstraintPtr                   theConstraint,
35     std::list<ConstraintWrapperPtr> theSolverConstraints)
36 {
37   SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraints);
38
39   // update point-point coincidence
40   if (!theSolverConstraints.empty() &&
41       theSolverConstraints.front()->type() == CONSTRAINT_PT_PT_COINCIDENT) {
42     std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
43     for (; aCIt != theSolverConstraints.end(); ++aCIt)
44       update(*aCIt);
45   }
46 }
47
48
49 bool PlaneGCSSolver_Storage::update(ConstraintWrapperPtr theConstraint)
50 {
51   bool isUpdated = false;
52   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
53       std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
54
55   // point-Line distance should be positive
56   if (aConstraint->type() == CONSTRAINT_PT_LINE_DISTANCE && aConstraint->value() < 0.0)
57     aConstraint->setValue(-aConstraint->value());
58
59   // make value of constraint unchangeable
60   ParameterWrapperPtr aValue = aConstraint->valueParameter();
61   if (aValue)
62     isUpdated = update(aValue) || isUpdated;
63
64   // update constrained entities
65   std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
66   std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
67   for (; anIt != anEntities.end(); ++anIt)
68     isUpdated = update(*anIt) || isUpdated;
69
70   if (aConstraint->id() == CID_UNKNOWN) {
71     const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
72     // check middle-point constraint conflicts with point-on-line
73     if (aConstraint->type() == CONSTRAINT_MIDDLE_POINT) {
74       std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
75           anIt = myConstraintMap.begin();
76       for (; anIt != myConstraintMap.end(); ++anIt) {
77         EntityWrapperPtr aPoint, aLine;
78         ConstraintWrapperPtr aCurrentConstr = anIt->second.front();
79         if (aCurrentConstr->type() != CONSTRAINT_PT_ON_LINE)
80           continue;
81         const std::list<EntityWrapperPtr>& aCurSubs = aCurrentConstr->entities();
82         std::list<EntityWrapperPtr>::const_iterator aSIt1, aSIt2;
83         for (aSIt1 = aSubs.begin(); aSIt1 != aSubs.end(); ++aSIt1) {
84           if ((*aSIt1)->type() == ENTITY_POINT)
85             aPoint = *aSIt1;
86           else if((*aSIt1)->type() == ENTITY_LINE)
87             aLine = *aSIt1;
88           else
89             continue;
90           for (aSIt2 = aCurSubs.begin(); aSIt2 != aCurSubs.end(); ++aSIt2)
91             if ((*aSIt1)->id() == (*aSIt2)->id())
92               break;
93           if (aSIt2 == aCurSubs.end())
94             break;
95         }
96         // point-on-line found, change it to bisector
97         if (aSIt1 == aSubs.end()) {
98           std::list<GCSConstraintPtr> aConstrList = aConstraint->constraints();
99           aConstrList.pop_front();
100           aConstraint->setConstraints(aConstrList);
101           break;
102         }
103       }
104     }
105
106     // Change ID of constraints
107     aConstraint->setId(++myConstraintLastID);
108   }
109
110   return isUpdated;
111 }
112
113 /// \brief Update coordinates of the point or scalar using its base attribute
114 static bool updateValues(EntityWrapperPtr& theEntity)
115 {
116   bool isUpdated = false;
117   AttributePtr anAttr = theEntity->baseAttribute();
118   const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
119
120   double aCoord[2];
121
122   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
123       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
124   if (aPoint2D) {
125     aCoord[0] = aPoint2D->x();
126     aCoord[1] = aPoint2D->y();
127   } else {
128     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
129     if (aScalar)
130       aCoord[0] = aScalar->value();
131   }
132
133   std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
134   for (int i = 0; anIt != aParams.end(); ++anIt, ++i)
135     if (fabs((*anIt)->value() - aCoord[i]) > tolerance) {
136       (*anIt)->setValue(aCoord[i]);
137       isUpdated = true;
138     }
139   return isUpdated;
140 }
141
142 bool PlaneGCSSolver_Storage::update(EntityWrapperPtr theEntity)
143 {
144   if (theEntity->type() == ENTITY_SKETCH)
145     return true; // sketch is not necessary for PlaneGCS, so it is always says true
146
147   bool isUpdated = false;
148
149   if (theEntity->baseAttribute()) {
150     isUpdated = updateValues(theEntity);
151     if (isUpdated)
152       setNeedToResolve(true);
153   }
154
155   // update parameters
156   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
157   std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
158   for (; aPIt != aParams.end(); ++aPIt)
159     isUpdated = update(*aPIt) || isUpdated;
160
161   // update sub-entities
162   std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
163   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
164   for (; aSIt != aSubEntities.end(); ++aSIt)
165     isUpdated = update(*aSIt) || isUpdated;
166
167   // additional constraints for the arc processing
168   if (theEntity->type() == ENTITY_ARC)
169     processArc(theEntity);
170
171   // Change entity's ID, if necessary
172   if (theEntity->id() == EID_UNKNOWN) {
173     if (theEntity->type() == ENTITY_POINT) {
174       std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
175           std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
176       if (!aPoint) {
177         aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
178             theEntity->subEntities().front());
179       }
180       aPoint->setId(++myEntityLastID);
181     } else if (theEntity->type() == ENTITY_SCALAR) {
182       std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aScalar =
183           std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
184       aScalar->setId(++myEntityLastID);
185     } else {
186       std::shared_ptr<PlaneGCSSolver_EntityWrapper> aGCSEnt =
187           std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity);
188       aGCSEnt->setId(++myEntityLastID);
189     }
190   }
191   return isUpdated;
192 }
193
194 bool PlaneGCSSolver_Storage::update(ParameterWrapperPtr theParameter)
195 {
196   std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
197       std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
198   if (aParam->isProcessed())
199     return false;
200   if (theParameter->group() != myGroupID || theParameter->isParametric())
201     myConst.push_back(aParam->parameter());
202   else
203     myParameters.push_back(aParam->parameter());
204   aParam->setProcessed(true);
205   return true;
206 }
207
208
209 bool PlaneGCSSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
210 {
211   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
212     std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
213
214   bool isFullyRemoved = true;
215   const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
216   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
217   for (; aSIt != aSubs.end(); ++ aSIt)
218     isFullyRemoved = remove(*aSIt) && isFullyRemoved;
219
220   if (aConstraint->valueParameter())
221     isFullyRemoved = remove(aConstraint->valueParameter()) && isFullyRemoved;
222   if (!isFullyRemoved && aConstraint->baseConstraint() &&
223      (!aConstraint->baseConstraint()->data() || !aConstraint->baseConstraint()->data()->isValid()))
224     isFullyRemoved = true;
225   setNeedToResolve(true);
226   myRemovedConstraints.insert(myRemovedConstraints.end(),
227       aConstraint->constraints().begin(), aConstraint->constraints().end());
228
229   if (isFullyRemoved && theConstraint->id() == myConstraintLastID)
230     --myConstraintLastID;
231
232   return isFullyRemoved;
233 }
234
235 bool PlaneGCSSolver_Storage::remove(EntityWrapperPtr theEntity)
236 {
237   if ((theEntity->baseAttribute() && isUsed(theEntity->baseAttribute())) ||
238       (theEntity->baseFeature() && isUsed(theEntity->baseFeature())))
239     return false;
240
241   bool isFullyRemoved = SketchSolver_Storage::remove(theEntity);
242   if (isFullyRemoved && theEntity->id() == myEntityLastID)
243     --myEntityLastID;
244   return isFullyRemoved;
245 }
246
247 bool PlaneGCSSolver_Storage::remove(ParameterWrapperPtr theParameter)
248 {
249   std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
250       std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
251   if (aParam->isProcessed()) {
252     double* aValPtr = aParam->parameter();
253     GCS::VEC_pD::iterator anIt =  myParameters.begin();
254     for (; anIt != myParameters.end(); ++anIt)
255       if (*anIt == aValPtr)
256         break;
257     if (anIt != myParameters.end()) {
258       myParameters.erase(anIt);
259       setNeedToResolve(true);
260     }
261     else {
262       for (anIt = myConst.begin(); anIt != myConst.end(); ++anIt)
263         if (*anIt == aValPtr)
264           break;
265       if (anIt != myConst.end()) {
266         myConst.erase(anIt);
267         setNeedToResolve(true);
268       }
269     }
270   }
271   aParam->setProcessed(false);
272   return true;
273 }
274
275
276 void PlaneGCSSolver_Storage::addCoincidentPoints(
277     EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
278 {
279   if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
280     return;
281
282   std::shared_ptr<PlaneGCSSolver_PointWrapper> aMaster =
283       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMaster);
284   if (!aMaster)
285     aMaster = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
286       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMaster)->subEntities().front());
287   std::shared_ptr<PlaneGCSSolver_PointWrapper> aSlave =
288       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theSlave);
289   if (!aSlave)
290     aSlave = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
291       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theSlave)->subEntities().front());
292
293   // Search available coincidence
294   CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(aMaster);
295   CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(aSlave);
296   if (aMasterFound == myCoincidentPoints.end() &&  aSlaveFound == myCoincidentPoints.end()) {
297     // try to find master and slave points in the lists of slaves of already existent coincidences
298     CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
299     for (; anIt != myCoincidentPoints.end(); ++anIt) {
300       if (anIt->second.find(aMaster) != anIt->second.end())
301         aMasterFound = anIt;
302       else if (anIt->second.find(aSlave) != anIt->second.end())
303         aSlaveFound = anIt;
304
305       if (aMasterFound != myCoincidentPoints.end() &&  aSlaveFound != myCoincidentPoints.end())
306         break;
307     }
308   }
309
310   if (aMasterFound == myCoincidentPoints.end()) {
311     // create new group
312     myCoincidentPoints[aMaster] = std::set<EntityWrapperPtr>();
313     aMasterFound = myCoincidentPoints.find(aMaster);
314   } else if (aMasterFound == aSlaveFound)
315     return; // already coincident
316
317   if (aSlaveFound != myCoincidentPoints.end()) {
318     // A slave has been found, we need to attach all points coincident with it to the new master
319     std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
320     aNewSlaves.insert(aSlaveFound->first);
321     myCoincidentPoints.erase(aSlaveFound);
322
323     std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
324     for (; aSlIt != aNewSlaves.end(); ++aSlIt)
325       addCoincidentPoints(aMaster, *aSlIt);
326   } else {
327     //std::list<ParameterWrapperPtr> aSlaveParams = aSlave->parameters();
328     //aSlave->setParameters(aMaster->parameters());
329
330     //// Remove slave's parameters
331     //std::list<ParameterWrapperPtr>::iterator aParIt = aSlaveParams.begin();
332     //for (; aParIt != aSlaveParams.end(); ++aParIt)
333     //  remove(*aParIt);
334
335     aMasterFound->second.insert(aSlave);
336   }
337 }
338
339
340 void PlaneGCSSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
341 {
342   theEntity->setGroup(theGroup);
343   if (theGroup == myGroupID)
344     makeVariable(theEntity);
345   else {
346     if (theEntity->type() == ENTITY_POINT)
347       update(theEntity);
348     makeConstant(theEntity);
349   }
350 }
351
352 void PlaneGCSSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
353 {
354   // TODO
355 }
356
357 void PlaneGCSSolver_Storage::verifyFixed()
358 {
359   // TODO
360 }
361
362 void PlaneGCSSolver_Storage::processArc(const EntityWrapperPtr& theArc)
363 {
364   // Calculate additional parameters necessary for PlaneGCS
365   const std::list<EntityWrapperPtr>& aSubs = theArc->subEntities();
366   std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
367   while ((*aSubIt)->type() == ENTITY_POINT) // search scalar entities
368     ++aSubIt;
369   double* aStartAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
370   double* aEndAngle   = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
371   double* aRadius     = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt)->scalar();
372
373   FeaturePtr anArcFeature = theArc->baseFeature();
374   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
375       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
376   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
377       anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
378   std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
379       anArcFeature->attribute(SketchPlugin_Arc::END_ID()));
380   if (!aCenterAttr || !aStartAttr || !aEndAttr)
381     return;
382   std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = aCenterAttr->pnt();
383   std::shared_ptr<GeomAPI_Pnt2d> aStartPnt  = aStartAttr->pnt();
384   std::shared_ptr<GeomAPI_Pnt2d> aEndPnt    = aEndAttr->pnt();
385
386   *aRadius = aCenterPnt->distance(aStartPnt);
387   if (!anArcFeature->lastResult())
388     return;
389   std::shared_ptr<GeomAPI_Edge> anArcEdge =
390       std::dynamic_pointer_cast<GeomAPI_Edge>(anArcFeature->lastResult()->shape());
391   if (!anArcEdge)
392     return;
393   anArcEdge->getRange(*aStartAngle, *aEndAngle);
394
395   // No need to add constraints if they are already exist
396   std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
397       aFound = myArcConstraintMap.find(theArc);
398   if (aFound != myArcConstraintMap.end())
399     return;
400
401   // Prepare additional constraints to produce the arc
402   std::vector<GCSConstraintPtr> anArcConstraints;
403   std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt =
404       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theArc);
405   std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt->entity());
406   // Distances from center till start and end points are equal to radius
407   anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
408       anArc->center, anArc->start, anArc->rad)));
409   anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
410       anArc->center, anArc->end, anArc->rad)));
411   // Angles of start and end points should be equal to given angles
412   anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
413       anArc->center, anArc->start, anArc->startAngle)));
414   anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
415       anArc->center, anArc->end, anArc->endAngle)));
416
417   myArcConstraintMap[theArc] = anArcConstraints;
418 }
419
420
421 void PlaneGCSSolver_Storage::makeConstant(const EntityWrapperPtr& theEntity)
422 {
423   toggleEntity(theEntity, myParameters, myConst);
424   if (theEntity->type() == ENTITY_POINT)
425     updateCoincident(theEntity);
426 }
427
428 void PlaneGCSSolver_Storage::makeVariable(const EntityWrapperPtr& theEntity)
429 {
430   toggleEntity(theEntity, myConst, myParameters);
431 }
432
433 static void getParametersToMove(const EntityWrapperPtr& theEntity, std::set<double*>& theParamList)
434 {
435   const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
436   std::list<ParameterWrapperPtr>::const_iterator aPIt = aParams.begin();
437   for (; aPIt != aParams.end(); ++aPIt)
438     theParamList.insert(
439         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*aPIt)->parameter());
440
441   const std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
442   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
443
444   if (theEntity->type() == ENTITY_ARC) {
445     // workaround for the arc processing, because the arc is fixed by a set of constraints,
446     // which will conflict with all parameters fixed:
447     // 1. take center
448     getParametersToMove(*aSIt++, theParamList);
449     // 2. take start point
450     getParametersToMove(*aSIt++, theParamList);
451     // 3. skip end point, radius and start angle, but take end angle parameter
452     getParametersToMove(*(++aSIt), theParamList);
453   } else {
454     for (; aSIt != aSubs.end(); ++aSIt)
455       getParametersToMove(*aSIt, theParamList);
456   }
457 }
458
459 void PlaneGCSSolver_Storage::toggleEntity(
460     const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo)
461 {
462   std::set<double*> aParamsToMove;
463   getParametersToMove(theEntity, aParamsToMove);
464
465   GCS::VEC_pD::iterator anIt = theFrom.begin();
466   while (anIt != theFrom.end()) {
467     if (aParamsToMove.find(*anIt) == aParamsToMove.end()) {
468       ++anIt;
469       continue;
470     }
471
472     theTo.push_back(*anIt);
473     int aShift = anIt - theFrom.begin();
474     theFrom.erase(anIt);
475     anIt = theFrom.begin() + aShift;
476   }
477 }
478
479 void PlaneGCSSolver_Storage::updateCoincident(const EntityWrapperPtr& thePoint)
480 {
481   CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
482   for (; anIt != myCoincidentPoints.end(); ++anIt) {
483     if (anIt->first == thePoint || anIt->second.find(thePoint) != anIt->second.end()) {
484       std::set<EntityWrapperPtr> aCoincident = anIt->second;
485       aCoincident.insert(anIt->first);
486
487       const std::list<ParameterWrapperPtr>& aBaseParams = thePoint->parameters();
488       std::list<ParameterWrapperPtr> aParams;
489       std::list<ParameterWrapperPtr>::const_iterator aBaseIt, anUpdIt;
490
491       std::set<EntityWrapperPtr>::const_iterator aCoincIt = aCoincident.begin();
492       for (; aCoincIt != aCoincident.end(); ++aCoincIt)
493         if (*aCoincIt != thePoint && (*aCoincIt)->group() != GID_OUTOFGROUP) {
494           aParams = (*aCoincIt)->parameters();
495           aBaseIt = aBaseParams.begin();
496           for (anUpdIt = aParams.begin(); anUpdIt != aParams.end(); ++anUpdIt, ++aBaseIt)
497             (*anUpdIt)->setValue((*aBaseIt)->value());
498         }
499
500       break;
501     }
502   }
503 }
504
505
506 bool PlaneGCSSolver_Storage::isRedundant(
507     GCSConstraintPtr theCheckedConstraint,
508     ConstraintWrapperPtr theParentConstraint) const
509 {
510   if (theParentConstraint->type() == CONSTRAINT_SYMMETRIC) {
511     if (theCheckedConstraint->getTypeId() == GCS::Perpendicular) {
512       BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
513       // check the initial point is placed on the mirror line
514       std::list<EntityWrapperPtr> aSubs = theParentConstraint->entities();
515       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(aSubs.front());
516       std::shared_ptr<GeomAPI_Lin2d> aLine = aBuilder->line(aSubs.back());
517       return aLine->distance(aPoint) < tolerance;
518     }
519   }
520
521   return false;
522 }
523
524 void PlaneGCSSolver_Storage::initializeSolver(SolverPtr theSolver)
525 {
526   std::shared_ptr<PlaneGCSSolver_Solver> aSolver =
527       std::dynamic_pointer_cast<PlaneGCSSolver_Solver>(theSolver);
528   if (!aSolver)
529     return;
530
531   if (myExistArc)
532     processArcs();
533
534   // initialize constraints
535   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
536       aCIt = myConstraintMap.begin();
537   for (; aCIt != myConstraintMap.end(); ++aCIt) {
538     std::list<ConstraintWrapperPtr>::const_iterator aCWIt = aCIt->second.begin();
539     for (; aCWIt != aCIt->second.end(); ++ aCWIt) {
540       std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCS =
541           std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(*aCWIt);
542       std::list<GCSConstraintPtr>::const_iterator anIt = aGCS->constraints().begin();
543       for (; anIt != aGCS->constraints().end(); ++anIt)
544         if (!isRedundant(*anIt, aGCS))
545           aSolver->addConstraint(*anIt);
546     }
547   }
548   // additional constraints for arcs
549   std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
550       anArcIt = myArcConstraintMap.begin();
551   for (; anArcIt != myArcConstraintMap.end(); ++anArcIt) {
552     std::vector<GCSConstraintPtr>::const_iterator anIt = anArcIt->second.begin();
553     for (; anIt != anArcIt->second.end(); ++anIt)
554       aSolver->addConstraint(*anIt);
555   }
556   // removed waste constraints
557   std::list<GCSConstraintPtr>::const_iterator aRemIt = myRemovedConstraints.begin();
558   for (; aRemIt != myRemovedConstraints.end(); ++aRemIt)
559     aSolver->removeConstraint(*aRemIt);
560   myRemovedConstraints.clear();
561   // initialize unknowns
562   aSolver->setParameters(myParameters);
563 }
564
565 void PlaneGCSSolver_Storage::refresh(bool theFixedOnly) const
566 {
567   //blockEvents(true);
568
569   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
570   std::list<ParameterWrapperPtr> aParams;
571   std::list<ParameterWrapperPtr>::const_iterator aParIt;
572   for (; anIt != myAttributeMap.end(); ++anIt) {
573     // the external feature always should keep the up to date values, so, 
574     // refresh from the solver is never needed
575     bool isExternal = false;
576     if (anIt->first.get()) {
577       std::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
578         std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
579       if (aSketchFeature.get() && aSketchFeature->isExternal())
580         isExternal = true;
581     }
582
583     // update parameter wrappers and obtain values of attributes
584     aParams = anIt->second->parameters();
585     double aCoords[3];
586     bool isUpd[3] = {false};
587     int i = 0;
588     for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
589       if (!theFixedOnly || isExternal ||
590           (*aParIt)->group() == GID_OUTOFGROUP || (*aParIt)->isParametric()) {
591         aCoords[i] = (*aParIt)->value();
592         isUpd[i] = true;
593       }
594     }
595     if (!isUpd[0] && !isUpd[1] && !isUpd[2])
596       continue; // nothing is updated
597
598     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
599         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
600     if (aPoint2D) {
601       if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
602           (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance) || isExternal) {
603         if (!isUpd[0] || isExternal) aCoords[0] = aPoint2D->x();
604         if (!isUpd[1] || isExternal) aCoords[1] = aPoint2D->y();
605         aPoint2D->setValue(aCoords[0], aCoords[1]);
606         // Find points coincident with this one (probably not in GID_OUTOFGROUP)
607         CoincidentPointsMap::const_iterator aCoincIt = myCoincidentPoints.begin();
608         for (; aCoincIt != myCoincidentPoints.end(); ++aCoincIt)
609           if (aCoincIt->first == anIt->second ||
610               aCoincIt->second.find(anIt->second) != aCoincIt->second.end())
611             break;
612         if (aCoincIt != myCoincidentPoints.end()) {
613           aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
614               aCoincIt->first->baseAttribute());
615           if (aPoint2D)
616             aPoint2D->setValue(aCoords[0], aCoords[1]);
617           std::set<EntityWrapperPtr>::const_iterator aSlaveIt = aCoincIt->second.begin();
618           for (; aSlaveIt != aCoincIt->second.end(); ++aSlaveIt) {
619             aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSlaveIt)->baseAttribute());
620             if (aPoint2D)
621               aPoint2D->setValue(aCoords[0], aCoords[1]);
622           }
623         }
624       }
625       continue;
626     }
627     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
628     if (aScalar && !isExternal) {
629       if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
630         aScalar->setValue(aCoords[0]);
631       continue;
632     }
633   }
634
635   //blockEvents(false);
636 }
637
638 EntityWrapperPtr PlaneGCSSolver_Storage::calculateMiddlePoint(
639     EntityWrapperPtr theBase, double theCoeff)
640 {
641   std::shared_ptr<PlaneGCSSolver_Builder> aBuilder =
642       std::dynamic_pointer_cast<PlaneGCSSolver_Builder>(PlaneGCSSolver_Builder::getInstance());
643
644   std::shared_ptr<GeomAPI_XY> aMidPoint;
645   if (theBase->type() == ENTITY_LINE) {
646     std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
647     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
648     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
649     for (int i = 0; i < 2; ++i, ++anIt)
650       aPoints[i] = aBuilder->point(*anIt);
651     aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
652         aPoints[1]->xy()->multiplied(theCoeff));
653   }
654   else if (theBase->type() == ENTITY_ARC) {
655     double theX, theY;
656     double anArcPoint[3][2];
657     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
658     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
659     for (int i = 0; i < 3; ++i, ++anIt) {
660       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
661       anArcPoint[i][0] = aPoint->x();
662       anArcPoint[i][1] = aPoint->y();
663     }
664     // project last point of arc on the arc
665     double x = anArcPoint[1][0] - anArcPoint[0][0];
666     double y = anArcPoint[1][1] - anArcPoint[0][1];
667     double aRad = sqrt(x*x + y*y);
668     x = anArcPoint[2][0] - anArcPoint[0][0];
669     y = anArcPoint[2][1] - anArcPoint[0][1];
670     double aNorm = sqrt(x*x + y*y);
671     if (aNorm >= tolerance) {
672       anArcPoint[2][0] = x * aRad / aNorm;
673       anArcPoint[2][1] = y * aRad / aNorm;
674     }
675     anArcPoint[1][0] -= anArcPoint[0][0];
676     anArcPoint[1][1] -= anArcPoint[0][1];
677     if (theCoeff < tolerance) {
678       theX = anArcPoint[0][0] + anArcPoint[1][0];
679       theY = anArcPoint[0][1] + anArcPoint[1][1];
680     } else if (1 - theCoeff < tolerance) {
681       theX = anArcPoint[0][0] + anArcPoint[2][0];
682       theY = anArcPoint[0][1] + anArcPoint[2][1];
683     } else {
684       std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
685       std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
686       double anAngle = aStartDir->angle(aEndDir);
687       if (anAngle < 0)
688         anAngle += 2.0 * PI;
689       anAngle *= theCoeff;
690       double aCos = cos(anAngle);
691       double aSin = sin(anAngle);
692       theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
693       theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
694     }
695     aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
696   }
697
698   if (!aMidPoint)
699     return EntityWrapperPtr();
700
701   std::list<ParameterWrapperPtr> aParameters;
702   aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->x()));
703   aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->y()));
704   // Create entity (parameters are not filled)
705   GCSPointPtr aPnt(new GCS::Point);
706   aPnt->x = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
707   aPnt->y = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
708
709   EntityWrapperPtr aResult(new PlaneGCSSolver_PointWrapper(AttributePtr(), aPnt));
710   aResult->setGroup(myGroupID);
711   aResult->setParameters(aParameters);
712
713   update(aResult);
714   return aResult;
715 }