Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMiddle.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_ConstraintMiddle.h>
21 #include <PlaneGCSSolver_ConstraintWrapper.h>
22 #include <PlaneGCSSolver_UpdateCoincidence.h>
23
24 void SketchSolver_ConstraintMiddle::getAttributes(
25     EntityWrapperPtr& theValue,
26     std::vector<EntityWrapperPtr>& theAttributes)
27 {
28   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
29 }
30
31 void SketchSolver_ConstraintMiddle::notify(const FeaturePtr&      theFeature,
32                                            PlaneGCSSolver_Update* theUpdater)
33 {
34   if (theFeature == myBaseConstraint && myInSolver)
35     return; // the constraint is already being updated
36
37   PlaneGCSSolver_UpdateCoincidence* anUpdater =
38       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
39   bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
40   if (isAccepted) {
41     if (!myInSolver) {
42       myInSolver = true;
43
44       if (myMiddle) {
45         // remove previously adde constraint
46         myStorage->removeConstraint(myBaseConstraint);
47         // merge divided constraints into single object
48         std::list<GCSConstraintPtr> aGCSConstraints;
49         std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
50             std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(myMiddle);
51         aGCSConstraints.push_back(aConstraint->constraints().front());
52         aConstraint =
53             std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(mySolverConstraint);
54         aGCSConstraints.push_back(aConstraint->constraints().front());
55
56         myMiddle = ConstraintWrapperPtr();
57         mySolverConstraint = ConstraintWrapperPtr(
58             new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints, CONSTRAINT_MIDDLE_POINT));
59       }
60
61       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
62     }
63   } else {
64     if (myInSolver) {
65       myStorage->removeConstraint(myBaseConstraint);
66       myInSolver = false;
67     }
68
69     if (!myMiddle) {
70       // divide solver constraints to the middle point and point-line coincidence
71       std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
72           std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(mySolverConstraint);
73       std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
74
75       myMiddle = ConstraintWrapperPtr(
76           new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.front(), CONSTRAINT_MIDDLE_POINT));
77       mySolverConstraint = ConstraintWrapperPtr(
78           new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints.back(), CONSTRAINT_MIDDLE_POINT));
79
80       // send middle constraint only
81       myStorage->addConstraint(myBaseConstraint, myMiddle);
82     }
83   }
84 }