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_Line.h>
30 #include <SketchPlugin_Point.h>
32 #include <GeomDataAPI_Point2D.h>
34 #include <GeomAPI_Pnt2d.h>
37 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
38 : SketchSolver_ConstraintFixed(ConstraintPtr()),
39 myMovedFeature(theFeature),
44 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
45 : SketchSolver_ConstraintFixed(ConstraintPtr()),
46 myDraggedPoint(thePoint),
49 myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
52 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
55 myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
58 void SketchSolver_ConstraintMovement::process()
61 if (!myMovedFeature || !myStorage) {
62 // Not enough parameters are initialized
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();
73 myStorage->addMovementConstraint(mySolverConstraint);
77 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
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());
91 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
93 ConstraintWrapperPtr aConstraint;
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);
103 EntityWrapperPtr anEntity =
104 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
106 myStorage->update(myMovedFeature, true);
108 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
113 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
116 aConstraint = fixFeature(anEntity);
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);
127 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
128 const EntityWrapperPtr& theArcExtremity)
130 static const int nbParams = 4;
131 myFixedValues.reserve(nbParams); // moved point and center of arc
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());
138 PointWrapperPtr aPoint =
139 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
141 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
142 anArc->center.x, anArc->center.y };
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);
152 return ConstraintWrapperPtr(
153 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
156 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
157 const EntityWrapperPtr& theCircular)
159 static const double scale = 0.01;
160 static const int nbParams = 4;
161 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
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());
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);
174 // create a moved point
175 GCS::Point aPointOnCircle;
176 aPointOnCircle.x = &myFixedValues[0];
177 aPointOnCircle.y = &myFixedValues[1];
179 std::list<GCSConstraintPtr> aConstraints;
181 GCSConstraintPtr aNewConstraint(
182 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
183 aNewConstraint->rescale(scale);
184 aConstraints.push_back(aNewConstraint);
186 aNewConstraint = GCSConstraintPtr(
187 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
188 aNewConstraint->rescale(scale);
189 aConstraints.push_back(aNewConstraint);
191 aNewConstraint = GCSConstraintPtr(
192 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
193 aNewConstraint->rescale(scale);
194 aConstraints.push_back(aNewConstraint);
196 return ConstraintWrapperPtr(
197 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
201 void SketchSolver_ConstraintMovement::startPoint(
202 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
204 myStartPoint = theStartPoint;
206 myFixedValues[0] = myStartPoint->x();
207 myFixedValues[1] = myStartPoint->y();
211 void SketchSolver_ConstraintMovement::moveTo(
212 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
215 return; // nothing to move
217 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
218 theDestinationPoint->y() - myStartPoint->y() };
220 #ifdef CHANGE_RADIUS_WHILE_MOVE
221 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
223 int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
225 for (int i = 0; i < aMaxSize; ++i)
226 myFixedValues[i] += aDelta[i % 2];