Salome HOME
Check the multi coincidence between different types of entities (issue #751)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintTangent.cpp
1 #include <SketchSolver_ConstraintTangent.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5
6 void SketchSolver_ConstraintTangent::process()
7 {
8   cleanErrorMsg();
9   if (!myBaseConstraint || !myStorage || myGroup == 0) {
10     /// TODO: Put error message here
11     return;
12   }
13   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
14     update(myBaseConstraint);
15
16   double aValue;
17   std::vector<Slvs_hEntity> anEntID;
18   getAttributes(aValue, anEntID);
19   if (!myErrorMsg.empty())
20     return;
21   // Check the quantity of entities of each type and their order (arcs first)
22   int aNbLines = 0;
23   int aNbArcs = 0;
24   Slvs_Entity anEntities[2];
25   myType = SLVS_C_CURVE_CURVE_TANGENT;
26   std::vector<Slvs_hEntity>::iterator anEntIter = anEntID.begin();
27   for (; anEntIter != anEntID.end(); anEntIter++) {
28     Slvs_Entity anEnt = myStorage->getEntity(*anEntIter);
29     if (anEnt.type == SLVS_E_LINE_SEGMENT) {
30       if (aNbLines == 0)
31         anEntities[1 + aNbLines] = anEnt;
32       aNbLines++;
33       myType = SLVS_C_ARC_LINE_TANGENT;
34     }
35     else if (anEnt.type == SLVS_E_ARC_OF_CIRCLE) {
36       if (aNbArcs < 2)
37         anEntities[aNbArcs] = anEnt;
38       aNbArcs++;
39     }
40   }
41
42   if (aNbLines + aNbArcs != 2) {
43     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
44     return;
45   } else if (aNbArcs < 1) {
46     myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
47     return;
48   }
49
50   // It is necessary to identify which points of entities are coincident
51   int aSlvsOtherFlag = 0;
52   int aSlvsOther2Flag = 0;
53   // Obtain start and end points of entities
54   Slvs_hEntity aPointsToFind[4];
55   for (int i = 0; i < 2; i++) {
56     int aShift = anEntities[i].type == SLVS_E_ARC_OF_CIRCLE ? 1 : 0;
57     aPointsToFind[2*i]  = anEntities[i].point[aShift];
58     aPointsToFind[2*i+1]= anEntities[i].point[aShift+1];
59   }
60   // Search coincident points
61   bool isPointFound = false;
62   for (int i = 0; i < 2 && !isPointFound; i++)
63     for (int j = 2; j < 4 && !isPointFound; j++)
64       if (myStorage->isEqual(aPointsToFind[i], aPointsToFind[j])) {
65         aSlvsOtherFlag = i;
66         aSlvsOther2Flag = j - 2;
67         isPointFound = true;
68       }
69   if (!isPointFound) {
70     // There is no coincident points between tangential objects. Generate error message
71     myErrorMsg = SketchSolver_Error::NO_COINCIDENT_POINTS();
72     return;
73   }
74
75   Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(),
76       getType(), myGroup->getWorkplaneId(), aValue,
77       SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, anEntities[0].h, anEntities[1].h);
78   aConstraint.other = aSlvsOtherFlag;
79   aConstraint.other2 = aSlvsOther2Flag;
80   aConstraint.h = myStorage->addConstraint(aConstraint);
81   mySlvsConstraints.push_back(aConstraint.h);
82   adjustConstraint();
83 }
84