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_EllipticArc.h>
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Point.h>
34 #include <GeomDataAPI_Point2D.h>
36 #include <GeomAPI_Pnt2d.h>
40 static GCS::Point createGCSPoint(double* x, double* y)
49 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
50 : SketchSolver_ConstraintFixed(ConstraintPtr()),
51 myMovedFeature(theFeature),
56 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
57 : SketchSolver_ConstraintFixed(ConstraintPtr()),
58 myDraggedPoint(thePoint),
61 myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
64 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
67 myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
70 void SketchSolver_ConstraintMovement::process()
73 if (!myMovedFeature || !myStorage) {
74 // Not enough parameters are initialized
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();
85 myStorage->addMovementConstraint(mySolverConstraint);
89 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
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()));
106 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
108 ConstraintWrapperPtr aConstraint;
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);
118 EntityWrapperPtr anEntity =
119 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
121 myStorage->update(myMovedFeature, true);
123 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
128 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
131 aConstraint = fixFeature(anEntity);
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);
139 else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTIC_ARC) {
140 // ellipse or elliptic arc has been moved
141 aConstraint = fixPointOnEllipse(anEntity);
148 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
149 const EntityWrapperPtr& theArcExtremity)
151 static const int nbParams = 4;
152 myFixedValues.reserve(nbParams); // moved point and center of arc
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());
161 PointWrapperPtr aPoint =
162 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
164 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y, 0, 0 };
166 aParams[2] = anArc->center.x;
167 aParams[3] = anArc->center.y;
170 aParams[2] = anEllArc->center.x;
171 aParams[3] = anEllArc->center.y;
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);
182 return ConstraintWrapperPtr(
183 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
186 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
187 const EntityWrapperPtr& theCircular)
189 static const double scale = 0.01;
190 static const int nbParams = 4;
191 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
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());
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);
204 // create a moved point
205 GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
207 std::list<GCSConstraintPtr> aConstraints;
209 GCSConstraintPtr aNewConstraint(
210 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
211 aNewConstraint->rescale(scale);
212 aConstraints.push_back(aNewConstraint);
214 aNewConstraint = GCSConstraintPtr(
215 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
216 aNewConstraint->rescale(scale);
217 aConstraints.push_back(aNewConstraint);
219 aNewConstraint = GCSConstraintPtr(
220 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
221 aNewConstraint->rescale(scale);
222 aConstraints.push_back(aNewConstraint);
224 return ConstraintWrapperPtr(
225 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
228 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
229 const EntityWrapperPtr& theConic)
231 static const double scale = 0.01;
232 static const int nbParams = 6;
233 myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
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());
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) {
251 double aMajorRad = aConic->getRadMaj();
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);
261 // create a moved point
262 GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
264 std::list<GCSConstraintPtr> aConstraints;
266 GCSConstraintPtr aNewConstraint(
267 new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
268 aNewConstraint->rescale(scale);
269 aConstraints.push_back(aNewConstraint);
271 aNewConstraint = GCSConstraintPtr(
272 new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
273 aNewConstraint->rescale(scale);
274 aConstraints.push_back(aNewConstraint);
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);
288 return ConstraintWrapperPtr(
289 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
292 void SketchSolver_ConstraintMovement::startPoint(
293 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
295 myStartPoint = theStartPoint;
297 myFixedValues[0] = myStartPoint->x();
298 myFixedValues[1] = myStartPoint->y();
302 void SketchSolver_ConstraintMovement::moveTo(
303 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
306 return; // nothing to move
308 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
309 theDestinationPoint->y() - myStartPoint->y() };
311 #ifdef CHANGE_RADIUS_WHILE_MOVE
312 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
314 int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
316 for (int i = 0; i < aMaxSize; ++i)
317 myFixedValues[i] += aDelta[i % 2];