1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchSolver_ConstraintFixed.h>
22 #include <SketchSolver_Error.h>
24 #include <PlaneGCSSolver_ConstraintWrapper.h>
25 #include <PlaneGCSSolver_EdgeWrapper.h>
26 #include <PlaneGCSSolver_EntityDestroyer.h>
27 #include <PlaneGCSSolver_FeatureBuilder.h>
28 #include <PlaneGCSSolver_PointWrapper.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <SketchPlugin_Feature.h>
33 /// \brief Get list of parameters of current entity
34 static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
37 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
38 : SketchSolver_Constraint(theConstraint)
40 myType = CONSTRAINT_FIXED;
43 void SketchSolver_ConstraintFixed::blockEvents(bool isBlocked)
45 SketchSolver_Constraint::blockEvents(isBlocked);
48 void SketchSolver_ConstraintFixed::process()
51 if (!myBaseConstraint || !myStorage) {
52 // Not enough parameters are assigned
56 EntityWrapperPtr aBaseEntity = entityToFix();
58 myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
59 if (!myErrorMsg.empty())
62 ConstraintWrapperPtr aConstraint = fixFeature(aBaseEntity);
63 myStorage->addConstraint(myBaseConstraint, aConstraint);
66 ConstraintWrapperPtr SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
68 GCS::VEC_pD aParameters = toParameters(theFeature);
70 // Fix given list of parameters
71 std::list<GCSConstraintPtr> aConstraints;
72 myFixedValues.reserve(aParameters.size());
73 GCS::VEC_pD::const_iterator anIt = aParameters.begin();
74 for (int i = 0; anIt != aParameters.end(); ++anIt, ++i) {
75 myFixedValues.push_back(**anIt);
76 aConstraints.push_back(
77 GCSConstraintPtr(new GCS::ConstraintEqual(&myFixedValues[i], *anIt)));
80 return ConstraintWrapperPtr(
81 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
84 EntityWrapperPtr SketchSolver_ConstraintFixed::entityToFix()
86 // Constraint Fixed is added by user.
87 // Get the attribute of constraint (it should be alone in the list of constraints).
88 EntityWrapperPtr aValue;
89 std::vector<EntityWrapperPtr> anAttributes;
90 SketchSolver_Constraint::getAttributes(aValue, anAttributes);
91 std::vector<EntityWrapperPtr>::const_iterator anIt = anAttributes.begin();
92 for (; anIt != anAttributes.end(); ++anIt)
95 return EntityWrapperPtr();
101 // ================== Auxiliary functions ==================
102 GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
104 GCS::VEC_pD aParameters;
108 std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
109 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
111 // Collect parameters for each type of entity
112 switch (theEntity->type()) {
114 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
115 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
116 aParameters.push_back(aPoint->point()->x);
117 aParameters.push_back(aPoint->point()->y);
121 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
122 aParameters.push_back(aLine->p1.x);
123 aParameters.push_back(aLine->p1.y);
124 aParameters.push_back(aLine->p2.x);
125 aParameters.push_back(aLine->p2.y);
128 case ENTITY_CIRCLE: {
129 std::shared_ptr<GCS::Circle> aCircle =
130 std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
131 aParameters.push_back(aCircle->center.x);
132 aParameters.push_back(aCircle->center.y);
133 aParameters.push_back(aCircle->rad);
137 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
138 aParameters.push_back(anArc->center.x);
139 aParameters.push_back(anArc->center.y);
140 aParameters.push_back(anArc->rad);
141 aParameters.push_back(anArc->startAngle);
142 aParameters.push_back(anArc->endAngle);