1 #include <SketchSolver_ConstraintAngle.h>
3 #include <GeomAPI_Dir2d.h>
4 #include <GeomAPI_Lin2d.h>
5 #include <GeomAPI_Pnt2d.h>
6 #include <GeomAPI_XY.h>
10 void SketchSolver_ConstraintAngle::getAttributes(
11 double& theValue, std::vector<Slvs_hEntity>& theAttributes)
13 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
19 void SketchSolver_ConstraintAngle::adjustConstraint()
21 Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
24 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
25 Slvs_hConstraint aFixedConstraint;
26 double aLineDir[2][2] = { {0.0, 0.0}, {0.0, 0.0} };
27 Slvs_hEntity anEnt[2] = {aConstraint.entityA, aConstraint.entityB};
28 for (int i = 0; i < 2; i++) {
29 const Slvs_Entity& aLine = myStorage->getEntity(anEnt[i]);
31 for (int j = 0; j < 2; j++, aCoef += 2.0) {
32 const Slvs_Entity& aPoint = myStorage->getEntity(aLine.point[j]);
34 for (int k = 0; k < 2; k++) {
35 aCoords[k] = myStorage->getParameter(aPoint.param[k]).val;
36 aLineDir[i][k] += aCoef * aCoords[k];
38 isFixed[i][j] = myStorage->isPointFixed(aPoint.h, aFixedConstraint, true);
39 aPoints[i][j] = std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCoords[0], aCoords[1]));
42 double aDot = aLineDir[0][0] * aLineDir[1][0] + aLineDir[0][1] * aLineDir[1][1];
44 aConstraint.other = aDot * (90.0 - fabs(aConstraint.valA)) < 0.0;
45 if ((90.0 - fabs(aConstraint.valA)) * (90.0 - fabs(myAngle)) < 0.0)
46 aConstraint.other = !aConstraint.other;
47 myStorage->updateConstraint(aConstraint);
49 if (fabs(myAngle - aConstraint.valA) < tolerance)
50 return; // the angle was not changed, no need to recalculate positions of lines
51 myAngle = aConstraint.valA;
53 if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
54 return; // both lines are fixed => no need to update them
56 // Recalculate positions of lines to avoid conflicting constraints
57 // while changing angle value several times
58 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
59 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
60 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
62 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
66 for (int i = 0; i < 2; i++)
67 for (int j = 0; j < 2; j++)
68 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
69 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
70 for (int i = 0; i < 2; i++)
71 if (aDist[i][1] > aDist[i][0])
72 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
73 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
75 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
76 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
78 double cosA = cos(myAngle * PI / 180.0);
79 double sinA = sin(myAngle * PI / 180.0);
80 if (aDir[0]->cross(aDir[1]) < 0.0)
83 if (isFixed[1][0] && isFixed[1][1]) {
87 double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
88 double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
90 std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
91 for (int i = 0; i < 2; i++) {
92 aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
93 new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
94 anIntersection->y() + y * aDist[aLineToUpd][i]));
97 std::shared_ptr<GeomAPI_XY> aDelta;
98 if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
99 aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
100 else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
101 aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
103 for (int i = 0; i < 2; i++) {
104 aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
105 aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
109 // Update positions of points
110 const Slvs_Entity& anUpdLine = myStorage->getEntity(anEnt[aLineToUpd]);
112 for (int i = 0; i < 2; i++) {
113 const Slvs_Entity& aPoint = myStorage->getEntity(anUpdLine.point[i]);
114 aParam = myStorage->getParameter(aPoint.param[0]);
115 aParam.val = aNewPoints[i]->x();
116 myStorage->updateParameter(aParam);
117 aParam = myStorage->getParameter(aPoint.param[1]);
118 aParam.val = aNewPoints[i]->y();
119 myStorage->updateParameter(aParam);