1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
28 static bool isArc(EntityWrapperPtr theEntity)
30 return theEntity->type() == ENTITY_ARC || theEntity->type() == ENTITY_ELLIPTIC_ARC;
33 void SketchSolver_ConstraintMiddle::getAttributes(
34 EntityWrapperPtr& theValue,
35 std::vector<EntityWrapperPtr>& theAttributes)
37 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
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);
44 myOddPoint = GCSPointPtr(new GCS::Point);
45 myOddPoint->x = aStorage->createParameter();
46 myOddPoint->y = aStorage->createParameter();
47 theAttributes[1] = PointWrapperPtr(new PlaneGCSSolver_PointWrapper(myOddPoint));
51 bool SketchSolver_ConstraintMiddle::remove()
54 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
55 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
58 aParams.insert(myOddPoint->x);
59 aParams.insert(myOddPoint->y);
60 aStorage->removeParameters(aParams);
62 return SketchSolver_ConstraintCoincidence::remove();
65 void SketchSolver_ConstraintMiddle::notify(const FeaturePtr& theFeature,
66 PlaneGCSSolver_Update* theUpdater)
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".
72 EntityWrapperPtr anArcEntity =
73 isArc(myAttributes.front()) ? myAttributes.front() : myAttributes.back();
74 EdgeWrapperPtr anArcEdge =
75 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(anArcEntity);
77 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEdge->entity());
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;
84 std::shared_ptr<GCS::ArcOfEllipse> aEllArc =
85 std::dynamic_pointer_cast<GCS::ArcOfEllipse>(anArcEdge->entity());
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;
97 PlaneGCSSolver_UpdateCoincidence* anUpdater =
98 static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
99 bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
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());
111 myMiddle = ConstraintWrapperPtr();
112 mySolverConstraint = ConstraintWrapperPtr(
113 new PlaneGCSSolver_ConstraintWrapper(aGCSConstraints, CONSTRAINT_MIDDLE_POINT));
116 myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
120 myStorage->removeConstraint(myBaseConstraint);
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();
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));
136 // send middle constraint only
137 myStorage->addConstraint(myBaseConstraint, myMiddle);