Salome HOME
Issue #2063 crash during trim: it was corrected with #2065 issue, TestTrimLine02...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintDistance.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
6
7 #include <GeomAPI_Dir2d.h>
8 #include <GeomAPI_Lin2d.h>
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomAPI_XY.h>
11
12 #include <math.h>
13
14
15 void SketchSolver_ConstraintDistance::getAttributes(
16     EntityWrapperPtr& theValue,
17     std::vector<EntityWrapperPtr>& theAttributes)
18 {
19   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
20   if (!myErrorMsg.empty() || !theAttributes[0]) {
21     theAttributes.clear();
22     return;
23   }
24
25   if (theAttributes[1])
26     myType = CONSTRAINT_PT_PT_DISTANCE;
27   else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
28     myType = CONSTRAINT_PT_LINE_DISTANCE;
29   else
30     theAttributes.clear();
31
32   myPrevValue = 0.0;
33 }
34
35 void SketchSolver_ConstraintDistance::adjustConstraint()
36 {
37   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
38
39   // Adjust point-point distance if the points are equal
40   if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
41 ////    AttributePtr aPt1 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
42 ////    AttributePtr aPt2 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B());
43 ////
44 ////    BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
45 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint1 = aBuilder->point(myStorage->entity(aPt1));
46 ////    EntityWrapperPtr anEntity2 = myStorage->entity(aPt2);
47 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint2 = aBuilder->point(anEntity2);
48 ////
49 ////////    if (aPoint1->distance(aPoint2) < tolerance) {
50 ////////      // Change X coordinate of second point to eliminate coincidence
51 ////////      ParameterWrapperPtr aX = aSubs.back()->parameters().front();
52 ////////      aX->setValue(aX->value() + 1.0);
53 ////////      myStorage->update(aX);
54 ////////    }
55     return;
56   }
57
58   // Adjust point-line distance
59   if (fabs(myPrevValue) == fabs(aConstraint->value())) {
60     // sign of distance is not changed
61 ////    aConstraint->setValue(myPrevValue);
62 ////    myStorage->addConstraint(myBaseConstraint, aConstraint);
63     return;
64   }
65
66 ////  // Adjust the sign of constraint value
67 ////  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
68 ////
69 ////  std::shared_ptr<GeomAPI_Lin2d> aLine;
70 ////  std::shared_ptr<GeomAPI_Pnt2d> aPoint;
71 ////  for (int i = 0; i < 2; ++i) {
72 ////    AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
73 ////    EntityWrapperPtr anEntity = myStorage->entity(anAttr);
74 ////    if (anEntity->type() == ENTITY_POINT)
75 ////      aPoint = aBuilder->point(anEntity);
76 ////    else if (anEntity->type() == ENTITY_LINE)
77 ////      aLine = aBuilder->line(anEntity);
78 ////  }
79 ////
80 ////  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
81 ////  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
82 ////  if (aLineVec->cross(aPtLineVec) * aConstraint->value() < 0.0)
83 ////    aConstraint->setValue(aConstraint->value() * (-1.0));
84 }
85
86 void SketchSolver_ConstraintDistance::update()
87 {
88   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
89   myPrevValue = aConstraint->value();
90
91   SketchSolver_Constraint::update();
92 }