Salome HOME
f8898477f9ab859a1f3abf29b9e97b63d11498b9
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 #include <SketchSolver_ConstraintDistance.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5 #include <GeomAPI_XY.h>
6
7
8 void SketchSolver_ConstraintDistance::process()
9 {
10   cleanErrorMsg();
11   if (!myBaseConstraint || !myStorage || myGroup == 0) {
12     /// TODO: Put error message here
13     return;
14   }
15   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
16     update(myBaseConstraint);
17
18   double aValue;
19   std::vector<Slvs_hEntity> anEntities;
20   getAttributes(aValue, anEntities);
21   if (!myErrorMsg.empty())
22     return;
23
24   // Obtain entities to identify the type of distance
25   static const int aNbPoints = 2;
26   Slvs_hEntity aPoint[aNbPoints] = {SLVS_E_UNKNOWN, SLVS_E_UNKNOWN};
27   Slvs_hEntity aLine = SLVS_E_UNKNOWN;
28   myType = SLVS_C_PT_PT_DISTANCE;
29   int aPtPos = 0;
30   std::vector<Slvs_hEntity>::iterator anEntIter = anEntities.begin();
31   for (; anEntIter != anEntities.end(); anEntIter++) {
32     if (*anEntIter == SLVS_E_UNKNOWN)
33       continue;
34     Slvs_Entity anEnt = myStorage->getEntity(*anEntIter);
35     if (anEnt.type == SLVS_E_POINT_IN_2D || anEnt.type == SLVS_E_POINT_IN_3D) {
36       if (aPtPos >= aNbPoints) {
37         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
38         return;
39       }
40       aPoint[aPtPos++] = *anEntIter;
41     }
42     else if (anEnt.type == SLVS_E_LINE_SEGMENT) {
43       if (myType == SLVS_C_PT_LINE_DISTANCE) {
44         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
45         return;
46       }
47       aLine = *anEntIter;
48       myType = SLVS_C_PT_LINE_DISTANCE;
49     }
50   }
51
52   Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(),
53       getType(), myGroup->getWorkplaneId(), aValue, aPoint[0], aPoint[1], aLine, SLVS_E_UNKNOWN);
54   aConstraint.h = myStorage->addConstraint(aConstraint);
55   mySlvsConstraints.push_back(aConstraint.h);
56
57   myPrevValue = 0.0;
58   adjustConstraint();
59 }
60
61 void SketchSolver_ConstraintDistance::adjustConstraint()
62 {
63   // Adjust point-line distance
64   if (getType() != SLVS_C_PT_LINE_DISTANCE)
65     return;
66
67   // Check the sign of distance is changed
68   Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
69   if (fabs(myPrevValue) == fabs(aConstraint.valA)) {
70     aConstraint.valA = myPrevValue;
71     myStorage->updateConstraint(aConstraint);
72     return;
73   }
74
75   // Get constraint parameters and check the sign of constraint value
76   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
77   for (; aCIter != mySlvsConstraints.end(); aCIter++) {
78     aConstraint = myStorage->getConstraint(*aCIter);
79     Slvs_Entity aLine = myStorage->getEntity(aConstraint.entityA);
80     // Obtain point and line coordinates
81     Slvs_hEntity aPointID[3] = {aConstraint.ptA, aLine.point[0], aLine.point[1]};
82     std::shared_ptr<GeomAPI_XY> aPoints[3];
83     for (int i = 0; i < 3; i++) {
84       Slvs_Entity aPoint = myStorage->getEntity(aPointID[i]);
85       Slvs_Param aParams[2] = {
86           myStorage->getParameter(aPoint.param[0]),
87           myStorage->getParameter(aPoint.param[1])};
88       aPoints[i] = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(aParams[0].val, aParams[1].val));
89     }
90     std::shared_ptr<GeomAPI_XY> aLineVec = aPoints[2]->decreased(aPoints[1]);
91     std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoints[0]->decreased(aPoints[1]);
92     if (aPtLineVec->cross(aLineVec) * aConstraint.valA < 0.0) {
93       aConstraint.valA *= -1.0;
94       myStorage->updateConstraint(aConstraint);
95     }
96   }
97 }
98
99 void SketchSolver_ConstraintDistance::update(ConstraintPtr theConstraint)
100 {
101   Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
102   myPrevValue = aConstraint.valA;
103   SketchSolver_Constraint::update(theConstraint);
104 }