]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintDistance.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <SketchSolver_ConstraintDistance.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
23
24 #include <GeomAPI_Dir2d.h>
25 #include <GeomAPI_Lin2d.h>
26 #include <GeomAPI_Pnt2d.h>
27 #include <GeomAPI_XY.h>
28
29 #include <math.h>
30
31
32 void SketchSolver_ConstraintDistance::getAttributes(
33     EntityWrapperPtr& theValue,
34     std::vector<EntityWrapperPtr>& theAttributes)
35 {
36   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
37   if (!myErrorMsg.empty() || !theAttributes[0]) {
38     theAttributes.clear();
39     return;
40   }
41
42   if (theAttributes[1])
43     myType = CONSTRAINT_PT_PT_DISTANCE;
44   else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
45     myType = CONSTRAINT_PT_LINE_DISTANCE;
46   else
47     theAttributes.clear();
48
49   myPrevValue = 0.0;
50 }
51
52 void SketchSolver_ConstraintDistance::adjustConstraint()
53 {
54   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
55
56   // Adjust point-point distance if the points are equal
57   if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
58 ////    AttributePtr aPt1 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
59 ////    AttributePtr aPt2 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B());
60 ////
61 ////    BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
62 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint1 = aBuilder->point(myStorage->entity(aPt1));
63 ////    EntityWrapperPtr anEntity2 = myStorage->entity(aPt2);
64 ////    std::shared_ptr<GeomAPI_Pnt2d> aPoint2 = aBuilder->point(anEntity2);
65 ////
66 ////////    if (aPoint1->distance(aPoint2) < tolerance) {
67 ////////      // Change X coordinate of second point to eliminate coincidence
68 ////////      ParameterWrapperPtr aX = aSubs.back()->parameters().front();
69 ////////      aX->setValue(aX->value() + 1.0);
70 ////////      myStorage->update(aX);
71 ////////    }
72     return;
73   }
74
75   // Adjust point-line distance
76   if (fabs(myPrevValue) == fabs(aConstraint->value())) {
77     // sign of distance is not changed
78 ////    aConstraint->setValue(myPrevValue);
79 ////    myStorage->addConstraint(myBaseConstraint, aConstraint);
80     return;
81   }
82
83 ////  // Adjust the sign of constraint value
84 ////  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
85 ////
86 ////  std::shared_ptr<GeomAPI_Lin2d> aLine;
87 ////  std::shared_ptr<GeomAPI_Pnt2d> aPoint;
88 ////  for (int i = 0; i < 2; ++i) {
89 ////    AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
90 ////    EntityWrapperPtr anEntity = myStorage->entity(anAttr);
91 ////    if (anEntity->type() == ENTITY_POINT)
92 ////      aPoint = aBuilder->point(anEntity);
93 ////    else if (anEntity->type() == ENTITY_LINE)
94 ////      aLine = aBuilder->line(anEntity);
95 ////  }
96 ////
97 ////  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
98 ////  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
99 ////  if (aLineVec->cross(aPtLineVec) * aConstraint->value() < 0.0)
100 ////    aConstraint->setValue(aConstraint->value() * (-1.0));
101 }
102
103 void SketchSolver_ConstraintDistance::update()
104 {
105   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
106   myPrevValue = aConstraint->value();
107
108   SketchSolver_Constraint::update();
109 }