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