1 #include <SketchSolver_ConstraintAngle.h>
2 #include <SketchSolver_Manager.h>
4 #include <GeomAPI_Dir2d.h>
5 #include <GeomAPI_Lin2d.h>
6 #include <GeomAPI_Pnt2d.h>
7 #include <GeomAPI_XY.h>
11 void SketchSolver_ConstraintAngle::getAttributes(
12 double& theValue, std::vector<EntityWrapperPtr>& theAttributes)
14 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
20 void SketchSolver_ConstraintAngle::adjustConstraint()
22 static const double aTol = 1000. * tolerance;
23 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
25 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
26 if (fabs(myAngle - aConstraint->value()) < aTol)
28 myAngle = aConstraint->value();
30 //// Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
33 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
34 const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
35 std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
36 for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
37 const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
38 std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
39 for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt) {
40 isFixed[i][j] = ((*aLPIt)->group() != myGroupID);
41 aPoints[i][j] = aBuilder->point(*aLPIt);
45 if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
46 return; // both lines are fixed => no need to update them
48 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
49 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
50 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
52 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
56 for (int i = 0; i < 2; i++) {
57 for (int j = 0; j < 2; j++) {
58 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
59 if (fabs(aDist[i][j]) <= tolerance)
62 if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
63 aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
64 // the intersection point is an inner point of the line,
65 // we change the sign of distance till start point to calculate correct coordinates
70 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
71 for (int i = 0; i < 2; i++)
72 if (aDist[i][1] > fabs(aDist[i][0]))
73 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
74 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
76 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
77 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
78 // main direction is opposite => change signs
79 if (aDist[i][0] < 0.0) {
85 //// aConstraint.other = false;
86 //// for (int i = 0; i < 2; i++)
87 //// if (aLine[i]->direction()->dot(aDir[i]) < 0.0)
88 //// aConstraint.other = !aConstraint.other;
89 //// myStorage->updateConstraint(aConstraint);
91 //// bool isChanged = fabs(myAngle - aConstraint.valA) > aTol;
92 //// // myAngle should be updated even if the angle of constraint is changed too little
93 //// myAngle = aConstraint.valA;
95 //// return; // the angle was not changed, no need to recalculate positions of lines
97 // Recalculate positions of lines to avoid conflicting constraints
98 // while changing angle value several times
99 double cosA = cos(myAngle * PI / 180.0);
100 double sinA = sin(myAngle * PI / 180.0);
101 if (aDir[0]->cross(aDir[1]) < 0.0)
104 if (isFixed[1][0] && isFixed[1][1]) {
108 double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
109 double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
111 std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
112 for (int i = 0; i < 2; i++) {
113 aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
114 new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
115 anIntersection->y() + y * aDist[aLineToUpd][i]));
118 std::shared_ptr<GeomAPI_XY> aDelta;
119 if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
120 aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
121 else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
122 aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
124 for (int i = 0; i < 2; i++) {
125 aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
126 aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
130 // Update positions of points
131 std::list<EntityWrapperPtr>::const_iterator anUpdLine = aConstrLines.begin();
132 if (aLineToUpd > 0) ++anUpdLine;
133 const std::list<EntityWrapperPtr>& anUpdPoints = (*anUpdLine)->subEntities();
134 std::list<EntityWrapperPtr>::const_iterator aPIt = anUpdPoints.begin();
135 for (int i = 0; aPIt != anUpdPoints.end(); ++aPIt, ++i) {
136 double aCoord[2] = {aNewPoints[i]->x(), aNewPoints[i]->y()};
137 const std::list<ParameterWrapperPtr>& aParams = (*aPIt)->parameters();
138 std::list<ParameterWrapperPtr>::const_iterator aParIt = aParams.begin();
139 for (int j = 0; aParIt != aParams.end(); ++j, ++aParIt)
140 (*aParIt)->setValue(aCoord[j]);
143 aBuilder->adjustConstraint(aConstraint);
144 myStorage->addConstraint(myBaseConstraint, aConstraint);