Salome HOME
PlaneGCS: Fix the problem regarding update of a distance during moving of a line...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 #include <SketchSolver_ConstraintDistance.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <GeomAPI_Dir2d.h>
6 #include <GeomAPI_Lin2d.h>
7 #include <GeomAPI_Pnt2d.h>
8 #include <GeomAPI_XY.h>
9
10 #include <math.h>
11
12
13 void SketchSolver_ConstraintDistance::getAttributes(
14     double& theValue,
15     std::vector<EntityWrapperPtr>& theAttributes)
16 {
17   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
18   if (!myErrorMsg.empty() || !theAttributes[0]) {
19     theAttributes.clear();
20     return;
21   }
22
23   if (theAttributes[1])
24     myType = CONSTRAINT_PT_PT_DISTANCE;
25   else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
26     myType = CONSTRAINT_PT_LINE_DISTANCE;
27   else
28     theAttributes.clear();
29
30   myPrevValue = 0.0;
31 }
32
33 void SketchSolver_ConstraintDistance::adjustConstraint()
34 {
35   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
36
37   // Adjust point-point distance if the points are equal
38   if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
39     const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
40     if (aSubs.front()->isEqual(aSubs.back())) {
41       // Change X coordinate of second point to eliminate coincidence
42       ParameterWrapperPtr aX = aSubs.back()->parameters().front();
43       aX->setValue(aX->value() + 1.0);
44       myStorage->update(aX);
45     }
46     return;
47   }
48
49   // Adjust point-line distance
50   if (fabs(myPrevValue) == fabs(aConstraint->value())) {
51     // sign of distance is not changed
52     aConstraint->setValue(myPrevValue);
53     myStorage->addConstraint(myBaseConstraint, aConstraint);
54     return;
55   }
56
57   // Adjust the sign of constraint value
58   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
59   aBuilder->adjustConstraint(aConstraint);
60   myStorage->addConstraint(myBaseConstraint, aConstraint);
61 }
62
63 void SketchSolver_ConstraintDistance::update()
64 {
65   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
66   myPrevValue = aConstraint->value();
67
68   SketchSolver_Constraint::update();
69 }