Salome HOME
Key_Escape processing in File selector widget. Otherwise, crash happens if file dialo...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintFixed.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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_ConstraintFixed.h>
22 #include <SketchSolver_Error.h>
23
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>
29
30 #include <GeomDataAPI_Point2D.h>
31 #include <SketchPlugin_Feature.h>
32
33 /// \brief Get list of parameters of current entity
34 static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
35
36
37 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
38   : SketchSolver_Constraint(theConstraint)
39 {
40   myType = CONSTRAINT_FIXED;
41 }
42
43 void SketchSolver_ConstraintFixed::blockEvents(bool isBlocked)
44 {
45   SketchSolver_Constraint::blockEvents(isBlocked);
46 }
47
48 void SketchSolver_ConstraintFixed::process()
49 {
50   cleanErrorMsg();
51   if (!myBaseConstraint || !myStorage) {
52     // Not enough parameters are assigned
53     return;
54   }
55
56   EntityWrapperPtr aBaseEntity = entityToFix();
57   if (!aBaseEntity)
58     myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
59   if (!myErrorMsg.empty())
60     return;
61
62   ConstraintWrapperPtr aConstraint = fixFeature(aBaseEntity);
63   myStorage->addConstraint(myBaseConstraint, aConstraint);
64 }
65
66 ConstraintWrapperPtr SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
67 {
68   GCS::VEC_pD aParameters = toParameters(theFeature);
69
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)));
78   }
79
80   return ConstraintWrapperPtr(
81       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
82 }
83
84 EntityWrapperPtr SketchSolver_ConstraintFixed::entityToFix()
85 {
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)
93     if (*anIt)
94       return *anIt;
95   return EntityWrapperPtr();
96 }
97
98
99
100
101 // ==================     Auxiliary functions     ==================
102 GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
103 {
104   GCS::VEC_pD aParameters;
105   if (!theEntity)
106     return aParameters;
107
108   std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
109       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
110
111   // Collect parameters for each type of entity
112   switch (theEntity->type()) {
113   case ENTITY_POINT: {
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);
118     break;
119     }
120   case ENTITY_LINE: {
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);
126     break;
127     }
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);
134     break;
135     }
136   case ENTITY_ARC: {
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);
143     break;
144     }
145   default:
146     break;
147   }
148
149   return aParameters;
150 }