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 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());
90 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
92 ConstraintWrapperPtr aConstraint;
94 // if the feature is copy, do not move it
95 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
96 std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
97 if (!aSketchFeature || aSketchFeature->isCopy()) {
98 myStorage->setNeedToResolve(true);
102 EntityWrapperPtr anEntity =
103 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
105 myStorage->update(myMovedFeature, true);
107 myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
112 mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
115 aConstraint = fixFeature(anEntity);
117 if (myDraggedPoint) // start or end point of arc has been moved
118 aConstraint = fixArcExtremity(anEntity);
119 else // arc or circle has been moved
120 aConstraint = fixPointOnCircle(anEntity);
126 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
127 const EntityWrapperPtr& theArcExtremity)
129 static const int nbParams = 4;
130 myFixedValues.reserve(nbParams); // moved point and center of arc
132 EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
133 myStorage->entity(myMovedFeature));
134 std::shared_ptr<GCS::Arc> anArc =
135 std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
137 PointWrapperPtr aPoint =
138 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
140 double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
141 anArc->center.x, anArc->center.y };
143 std::list<GCSConstraintPtr> aConstraints;
144 for (int i = 0; i < nbParams; ++i) {
145 myFixedValues.push_back(*aParams[i]);
146 GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
147 aNewConstraint->rescale(0.01);
148 aConstraints.push_back(aNewConstraint);
151 return ConstraintWrapperPtr(
152 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
155 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
156 const EntityWrapperPtr& theCircular)
158 static const double scale = 0.01;
159 static const int nbParams = 4;
160 myFixedValues.reserve(nbParams); // moved point and center of arc/circle
162 EdgeWrapperPtr aCircularEntity =
163 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
164 std::shared_ptr<GCS::Circle> aCircular =
165 std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
167 // initialize fixed values
168 myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
169 myFixedValues.push_back(*aCircular->center.y);
170 myFixedValues.push_back(*aCircular->center.x);
171 myFixedValues.push_back(*aCircular->center.y);
173 // create a moved point
174 GCS::Point aPointOnCircle;
175 aPointOnCircle.x = &myFixedValues[0];
176 aPointOnCircle.y = &myFixedValues[1];
178 std::list<GCSConstraintPtr> aConstraints;
180 GCSConstraintPtr aNewConstraint(
181 new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
182 aNewConstraint->rescale(scale);
183 aConstraints.push_back(aNewConstraint);
185 aNewConstraint = GCSConstraintPtr(
186 new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
187 aNewConstraint->rescale(scale);
188 aConstraints.push_back(aNewConstraint);
190 aNewConstraint = GCSConstraintPtr(
191 new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
192 aNewConstraint->rescale(scale);
193 aConstraints.push_back(aNewConstraint);
195 return ConstraintWrapperPtr(
196 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
200 void SketchSolver_ConstraintMovement::startPoint(
201 const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
203 myStartPoint = theStartPoint;
205 myFixedValues[0] = myStartPoint->x();
206 myFixedValues[1] = myStartPoint->y();
210 void SketchSolver_ConstraintMovement::moveTo(
211 const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
213 double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
214 theDestinationPoint->y() - myStartPoint->y() };
216 int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
217 for (int i = 0; i < aMaxSize; ++i)
218 myFixedValues[i] += aDelta[i % 2];