Salome HOME
Task 2.11. Ability to impose a midpoint on an arc (refers issue #3002)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMiddle.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include <SketchSolver_ConstraintMiddle.h>
21 #include <PlaneGCSSolver_ConstraintWrapper.h>
22 #include <PlaneGCSSolver_EdgeWrapper.h>
23 #include <PlaneGCSSolver_PointWrapper.h>
24 #include <PlaneGCSSolver_Storage.h>
25 #include <PlaneGCSSolver_Tools.h>
26 #include <PlaneGCSSolver_UpdateCoincidence.h>
27
28 void SketchSolver_ConstraintMiddle::getAttributes(
29     EntityWrapperPtr& theValue,
30     std::vector<EntityWrapperPtr>& theAttributes)
31 {
32   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
33
34   // create auxiliary point if middle point on arc is specified
35   if (theAttributes[2]->type() == ENTITY_ARC) {
36     std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
37         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
38
39     myOddPoint = GCSPointPtr(new GCS::Point);
40     myOddPoint->x = aStorage->createParameter();
41     myOddPoint->y = aStorage->createParameter();
42     theAttributes[1] = PointWrapperPtr(new PlaneGCSSolver_PointWrapper(myOddPoint));
43   }
44 }
45
46 bool SketchSolver_ConstraintMiddle::remove()
47 {
48   if (myOddPoint) {
49     std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
50         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
51
52     GCS::SET_pD aParams;
53     aParams.insert(myOddPoint->x);
54     aParams.insert(myOddPoint->y);
55     aStorage->removeParameters(aParams);
56   }
57   return SketchSolver_ConstraintCoincidence::remove();
58 }
59
60 void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
61                                            PlaneGCSSolver_Update* theUpdater)
62 {
63   if (theFeature == myBaseConstraint && myInSolver) {
64     // the constraint is already being updated,
65     // update the middle point parameter if the constraint is "point-on-arc".
66     if (myOddPoint) {
67       EntityWrapperPtr anArcEntity =
68           myAttributes.front()->type() == ENTITY_ARC ? myAttributes.front() : myAttributes.back();
69       EdgeWrapperPtr anArcEdge =
70           std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(anArcEntity);
71       std::shared_ptr<GCS::Arc> anArc;
72       if (anArcEdge)
73         anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEdge->entity());
74       if (anArc) {
75         // recalculate parameters of middle point according to arc
76         *myOddPoint->x = (*anArc->startAngle + *anArc->endAngle) * 0.5;
77         *myOddPoint->y = (*anArc->endAngle - *anArc->startAngle) * 0.5;
78       }
79     }
80     return;
81   }
82
83   PlaneGCSSolver_UpdateCoincidence* anUpdater =
84       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
85   bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
86   if (isAccepted) {
87     if (!myInSolver) {
88       myInSolver = true;
89
90       if (myMiddle) {
91         // remove previously adde constraint
92         myStorage->removeConstraint(myBaseConstraint);
93         // merge divided constraints into single object
94         std::list<GCSConstraintPtr> aGCSConstraints = myMiddle->constraints();
95         aGCSConstraints.push_front(mySolverConstraint->constraints().front());
96
97         myMiddle = ConstraintWrapperPtr();
98         mySolverConstraint = ConstraintWrapperPtr(
99             new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints, CONSTRAINT_MIDDLE_POINT));
100       }
101
102       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
103     }
104   } else {
105     if (myInSolver) {
106       myStorage->removeConstraint(myBaseConstraint);
107       myInSolver = false;
108     }
109
110     if (!myMiddle) {
111       // divide solver constraints to the middle point and point-line coincidence
112       std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
113           std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(mySolverConstraint);
114       std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
115
116       mySolverConstraint = ConstraintWrapperPtr(
117         new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.front(), CONSTRAINT_MIDDLE_POINT));
118       aGCSConstraints.pop_front();
119       myMiddle = ConstraintWrapperPtr(
120           new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints, CONSTRAINT_MIDDLE_POINT));
121
122       // send middle constraint only
123       myStorage->addConstraint(myBaseConstraint, myMiddle);
124     }
125   }
126 }