]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintMovement.cpp
Salome HOME
Merge branch 'Pre_2.8.0_development'
[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   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   return isSimple;
88 }
89
90 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
91 {
92   ConstraintWrapperPtr aConstraint;
93
94   // if the feature is copy, do not move it
95   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
96       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
97   if (!aSketchFeature || aSketchFeature->isCopy()) {
98     myStorage->setNeedToResolve(true);
99     return aConstraint;
100   }
101
102   EntityWrapperPtr anEntity =
103       myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
104   if (!anEntity) {
105     myStorage->update(myMovedFeature, true);
106     anEntity =
107         myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
108     if (!anEntity)
109       return aConstraint;
110   }
111
112   mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
113
114   if (mySimpleMove)
115     aConstraint = fixFeature(anEntity);
116   else {
117     if (myDraggedPoint) // start or end point of arc has been moved
118       aConstraint = fixArcExtremity(anEntity);
119     else // arc or circle has been moved
120       aConstraint = fixPointOnCircle(anEntity);
121   }
122
123   return aConstraint;
124 }
125
126 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
127     const EntityWrapperPtr& theArcExtremity)
128 {
129   static const int nbParams = 4;
130   myFixedValues.reserve(nbParams); // moved point and center of arc
131
132   EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
133       myStorage->entity(myMovedFeature));
134   std::shared_ptr<GCS::Arc> anArc =
135       std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
136
137   PointWrapperPtr aPoint =
138       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
139
140   double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
141                                 anArc->center.x, anArc->center.y };
142
143   std::list<GCSConstraintPtr> aConstraints;
144   for (int i = 0; i < nbParams; ++i) {
145     myFixedValues.push_back(*aParams[i]);
146     GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
147     aNewConstraint->rescale(0.01);
148     aConstraints.push_back(aNewConstraint);
149   }
150
151   return ConstraintWrapperPtr(
152       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
153 }
154
155 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
156     const EntityWrapperPtr& theCircular)
157 {
158   static const double scale = 0.01;
159   static const int nbParams = 4;
160   myFixedValues.reserve(nbParams); // moved point and center of arc/circle
161
162   EdgeWrapperPtr aCircularEntity =
163       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
164   std::shared_ptr<GCS::Circle> aCircular =
165       std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
166
167   // initialize fixed values
168   myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
169   myFixedValues.push_back(*aCircular->center.y);
170   myFixedValues.push_back(*aCircular->center.x);
171   myFixedValues.push_back(*aCircular->center.y);
172
173   // create a moved point
174   GCS::Point aPointOnCircle;
175   aPointOnCircle.x = &myFixedValues[0];
176   aPointOnCircle.y = &myFixedValues[1];
177
178   std::list<GCSConstraintPtr> aConstraints;
179   // point-on-circle
180   GCSConstraintPtr aNewConstraint(
181       new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
182   aNewConstraint->rescale(scale);
183   aConstraints.push_back(aNewConstraint);
184   // fixed center (x)
185   aNewConstraint = GCSConstraintPtr(
186       new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
187   aNewConstraint->rescale(scale);
188   aConstraints.push_back(aNewConstraint);
189   // fixed center (y)
190   aNewConstraint = GCSConstraintPtr(
191       new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
192   aNewConstraint->rescale(scale);
193   aConstraints.push_back(aNewConstraint);
194
195   return ConstraintWrapperPtr(
196       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
197 }
198
199
200 void SketchSolver_ConstraintMovement::startPoint(
201     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
202 {
203   myStartPoint = theStartPoint;
204   if (!mySimpleMove) {
205     myFixedValues[0] = myStartPoint->x();
206     myFixedValues[1] = myStartPoint->y();
207   }
208 }
209
210 void SketchSolver_ConstraintMovement::moveTo(
211     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
212 {
213   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
214                        theDestinationPoint->y() - myStartPoint->y() };
215
216   int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
217   for (int i = 0; i < aMaxSize; ++i)
218     myFixedValues[i] += aDelta[i % 2];
219 }