Salome HOME
59dcac14af2103845d43359d1a624fd23c6c887a
[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   ConstraintWrapperPtr aConstraint = fixFeature(aMovedEntity);
53   myStorage->addMovementConstraint(aConstraint);
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   return myStorage->entity(myMovedFeature);
68 }
69
70 void SketchSolver_ConstraintMovement::moveTo(const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
71 {
72 #ifdef SUPPORT_NEW_MOVE
73   EntityWrapperPtr aMovedEntity = myStorage->entity(myMovedFeature);
74   if (!aMovedEntity)
75     return;
76
77   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
78                        theDestinationPoint->y() - myStartPoint->y() };
79
80   GCS::VEC_pD aFixedParams = toParameters(aMovedEntity);
81   for (int i = 0; i < aFixedParams.size() && i < myFixedValues.size(); ++i)
82     myFixedValues[i] = *(aFixedParams[i]) + aDelta[i % 2];
83
84 #endif
85 }