]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintFixed.cpp
Salome HOME
Task 2.4. Ability to modify the radius of circles and arcs of circle with the mouse
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintFixed.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintFixed.h>
4 #include <SketchSolver_Error.h>
5
6 #include <PlaneGCSSolver_ConstraintWrapper.h>
7 #include <PlaneGCSSolver_EdgeWrapper.h>
8 #include <PlaneGCSSolver_EntityDestroyer.h>
9 #include <PlaneGCSSolver_FeatureBuilder.h>
10 #include <PlaneGCSSolver_PointWrapper.h>
11
12 #include <GeomDataAPI_Point2D.h>
13 #include <SketchPlugin_Feature.h>
14
15 /// \brief Get list of parameters of current entity
16 static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
17
18
19 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
20   : SketchSolver_Constraint(theConstraint)
21 {
22   myType = CONSTRAINT_FIXED;
23 }
24
25 void SketchSolver_ConstraintFixed::blockEvents(bool isBlocked)
26 {
27   SketchSolver_Constraint::blockEvents(isBlocked);
28 }
29
30 void SketchSolver_ConstraintFixed::process()
31 {
32   cleanErrorMsg();
33   if (!myBaseConstraint || !myStorage) {
34     // Not enough parameters are assigned
35     return;
36   }
37
38   EntityWrapperPtr aBaseEntity = entityToFix();
39   if (!aBaseEntity)
40     myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
41   if (!myErrorMsg.empty())
42     return;
43
44   ConstraintWrapperPtr aConstraint = fixFeature(aBaseEntity);
45   myStorage->addConstraint(myBaseConstraint, aConstraint);
46 }
47
48 ConstraintWrapperPtr SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
49 {
50   GCS::VEC_pD aParameters = toParameters(theFeature);
51
52   // Fix given list of parameters
53   std::list<GCSConstraintPtr> aConstraints;
54   myFixedValues.reserve(aParameters.size());
55   GCS::VEC_pD::const_iterator anIt = aParameters.begin();
56   for (int i = 0; anIt != aParameters.end(); ++anIt, ++i) {
57     myFixedValues.push_back(**anIt);
58     aConstraints.push_back(
59         GCSConstraintPtr(new GCS::ConstraintEqual(&myFixedValues[i], *anIt)));
60   }
61
62   return ConstraintWrapperPtr(
63       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
64 }
65
66 EntityWrapperPtr SketchSolver_ConstraintFixed::entityToFix()
67 {
68   // Constraint Fixed is added by user.
69   // Get the attribute of constraint (it should be alone in the list of constraints).
70   EntityWrapperPtr aValue;
71   std::vector<EntityWrapperPtr> anAttributes;
72   SketchSolver_Constraint::getAttributes(aValue, anAttributes);
73   std::vector<EntityWrapperPtr>::const_iterator anIt = anAttributes.begin();
74   for (; anIt != anAttributes.end(); ++anIt)
75     if (*anIt)
76       return *anIt;
77   return EntityWrapperPtr();
78 }
79
80
81
82
83 // ==================     Auxiliary functions     ==================
84 GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
85 {
86   GCS::VEC_pD aParameters;
87   if (!theEntity)
88     return aParameters;
89
90   std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
91       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
92
93   // Collect parameters for each type of entity
94   switch (theEntity->type()) {
95   case ENTITY_POINT: {
96     std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
97         std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
98     aParameters.push_back(aPoint->point()->x);
99     aParameters.push_back(aPoint->point()->y);
100     break;
101     }
102   case ENTITY_LINE: {
103     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
104     aParameters.push_back(aLine->p1.x);
105     aParameters.push_back(aLine->p1.y);
106     aParameters.push_back(aLine->p2.x);
107     aParameters.push_back(aLine->p2.y);
108     break;
109     }
110   case ENTITY_CIRCLE: {
111     std::shared_ptr<GCS::Circle> aCircle =
112         std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
113     aParameters.push_back(aCircle->center.x);
114     aParameters.push_back(aCircle->center.y);
115     aParameters.push_back(aCircle->rad);
116     break;
117     }
118   case ENTITY_ARC: {
119     std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
120     aParameters.push_back(anArc->center.x);
121     aParameters.push_back(anArc->center.y);
122     aParameters.push_back(anArc->rad);
123     aParameters.push_back(anArc->startAngle);
124     aParameters.push_back(anArc->endAngle);
125     break;
126     }
127   default:
128     break;
129   }
130
131   return aParameters;
132 }