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