Salome HOME
901053c6d7a90d70d09cefaad4169ce0efea0f39
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_ConstraintDistance.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
24
25 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
26 #include <SketchPlugin_ConstraintDistanceVertical.h>
27
28 #include <GeomAPI_Dir2d.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_XY.h>
32
33 #include <math.h>
34
35
36 void SketchSolver_ConstraintDistance::getAttributes(
37     EntityWrapperPtr& theValue,
38     std::vector<EntityWrapperPtr>& theAttributes)
39 {
40   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
41   if (!myErrorMsg.empty() || !theAttributes[0]) {
42     theAttributes.clear();
43     return;
44   }
45
46   if (theAttributes[1]) {
47     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
48       myType = CONSTRAINT_HORIZONTAL_DISTANCE;
49     else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
50       myType = CONSTRAINT_VERTICAL_DISTANCE;
51     else
52       myType = CONSTRAINT_PT_PT_DISTANCE;
53   } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
54     myType = CONSTRAINT_PT_LINE_DISTANCE;
55   else
56     theAttributes.clear();
57
58   myPrevValue = 0.0;
59 }
60
61 void SketchSolver_ConstraintDistance::adjustConstraint()
62 {
63   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
64
65   // Adjust point-point distance if the points are equal
66   if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
67 ////    AttributePtr aPt1 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
68 ////    AttributePtr aPt2 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B());
69 ////
70 ////    BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
71 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint1 = aBuilder->point(myStorage->entity(aPt1));
72 ////    EntityWrapperPtr anEntity2 = myStorage->entity(aPt2);
73 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint2 = aBuilder->point(anEntity2);
74 ////
75 ////////    if (aPoint1->distance(aPoint2) < tolerance) {
76 ////////      // Change X coordinate of second point to eliminate coincidence
77 ////////      ParameterWrapperPtr aX = aSubs.back()->parameters().front();
78 ////////      aX->setValue(aX->value() + 1.0);
79 ////////      myStorage->update(aX);
80 ////////    }
81     return;
82   }
83
84   // Adjust point-line distance
85   if (fabs(myPrevValue) == fabs(aConstraint->value())) {
86     // sign of distance is not changed
87 ////    aConstraint->setValue(myPrevValue);
88 ////    myStorage->addConstraint(myBaseConstraint, aConstraint);
89     return;
90   }
91
92 ////  // Adjust the sign of constraint value
93 ////  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
94 ////
95 ////  std::shared_ptr<GeomAPI_Lin2d> aLine;
96 ////  std::shared_ptr<GeomAPI_Pnt2d> aPoint;
97 ////  for (int i = 0; i < 2; ++i) {
98 ////    AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
99 ////    EntityWrapperPtr anEntity = myStorage->entity(anAttr);
100 ////    if (anEntity->type() == ENTITY_POINT)
101 ////      aPoint = aBuilder->point(anEntity);
102 ////    else if (anEntity->type() == ENTITY_LINE)
103 ////      aLine = aBuilder->line(anEntity);
104 ////  }
105 ////
106 ////  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
107 ////  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
108 ////  if (aLineVec->cross(aPtLineVec) * aConstraint->value() < 0.0)
109 ////    aConstraint->setValue(aConstraint->value() * (-1.0));
110 }
111
112 void SketchSolver_ConstraintDistance::update()
113 {
114   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
115   myPrevValue = aConstraint->value();
116
117   SketchSolver_Constraint::update();
118 }