]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintMovement.cpp
Salome HOME
fdc4b0e78ff85afa6e34be69fb70fc3341219d67
[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_Line.h>
30 #include <SketchPlugin_Point.h>
31
32 #include <GeomDataAPI_Point2D.h>
33
34 #include <GeomAPI_Pnt2d.h>
35
36
37 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
38   : SketchSolver_ConstraintFixed(ConstraintPtr()),
39     myMovedFeature(theFeature),
40     mySimpleMove(true)
41 {
42 }
43
44 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
45   : SketchSolver_ConstraintFixed(ConstraintPtr()),
46     myDraggedPoint(thePoint),
47     mySimpleMove(true)
48 {
49   myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
50 }
51
52 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
53 {
54   if (myMovedFeature)
55     myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
56 }
57
58 void SketchSolver_ConstraintMovement::process()
59 {
60   cleanErrorMsg();
61   if (!myMovedFeature || !myStorage) {
62     // Not enough parameters are initialized
63     return;
64   }
65
66   mySolverConstraint = initMovement();
67   if (!myErrorMsg.empty() || !mySolverConstraint) {
68     // Nothing to move, clear the feature to avoid changing its group
69     // after removing the Movement constraint.
70     myMovedFeature = FeaturePtr();
71     return;
72   }
73   myStorage->addMovementConstraint(mySolverConstraint);
74 }
75
76
77 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
78 {
79   bool isSimple = true;
80 #ifdef CHANGE_RADIUS_WHILE_MOVE
81   if (theMovedFeature->getKind() == SketchPlugin_Circle::ID())
82     isSimple = (theDraggedPoint.get() != 0);
83   else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID()) {
84     isSimple = (theDraggedPoint.get() != 0 &&
85                 theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID());
86   }
87 #endif
88   return isSimple;
89 }
90
91 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
92 {
93   ConstraintWrapperPtr aConstraint;
94
95   // if the feature is copy, do not move it
96   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
97       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
98   if (!aSketchFeature || aSketchFeature->isCopy()) {
99     myStorage->setNeedToResolve(true);
100     return aConstraint;
101   }
102
103   EntityWrapperPtr anEntity =
104       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
105   if (!anEntity) {
106     myStorage->update(myMovedFeature, true);
107     anEntity =
108         myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
109     if (!anEntity)
110       return aConstraint;
111   }
112
113   mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
114
115   if (mySimpleMove)
116     aConstraint = fixFeature(anEntity);
117   else {
118     if (myDraggedPoint) // start or end point of arc has been moved
119       aConstraint = fixArcExtremity(anEntity);
120     else // arc or circle has been moved
121       aConstraint = fixPointOnCircle(anEntity);
122   }
123
124   return aConstraint;
125 }
126
127 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
128     const EntityWrapperPtr& theArcExtremity)
129 {
130   static const int nbParams = 4;
131   myFixedValues.reserve(nbParams); // moved point and center of arc
132
133   EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
134       myStorage->entity(myMovedFeature));
135   std::shared_ptr<GCS::Arc> anArc =
136       std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
137
138   PointWrapperPtr aPoint =
139       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
140
141   double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
142                                 anArc->center.x, anArc->center.y };
143
144   std::list<GCSConstraintPtr> aConstraints;
145   for (int i = 0; i < nbParams; ++i) {
146     myFixedValues.push_back(*aParams[i]);
147     GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
148     aNewConstraint->rescale(0.01);
149     aConstraints.push_back(aNewConstraint);
150   }
151
152   return ConstraintWrapperPtr(
153       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
154 }
155
156 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
157     const EntityWrapperPtr& theCircular)
158 {
159   static const double scale = 0.01;
160   static const int nbParams = 4;
161   myFixedValues.reserve(nbParams); // moved point and center of arc/circle
162
163   EdgeWrapperPtr aCircularEntity =
164       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
165   std::shared_ptr<GCS::Circle> aCircular =
166       std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
167
168   // initialize fixed values
169   myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
170   myFixedValues.push_back(*aCircular->center.y);
171   myFixedValues.push_back(*aCircular->center.x);
172   myFixedValues.push_back(*aCircular->center.y);
173
174   // create a moved point
175   GCS::Point aPointOnCircle;
176   aPointOnCircle.x = &myFixedValues[0];
177   aPointOnCircle.y = &myFixedValues[1];
178
179   std::list<GCSConstraintPtr> aConstraints;
180   // point-on-circle
181   GCSConstraintPtr aNewConstraint(
182       new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
183   aNewConstraint->rescale(scale);
184   aConstraints.push_back(aNewConstraint);
185   // fixed center (x)
186   aNewConstraint = GCSConstraintPtr(
187       new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
188   aNewConstraint->rescale(scale);
189   aConstraints.push_back(aNewConstraint);
190   // fixed center (y)
191   aNewConstraint = GCSConstraintPtr(
192       new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
193   aNewConstraint->rescale(scale);
194   aConstraints.push_back(aNewConstraint);
195
196   return ConstraintWrapperPtr(
197       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
198 }
199
200
201 void SketchSolver_ConstraintMovement::startPoint(
202     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
203 {
204   myStartPoint = theStartPoint;
205   if (!mySimpleMove) {
206     myFixedValues[0] = myStartPoint->x();
207     myFixedValues[1] = myStartPoint->y();
208   }
209 }
210
211 void SketchSolver_ConstraintMovement::moveTo(
212     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
213 {
214   if (!myMovedFeature)
215     return; // nothing to move
216
217   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
218                        theDestinationPoint->y() - myStartPoint->y() };
219
220 #ifdef CHANGE_RADIUS_WHILE_MOVE
221   int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
222 #else
223   int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
224 #endif
225   for (int i = 0; i < aMaxSize; ++i)
226     myFixedValues[i] += aDelta[i % 2];
227 }