1 // Copyright (C) 2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchSolver_ConstraintMovement.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
25 #include <PlaneGCSSolver_EdgeWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Line.h>
31 #include <SketchPlugin_Point.h>
33 #include <GeomDataAPI_Point2D.h>
35 #include <GeomAPI_Pnt2d.h>
38 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
39 : SketchSolver_ConstraintFixed(ConstraintPtr()),
40 myMovedFeature(theFeature),
45 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
46 : SketchSolver_ConstraintFixed(ConstraintPtr()),
47 myDraggedPoint(thePoint),
50 myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
53 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
56 myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
59 void SketchSolver_ConstraintMovement::process()
62 if (!myMovedFeature || !myStorage) {
63 // Not enough parameters are initialized
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();
74 myStorage->addMovementConstraint(mySolverConstraint);
78 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
81 #ifdef CHANGE_RADIUS_WHILE_MOVE
82 if (theMovedFeature->getKind() == SketchPlugin_Circle::ID())
83 isSimple = (theDraggedPoint.get() != 0);
84 else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID()) {
85 isSimple = (theDraggedPoint.get() != 0 &&
86 theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID());
92 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
94 ConstraintWrapperPtr aConstraint;
96 // if the feature is copy, do not move it
97 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
98 std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
99 if (!aSketchFeature || aSketchFeature->isCopy()) {
100 myStorage->setNeedToResolve(true);
104 EntityWrapperPtr anEntity =
105 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
107 myStorage->update(myMovedFeature, true);
109 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
114 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
117 aConstraint = fixFeature(anEntity);
119 if (myDraggedPoint) // start or end point of arc has been moved
120 aConstraint = fixArcExtremity(anEntity);
121 else // arc or circle has been moved
122 aConstraint = fixPointOnCircle(anEntity);
128 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
129 const EntityWrapperPtr& theArcExtremity)
131 static const int nbParams = 4;
132 myFixedValues.reserve(nbParams); // moved point and center of arc
134 EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
135 myStorage->entity(myMovedFeature));
136 std::shared_ptr<GCS::Arc> anArc =
137 std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
139 PointWrapperPtr aPoint =
140 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
142 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
143 anArc->center.x, anArc->center.y };
145 std::list<GCSConstraintPtr> aConstraints;
146 for (int i = 0; i < nbParams; ++i) {
147 myFixedValues.push_back(*aParams[i]);
148 GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
149 aNewConstraint->rescale(0.01);
150 aConstraints.push_back(aNewConstraint);
153 return ConstraintWrapperPtr(
154 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
157 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
158 const EntityWrapperPtr& theCircular)
160 static const double scale = 0.01;
161 static const int nbParams = 4;
162 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
164 EdgeWrapperPtr aCircularEntity =
165 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
166 std::shared_ptr<GCS::Circle> aCircular =
167 std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
169 // initialize fixed values
170 myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
171 myFixedValues.push_back(*aCircular->center.y);
172 myFixedValues.push_back(*aCircular->center.x);
173 myFixedValues.push_back(*aCircular->center.y);
175 // create a moved point
176 GCS::Point aPointOnCircle;
177 aPointOnCircle.x = &myFixedValues[0];
178 aPointOnCircle.y = &myFixedValues[1];
180 std::list<GCSConstraintPtr> aConstraints;
182 GCSConstraintPtr aNewConstraint(
183 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
184 aNewConstraint->rescale(scale);
185 aConstraints.push_back(aNewConstraint);
187 aNewConstraint = GCSConstraintPtr(
188 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
189 aNewConstraint->rescale(scale);
190 aConstraints.push_back(aNewConstraint);
192 aNewConstraint = GCSConstraintPtr(
193 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
194 aNewConstraint->rescale(scale);
195 aConstraints.push_back(aNewConstraint);
197 return ConstraintWrapperPtr(
198 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
202 void SketchSolver_ConstraintMovement::startPoint(
203 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
205 myStartPoint = theStartPoint;
207 myFixedValues[0] = myStartPoint->x();
208 myFixedValues[1] = myStartPoint->y();
212 void SketchSolver_ConstraintMovement::moveTo(
213 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
216 return; // nothing to move
218 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
219 theDestinationPoint->y() - myStartPoint->y() };
221 #ifdef CHANGE_RADIUS_WHILE_MOVE
222 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
224 int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
226 for (int i = 0; i < aMaxSize; ++i)
227 myFixedValues[i] += aDelta[i % 2];