Salome HOME
Correct processing of the fixed arc in PlaneGCS (issue #1280)
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Builder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Builder.cpp
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_Builder.h>
8 #include <PlaneGCSSolver_Solver.h>
9 #include <PlaneGCSSolver_Storage.h>
10 #include <PlaneGCSSolver_ParameterWrapper.h>
11 #include <PlaneGCSSolver_AngleWrapper.h>
12 #include <PlaneGCSSolver_EntityWrapper.h>
13 #include <PlaneGCSSolver_PointWrapper.h>
14 #include <PlaneGCSSolver_ScalarWrapper.h>
15 #include <PlaneGCSSolver_ConstraintWrapper.h>
16
17 #include <SketchSolver_Manager.h>
18
19 #include <GeomAPI_Dir2d.h>
20 #include <GeomAPI_Pnt2d.h>
21 #include <GeomAPI_XY.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomDataAPI_Point.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_Attribute.h>
26 #include <ModelAPI_AttributeRefAttr.h>
27
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
32 #include <SketchPlugin_ConstraintAngle.h>
33
34 #include <math.h>
35
36
37 #define GCS_ENTITY_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(x)
38 #define GCS_POINT_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(x)
39 #define GCS_PARAMETER_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(x)
40
41
42 /// \brief Converts a value to SolveSpace parameter
43 /// \param theGroup [in]  group to store parameter
44 /// \param theValue [in]  value of parameter
45 /// \param theExpr  [in]  shows the parameter is given by expression
46 /// \return Created parameter's wrapper
47 static ParameterWrapperPtr createParameter(const GroupID& theGroup,
48                                            const double theValue = 0.0,
49                                            const bool theExpr = false);
50
51 static ParameterWrapperPtr createParamAngle(const GroupID& theGroup,
52                                             const double& theValue = 0.0);
53
54 static std::shared_ptr<PlaneGCSSolver_ScalarWrapper>
55     createScalar(const GroupID& theGroupID,
56                  AttributeDoublePtr theDoubleAttr = AttributeDoublePtr());
57
58 static EntityWrapperPtr createLine(FeaturePtr theFeature,
59                                    const std::list<EntityWrapperPtr>& theAttributes,
60                                    const GroupID& theGroupID);
61 static EntityWrapperPtr createCircle(FeaturePtr theFeature,
62                                      const std::list<EntityWrapperPtr>& theAttributes,
63                                      const GroupID& theGroupID);
64 static EntityWrapperPtr createArc(FeaturePtr theFeature,
65                                   const std::list<EntityWrapperPtr>& theAttributes,
66                                   const GroupID& theGroupID);
67
68
69 static ConstraintWrapperPtr
70   createConstraintCoincidence(ConstraintPtr theConstraint, 
71                               const GroupID& theGroupID,
72                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
73                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
74 static ConstraintWrapperPtr
75   createConstraintPointOnEntity(ConstraintPtr theConstraint, 
76                                 const GroupID& theGroupID,
77                                 const SketchSolver_ConstraintType& theType,
78                                 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
79                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
80 static ConstraintWrapperPtr
81   createConstraintDistancePointPoint(ConstraintPtr theConstraint,
82                                      const GroupID& theGroupID,
83                                      std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
84                                      std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
85                                      std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
86 static ConstraintWrapperPtr
87   createConstraintDistancePointLine(ConstraintPtr theConstraint,
88                                     const GroupID& theGroupID,
89                                     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
90                                     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
91                                     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
92 static ConstraintWrapperPtr
93   createConstraintRadius(ConstraintPtr theConstraint,
94                          const GroupID& theGroupID,
95                          std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
96                          std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
97 static ConstraintWrapperPtr
98   createConstraintAngle(ConstraintPtr theConstraint,
99                         const GroupID& theGroupID,
100                         std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
101                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
102                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
103 static ConstraintWrapperPtr
104   createConstraintHorizVert(ConstraintPtr theConstraint,
105                             const GroupID& theGroupID,
106                             const SketchSolver_ConstraintType& theType,
107                             std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
108 static ConstraintWrapperPtr
109   createConstraintParallel(ConstraintPtr theConstraint,
110                            const GroupID& theGroupID,
111                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
112                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
113 static ConstraintWrapperPtr
114   createConstraintPerpendicular(ConstraintPtr theConstraint,
115                                 const GroupID& theGroupID,
116                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
117                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
118 static ConstraintWrapperPtr
119   createConstraintEqual(ConstraintPtr theConstraint,
120                         const GroupID& theGroupID,
121                         const SketchSolver_ConstraintType& theType,
122                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
123                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
124                         std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed);
125 static ConstraintWrapperPtr
126   createConstraintTangent(ConstraintPtr theConstraint,
127                           const GroupID& theGroupID,
128                           const SketchSolver_ConstraintType& theType,
129                           std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
130                           std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
131 static ConstraintWrapperPtr
132   createConstraintCollinear(ConstraintPtr theConstraint,
133                            const GroupID& theGroupID,
134                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
135                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
136 static ConstraintWrapperPtr
137   createConstraintMiddlePoint(ConstraintPtr theConstraint,
138                               const GroupID& theGroupID,
139                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
140                               std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
141
142
143
144 /// \brief Set flags for angle constraint
145 static void adjustAngle(ConstraintWrapperPtr theConstraint);
146 /// \brief Update mirror points
147 static void adjustMirror(ConstraintWrapperPtr theConstraint);
148 /// \brief Update a sign of the point-line distance constraint
149 static void adjustPtLineDistance(ConstraintWrapperPtr theConstraint);
150
151 /// \brief Transform points to be symmetric regarding to the mirror line
152 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
153                              EntityWrapperPtr theMirrored,
154                              EntityWrapperPtr theMirrorLine);
155
156
157
158 // Initialization of constraint builder self pointer
159 BuilderPtr PlaneGCSSolver_Builder::mySelf = PlaneGCSSolver_Builder::getInstance();
160
161 BuilderPtr PlaneGCSSolver_Builder::getInstance()
162 {
163   if (!mySelf) {
164     mySelf = BuilderPtr(new PlaneGCSSolver_Builder);
165     SketchSolver_Manager::instance()->setBuilder(mySelf);
166   }
167   return mySelf;
168 }
169
170 StoragePtr PlaneGCSSolver_Builder::createStorage(const GroupID& theGroup) const
171 {
172   return StoragePtr(new PlaneGCSSolver_Storage(theGroup));
173 }
174
175 SolverPtr PlaneGCSSolver_Builder::createSolver() const
176 {
177   return SolverPtr(new PlaneGCSSolver_Solver);
178 }
179
180
181 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
182     ConstraintPtr theConstraint,
183     const GroupID& theGroupID,
184     const EntityID& theSketchID,
185     const SketchSolver_ConstraintType& theType,
186     const double& theValue,
187     const EntityWrapperPtr& thePoint1,
188     const EntityWrapperPtr& thePoint2,
189     const EntityWrapperPtr& theEntity1,
190     const EntityWrapperPtr& theEntity2) const
191 {
192   ConstraintWrapperPtr aResult;
193   ParameterWrapperPtr anIntermediate;
194
195   std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(thePoint1);
196   if (!aPoint1 && thePoint1 && thePoint1->type() == ENTITY_POINT)
197     aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint1)->subEntities().front() );
198   std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(thePoint2);
199   if (!aPoint2 && thePoint2 && thePoint2->type() == ENTITY_POINT)
200     aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint2)->subEntities().front() );
201
202   switch (theType) {
203   case CONSTRAINT_PT_PT_COINCIDENT:
204     aResult = createConstraintCoincidence(theConstraint, theGroupID, aPoint1, aPoint2);
205     break;
206   case CONSTRAINT_PT_ON_LINE:
207   case CONSTRAINT_PT_ON_CIRCLE:
208     aResult = createConstraintPointOnEntity(theConstraint, theGroupID, theType,
209                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
210     break;
211   case CONSTRAINT_MIDDLE_POINT:
212     aResult = createConstraintMiddlePoint(theConstraint, theGroupID,
213                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
214     break;
215   case CONSTRAINT_PT_PT_DISTANCE:
216     aResult = createConstraintDistancePointPoint(theConstraint, theGroupID,
217                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
218                   aPoint1, aPoint2);
219     break;
220   case CONSTRAINT_PT_LINE_DISTANCE:
221     aResult = createConstraintDistancePointLine(theConstraint, theGroupID,
222                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
223                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
224     break;
225   case CONSTRAINT_RADIUS:
226     aResult = createConstraintRadius(theConstraint, theGroupID,
227                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
228                   GCS_ENTITY_WRAPPER(theEntity1));
229     break;
230   case CONSTRAINT_ANGLE:
231     aResult = createConstraintAngle(theConstraint, theGroupID,
232                   GCS_PARAMETER_WRAPPER(createParamAngle(GID_OUTOFGROUP, theValue)),
233                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
234     break;
235   case CONSTRAINT_FIXED:
236     break;
237   case CONSTRAINT_HORIZONTAL:
238   case CONSTRAINT_VERTICAL:
239     aResult = createConstraintHorizVert(theConstraint, theGroupID, theType,
240                   GCS_ENTITY_WRAPPER(theEntity1));
241     break;
242   case CONSTRAINT_PARALLEL:
243     aResult = createConstraintParallel(theConstraint, theGroupID,
244                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
245     break;
246   case CONSTRAINT_PERPENDICULAR:
247     aResult = createConstraintPerpendicular(theConstraint, theGroupID,
248                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
249     break;
250   case CONSTRAINT_EQUAL_LINES:
251     anIntermediate = createParameter(theGroupID);
252   case CONSTRAINT_EQUAL_LINE_ARC:
253   case CONSTRAINT_EQUAL_RADIUS:
254     aResult = createConstraintEqual(theConstraint, theGroupID, theType,
255                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2),
256                   GCS_PARAMETER_WRAPPER(anIntermediate));
257     break;
258   case CONSTRAINT_TANGENT_ARC_LINE:
259   case CONSTRAINT_TANGENT_CIRCLE_LINE:
260   case CONSTRAINT_TANGENT_ARC_ARC:
261     aResult = createConstraintTangent(theConstraint, theGroupID, theType,
262                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
263     break;
264   case CONSTRAINT_COLLINEAR:
265     aResult = createConstraintCollinear(theConstraint, theGroupID,
266                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
267     break;
268   case CONSTRAINT_MULTI_TRANSLATION:
269   case CONSTRAINT_MULTI_ROTATION:
270     break;
271   case CONSTRAINT_SYMMETRIC:
272     return createMirror(theConstraint, theGroupID, theSketchID,
273                         thePoint1, thePoint2, theEntity1);
274   default:
275     break;
276   }
277
278   if (!aResult)
279     return std::list<ConstraintWrapperPtr>();
280   adjustConstraint(aResult);
281   return std::list<ConstraintWrapperPtr>(1, aResult);
282 }
283
284 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
285     ConstraintPtr theConstraint,
286     const GroupID& theGroupID,
287     const EntityID& theSketchID,
288     const SketchSolver_ConstraintType& theType,
289     const double& theValue,
290     const bool theFullValue,
291     const EntityWrapperPtr& thePoint1,
292     const EntityWrapperPtr& thePoint2,
293     const std::list<EntityWrapperPtr>& theTrsfEnt) const
294 {
295   ParameterWrapperPtr anAngleParam;
296   if (theType == CONSTRAINT_MULTI_ROTATION)
297     anAngleParam = createParamAngle(theGroupID, theValue);
298   else if (theType != CONSTRAINT_MULTI_TRANSLATION)
299     return std::list<ConstraintWrapperPtr>();
300
301   std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
302   if (thePoint2)
303     aConstrAttrList.push_front(thePoint2);
304   aConstrAttrList.push_front(thePoint1);
305
306   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
307       new PlaneGCSSolver_ConstraintWrapper(theConstraint, std::list<GCSConstraintPtr>(), theType));
308   aResult->setGroup(theGroupID);
309   aResult->setEntities(aConstrAttrList);
310   if (anAngleParam)
311     aResult->setValueParameter(anAngleParam);
312   aResult->setIsFullValue(theFullValue);
313   return std::list<ConstraintWrapperPtr>(1, aResult);
314 }
315
316
317 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createMirror(
318     ConstraintPtr theConstraint,
319     const GroupID& theGroupID,
320     const EntityID& theSketchID,
321     const EntityWrapperPtr& theEntity1,
322     const EntityWrapperPtr& theEntity2,
323     const EntityWrapperPtr& theMirrorLine) const
324 {
325   std::list<ConstraintWrapperPtr> aResult;
326   std::list<EntityWrapperPtr> aConstrAttrList;
327   if (theEntity1->type() == ENTITY_POINT) {
328     if (theEntity2->group() == theGroupID) // theEntity2 is not fixed
329       makeMirrorPoints(theEntity1, theEntity2, theMirrorLine);
330
331     std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(theEntity1);
332     if (!aPoint1 && theEntity1->type() == ENTITY_POINT)
333       aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity1)->subEntities().front() );
334     std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(theEntity2);
335     if (!aPoint2 && theEntity2->type() == ENTITY_POINT)
336       aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity2)->subEntities().front() );
337
338     std::shared_ptr<PlaneGCSSolver_EntityWrapper> aMirrorLine = 
339         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMirrorLine);
340     std::shared_ptr<GCS::Line> aLine =
341         std::dynamic_pointer_cast<GCS::Line>(aMirrorLine->entity());
342
343     std::list<GCSConstraintPtr> aConstrList;
344     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintPerpendicular(
345         *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
346     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintMidpointOnLine(
347         *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
348
349     ConstraintWrapperPtr aSubResult(new PlaneGCSSolver_ConstraintWrapper(
350         theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
351     aSubResult->setGroup(theGroupID);
352     std::list<EntityWrapperPtr> aSubs(1, theEntity1);
353     aSubs.push_back(theEntity2);
354     aSubs.push_back(theMirrorLine);
355     aSubResult->setEntities(aSubs);
356     aResult.push_back(aSubResult);
357   }
358   else if (theEntity1->type() == ENTITY_LINE) {
359     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
360     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
361     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
362     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
363     for (; anIt1 != aPoints1.end() && anIt2 != aPoints2.end(); ++anIt1, ++anIt2) {
364       std::list<ConstraintWrapperPtr> aMrrList =
365           createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
366       aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
367     }
368   }
369   else if (theEntity1->type() == ENTITY_CIRCLE) {
370     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
371     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
372     for (; anIt1 != aPoints1.end(); ++anIt1)
373       if ((*anIt1)->type() == ENTITY_POINT)
374         break;
375     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
376     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
377     for (; anIt2 != aPoints2.end(); ++anIt2)
378       if ((*anIt2)->type() == ENTITY_POINT)
379         break;
380
381     std::list<ConstraintWrapperPtr> aMrrList =
382         createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
383     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
384
385     // Additional constraint for equal radii
386     aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
387         0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
388     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
389   }
390   else if (theEntity1->type() == ENTITY_ARC) {
391     // Do not allow mirrored arc recalculate its position until coordinated of all points recalculated
392     FeaturePtr aMirrArc = theEntity2->baseFeature();
393     aMirrArc->data()->blockSendAttributeUpdated(true);
394
395     // Make mirror for center and start point of original arc
396     std::list<ConstraintWrapperPtr> aMrrList;
397     std::list<EntityWrapperPtr>::const_iterator anIt1 = theEntity1->subEntities().begin();
398     std::list<EntityWrapperPtr>::const_iterator anIt2 = theEntity2->subEntities().begin();
399     aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
400     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
401
402     ++anIt1;
403     ++anIt2; ++anIt2;
404     aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
405     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
406
407     // make symmetric last point of original arc and first point of mirrored arc without additional constraint
408     ++anIt1;
409     --anIt2;
410     makeMirrorPoints(*anIt1, *anIt2, theMirrorLine);
411
412     // Additionally, make equal radii...
413     aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
414         0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
415     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
416     // ... and make parametric length of arcs the same
417     std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt1 = 
418         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity1);
419     std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt2 = 
420         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity2);
421     std::shared_ptr<GCS::Arc> anArc1 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt1->entity());
422     std::shared_ptr<GCS::Arc> anArc2 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt2->entity());
423     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> anIntermed =
424         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(
425         createParameter(theGroupID, *(anArc1->endAngle) - *(anArc1->startAngle)));
426     // By the way, recalculate start and end angles of mirrored arc
427     std::shared_ptr<GeomAPI_Dir2d> anOX(new GeomAPI_Dir2d(1.0, 0.0));
428     std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
429         *(anArc2->start.x) - *(anArc2->center.x), *(anArc2->start.y) - *(anArc2->center.y)));
430     std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
431         *(anArc2->end.x) - *(anArc2->center.x), *(anArc2->end.y) - *(anArc2->center.y)));
432     *anArc2->startAngle = anOX->angle(aStartDir);
433     *anArc2->endAngle = anOX->angle(aEndDir);
434
435     std::list<GCSConstraintPtr> aConstrList;
436     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
437         anArc1->endAngle, anArc1->startAngle, anIntermed->parameter())));
438     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
439         anArc2->endAngle, anArc2->startAngle, anIntermed->parameter())));
440
441     std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aSubResult(
442         new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
443     aSubResult->setGroup(theGroupID);
444     std::list<EntityWrapperPtr> aSubs(1, theEntity1);
445     aSubs.push_back(theEntity2);
446     aSubs.push_back(theMirrorLine);
447     aSubResult->setEntities(aSubs);
448     aSubResult->setValueParameter(anIntermed);
449     aResult.push_back(aSubResult);
450
451     // Restore event sending
452     aMirrArc->data()->blockSendAttributeUpdated(false);
453   }
454   return aResult;
455 }
456
457 void PlaneGCSSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
458 {
459   SketchSolver_ConstraintType aType = theConstraint->type();
460   // Update flags and parameters in constraints
461   if (aType == CONSTRAINT_ANGLE)
462     adjustAngle(theConstraint);
463   else if (aType == CONSTRAINT_PT_LINE_DISTANCE)
464     adjustPtLineDistance(theConstraint);
465 }
466
467 EntityWrapperPtr PlaneGCSSolver_Builder::createFeature(
468     FeaturePtr theFeature,
469     const std::list<EntityWrapperPtr>& theAttributes,
470     const GroupID& theGroupID,
471     const EntityID& /*theSketchID*/) const
472 {
473   static EntityWrapperPtr aDummy;
474   if (!theFeature->data()->isValid())
475     return aDummy;
476
477   // Sketch
478   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
479   if (aSketch)
480     return createSketchEntity(aSketch, theGroupID);
481
482   // SketchPlugin features
483   std::shared_ptr<SketchPlugin_Feature> aFeature =
484       std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
485   if (!aFeature)
486     return aDummy;
487
488   // Verify the feature by its kind
489   const std::string& aFeatureKind = aFeature->getKind();
490   // Line
491   if (aFeatureKind == SketchPlugin_Line::ID())
492     return createLine(theFeature, theAttributes, theGroupID);
493   // Circle
494   else if (aFeatureKind == SketchPlugin_Circle::ID())
495     return createCircle(theFeature, theAttributes, theGroupID);
496   // Arc
497   else if (aFeatureKind == SketchPlugin_Arc::ID())
498     return createArc(theFeature, theAttributes, theGroupID);
499   // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
500   else if (aFeatureKind == SketchPlugin_Point::ID()) {
501     EntityWrapperPtr aSub;
502     if (theAttributes.size() == 1)
503       aSub = theAttributes.front();
504     else {
505       AttributePtr aPoint = theFeature->attribute(SketchPlugin_Point::COORD_ID());
506       if (aPoint->isInitialized())
507         aSub = createAttribute(aPoint, theGroupID);
508     }
509     if (!aSub)
510       return aDummy;
511
512     GCSPointPtr aSubEnt =
513         std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aSub)->point();
514     EntityWrapperPtr aNewEntity(new PlaneGCSSolver_EntityWrapper(theFeature));
515     aNewEntity->setSubEntities(std::list<EntityWrapperPtr>(1, aSub));
516     return aNewEntity;
517   }
518
519   // wrong entity
520   return aDummy;
521 }
522
523 EntityWrapperPtr PlaneGCSSolver_Builder::createAttribute(
524     AttributePtr theAttribute,
525     const GroupID& theGroupID,
526     const EntityID& theSketchID) const
527 {
528   AttributePtr anAttribute = theAttribute;
529   AttributeRefAttrPtr aRefAttr =
530       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
531   if (aRefAttr) {
532     if (aRefAttr->isObject()) {
533       // do not create features here
534       return EntityWrapperPtr();
535     } else
536       anAttribute = aRefAttr->attr();
537   }
538
539   std::list<ParameterWrapperPtr> aParameters;
540   EntityWrapperPtr aResult;
541
542   // Point in 2D
543   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
544     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
545   if (aPoint2D) {
546     aParameters.push_back(::createParameter(theGroupID, aPoint2D->x(), !aPoint2D->textX().empty()));
547     aParameters.push_back(::createParameter(theGroupID, aPoint2D->y(), !aPoint2D->textY().empty()));
548     GCSPointPtr aGCSPoint(new GCS::Point);
549     aGCSPoint->x = std::dynamic_pointer_cast<
550       PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
551     aGCSPoint->y = std::dynamic_pointer_cast<
552       PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
553     // Create entity (parameters are not filled)
554     aResult = EntityWrapperPtr(new PlaneGCSSolver_PointWrapper(theAttribute, aGCSPoint));
555   } else {
556     // Scalar value (used for the distance entities)
557     AttributeDoublePtr aScalar =
558       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
559     if (aScalar)
560       return createScalar(theGroupID, aScalar);
561   }
562
563   if (!aResult) {
564     // unknown attribute type
565     return EntityWrapperPtr();
566   }
567
568   aResult->setGroup(theGroupID);
569   aResult->setParameters(aParameters);
570   return aResult;
571 }
572
573 ParameterWrapperPtr PlaneGCSSolver_Builder::createParameter(
574     const GroupID& theGroupID, double theValue) const
575 {
576   return ::createParameter(theGroupID, theValue);
577 }
578
579
580 EntityWrapperPtr PlaneGCSSolver_Builder::createSketchEntity(
581     CompositeFeaturePtr theSketch,
582     const GroupID& theGroupID) const
583 {
584   DataPtr aSketchData = theSketch->data();
585   if (!aSketchData || !aSketchData->isValid())
586     return EntityWrapperPtr(); // the sketch is incorrect
587
588   // Create dummy wrapper representing workplane
589   std::shared_ptr<PlaneGCSSolver_EntityWrapper> aSketchEnt(
590       new PlaneGCSSolver_EntityWrapper(FeaturePtr(theSketch)));
591   aSketchEnt->setGroup(theGroupID);
592   aSketchEnt->setId(EID_SKETCH);
593   return aSketchEnt;
594 }
595
596
597
598
599
600 // ================   Auxiliary functions   ==========================
601 ParameterWrapperPtr createParameter(
602     const GroupID& theGroup, const double theValue, const bool theExpr)
603 {
604   double* aParam = new double(theValue);
605   ParameterWrapperPtr aWrapper(new PlaneGCSSolver_ParameterWrapper(aParam));
606   aWrapper->setGroup(theGroup);
607   aWrapper->setIsParametric(theExpr);
608   return aWrapper;
609 }
610
611 ParameterWrapperPtr createParamAngle(const GroupID& theGroup, const double& theValue)
612 {
613   double* aParam = new double(theValue);
614   ParameterWrapperPtr aWrapper(new PlaneGCSSolver_AngleWrapper(aParam));
615   aWrapper->setGroup(theGroup);
616   return aWrapper;
617 }
618
619 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> createScalar(
620     const GroupID& theGroupID,
621     AttributeDoublePtr theDoubleAttr)
622 {
623   ParameterWrapperPtr aParam = createParameter(theGroupID, theDoubleAttr ? theDoubleAttr->value() : 0.0);
624   return std::shared_ptr<PlaneGCSSolver_ScalarWrapper>(
625       new PlaneGCSSolver_ScalarWrapper(theDoubleAttr, aParam));
626 }
627
628 EntityWrapperPtr createLine(FeaturePtr theFeature,
629                             const std::list<EntityWrapperPtr>& theAttributes,
630                             const GroupID& theGroupID)
631 {
632   EntityWrapperPtr aNewEntity;
633   std::list<EntityWrapperPtr> aSubs;
634
635   AttributePtr aStart = theFeature->attribute(SketchPlugin_Line::START_ID());
636   AttributePtr aEnd = theFeature->attribute(SketchPlugin_Line::END_ID());
637   if (!aStart->isInitialized() || !aEnd->isInitialized())
638     return aNewEntity;
639
640   std::shared_ptr<PlaneGCSSolver_PointWrapper> aStartEnt, aEndEnt;
641   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
642   for (; anIt != theAttributes.end(); ++anIt) {
643     std::shared_ptr<PlaneGCSSolver_PointWrapper> aWrapper = 
644         std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
645     if (!aWrapper)
646       continue;
647     if (aWrapper->isBase(aStart))
648       aStartEnt = aWrapper;
649     else if (aWrapper->isBase(aEnd))
650       aEndEnt = aWrapper;
651   }
652   if (!aStartEnt || !aEndEnt)
653     return aNewEntity;
654
655   aSubs.push_back(aStartEnt);
656   aSubs.push_back(aEndEnt);
657
658   std::shared_ptr<GCS::Line> aLine(new GCS::Line);
659   aLine->p1 = *(aStartEnt->point());
660   aLine->p2 = *(aEndEnt->point());
661
662   aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, aLine));
663   aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
664   aNewEntity->setSubEntities(aSubs);
665   return aNewEntity;
666 }
667
668 EntityWrapperPtr createCircle(FeaturePtr theFeature,
669                               const std::list<EntityWrapperPtr>& theAttributes,
670                               const GroupID& theGroupID)
671 {
672   EntityWrapperPtr aNewEntity;
673   std::list<EntityWrapperPtr> aSubs;
674
675   AttributePtr aCenter = theFeature->attribute(SketchPlugin_Circle::CENTER_ID());
676   AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
677   if (!aCenter->isInitialized() || !aRadius->isInitialized())
678     return aNewEntity;
679
680   std::shared_ptr<PlaneGCSSolver_PointWrapper> aCenterEnt;
681   std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aRadiusEnt;
682   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
683   for (; anIt != theAttributes.end(); ++anIt) {
684     if ((*anIt)->isBase(aCenter))
685       aCenterEnt = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
686     else if ((*anIt)->isBase(aRadius))
687       aRadiusEnt = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*anIt);
688   }
689   if (!aCenterEnt || !aRadiusEnt)
690     return aNewEntity;
691
692   aSubs.push_back(aCenterEnt);
693   aSubs.push_back(aRadiusEnt);
694
695   std::shared_ptr<GCS::Circle> aCircle(new GCS::Circle);
696   aCircle->center = *(aCenterEnt->point());
697   aCircle->rad = aRadiusEnt->scalar();
698
699   aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, aCircle));
700   aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
701   aNewEntity->setSubEntities(aSubs);
702   return aNewEntity;
703 }
704
705 EntityWrapperPtr createArc(FeaturePtr theFeature,
706                            const std::list<EntityWrapperPtr>& theAttributes,
707                            const GroupID& theGroupID)
708 {
709   EntityWrapperPtr aNewEntity;
710   std::list<EntityWrapperPtr> aSubs;
711
712   AttributePtr aCenter = theFeature->attribute(SketchPlugin_Arc::CENTER_ID());
713   AttributePtr aStart = theFeature->attribute(SketchPlugin_Arc::START_ID());
714   AttributePtr aEnd = theFeature->attribute(SketchPlugin_Arc::END_ID());
715   if (!aCenter->isInitialized() || !aStart->isInitialized() || !aEnd->isInitialized())
716     return aNewEntity;
717
718   std::shared_ptr<PlaneGCSSolver_PointWrapper> aCenterEnt, aStartEnt, aEndEnt;
719   std::list<EntityWrapperPtr>::const_iterator anIt = theAttributes.begin();
720   for (; anIt != theAttributes.end(); ++anIt) {
721     std::shared_ptr<PlaneGCSSolver_PointWrapper> aWrapper = 
722         std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(*anIt);
723     if (!aWrapper)
724       continue;
725     if (aWrapper->isBase(aCenter))
726       aCenterEnt = aWrapper;
727     else if (aWrapper->isBase(aStart))
728       aStartEnt = aWrapper;
729     else if (aWrapper->isBase(aEnd))
730       aEndEnt = aWrapper;
731   }
732   if (!aCenterEnt || !aStartEnt || !aEndEnt)
733     return aNewEntity;
734
735   std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aStartAng, aEndAng, aRadius;
736   aStartAng = createScalar(theGroupID);
737   aEndAng   = createScalar(theGroupID);
738   aRadius   = createScalar(theGroupID);
739
740   aSubs.push_back(aCenterEnt);
741   aSubs.push_back(aStartEnt);
742   aSubs.push_back(aEndEnt);
743   aSubs.push_back(aStartAng);
744   aSubs.push_back(aEndAng);
745   aSubs.push_back(aRadius);
746
747   std::shared_ptr<GCS::Arc> anArc(new GCS::Arc);
748   anArc->center     = *(aCenterEnt->point());
749   anArc->start      = *(aStartEnt->point());
750   anArc->end        = *(aEndEnt->point());
751   anArc->startAngle = aStartAng->scalar();
752   anArc->endAngle   = aEndAng->scalar();
753   anArc->rad        = aRadius->scalar();
754
755   aNewEntity = EntityWrapperPtr(new PlaneGCSSolver_EntityWrapper(theFeature, anArc));
756   aNewEntity->setGroup(theGroupID); // sub-entities should not change their groups, therefore they are added later
757   aNewEntity->setSubEntities(aSubs);
758   return aNewEntity;
759 }
760
761
762
763 ConstraintWrapperPtr createConstraintCoincidence(
764     ConstraintPtr theConstraint,
765     const GroupID& theGroupID,
766     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
767     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2)
768 {
769   // Create equality constraint for corresponding attributes of the points
770   std::list<GCSConstraintPtr> aConstrList;
771   std::list<ParameterWrapperPtr>::const_iterator anIt1 = thePoint1->parameters().begin();
772   std::list<ParameterWrapperPtr>::const_iterator anIt2 = thePoint2->parameters().begin();
773   for (; anIt1 != thePoint1->parameters().end(); ++anIt1, ++anIt2) {
774     if (*anIt1 == *anIt2)
775       continue; // points use same parameters, no need additional constraints
776     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam1 =
777         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*anIt1);
778     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam2 =
779         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*anIt2);
780     aConstrList.push_back(
781         GCSConstraintPtr(new GCS::ConstraintEqual(aParam1->parameter(), aParam2->parameter())));
782   }
783
784   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
785       theConstraint, aConstrList, CONSTRAINT_PT_PT_COINCIDENT));
786   aResult->setGroup(theGroupID);
787   std::list<EntityWrapperPtr> aSubs(1, thePoint1);
788   aSubs.push_back(thePoint2);
789   aResult->setEntities(aSubs);
790   return aResult;
791 }
792
793 ConstraintWrapperPtr createConstraintPointOnEntity(
794     ConstraintPtr theConstraint, 
795     const GroupID& theGroupID,
796     const SketchSolver_ConstraintType& theType,
797     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
798     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
799 {
800   GCSConstraintPtr aNewConstr;
801
802   switch (theEntity->type()) {
803   case ENTITY_LINE: {
804     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
805     aNewConstr = GCSConstraintPtr(new GCS::ConstraintPointOnLine(*(thePoint->point()), *aLine));
806     break;
807     }
808   case ENTITY_ARC:
809   case ENTITY_CIRCLE: {
810     std::shared_ptr<GCS::Circle> aCirc = std::dynamic_pointer_cast<GCS::Circle>(theEntity->entity());
811     aNewConstr = GCSConstraintPtr(
812         new GCS::ConstraintP2PDistance(*(thePoint->point()), aCirc->center, aCirc->rad));
813     break;
814     }
815   default:
816     return ConstraintWrapperPtr();
817   }
818
819   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
820       theConstraint, aNewConstr, theType));
821   aResult->setGroup(theGroupID);
822   std::list<EntityWrapperPtr> aSubs(1, thePoint);
823   aSubs.push_back(theEntity);
824   aResult->setEntities(aSubs);
825   return aResult;
826 }
827
828 // calculate length of the line
829 static inline double lineLength(const GCS::Line& theLine)
830 {
831   double aDir[2] = {*(theLine.p2.x) - *(theLine.p1.x), *(theLine.p2.y) - *(theLine.p1.y)};
832   return sqrt(aDir[0] * aDir[0] + aDir[1] * aDir[1]);
833 }
834
835 // check the point is on the line
836 static inline bool isPointOnLine(const GCS::Point& thePoint, const GCS::Line& theLine)
837 {
838   double aDir[2] = {*(theLine.p2.x) - *(theLine.p1.x), *(theLine.p2.y) - *(theLine.p1.y)};
839   double aVec[2] = {*(thePoint.x) - *(theLine.p1.x), *(thePoint.y) - *(theLine.p1.y)};
840   double aCross = aVec[0] * aDir[1] - aVec[1] * aDir[0];
841   return fabs(aCross) < tolerance;
842 }
843
844 ConstraintWrapperPtr createConstraintMiddlePoint(
845     ConstraintPtr theConstraint,
846     const GroupID& theGroupID,
847     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
848     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
849 {
850   GCSPointPtr aPoint = thePoint->point();
851   std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
852   if (!aLine)
853     return ConstraintWrapperPtr();
854
855   std::list<GCSConstraintPtr> aConstrList;
856   aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintPointOnLine(*aPoint, *aLine)));
857   double aDist = lineLength(*aLine);
858   std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aDistance =
859       std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(createParameter(theGroupID, aDist));
860   aConstrList.push_back(GCSConstraintPtr(
861       new GCS::ConstraintP2PDistance(*aPoint, aLine->p1, aDistance->parameter())));
862   aConstrList.push_back(GCSConstraintPtr(
863       new GCS::ConstraintP2PDistance(*aPoint, aLine->p2, aDistance->parameter())));
864
865   // Workaround to avoid conflicting constraints when the point is already placed on line
866   if (thePoint->group() != GID_UNKNOWN && isPointOnLine(*aPoint, *aLine)) {
867     std::shared_ptr<GeomDataAPI_Point2D> aCoord =
868         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint->baseAttribute());
869     if (aCoord) {
870       *(aPoint->x) = (*(aLine->p1.x) + *(aLine->p2.x)) * 0.5;
871       *(aPoint->y) = (*(aLine->p1.y) + *(aLine->p2.y)) * 0.5;
872       aCoord->setValue(*(aPoint->x), *(aPoint->y));
873     }
874   }
875
876   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(new PlaneGCSSolver_ConstraintWrapper(
877       theConstraint, aConstrList, CONSTRAINT_MIDDLE_POINT));
878   aResult->setGroup(theGroupID);
879   std::list<EntityWrapperPtr> aSubs(1, thePoint);
880   aSubs.push_back(theEntity);
881   aResult->setEntities(aSubs);
882   aResult->setValueParameter(aDistance);
883   return aResult;
884 }
885
886
887 ConstraintWrapperPtr createConstraintDistancePointPoint(
888     ConstraintPtr theConstraint,
889     const GroupID& theGroupID,
890     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
891     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
892     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2)
893 {
894   GCSConstraintPtr aNewConstr(new GCS::ConstraintP2PDistance(
895       *(thePoint1->point()), *(thePoint2->point()), theValue->parameter()));
896
897   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
898       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PT_PT_DISTANCE));
899   aResult->setGroup(theGroupID);
900   std::list<EntityWrapperPtr> aSubs(1, thePoint1);
901   aSubs.push_back(thePoint2);
902   aResult->setEntities(aSubs);
903   aResult->setValueParameter(theValue);
904   return aResult;
905 }
906
907 ConstraintWrapperPtr createConstraintDistancePointLine(
908     ConstraintPtr theConstraint,
909     const GroupID& theGroupID,
910     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
911     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
912     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
913 {
914   std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
915   GCSConstraintPtr aNewConstr(new GCS::ConstraintP2LDistance(
916       *(thePoint->point()), *(aLine), theValue->parameter()));
917
918   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
919       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PT_LINE_DISTANCE));
920   aResult->setGroup(theGroupID);
921   std::list<EntityWrapperPtr> aSubs(1, thePoint);
922   aSubs.push_back(theEntity);
923   aResult->setEntities(aSubs);
924   aResult->setValueParameter(theValue);
925   return aResult;
926 }
927
928 ConstraintWrapperPtr createConstraintRadius(
929     ConstraintPtr theConstraint,
930     const GroupID& theGroupID,
931     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
932     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
933 {
934   std::shared_ptr<GCS::Circle> aCircle = std::dynamic_pointer_cast<GCS::Circle>(theEntity->entity());
935   GCSConstraintPtr aNewConstr(new GCS::ConstraintEqual(aCircle->rad, theValue->parameter()));
936
937   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
938       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_RADIUS));
939   aResult->setGroup(theGroupID);
940   std::list<EntityWrapperPtr> aSubs(1, theEntity);
941   aResult->setEntities(aSubs);
942   aResult->setValueParameter(theValue);
943   return aResult;
944 }
945
946 ConstraintWrapperPtr createConstraintAngle(
947     ConstraintPtr theConstraint,
948     const GroupID& theGroupID,
949     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
950     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
951     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
952 {
953   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
954   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
955   GCSConstraintPtr aNewConstr(new GCS::ConstraintL2LAngle(
956       *(aLine1), *(aLine2), theValue->parameter()));
957
958   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
959       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_ANGLE));
960   aResult->setGroup(theGroupID);
961   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
962   aSubs.push_back(theEntity2);
963   aResult->setEntities(aSubs);
964   aResult->setValueParameter(theValue);
965   return aResult;
966 }
967
968 ConstraintWrapperPtr createConstraintHorizVert(
969     ConstraintPtr theConstraint,
970     const GroupID& theGroupID,
971     const SketchSolver_ConstraintType& theType,
972     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
973 {
974   std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
975   GCSConstraintPtr aNewConstr;
976   if (theType == CONSTRAINT_HORIZONTAL)
977     aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.y, aLine->p2.y));
978   else
979     aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.x, aLine->p2.x));
980
981   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
982       theConstraint, aNewConstr, theType));
983   aResult->setGroup(theGroupID);
984   std::list<EntityWrapperPtr> aSubs(1, theEntity);
985   aResult->setEntities(aSubs);
986   return aResult;
987 }
988
989 ConstraintWrapperPtr createConstraintCollinear(
990     ConstraintPtr theConstraint,
991     const GroupID& theGroupID,
992     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
993     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
994 {
995   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
996   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
997
998   // create two point-on-line constraints
999   std::list<GCSConstraintPtr> aConstrList;
1000   aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p1, *aLine1)) );
1001   aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p2, *aLine1)) );
1002
1003   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
1004       theConstraint, aConstrList, CONSTRAINT_COLLINEAR));
1005   aResult->setGroup(theGroupID);
1006   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1007   aSubs.push_back(theEntity2);
1008   aResult->setEntities(aSubs);
1009   return aResult;
1010 }
1011
1012 ConstraintWrapperPtr createConstraintParallel(
1013     ConstraintPtr theConstraint,
1014     const GroupID& theGroupID,
1015     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1016     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1017 {
1018   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1019   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1020   GCSConstraintPtr aNewConstr(new GCS::ConstraintParallel(*(aLine1), *(aLine2)));
1021
1022   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1023       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PARALLEL));
1024   aResult->setGroup(theGroupID);
1025   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1026   aSubs.push_back(theEntity2);
1027   aResult->setEntities(aSubs);
1028   return aResult;
1029 }
1030
1031 ConstraintWrapperPtr createConstraintPerpendicular(
1032     ConstraintPtr theConstraint,
1033     const GroupID& theGroupID,
1034     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1035     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1036 {
1037   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1038   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1039   GCSConstraintPtr aNewConstr(new GCS::ConstraintPerpendicular(*(aLine1), *(aLine2)));
1040
1041   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1042       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PERPENDICULAR));
1043   aResult->setGroup(theGroupID);
1044   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1045   aSubs.push_back(theEntity2);
1046   aResult->setEntities(aSubs);
1047   return aResult;
1048 }
1049
1050 ConstraintWrapperPtr createConstraintEqual(
1051     ConstraintPtr theConstraint,
1052     const GroupID& theGroupID,
1053     const SketchSolver_ConstraintType& theType,
1054     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1055     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
1056     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed)
1057 {
1058   if (theType == CONSTRAINT_EQUAL_LINE_ARC)
1059     return ConstraintWrapperPtr(); // line-arc equivalence is not supported yet
1060
1061   std::list<GCSConstraintPtr> aConstrList;
1062   if (theType == CONSTRAINT_EQUAL_LINES) {
1063     std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1064     std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1065
1066     aConstrList.push_back(GCSConstraintPtr(
1067         new GCS::ConstraintP2PDistance(aLine1->p1, aLine1->p2, theIntermed->parameter())));
1068     aConstrList.push_back(GCSConstraintPtr(
1069         new GCS::ConstraintP2PDistance(aLine2->p1, aLine2->p2, theIntermed->parameter())));
1070   } else {
1071     std::shared_ptr<GCS::Circle> aCirc1 =
1072         std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1073     std::shared_ptr<GCS::Circle> aCirc2 =
1074         std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1075
1076     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintEqual(aCirc1->rad, aCirc2->rad)));
1077   }
1078
1079   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1080       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, theType));
1081   aResult->setGroup(theGroupID);
1082   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1083   aSubs.push_back(theEntity2);
1084   aResult->setEntities(aSubs);
1085   if (theIntermed)
1086     aResult->setValueParameter(theIntermed);
1087   return aResult;
1088 }
1089
1090 ConstraintWrapperPtr createConstraintTangent(
1091     ConstraintPtr theConstraint,
1092     const GroupID& theGroupID,
1093     const SketchSolver_ConstraintType& theType,
1094     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1095     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1096 {
1097   GCSConstraintPtr aNewConstr;
1098   if (theType == CONSTRAINT_TANGENT_ARC_LINE || theType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
1099     std::shared_ptr<GCS::Circle> aCirc = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1100     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1101
1102     aNewConstr = GCSConstraintPtr(new GCS::ConstraintP2LDistance(aCirc->center, *aLine, aCirc->rad));
1103   } else {
1104     std::shared_ptr<GCS::Circle> aCirc1 = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1105     std::shared_ptr<GCS::Circle> aCirc2 = std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1106
1107     double aDX = *(aCirc1->center.x) - *(aCirc2->center.x);
1108     double aDY = *(aCirc1->center.y) - *(aCirc2->center.y);
1109     double aDist = sqrt(aDX * aDX + aDY * aDY);
1110     aNewConstr = GCSConstraintPtr(new GCS::ConstraintTangentCircumf(aCirc1->center, aCirc2->center,
1111         aCirc1->rad, aCirc2->rad, (aDist < *(aCirc1->rad) || aDist < *(aCirc2->rad))));
1112   }
1113
1114   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1115       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, theType));
1116   aResult->setGroup(theGroupID);
1117   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1118   aSubs.push_back(theEntity2);
1119   aResult->setEntities(aSubs);
1120   return aResult;
1121 }
1122
1123
1124
1125
1126
1127 void adjustAngle(ConstraintWrapperPtr theConstraint)
1128 {
1129   BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1130
1131   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
1132     std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
1133
1134   std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
1135   const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
1136   std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
1137   for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
1138     const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
1139     std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
1140     for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt)
1141       aPoints[i][j] = aBuilder->point(*aLPIt);
1142   }
1143
1144   std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
1145     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
1146     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
1147   };
1148   std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
1149   if (!anIntersection)
1150     return;
1151   double aDist[2][2];
1152   for (int i = 0; i < 2; i++) {
1153     for (int j = 0; j < 2; j++) {
1154       aDist[i][j] = anIntersection->distance(aPoints[i][j]);
1155       if (fabs(aDist[i][j]) <= tolerance)
1156         aDist[i][j] = 0.0;
1157     }
1158     if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
1159         aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
1160       // the intersection point is an inner point of the line,
1161       // we change the sign of distance till start point to calculate correct coordinates
1162       // after rotation
1163       aDist[i][0] *= -1.0;
1164     }
1165   }
1166   std::shared_ptr<GeomAPI_Dir2d> aDir[2];
1167   for (int i = 0; i < 2; i++) {
1168     if (aDist[i][1] > fabs(aDist[i][0]))
1169       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
1170           aPoints[i][1]->xy()->decreased(anIntersection->xy())));
1171     else {
1172       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
1173           aPoints[i][0]->xy()->decreased(anIntersection->xy())));
1174       // main direction is opposite => change signs
1175       if (aDist[i][0] < 0.0) {
1176         aDist[i][0] *= -1.0;
1177         aDist[i][1] *= -1.0;
1178       }
1179     }
1180   }
1181
1182   double anAngle = aLine[0]->direction()->angle(aLine[1]->direction()) / PI * 180;
1183   if (anAngle * aConstraint->value() < 0.0)
1184     aConstraint->setValue(-aConstraint->value());
1185   if ((90.0 - fabs(anAngle)) * (fabs(aConstraint->value()) - 90.0) > 0.0) {
1186     if (aConstraint->value() < 0.0)
1187       aConstraint->setValue(-180.0 - aConstraint->value());
1188     else
1189       aConstraint->setValue(180.0 - aConstraint->value());
1190   }
1191 }
1192
1193 void makeMirrorPoints(EntityWrapperPtr theOriginal,
1194                       EntityWrapperPtr theMirrored,
1195                       EntityWrapperPtr theMirrorLine)
1196 {
1197   BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1198
1199   std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
1200   std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
1201   // orthogonal direction
1202   aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
1203
1204   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
1205   std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
1206   double aDist = aVec->dot(aMLDir->xy());
1207   aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
1208   double aCoord[2] = {aVec->x(), aVec->y()};
1209   std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
1210   for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
1211     (*aMIt)->setValue(aCoord[i]);
1212
1213   // update corresponding attribute
1214   AttributePtr anAttr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMirrored)->baseAttribute();
1215   if (anAttr) {
1216     std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
1217     aMirroredPnt->setValue(aCoord[0], aCoord[1]);
1218   }
1219 }
1220
1221 void adjustPtLineDistance(ConstraintWrapperPtr theConstraint)
1222 {
1223   BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1224
1225   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1226   std::shared_ptr<GeomAPI_Lin2d> aLine;
1227   std::list<EntityWrapperPtr> aSubs = theConstraint->entities();
1228   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
1229   for (; aSIt != aSubs.end(); ++aSIt) {
1230     if ((*aSIt)->type() == ENTITY_POINT)
1231       aPoint = aBuilder->point(*aSIt);
1232     else if ((*aSIt)->type() == ENTITY_LINE)
1233       aLine = aBuilder->line(*aSIt);
1234   }
1235
1236   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
1237   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
1238   if (aPtLineVec->cross(aLineVec) * theConstraint->value() < 0.0)
1239     theConstraint->setValue(theConstraint->value() * (-1.0));
1240 }
1241