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