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_ConstraintFixed.h>
21 #include <SketchSolver_Error.h>
23 #include <PlaneGCSSolver_ConstraintWrapper.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_EntityDestroyer.h>
26 #include <PlaneGCSSolver_FeatureBuilder.h>
27 #include <PlaneGCSSolver_PointWrapper.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <SketchPlugin_Feature.h>
32 /// \brief Get list of parameters of current entity
33 static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
36 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
37 : SketchSolver_Constraint(theConstraint)
39 myType = CONSTRAINT_FIXED;
42 void SketchSolver_ConstraintFixed::blockEvents(bool isBlocked)
44 SketchSolver_Constraint::blockEvents(isBlocked);
47 void SketchSolver_ConstraintFixed::process()
50 if (!myBaseConstraint || !myStorage) {
51 // Not enough parameters are assigned
55 EntityWrapperPtr aBaseEntity = entityToFix();
57 myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
58 if (!myErrorMsg.empty())
61 ConstraintWrapperPtr aConstraint = fixFeature(aBaseEntity);
62 myStorage->addConstraint(myBaseConstraint, aConstraint);
65 ConstraintWrapperPtr SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
67 GCS::VEC_pD aParameters = toParameters(theFeature);
69 // Fix given list of parameters
70 std::list<GCSConstraintPtr> aConstraints;
71 myFixedValues.reserve(aParameters.size());
72 GCS::VEC_pD::const_iterator anIt = aParameters.begin();
73 for (int i = 0; anIt != aParameters.end(); ++anIt, ++i) {
74 myFixedValues.push_back(**anIt);
75 aConstraints.push_back(
76 GCSConstraintPtr(new GCS::ConstraintEqual(&myFixedValues[i], *anIt)));
79 return ConstraintWrapperPtr(
80 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
83 EntityWrapperPtr SketchSolver_ConstraintFixed::entityToFix()
85 // Constraint Fixed is added by user.
86 // Get the attribute of constraint (it should be alone in the list of constraints).
87 EntityWrapperPtr aValue;
88 std::vector<EntityWrapperPtr> anAttributes;
89 SketchSolver_Constraint::getAttributes(aValue, anAttributes);
90 std::vector<EntityWrapperPtr>::const_iterator anIt = anAttributes.begin();
91 for (; anIt != anAttributes.end(); ++anIt)
94 return EntityWrapperPtr();
100 // ================== Auxiliary functions ==================
101 GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
103 GCS::VEC_pD aParameters;
107 std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
108 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
110 // Collect parameters for each type of entity
111 switch (theEntity->type()) {
113 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
114 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
115 aParameters.push_back(aPoint->point()->x);
116 aParameters.push_back(aPoint->point()->y);
120 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
121 aParameters.push_back(aLine->p1.x);
122 aParameters.push_back(aLine->p1.y);
123 aParameters.push_back(aLine->p2.x);
124 aParameters.push_back(aLine->p2.y);
127 case ENTITY_CIRCLE: {
128 std::shared_ptr<GCS::Circle> aCircle =
129 std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
130 aParameters.push_back(aCircle->center.x);
131 aParameters.push_back(aCircle->center.y);
132 aParameters.push_back(aCircle->rad);
136 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
137 aParameters.push_back(anArc->center.x);
138 aParameters.push_back(anArc->center.y);
139 aParameters.push_back(anArc->rad);
140 aParameters.push_back(anArc->startAngle);
141 aParameters.push_back(anArc->endAngle);