1 // Copyright (C) 2017-2023 CEA, EDF
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_PointArrayWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Ellipse.h>
31 #include <SketchPlugin_EllipticArc.h>
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_Point.h>
35 #include <GeomDataAPI_Point2D.h>
37 #include <GeomAPI_Pnt2d.h>
41 static GCS::Point createGCSPoint(double* x, double* y)
50 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
51 : SketchSolver_ConstraintFixed(ConstraintPtr()),
52 myMovedFeature(theFeature),
57 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr theAttribute,
58 const int thePointIndex)
59 : SketchSolver_ConstraintFixed(ConstraintPtr()),
60 myDraggedAttribute(theAttribute),
61 myDraggedPointIndex(thePointIndex),
64 myMovedFeature = ModelAPI_Feature::feature(theAttribute->owner());
67 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
70 myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
73 void SketchSolver_ConstraintMovement::process()
76 if (!myMovedFeature || !myStorage) {
77 // Not enough parameters are initialized
81 mySolverConstraint = initMovement();
82 if (!myErrorMsg.empty() || !mySolverConstraint) {
83 // Nothing to move, clear the feature to avoid changing its group
84 // after removing the Movement constraint.
85 myMovedFeature = FeaturePtr();
88 myStorage->addMovementConstraint(mySolverConstraint);
92 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
95 #ifdef CHANGE_RADIUS_WHILE_MOVE
96 if (theMovedFeature->getKind() == SketchPlugin_Circle::ID() ||
97 theMovedFeature->getKind() == SketchPlugin_Ellipse::ID())
98 isSimple = (theDraggedPoint.get() != 0);
99 else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID() ||
100 theMovedFeature->getKind() == SketchPlugin_EllipticArc::ID()) {
101 isSimple = (theDraggedPoint.get() != 0 &&
102 (theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID() ||
103 theDraggedPoint->id() == SketchPlugin_EllipticArc::CENTER_ID()));
109 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
111 ConstraintWrapperPtr aConstraint;
113 // if the feature is copy, do not move it
114 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
115 std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
116 if (!aSketchFeature || aSketchFeature->isCopy()) {
117 myStorage->setNeedToResolve(true);
121 EntityWrapperPtr anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
122 : myStorage->entity(myMovedFeature);
124 myStorage->update(myMovedFeature, true);
125 anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
126 : myStorage->entity(myMovedFeature);
131 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedAttribute);
134 if (anEntity->type() == ENTITY_POINT_ARRAY) {
135 anEntity = std::dynamic_pointer_cast<PlaneGCSSolver_PointArrayWrapper>(anEntity)
136 ->value(myDraggedPointIndex);
138 aConstraint = fixFeature(anEntity);
141 if (myDraggedAttribute) // start or end point of arc has been moved
142 aConstraint = fixArcExtremity(anEntity);
143 else if (anEntity->type() == ENTITY_CIRCLE || anEntity->type() == ENTITY_ARC) {
144 // arc or circle has been moved
145 aConstraint = fixPointOnCircle(anEntity);
147 else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTIC_ARC) {
148 // ellipse or elliptic arc has been moved
149 aConstraint = fixPointOnEllipse(anEntity);
156 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
157 const EntityWrapperPtr& theArcExtremity)
159 static const int nbParams = 4;
160 myFixedValues.reserve(nbParams); // moved point and center of arc
162 EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
163 myStorage->entity(myMovedFeature));
164 std::shared_ptr<GCS::Arc> anArc =
165 std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
166 std::shared_ptr<GCS::ArcOfEllipse> anEllArc =
167 std::dynamic_pointer_cast<GCS::ArcOfEllipse>(aCircularEntity->entity());
169 PointWrapperPtr aPoint =
170 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
172 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y, 0, 0 };
174 aParams[2] = anArc->center.x;
175 aParams[3] = anArc->center.y;
178 aParams[2] = anEllArc->center.x;
179 aParams[3] = anEllArc->center.y;
182 std::list<GCSConstraintPtr> aConstraints;
183 for (int i = 0; i < nbParams; ++i) {
184 myFixedValues.push_back(*aParams[i]);
185 GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
186 aNewConstraint->rescale(0.01);
187 aConstraints.push_back(aNewConstraint);
190 return ConstraintWrapperPtr(
191 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
194 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
195 const EntityWrapperPtr& theCircular)
197 static const double scale = 0.01;
198 static const int nbParams = 4;
199 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
201 EdgeWrapperPtr aCircularEntity =
202 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
203 std::shared_ptr<GCS::Circle> aCircular =
204 std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
206 // initialize fixed values
207 myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
208 myFixedValues.push_back(*aCircular->center.y);
209 myFixedValues.push_back(*aCircular->center.x);
210 myFixedValues.push_back(*aCircular->center.y);
212 // create a moved point
213 GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
215 std::list<GCSConstraintPtr> aConstraints;
217 GCSConstraintPtr aNewConstraint(
218 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
219 aNewConstraint->rescale(scale);
220 aConstraints.push_back(aNewConstraint);
222 aNewConstraint = GCSConstraintPtr(
223 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
224 aNewConstraint->rescale(scale);
225 aConstraints.push_back(aNewConstraint);
227 aNewConstraint = GCSConstraintPtr(
228 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
229 aNewConstraint->rescale(scale);
230 aConstraints.push_back(aNewConstraint);
232 return ConstraintWrapperPtr(
233 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
236 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
237 const EntityWrapperPtr& theConic)
239 static const double scale = 0.01;
240 static const int nbParams = 6;
241 myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
243 EdgeWrapperPtr anEdge = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theConic);
244 std::shared_ptr<GCS::Ellipse> aConic = std::dynamic_pointer_cast<GCS::Ellipse>(anEdge->entity());
246 // major axis direction
247 double dx = *aConic->focus1.x - *aConic->center.x;
248 double dy = *aConic->focus1.y - *aConic->center.y;
249 double norm = sqrt(dx * dx + dy* dy);
250 if (norm < tolerance) {
259 double aMajorRad = aConic->getRadMaj();
261 // initialize fixed values
262 myFixedValues.push_back(*aConic->center.x + dx * aMajorRad);
263 myFixedValues.push_back(*aConic->center.y + dy * aMajorRad);
264 myFixedValues.push_back(*aConic->center.x);
265 myFixedValues.push_back(*aConic->center.y);
266 myFixedValues.push_back(*aConic->focus1.x);
267 myFixedValues.push_back(*aConic->focus1.y);
269 // create a moved point
270 GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
272 std::list<GCSConstraintPtr> aConstraints;
274 GCSConstraintPtr aNewConstraint(
275 new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
276 aNewConstraint->rescale(scale);
277 aConstraints.push_back(aNewConstraint);
279 aNewConstraint = GCSConstraintPtr(
280 new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
281 aNewConstraint->rescale(scale);
282 aConstraints.push_back(aNewConstraint);
284 aNewConstraint = GCSConstraintPtr(
285 new GCS::ConstraintEqual(&myFixedValues[3], aConic->center.y));
286 aNewConstraint->rescale(scale);
287 aConstraints.push_back(aNewConstraint);
288 // focus on the major axis
289 GCS::Point aStartPoint = createGCSPoint(&myFixedValues[2], &myFixedValues[3]);
290 GCS::Point aEndPoint = createGCSPoint(&myFixedValues[4], &myFixedValues[5]);
291 aNewConstraint = GCSConstraintPtr(
292 new GCS::ConstraintPointOnLine(aConic->focus1, aStartPoint, aEndPoint));
293 aNewConstraint->rescale(scale);
294 aConstraints.push_back(aNewConstraint);
296 return ConstraintWrapperPtr(
297 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
300 void SketchSolver_ConstraintMovement::startPoint(
301 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
303 myStartPoint = theStartPoint;
305 myFixedValues[0] = myStartPoint->x();
306 myFixedValues[1] = myStartPoint->y();
310 void SketchSolver_ConstraintMovement::moveTo(
311 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
314 return; // nothing to move
316 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
317 theDestinationPoint->y() - myStartPoint->y() };
319 #ifdef CHANGE_RADIUS_WHILE_MOVE
320 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
323 myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedAttribute ? 4 : 2;
325 for (int i = 0; i < aMaxSize; ++i)
326 myFixedValues[i] += aDelta[i % 2];