Salome HOME
Dump Python in the High Level Parameterized Geometry API (issue #1648)
[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_IntersectionPoint.h>
33 #include <SketchPlugin_ConstraintAngle.h>
34
35 #include <math.h>
36
37
38 #define GCS_ENTITY_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(x)
39 #define GCS_POINT_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(x)
40 #define GCS_PARAMETER_WRAPPER(x) std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(x)
41
42
43 /// \brief Converts a value to SolveSpace parameter
44 /// \param theGroup [in]  group to store parameter
45 /// \param theValue [in]  value of parameter
46 /// \param theExpr  [in]  shows the parameter is given by expression
47 /// \return Created parameter's wrapper
48 static ParameterWrapperPtr createParameter(const GroupID& theGroup,
49                                            const double theValue = 0.0,
50                                            const bool theExpr = false);
51
52 static ParameterWrapperPtr createParamAngle(const GroupID& theGroup,
53                                             const double& theValue = 0.0);
54
55 static std::shared_ptr<PlaneGCSSolver_ScalarWrapper>
56     createScalar(const GroupID& theGroupID,
57                  AttributeDoublePtr theDoubleAttr = AttributeDoublePtr());
58
59 static EntityWrapperPtr createLine(FeaturePtr theFeature,
60                                    const std::list<EntityWrapperPtr>& theAttributes,
61                                    const GroupID& theGroupID);
62 static EntityWrapperPtr createCircle(FeaturePtr theFeature,
63                                      const std::list<EntityWrapperPtr>& theAttributes,
64                                      const GroupID& theGroupID);
65 static EntityWrapperPtr createArc(FeaturePtr theFeature,
66                                   const std::list<EntityWrapperPtr>& theAttributes,
67                                   const GroupID& theGroupID);
68
69
70 static ConstraintWrapperPtr
71   createConstraintCoincidence(ConstraintPtr theConstraint, 
72                               const GroupID& theGroupID,
73                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
74                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
75 static ConstraintWrapperPtr
76   createConstraintPointOnEntity(ConstraintPtr theConstraint, 
77                                 const GroupID& theGroupID,
78                                 const SketchSolver_ConstraintType& theType,
79                                 std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
80                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
81 static ConstraintWrapperPtr
82   createConstraintDistancePointPoint(ConstraintPtr theConstraint,
83                                      const GroupID& theGroupID,
84                                      std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
85                                      std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint1,
86                                      std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint2);
87 static ConstraintWrapperPtr
88   createConstraintDistancePointLine(ConstraintPtr theConstraint,
89                                     const GroupID& theGroupID,
90                                     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
91                                     std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
92                                     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
93 static ConstraintWrapperPtr
94   createConstraintRadius(ConstraintPtr theConstraint,
95                          const GroupID& theGroupID,
96                          std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
97                          std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
98 static ConstraintWrapperPtr
99   createConstraintAngle(ConstraintPtr theConstraint,
100                         const GroupID& theGroupID,
101                         std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theValue,
102                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
103                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
104 static ConstraintWrapperPtr
105   createConstraintHorizVert(ConstraintPtr theConstraint,
106                             const GroupID& theGroupID,
107                             const SketchSolver_ConstraintType& theType,
108                             std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
109 static ConstraintWrapperPtr
110   createConstraintParallel(ConstraintPtr theConstraint,
111                            const GroupID& theGroupID,
112                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
113                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
114 static ConstraintWrapperPtr
115   createConstraintPerpendicular(ConstraintPtr theConstraint,
116                                 const GroupID& theGroupID,
117                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
118                                 std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
119 static ConstraintWrapperPtr
120   createConstraintEqual(ConstraintPtr theConstraint,
121                         const GroupID& theGroupID,
122                         const SketchSolver_ConstraintType& theType,
123                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
124                         std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
125                         std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed);
126 static ConstraintWrapperPtr
127   createConstraintTangent(ConstraintPtr theConstraint,
128                           const GroupID& theGroupID,
129                           const SketchSolver_ConstraintType& theType,
130                           std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
131                           std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
132 static ConstraintWrapperPtr
133   createConstraintCollinear(ConstraintPtr theConstraint,
134                            const GroupID& theGroupID,
135                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
136                            std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2);
137 static ConstraintWrapperPtr
138   createConstraintMiddlePoint(ConstraintPtr theConstraint,
139                               const GroupID& theGroupID,
140                               std::shared_ptr<PlaneGCSSolver_PointWrapper> thePoint,
141                               std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity);
142
143
144
145 /// \brief Update mirror points
146 static void adjustMirror(ConstraintWrapperPtr theConstraint);
147 /// \brief Update a sign of the point-line distance constraint
148 static void adjustPtLineDistance(ConstraintWrapperPtr theConstraint);
149
150 /// \brief Transform points to be symmetric regarding to the mirror line
151 static void makeMirrorPoints(EntityWrapperPtr theOriginal,
152                              EntityWrapperPtr theMirrored,
153                              EntityWrapperPtr theMirrorLine);
154
155
156
157 // Initialization of constraint builder self pointer
158 BuilderPtr PlaneGCSSolver_Builder::mySelf = PlaneGCSSolver_Builder::getInstance();
159
160 BuilderPtr PlaneGCSSolver_Builder::getInstance()
161 {
162   if (!mySelf) {
163     mySelf = BuilderPtr(new PlaneGCSSolver_Builder);
164     SketchSolver_Manager::instance()->setBuilder(mySelf);
165   }
166   return mySelf;
167 }
168
169 StoragePtr PlaneGCSSolver_Builder::createStorage(const GroupID& theGroup) const
170 {
171   return StoragePtr(new PlaneGCSSolver_Storage(theGroup));
172 }
173
174 SolverPtr PlaneGCSSolver_Builder::createSolver() const
175 {
176   return SolverPtr(new PlaneGCSSolver_Solver);
177 }
178
179
180 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
181     ConstraintPtr theConstraint,
182     const GroupID& theGroupID,
183     const EntityID& theSketchID,
184     const SketchSolver_ConstraintType& theType,
185     const double& theValue,
186     const EntityWrapperPtr& thePoint1,
187     const EntityWrapperPtr& thePoint2,
188     const EntityWrapperPtr& theEntity1,
189     const EntityWrapperPtr& theEntity2) const
190 {
191   ConstraintWrapperPtr aResult;
192   ParameterWrapperPtr anIntermediate;
193
194   std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(thePoint1);
195   if (!aPoint1 && thePoint1 && thePoint1->type() == ENTITY_POINT)
196     aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint1)->subEntities().front() );
197   std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(thePoint2);
198   if (!aPoint2 && thePoint2 && thePoint2->type() == ENTITY_POINT)
199     aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(thePoint2)->subEntities().front() );
200
201   switch (theType) {
202   case CONSTRAINT_PT_PT_COINCIDENT:
203     aResult = createConstraintCoincidence(theConstraint, theGroupID, aPoint1, aPoint2);
204     break;
205   case CONSTRAINT_PT_ON_LINE:
206   case CONSTRAINT_PT_ON_CIRCLE:
207     aResult = createConstraintPointOnEntity(theConstraint, theGroupID, theType,
208                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
209     break;
210   case CONSTRAINT_MIDDLE_POINT:
211     aResult = createConstraintMiddlePoint(theConstraint, theGroupID,
212                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
213     break;
214   case CONSTRAINT_PT_PT_DISTANCE:
215     aResult = createConstraintDistancePointPoint(theConstraint, theGroupID,
216                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
217                   aPoint1, aPoint2);
218     break;
219   case CONSTRAINT_PT_LINE_DISTANCE:
220     aResult = createConstraintDistancePointLine(theConstraint, theGroupID,
221                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
222                   aPoint1, GCS_ENTITY_WRAPPER(theEntity1));
223     break;
224   case CONSTRAINT_RADIUS:
225     aResult = createConstraintRadius(theConstraint, theGroupID,
226                   GCS_PARAMETER_WRAPPER(createParameter(GID_OUTOFGROUP, theValue)),
227                   GCS_ENTITY_WRAPPER(theEntity1));
228     break;
229   case CONSTRAINT_ANGLE:
230     aResult = createConstraintAngle(theConstraint, theGroupID,
231                   GCS_PARAMETER_WRAPPER(createParamAngle(GID_OUTOFGROUP, theValue)),
232                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
233     break;
234   case CONSTRAINT_FIXED:
235     break;
236   case CONSTRAINT_HORIZONTAL:
237   case CONSTRAINT_VERTICAL:
238     aResult = createConstraintHorizVert(theConstraint, theGroupID, theType,
239                   GCS_ENTITY_WRAPPER(theEntity1));
240     break;
241   case CONSTRAINT_PARALLEL:
242     aResult = createConstraintParallel(theConstraint, theGroupID,
243                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
244     break;
245   case CONSTRAINT_PERPENDICULAR:
246     aResult = createConstraintPerpendicular(theConstraint, theGroupID,
247                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
248     break;
249   case CONSTRAINT_EQUAL_LINES:
250     anIntermediate = createParameter(theGroupID);
251   case CONSTRAINT_EQUAL_LINE_ARC:
252   case CONSTRAINT_EQUAL_RADIUS:
253     aResult = createConstraintEqual(theConstraint, theGroupID, theType,
254                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2),
255                   GCS_PARAMETER_WRAPPER(anIntermediate));
256     break;
257   case CONSTRAINT_TANGENT_ARC_LINE:
258   case CONSTRAINT_TANGENT_CIRCLE_LINE:
259   case CONSTRAINT_TANGENT_ARC_ARC:
260     aResult = createConstraintTangent(theConstraint, theGroupID, theType,
261                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
262     break;
263   case CONSTRAINT_COLLINEAR:
264     aResult = createConstraintCollinear(theConstraint, theGroupID,
265                   GCS_ENTITY_WRAPPER(theEntity1), GCS_ENTITY_WRAPPER(theEntity2));
266     break;
267   case CONSTRAINT_MULTI_TRANSLATION:
268   case CONSTRAINT_MULTI_ROTATION:
269     break;
270   case CONSTRAINT_SYMMETRIC:
271     return createMirror(theConstraint, theGroupID, theSketchID,
272                         thePoint1, thePoint2, theEntity1);
273   default:
274     break;
275   }
276
277   if (!aResult)
278     return std::list<ConstraintWrapperPtr>();
279   adjustConstraint(aResult);
280   return std::list<ConstraintWrapperPtr>(1, aResult);
281 }
282
283 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createConstraint(
284     ConstraintPtr theConstraint,
285     const GroupID& theGroupID,
286     const EntityID& theSketchID,
287     const SketchSolver_ConstraintType& theType,
288     const double& theValue,
289     const bool theFullValue,
290     const EntityWrapperPtr& thePoint1,
291     const EntityWrapperPtr& thePoint2,
292     const std::list<EntityWrapperPtr>& theTrsfEnt) const
293 {
294   ParameterWrapperPtr anAngleParam;
295   if (theType == CONSTRAINT_MULTI_ROTATION)
296     anAngleParam = createParamAngle(theGroupID, theValue);
297   else if (theType != CONSTRAINT_MULTI_TRANSLATION)
298     return std::list<ConstraintWrapperPtr>();
299
300   std::list<EntityWrapperPtr> aConstrAttrList = theTrsfEnt;
301   if (thePoint2)
302     aConstrAttrList.push_front(thePoint2);
303   aConstrAttrList.push_front(thePoint1);
304
305   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
306       new PlaneGCSSolver_ConstraintWrapper(theConstraint, std::list<GCSConstraintPtr>(), theType));
307   aResult->setGroup(theGroupID);
308   aResult->setEntities(aConstrAttrList);
309   if (anAngleParam)
310     aResult->setValueParameter(anAngleParam);
311   aResult->setIsFullValue(theFullValue);
312   return std::list<ConstraintWrapperPtr>(1, aResult);
313 }
314
315
316 std::list<ConstraintWrapperPtr> PlaneGCSSolver_Builder::createMirror(
317     ConstraintPtr theConstraint,
318     const GroupID& theGroupID,
319     const EntityID& theSketchID,
320     const EntityWrapperPtr& theEntity1,
321     const EntityWrapperPtr& theEntity2,
322     const EntityWrapperPtr& theMirrorLine) const
323 {
324   std::list<ConstraintWrapperPtr> aResult;
325   std::list<EntityWrapperPtr> aConstrAttrList;
326   if (theEntity1->type() == ENTITY_POINT) {
327     if (theEntity2->group() == theGroupID) // theEntity2 is not fixed
328       makeMirrorPoints(theEntity1, theEntity2, theMirrorLine);
329
330     std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint1 = GCS_POINT_WRAPPER(theEntity1);
331     if (!aPoint1 && theEntity1->type() == ENTITY_POINT)
332       aPoint1 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity1)->subEntities().front() );
333     std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint2 = GCS_POINT_WRAPPER(theEntity2);
334     if (!aPoint2 && theEntity2->type() == ENTITY_POINT)
335       aPoint2 = GCS_POINT_WRAPPER( GCS_ENTITY_WRAPPER(theEntity2)->subEntities().front() );
336
337     std::shared_ptr<PlaneGCSSolver_EntityWrapper> aMirrorLine = 
338         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMirrorLine);
339     std::shared_ptr<GCS::Line> aLine =
340         std::dynamic_pointer_cast<GCS::Line>(aMirrorLine->entity());
341
342     std::list<GCSConstraintPtr> aConstrList;
343     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintMidpointOnLine(
344         *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
345     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintPerpendicular(
346         *(aPoint1->point()), *(aPoint2->point()), aLine->p1, aLine->p2)));
347
348     ConstraintWrapperPtr aSubResult(new PlaneGCSSolver_ConstraintWrapper(
349         theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
350     aSubResult->setGroup(theGroupID);
351     std::list<EntityWrapperPtr> aSubs(1, theEntity1);
352     aSubs.push_back(theEntity2);
353     aSubs.push_back(theMirrorLine);
354     aSubResult->setEntities(aSubs);
355     aResult.push_back(aSubResult);
356   }
357   else if (theEntity1->type() == ENTITY_LINE) {
358     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
359     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
360     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
361     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
362     for (; anIt1 != aPoints1.end() && anIt2 != aPoints2.end(); ++anIt1, ++anIt2) {
363       std::list<ConstraintWrapperPtr> aMrrList =
364           createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
365       aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
366     }
367   }
368   else if (theEntity1->type() == ENTITY_CIRCLE) {
369     const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
370     std::list<EntityWrapperPtr>::const_iterator anIt1 = aPoints1.begin();
371     for (; anIt1 != aPoints1.end(); ++anIt1)
372       if ((*anIt1)->type() == ENTITY_POINT)
373         break;
374     const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
375     std::list<EntityWrapperPtr>::const_iterator anIt2 = aPoints2.begin();
376     for (; anIt2 != aPoints2.end(); ++anIt2)
377       if ((*anIt2)->type() == ENTITY_POINT)
378         break;
379
380     std::list<ConstraintWrapperPtr> aMrrList =
381         createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
382     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
383
384     // Additional constraint for equal radii
385     aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
386         0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
387     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
388   }
389   else if (theEntity1->type() == ENTITY_ARC) {
390     // Do not allow mirrored arc recalculate its position until coordinated of all points recalculated
391     FeaturePtr aMirrArc = theEntity2->baseFeature();
392     aMirrArc->data()->blockSendAttributeUpdated(true);
393
394     // Make mirror for center and start point of original arc
395     std::list<ConstraintWrapperPtr> aMrrList;
396     std::list<EntityWrapperPtr>::const_iterator anIt1 = theEntity1->subEntities().begin();
397     std::list<EntityWrapperPtr>::const_iterator anIt2 = theEntity2->subEntities().begin();
398     aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
399     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
400
401     ++anIt1;
402     ++anIt2; ++anIt2;
403     aMrrList = createMirror(theConstraint, theGroupID, theSketchID, *anIt1, *anIt2, theMirrorLine);
404     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
405
406     // make symmetric last point of original arc and first point of mirrored arc without additional constraint
407     ++anIt1;
408     --anIt2;
409     makeMirrorPoints(*anIt1, *anIt2, theMirrorLine);
410
411     // Additionally, make equal radii...
412     aMrrList = createConstraint(theConstraint, theGroupID, theSketchID, CONSTRAINT_EQUAL_RADIUS,
413         0.0, EntityWrapperPtr(), EntityWrapperPtr(), theEntity1, theEntity2);
414     aResult.insert(aResult.end(), aMrrList.begin(), aMrrList.end());
415     // ... and make parametric length of arcs the same
416     std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt1 = 
417         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity1);
418     std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt2 = 
419         std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity2);
420     std::shared_ptr<GCS::Arc> anArc1 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt1->entity());
421     std::shared_ptr<GCS::Arc> anArc2 = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt2->entity());
422     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> anIntermed =
423         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(
424         createParameter(theGroupID, *(anArc1->endAngle) - *(anArc1->startAngle)));
425     // By the way, recalculate start and end angles of mirrored arc
426     std::shared_ptr<GeomAPI_Dir2d> anOX(new GeomAPI_Dir2d(1.0, 0.0));
427     std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(
428         *(anArc2->start.x) - *(anArc2->center.x), *(anArc2->start.y) - *(anArc2->center.y)));
429     std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(
430         *(anArc2->end.x) - *(anArc2->center.x), *(anArc2->end.y) - *(anArc2->center.y)));
431     *anArc2->startAngle = anOX->angle(aStartDir);
432     *anArc2->endAngle = anOX->angle(aEndDir);
433
434     std::list<GCSConstraintPtr> aConstrList;
435     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
436         anArc1->endAngle, anArc1->startAngle, anIntermed->parameter())));
437     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintDifference(
438         anArc2->endAngle, anArc2->startAngle, anIntermed->parameter())));
439
440     std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aSubResult(
441         new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, CONSTRAINT_SYMMETRIC));
442     aSubResult->setGroup(theGroupID);
443     std::list<EntityWrapperPtr> aSubs(1, theEntity1);
444     aSubs.push_back(theEntity2);
445     aSubs.push_back(theMirrorLine);
446     aSubResult->setEntities(aSubs);
447     aSubResult->setValueParameter(anIntermed);
448     aResult.push_back(aSubResult);
449
450     // Restore event sending
451     aMirrArc->data()->blockSendAttributeUpdated(false);
452   }
453   return aResult;
454 }
455
456 void PlaneGCSSolver_Builder::adjustConstraint(ConstraintWrapperPtr theConstraint) const
457 {
458   SketchSolver_ConstraintType aType = theConstraint->type();
459   // Update flags and parameters in constraints
460   if (aType == CONSTRAINT_PT_LINE_DISTANCE)
461     adjustPtLineDistance(theConstraint);
462   else if (aType == CONSTRAINT_SYMMETRIC)
463     adjustMirror(theConstraint);
464 }
465
466 EntityWrapperPtr PlaneGCSSolver_Builder::createFeature(
467     FeaturePtr theFeature,
468     const std::list<EntityWrapperPtr>& theAttributes,
469     const GroupID& theGroupID,
470     const EntityID& /*theSketchID*/) const
471 {
472   static EntityWrapperPtr aDummy;
473   if (!theFeature->data()->isValid())
474     return aDummy;
475
476   // Sketch
477   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
478   if (aSketch)
479     return createSketchEntity(aSketch, theGroupID);
480
481   // SketchPlugin features
482   std::shared_ptr<SketchPlugin_Feature> aFeature =
483       std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
484   if (!aFeature)
485     return aDummy;
486
487   // Verify the feature by its kind
488   const std::string& aFeatureKind = aFeature->getKind();
489   // Line
490   if (aFeatureKind == SketchPlugin_Line::ID())
491     return createLine(theFeature, theAttributes, theGroupID);
492   // Circle
493   else if (aFeatureKind == SketchPlugin_Circle::ID())
494     return createCircle(theFeature, theAttributes, theGroupID);
495   // Arc
496   else if (aFeatureKind == SketchPlugin_Arc::ID())
497     return createArc(theFeature, theAttributes, theGroupID);
498   // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
499   else if (aFeatureKind == SketchPlugin_Point::ID() ||
500            aFeatureKind == SketchPlugin_IntersectionPoint::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   bool isLine1Rev = theConstraint->boolean(
955       SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
956   GCS::Point aLine1Pt1 = isLine1Rev ? aLine1->p2 : aLine1->p1;
957   GCS::Point aLine1Pt2 = isLine1Rev ? aLine1->p1 : aLine1->p2;
958
959   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
960   bool isLine2Rev = theConstraint->boolean(
961       SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
962   GCS::Point aLine2Pt1 = isLine2Rev ? aLine2->p2 : aLine2->p1;
963   GCS::Point aLine2Pt2 = isLine2Rev ? aLine2->p1 : aLine2->p2;
964
965   GCSConstraintPtr aNewConstr(new GCS::ConstraintL2LAngle(
966       aLine1Pt1, aLine1Pt2, aLine2Pt1, aLine2Pt2, theValue->parameter()));
967
968   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
969       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_ANGLE));
970   aResult->setGroup(theGroupID);
971   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
972   aSubs.push_back(theEntity2);
973   aResult->setEntities(aSubs);
974   aResult->setValueParameter(theValue);
975   return aResult;
976 }
977
978 ConstraintWrapperPtr createConstraintHorizVert(
979     ConstraintPtr theConstraint,
980     const GroupID& theGroupID,
981     const SketchSolver_ConstraintType& theType,
982     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity)
983 {
984   std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity->entity());
985   GCSConstraintPtr aNewConstr;
986   if (theType == CONSTRAINT_HORIZONTAL)
987     aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.y, aLine->p2.y));
988   else
989     aNewConstr = GCSConstraintPtr(new GCS::ConstraintEqual(aLine->p1.x, aLine->p2.x));
990
991   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
992       theConstraint, aNewConstr, theType));
993   aResult->setGroup(theGroupID);
994   std::list<EntityWrapperPtr> aSubs(1, theEntity);
995   aResult->setEntities(aSubs);
996   return aResult;
997 }
998
999 ConstraintWrapperPtr createConstraintCollinear(
1000     ConstraintPtr theConstraint,
1001     const GroupID& theGroupID,
1002     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1003     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1004 {
1005   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1006   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1007
1008   // create two point-on-line constraints
1009   std::list<GCSConstraintPtr> aConstrList;
1010   aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p1, *aLine1)) );
1011   aConstrList.push_back( GCSConstraintPtr(new GCS::ConstraintPointOnLine(aLine2->p2, *aLine1)) );
1012
1013   ConstraintWrapperPtr aResult(new PlaneGCSSolver_ConstraintWrapper(
1014       theConstraint, aConstrList, CONSTRAINT_COLLINEAR));
1015   aResult->setGroup(theGroupID);
1016   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1017   aSubs.push_back(theEntity2);
1018   aResult->setEntities(aSubs);
1019   return aResult;
1020 }
1021
1022 ConstraintWrapperPtr createConstraintParallel(
1023     ConstraintPtr theConstraint,
1024     const GroupID& theGroupID,
1025     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1026     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1027 {
1028   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1029   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1030   GCSConstraintPtr aNewConstr(new GCS::ConstraintParallel(*(aLine1), *(aLine2)));
1031
1032   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1033       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PARALLEL));
1034   aResult->setGroup(theGroupID);
1035   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1036   aSubs.push_back(theEntity2);
1037   aResult->setEntities(aSubs);
1038   return aResult;
1039 }
1040
1041 ConstraintWrapperPtr createConstraintPerpendicular(
1042     ConstraintPtr theConstraint,
1043     const GroupID& theGroupID,
1044     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1045     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1046 {
1047   std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1048   std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1049   GCSConstraintPtr aNewConstr(new GCS::ConstraintPerpendicular(*(aLine1), *(aLine2)));
1050
1051   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1052       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, CONSTRAINT_PERPENDICULAR));
1053   aResult->setGroup(theGroupID);
1054   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1055   aSubs.push_back(theEntity2);
1056   aResult->setEntities(aSubs);
1057   return aResult;
1058 }
1059
1060 ConstraintWrapperPtr createConstraintEqual(
1061     ConstraintPtr theConstraint,
1062     const GroupID& theGroupID,
1063     const SketchSolver_ConstraintType& theType,
1064     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1065     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2,
1066     std::shared_ptr<PlaneGCSSolver_ParameterWrapper> theIntermed)
1067 {
1068   if (theType == CONSTRAINT_EQUAL_LINE_ARC)
1069     return ConstraintWrapperPtr(); // line-arc equivalence is not supported yet
1070
1071   std::list<GCSConstraintPtr> aConstrList;
1072   if (theType == CONSTRAINT_EQUAL_LINES) {
1073     std::shared_ptr<GCS::Line> aLine1 = std::dynamic_pointer_cast<GCS::Line>(theEntity1->entity());
1074     std::shared_ptr<GCS::Line> aLine2 = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1075
1076     aConstrList.push_back(GCSConstraintPtr(
1077         new GCS::ConstraintP2PDistance(aLine1->p1, aLine1->p2, theIntermed->parameter())));
1078     aConstrList.push_back(GCSConstraintPtr(
1079         new GCS::ConstraintP2PDistance(aLine2->p1, aLine2->p2, theIntermed->parameter())));
1080     // update value of intermediate parameter
1081     double x = *aLine1->p1.x - *aLine1->p2.x;
1082     double y = *aLine1->p1.y - *aLine1->p2.y;
1083     double aLen = sqrt(x*x + y*y);
1084     theIntermed->setValue(aLen);
1085   } else {
1086     std::shared_ptr<GCS::Circle> aCirc1 =
1087         std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1088     std::shared_ptr<GCS::Circle> aCirc2 =
1089         std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1090
1091     aConstrList.push_back(GCSConstraintPtr(new GCS::ConstraintEqual(aCirc1->rad, aCirc2->rad)));
1092   }
1093
1094   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1095       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aConstrList, theType));
1096   aResult->setGroup(theGroupID);
1097   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1098   aSubs.push_back(theEntity2);
1099   aResult->setEntities(aSubs);
1100   if (theIntermed)
1101     aResult->setValueParameter(theIntermed);
1102   return aResult;
1103 }
1104
1105 ConstraintWrapperPtr createConstraintTangent(
1106     ConstraintPtr theConstraint,
1107     const GroupID& theGroupID,
1108     const SketchSolver_ConstraintType& theType,
1109     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity1,
1110     std::shared_ptr<PlaneGCSSolver_EntityWrapper> theEntity2)
1111 {
1112   GCSConstraintPtr aNewConstr;
1113   if (theType == CONSTRAINT_TANGENT_ARC_LINE || theType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
1114     std::shared_ptr<GCS::Circle> aCirc = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1115     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(theEntity2->entity());
1116
1117     aNewConstr = GCSConstraintPtr(new GCS::ConstraintP2LDistance(aCirc->center, *aLine, aCirc->rad));
1118   } else {
1119     std::shared_ptr<GCS::Circle> aCirc1 = std::dynamic_pointer_cast<GCS::Circle>(theEntity1->entity());
1120     std::shared_ptr<GCS::Circle> aCirc2 = std::dynamic_pointer_cast<GCS::Circle>(theEntity2->entity());
1121
1122     double aDX = *(aCirc1->center.x) - *(aCirc2->center.x);
1123     double aDY = *(aCirc1->center.y) - *(aCirc2->center.y);
1124     double aDist = sqrt(aDX * aDX + aDY * aDY);
1125     aNewConstr = GCSConstraintPtr(new GCS::ConstraintTangentCircumf(aCirc1->center, aCirc2->center,
1126         aCirc1->rad, aCirc2->rad, (aDist < *(aCirc1->rad) || aDist < *(aCirc2->rad))));
1127   }
1128
1129   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aResult(
1130       new PlaneGCSSolver_ConstraintWrapper(theConstraint, aNewConstr, theType));
1131   aResult->setGroup(theGroupID);
1132   std::list<EntityWrapperPtr> aSubs(1, theEntity1);
1133   aSubs.push_back(theEntity2);
1134   aResult->setEntities(aSubs);
1135   return aResult;
1136 }
1137
1138
1139
1140
1141
1142 void makeMirrorPoints(EntityWrapperPtr theOriginal,
1143                       EntityWrapperPtr theMirrored,
1144                       EntityWrapperPtr theMirrorLine)
1145 {
1146   BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1147
1148   std::shared_ptr<GeomAPI_Lin2d> aMirrorLine = aBuilder->line(theMirrorLine);
1149   std::shared_ptr<GeomAPI_Dir2d> aMLDir = aMirrorLine->direction();
1150   // orthogonal direction
1151   aMLDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aMLDir->y(), -aMLDir->x()));
1152
1153   std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(theOriginal);
1154   std::shared_ptr<GeomAPI_XY> aVec = aPoint->xy()->decreased(aMirrorLine->location()->xy());
1155   double aDist = aVec->dot(aMLDir->xy());
1156   aVec = aPoint->xy()->added(aMLDir->xy()->multiplied(-2.0 * aDist));
1157   double aCoord[2] = {aVec->x(), aVec->y()};
1158   std::list<ParameterWrapperPtr>::const_iterator aMIt = theMirrored->parameters().begin();
1159   for (int i = 0; aMIt != theMirrored->parameters().end(); ++aMIt, ++i)
1160     (*aMIt)->setValue(aCoord[i]);
1161
1162   // update corresponding attribute
1163   AttributePtr anAttr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMirrored)->baseAttribute();
1164   if (anAttr) {
1165     std::shared_ptr<GeomDataAPI_Point2D> aMirroredPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
1166     aMirroredPnt->setValue(aCoord[0], aCoord[1]);
1167   }
1168 }
1169
1170 void adjustPtLineDistance(ConstraintWrapperPtr theConstraint)
1171 {
1172   BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
1173
1174   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1175   std::shared_ptr<GeomAPI_Lin2d> aLine;
1176   std::list<EntityWrapperPtr> aSubs = theConstraint->entities();
1177   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
1178   for (; aSIt != aSubs.end(); ++aSIt) {
1179     if ((*aSIt)->type() == ENTITY_POINT)
1180       aPoint = aBuilder->point(*aSIt);
1181     else if ((*aSIt)->type() == ENTITY_LINE)
1182       aLine = aBuilder->line(*aSIt);
1183   }
1184
1185   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
1186   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
1187   if (aPtLineVec->cross(aLineVec) * theConstraint->value() < 0.0)
1188     theConstraint->setValue(theConstraint->value() * (-1.0));
1189 }
1190
1191 void adjustMirror(ConstraintWrapperPtr theConstraint)
1192 {
1193   std::vector<EntityWrapperPtr> aPoints;
1194   EntityWrapperPtr aMirrorLine;
1195
1196   const std::list<EntityWrapperPtr>& aSubs = theConstraint->entities();
1197   std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
1198   for (; anIt != aSubs.end(); ++anIt) {
1199     if ((*anIt)->type() == ENTITY_POINT)
1200       aPoints.push_back(*anIt);
1201     else if ((*anIt)->type() == ENTITY_LINE)
1202       aMirrorLine = *anIt;
1203   }
1204
1205   if (aPoints.size() == 2)
1206     makeMirrorPoints(aPoints[0], aPoints[1], aMirrorLine);
1207
1208   // update scales of constraints
1209   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCSConstraint = 
1210       std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
1211   std::list<GCSConstraintPtr>::const_iterator aCIt = aGCSConstraint->constraints().begin();
1212   for (; aCIt != aGCSConstraint->constraints().end(); ++aCIt)
1213     (*aCIt)->rescale();
1214 }
1215