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();
31 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
32 const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
33 std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
34 for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
35 const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
36 std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
37 for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt) {
38 isFixed[i][j] = ((*aLPIt)->group() != myGroupID);
39 aPoints[i][j] = aBuilder->point(*aLPIt);
43 if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
44 return; // both lines are fixed => no need to update them
46 std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
47 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
48 std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
50 std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
54 for (int i = 0; i < 2; i++) {
55 for (int j = 0; j < 2; j++) {
56 aDist[i][j] = anIntersection->distance(aPoints[i][j]);
57 if (fabs(aDist[i][j]) <= tolerance)
60 if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
61 aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
62 // the intersection point is an inner point of the line,
63 // we change the sign of distance till start point to calculate correct coordinates
68 std::shared_ptr<GeomAPI_Dir2d> aDir[2];
69 for (int i = 0; i < 2; i++)
70 if (aDist[i][1] > fabs(aDist[i][0]))
71 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
72 aPoints[i][1]->xy()->decreased(anIntersection->xy())));
74 aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
75 aPoints[i][0]->xy()->decreased(anIntersection->xy())));
76 // main direction is opposite => change signs
77 if (aDist[i][0] < 0.0) {
83 // Recalculate positions of lines to avoid conflicting constraints
84 // while changing angle value several times
85 double cosA = cos(myAngle * PI / 180.0);
86 double sinA = sin(myAngle * PI / 180.0);
87 if (aDir[0]->cross(aDir[1]) < 0.0)
90 if (isFixed[1][0] && isFixed[1][1]) {
94 double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
95 double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
97 std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
98 for (int i = 0; i < 2; i++) {
99 aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
100 new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
101 anIntersection->y() + y * aDist[aLineToUpd][i]));
104 std::shared_ptr<GeomAPI_XY> aDelta;
105 if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
106 aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
107 else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
108 aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
110 for (int i = 0; i < 2; i++) {
111 aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
112 aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
116 // Update positions of points
117 std::list<EntityWrapperPtr>::const_iterator anUpdLine = aConstrLines.begin();
118 if (aLineToUpd > 0) ++anUpdLine;
119 const std::list<EntityWrapperPtr>& anUpdPoints = (*anUpdLine)->subEntities();
120 std::list<EntityWrapperPtr>::const_iterator aPIt = anUpdPoints.begin();
121 for (int i = 0; aPIt != anUpdPoints.end(); ++aPIt, ++i) {
122 double aCoord[2] = {aNewPoints[i]->x(), aNewPoints[i]->y()};
123 const std::list<ParameterWrapperPtr>& aParams = (*aPIt)->parameters();
124 std::list<ParameterWrapperPtr>::const_iterator aParIt = aParams.begin();
125 for (int j = 0; aParIt != aParams.end(); ++j, ++aParIt)
126 (*aParIt)->setValue(aCoord[j]);
129 aBuilder->adjustConstraint(aConstraint);
130 myStorage->addConstraint(myBaseConstraint, aConstraint);