1 // Copyright (C) 2017-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <SketchSolver_ConstraintMovement.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
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>
33 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pnt2d.h>
39 static GCS::Point createGCSPoint(double* x, double* y)
48 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
49 : SketchSolver_ConstraintFixed(ConstraintPtr()),
50 myMovedFeature(theFeature),
55 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
56 : SketchSolver_ConstraintFixed(ConstraintPtr()),
57 myDraggedPoint(thePoint),
60 myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
63 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
66 myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
69 void SketchSolver_ConstraintMovement::process()
72 if (!myMovedFeature || !myStorage) {
73 // Not enough parameters are initialized
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();
84 myStorage->addMovementConstraint(mySolverConstraint);
88 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
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());
103 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
105 ConstraintWrapperPtr aConstraint;
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);
115 EntityWrapperPtr anEntity =
116 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
118 myStorage->update(myMovedFeature, true);
120 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
125 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
128 aConstraint = fixFeature(anEntity);
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);
136 else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTICAL_ARC) {
137 // ellipse or elliptical arc has been moved
138 aConstraint = fixPointOnEllipse(anEntity);
145 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
146 const EntityWrapperPtr& theArcExtremity)
148 static const int nbParams = 4;
149 myFixedValues.reserve(nbParams); // moved point and center of arc
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());
156 PointWrapperPtr aPoint =
157 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
159 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
160 anArc->center.x, anArc->center.y };
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);
170 return ConstraintWrapperPtr(
171 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
174 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
175 const EntityWrapperPtr& theCircular)
177 static const double scale = 0.01;
178 static const int nbParams = 4;
179 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
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());
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);
192 // create a moved point
193 GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
195 std::list<GCSConstraintPtr> aConstraints;
197 GCSConstraintPtr aNewConstraint(
198 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
199 aNewConstraint->rescale(scale);
200 aConstraints.push_back(aNewConstraint);
202 aNewConstraint = GCSConstraintPtr(
203 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
204 aNewConstraint->rescale(scale);
205 aConstraints.push_back(aNewConstraint);
207 aNewConstraint = GCSConstraintPtr(
208 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
209 aNewConstraint->rescale(scale);
210 aConstraints.push_back(aNewConstraint);
212 return ConstraintWrapperPtr(
213 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
216 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
217 const EntityWrapperPtr& theConic)
219 static const double scale = 0.01;
220 static const int nbParams = 6;
221 myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
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());
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) {
239 double aMajorRad = aConic->getRadMaj();
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);
249 // create a moved point
250 GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
252 std::list<GCSConstraintPtr> aConstraints;
254 GCSConstraintPtr aNewConstraint(
255 new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
256 aNewConstraint->rescale(scale);
257 aConstraints.push_back(aNewConstraint);
259 aNewConstraint = GCSConstraintPtr(
260 new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
261 aNewConstraint->rescale(scale);
262 aConstraints.push_back(aNewConstraint);
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);
276 return ConstraintWrapperPtr(
277 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
280 void SketchSolver_ConstraintMovement::startPoint(
281 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
283 myStartPoint = theStartPoint;
285 myFixedValues[0] = myStartPoint->x();
286 myFixedValues[1] = myStartPoint->y();
290 void SketchSolver_ConstraintMovement::moveTo(
291 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
294 return; // nothing to move
296 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
297 theDestinationPoint->y() - myStartPoint->y() };
299 #ifdef CHANGE_RADIUS_WHILE_MOVE
300 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
302 int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
304 for (int i = 0; i < aMaxSize; ++i)
305 myFixedValues[i] += aDelta[i % 2];