Salome HOME
Task 2.4. Ability to modify the radius of circles and arcs of circle with the mouse
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintMovement.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
6
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Line.h>
10 #include <SketchPlugin_Point.h>
11
12 #include <GeomDataAPI_Point2D.h>
13
14 #include <GeomAPI_Pnt2d.h>
15
16
17 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
18   : SketchSolver_ConstraintFixed(ConstraintPtr()),
19     myMovedFeature(theFeature)
20 {
21 }
22
23 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
24   : SketchSolver_ConstraintFixed(ConstraintPtr()),
25     myDraggedPoint(thePoint)
26 {
27   myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
28 }
29
30 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
31 {
32   if (myMovedFeature)
33     myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
34 }
35
36 void SketchSolver_ConstraintMovement::process()
37 {
38   cleanErrorMsg();
39   if (!myMovedFeature || !myStorage) {
40     // Not enough parameters are initialized
41     return;
42   }
43
44   EntityWrapperPtr aMovedEntity = entityToFix();
45   if (!myErrorMsg.empty() || !aMovedEntity) {
46     // Nothing to move, clear the feature to avoid changing its group
47     // after removing the Movement constraint.
48     myMovedFeature = FeaturePtr();
49     return;
50   }
51
52   mySolverConstraint = fixFeature(aMovedEntity);
53   myStorage->addMovementConstraint(mySolverConstraint);
54 }
55
56
57 EntityWrapperPtr SketchSolver_ConstraintMovement::entityToFix()
58 {
59   // if the feature is copy, do not move it
60   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
61       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
62   if (!aSketchFeature || aSketchFeature->isCopy()) {
63     myStorage->setNeedToResolve(true);
64     return EntityWrapperPtr();
65   }
66
67   EntityWrapperPtr anEntity =
68       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
69   if (!anEntity) {
70     myStorage->update(myMovedFeature, true);
71     anEntity = myStorage->entity(myMovedFeature);
72   }
73   return anEntity;
74 }
75
76 void SketchSolver_ConstraintMovement::moveTo(
77     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
78 {
79   EntityWrapperPtr aMovedEntity =
80       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
81   if (!aMovedEntity)
82     return;
83
84   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
85                        theDestinationPoint->y() - myStartPoint->y() };
86
87   GCS::VEC_pD aFixedParams = toParameters(aMovedEntity);
88   for (int i = 0; i < aFixedParams.size() && i < myFixedValues.size(); ++i)
89     myFixedValues[i] = *(aFixedParams[i]) + aDelta[i % 2];
90
91   // no persistent constraints in the storage, thus store values directly to the feature
92   if (myStorage->isEmpty()) {
93     for (int i = 0; i < aFixedParams.size() && i < myFixedValues.size(); ++i)
94       *(aFixedParams[i]) = myFixedValues[i];
95     myStorage->setNeedToResolve(true);
96   }
97 }