Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
1 // Copyright (C) 2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_ConstraintMovement.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
24
25 #include <PlaneGCSSolver_EdgeWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
27
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
32
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <GeomAPI_Pnt2d.h>
36
37
38 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
39   : SketchSolver_ConstraintFixed(ConstraintPtr()),
40     myMovedFeature(theFeature),
41     mySimpleMove(true)
42 {
43 }
44
45 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
46   : SketchSolver_ConstraintFixed(ConstraintPtr()),
47     myDraggedPoint(thePoint),
48     mySimpleMove(true)
49 {
50   myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
51 }
52
53 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
54 {
55   if (myMovedFeature)
56     myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
57 }
58
59 void SketchSolver_ConstraintMovement::process()
60 {
61   cleanErrorMsg();
62   if (!myMovedFeature || !myStorage) {
63     // Not enough parameters are initialized
64     return;
65   }
66
67   mySolverConstraint = initMovement();
68   if (!myErrorMsg.empty() || !mySolverConstraint) {
69     // Nothing to move, clear the feature to avoid changing its group
70     // after removing the Movement constraint.
71     myMovedFeature = FeaturePtr();
72     return;
73   }
74   myStorage->addMovementConstraint(mySolverConstraint);
75 }
76
77
78 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
79 {
80   bool isSimple = true;
81 #ifdef CHANGE_RADIUS_WHILE_MOVE
82   if (theMovedFeature->getKind() == SketchPlugin_Circle::ID())
83     isSimple = (theDraggedPoint.get() != 0);
84   else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID()) {
85     isSimple = (theDraggedPoint.get() != 0 &&
86                 theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID());
87   }
88 #endif
89   return isSimple;
90 }
91
92 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
93 {
94   ConstraintWrapperPtr aConstraint;
95
96   // if the feature is copy, do not move it
97   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
98       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
99   if (!aSketchFeature || aSketchFeature->isCopy()) {
100     myStorage->setNeedToResolve(true);
101     return aConstraint;
102   }
103
104   EntityWrapperPtr anEntity =
105       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
106   if (!anEntity) {
107     myStorage->update(myMovedFeature, true);
108     anEntity =
109         myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
110     if (!anEntity)
111       return aConstraint;
112   }
113
114   mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
115
116   if (mySimpleMove)
117     aConstraint = fixFeature(anEntity);
118   else {
119     if (myDraggedPoint) // start or end point of arc has been moved
120       aConstraint = fixArcExtremity(anEntity);
121     else // arc or circle has been moved
122       aConstraint = fixPointOnCircle(anEntity);
123   }
124
125   return aConstraint;
126 }
127
128 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
129     const EntityWrapperPtr& theArcExtremity)
130 {
131   static const int nbParams = 4;
132   myFixedValues.reserve(nbParams); // moved point and center of arc
133
134   EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
135       myStorage->entity(myMovedFeature));
136   std::shared_ptr<GCS::Arc> anArc =
137       std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
138
139   PointWrapperPtr aPoint =
140       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
141
142   double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
143                                 anArc->center.x, anArc->center.y };
144
145   std::list<GCSConstraintPtr> aConstraints;
146   for (int i = 0; i < nbParams; ++i) {
147     myFixedValues.push_back(*aParams[i]);
148     GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
149     aNewConstraint->rescale(0.01);
150     aConstraints.push_back(aNewConstraint);
151   }
152
153   return ConstraintWrapperPtr(
154       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
155 }
156
157 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
158     const EntityWrapperPtr& theCircular)
159 {
160   static const double scale = 0.01;
161   static const int nbParams = 4;
162   myFixedValues.reserve(nbParams); // moved point and center of arc/circle
163
164   EdgeWrapperPtr aCircularEntity =
165       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
166   std::shared_ptr<GCS::Circle> aCircular =
167       std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
168
169   // initialize fixed values
170   myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
171   myFixedValues.push_back(*aCircular->center.y);
172   myFixedValues.push_back(*aCircular->center.x);
173   myFixedValues.push_back(*aCircular->center.y);
174
175   // create a moved point
176   GCS::Point aPointOnCircle;
177   aPointOnCircle.x = &myFixedValues[0];
178   aPointOnCircle.y = &myFixedValues[1];
179
180   std::list<GCSConstraintPtr> aConstraints;
181   // point-on-circle
182   GCSConstraintPtr aNewConstraint(
183       new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
184   aNewConstraint->rescale(scale);
185   aConstraints.push_back(aNewConstraint);
186   // fixed center (x)
187   aNewConstraint = GCSConstraintPtr(
188       new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
189   aNewConstraint->rescale(scale);
190   aConstraints.push_back(aNewConstraint);
191   // fixed center (y)
192   aNewConstraint = GCSConstraintPtr(
193       new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
194   aNewConstraint->rescale(scale);
195   aConstraints.push_back(aNewConstraint);
196
197   return ConstraintWrapperPtr(
198       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
199 }
200
201
202 void SketchSolver_ConstraintMovement::startPoint(
203     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
204 {
205   myStartPoint = theStartPoint;
206   if (!mySimpleMove) {
207     myFixedValues[0] = myStartPoint->x();
208     myFixedValues[1] = myStartPoint->y();
209   }
210 }
211
212 void SketchSolver_ConstraintMovement::moveTo(
213     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
214 {
215   if (!myMovedFeature)
216     return; // nothing to move
217
218   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
219                        theDestinationPoint->y() - myStartPoint->y() };
220
221 #ifdef CHANGE_RADIUS_WHILE_MOVE
222   int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
223 #else
224   int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
225 #endif
226   for (int i = 0; i < aMaxSize; ++i)
227     myFixedValues[i] += aDelta[i % 2];
228 }