]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintMovement.cpp
Salome HOME
Update translations of plugins.
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
1 // Copyright (C) 2017-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchSolver_ConstraintMovement.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
23
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Circle.h>
29 #include <SketchPlugin_Ellipse.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 #include <cmath>
38
39 static GCS::Point createGCSPoint(double* x, double* y)
40 {
41   GCS::Point aPoint;
42   aPoint.x = x;
43   aPoint.y = y;
44   return aPoint;
45 }
46
47
48 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
49   : SketchSolver_ConstraintFixed(ConstraintPtr()),
50     myMovedFeature(theFeature),
51     mySimpleMove(true)
52 {
53 }
54
55 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
56   : SketchSolver_ConstraintFixed(ConstraintPtr()),
57     myDraggedPoint(thePoint),
58     mySimpleMove(true)
59 {
60   myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
61 }
62
63 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
64 {
65   if (myMovedFeature)
66     myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
67 }
68
69 void SketchSolver_ConstraintMovement::process()
70 {
71   cleanErrorMsg();
72   if (!myMovedFeature || !myStorage) {
73     // Not enough parameters are initialized
74     return;
75   }
76
77   mySolverConstraint = initMovement();
78   if (!myErrorMsg.empty() || !mySolverConstraint) {
79     // Nothing to move, clear the feature to avoid changing its group
80     // after removing the Movement constraint.
81     myMovedFeature = FeaturePtr();
82     return;
83   }
84   myStorage->addMovementConstraint(mySolverConstraint);
85 }
86
87
88 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
89 {
90   bool isSimple = true;
91 #ifdef CHANGE_RADIUS_WHILE_MOVE
92   if (theMovedFeature->getKind() == SketchPlugin_Circle::ID() ||
93       theMovedFeature->getKind() == SketchPlugin_Ellipse::ID())
94     isSimple = (theDraggedPoint.get() != 0);
95   else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID()) {
96     isSimple = (theDraggedPoint.get() != 0 &&
97                 theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID());
98   }
99 #endif
100   return isSimple;
101 }
102
103 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
104 {
105   ConstraintWrapperPtr aConstraint;
106
107   // if the feature is copy, do not move it
108   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
109       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
110   if (!aSketchFeature || aSketchFeature->isCopy()) {
111     myStorage->setNeedToResolve(true);
112     return aConstraint;
113   }
114
115   EntityWrapperPtr anEntity =
116       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
117   if (!anEntity) {
118     myStorage->update(myMovedFeature, true);
119     anEntity =
120         myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
121     if (!anEntity)
122       return aConstraint;
123   }
124
125   mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
126
127   if (mySimpleMove)
128     aConstraint = fixFeature(anEntity);
129   else {
130     if (myDraggedPoint) // start or end point of arc has been moved
131       aConstraint = fixArcExtremity(anEntity);
132     else if (anEntity->type() == ENTITY_CIRCLE || anEntity->type() == ENTITY_ARC) {
133       // arc or circle has been moved
134       aConstraint = fixPointOnCircle(anEntity);
135     }
136     else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTICAL_ARC) {
137       // ellipse or elliptical arc has been moved
138       aConstraint = fixPointOnEllipse(anEntity);
139     }
140   }
141
142   return aConstraint;
143 }
144
145 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
146     const EntityWrapperPtr& theArcExtremity)
147 {
148   static const int nbParams = 4;
149   myFixedValues.reserve(nbParams); // moved point and center of arc
150
151   EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
152       myStorage->entity(myMovedFeature));
153   std::shared_ptr<GCS::Arc> anArc =
154       std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
155
156   PointWrapperPtr aPoint =
157       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
158
159   double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
160                                 anArc->center.x, anArc->center.y };
161
162   std::list<GCSConstraintPtr> aConstraints;
163   for (int i = 0; i < nbParams; ++i) {
164     myFixedValues.push_back(*aParams[i]);
165     GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
166     aNewConstraint->rescale(0.01);
167     aConstraints.push_back(aNewConstraint);
168   }
169
170   return ConstraintWrapperPtr(
171       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
172 }
173
174 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
175     const EntityWrapperPtr& theCircular)
176 {
177   static const double scale = 0.01;
178   static const int nbParams = 4;
179   myFixedValues.reserve(nbParams); // moved point and center of arc/circle
180
181   EdgeWrapperPtr aCircularEntity =
182       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
183   std::shared_ptr<GCS::Circle> aCircular =
184       std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
185
186   // initialize fixed values
187   myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
188   myFixedValues.push_back(*aCircular->center.y);
189   myFixedValues.push_back(*aCircular->center.x);
190   myFixedValues.push_back(*aCircular->center.y);
191
192   // create a moved point
193   GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
194
195   std::list<GCSConstraintPtr> aConstraints;
196   // point-on-circle
197   GCSConstraintPtr aNewConstraint(
198       new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
199   aNewConstraint->rescale(scale);
200   aConstraints.push_back(aNewConstraint);
201   // fixed center (x)
202   aNewConstraint = GCSConstraintPtr(
203       new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
204   aNewConstraint->rescale(scale);
205   aConstraints.push_back(aNewConstraint);
206   // fixed center (y)
207   aNewConstraint = GCSConstraintPtr(
208       new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
209   aNewConstraint->rescale(scale);
210   aConstraints.push_back(aNewConstraint);
211
212   return ConstraintWrapperPtr(
213       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
214 }
215
216 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
217     const EntityWrapperPtr& theConic)
218 {
219   static const double scale = 0.01;
220   static const int nbParams = 6;
221   myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
222
223   EdgeWrapperPtr anEdge = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theConic);
224   std::shared_ptr<GCS::Ellipse> aConic = std::dynamic_pointer_cast<GCS::Ellipse>(anEdge->entity());
225
226   // major axis direction
227   double dx = *aConic->focus1.x - *aConic->center.x;
228   double dy = *aConic->focus1.y - *aConic->center.y;
229   double norm = sqrt(dx * dx + dy* dy);
230   if (norm < tolerance) {
231     dx = 1.0;
232     dy = 0.0;
233   }
234   else {
235     dx /= norm;
236     dy /= norm;
237   }
238
239   double aMajorRad = aConic->getRadMaj();
240
241   // initialize fixed values
242   myFixedValues.push_back(*aConic->center.x + dx * aMajorRad);
243   myFixedValues.push_back(*aConic->center.y + dy * aMajorRad);
244   myFixedValues.push_back(*aConic->center.x);
245   myFixedValues.push_back(*aConic->center.y);
246   myFixedValues.push_back(*aConic->focus1.x);
247   myFixedValues.push_back(*aConic->focus1.y);
248
249   // create a moved point
250   GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
251
252   std::list<GCSConstraintPtr> aConstraints;
253   // point-on-circle
254   GCSConstraintPtr aNewConstraint(
255     new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
256   aNewConstraint->rescale(scale);
257   aConstraints.push_back(aNewConstraint);
258   // fixed center (x)
259   aNewConstraint = GCSConstraintPtr(
260     new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
261   aNewConstraint->rescale(scale);
262   aConstraints.push_back(aNewConstraint);
263   // fixed center (y)
264   aNewConstraint = GCSConstraintPtr(
265     new GCS::ConstraintEqual(&myFixedValues[3], aConic->center.y));
266   aNewConstraint->rescale(scale);
267   aConstraints.push_back(aNewConstraint);
268   // focus on the major axis
269   GCS::Point aStartPoint = createGCSPoint(&myFixedValues[2], &myFixedValues[3]);
270   GCS::Point aEndPoint   = createGCSPoint(&myFixedValues[4], &myFixedValues[5]);
271   aNewConstraint = GCSConstraintPtr(
272     new GCS::ConstraintPointOnLine(aConic->focus1, aStartPoint, aEndPoint));
273   aNewConstraint->rescale(scale);
274   aConstraints.push_back(aNewConstraint);
275
276   return ConstraintWrapperPtr(
277     new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
278 }
279
280 void SketchSolver_ConstraintMovement::startPoint(
281     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
282 {
283   myStartPoint = theStartPoint;
284   if (!mySimpleMove) {
285     myFixedValues[0] = myStartPoint->x();
286     myFixedValues[1] = myStartPoint->y();
287   }
288 }
289
290 void SketchSolver_ConstraintMovement::moveTo(
291     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
292 {
293   if (!myMovedFeature)
294     return; // nothing to move
295
296   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
297                        theDestinationPoint->y() - myStartPoint->y() };
298
299 #ifdef CHANGE_RADIUS_WHILE_MOVE
300   int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
301 #else
302   int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
303 #endif
304   for (int i = 0; i < aMaxSize; ++i)
305     myFixedValues[i] += aDelta[i % 2];
306 }