Salome HOME
3640d881dc89eb1d012caa6b278ba919f78cbbae
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Constraint.h
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Constraint_H_
8 #define SketchSolver_Constraint_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Storage.h>
12
13 #include <SketchPlugin_Constraint.h>
14
15 #include <ModelAPI_AttributeRefAttr.h>
16
17 #include <string>
18 #include <vector>
19
20 class SketchSolver_Group;
21
22 /** \class   SketchSolver_Constraint
23  *  \ingroup Plugins
24  *  \brief   Stores mapping between SketchPlugin and SolveSpace constraints data
25  */
26 class SketchSolver_Constraint
27 {
28 protected:
29   /// Default constructor
30   SketchSolver_Constraint() {}
31   SketchSolver_Constraint(ConstraintPtr theConstraint);
32
33 public:
34   virtual ~SketchSolver_Constraint();
35
36   /// \brief Initializes the storage of SolveSpace constraints
37   void setStorage(StoragePtr theStorage);
38   /// \brief Initializes group ID for this constraint
39   void setGroup(SketchSolver_Group* theGroup);
40
41   /// \brief Update constraint
42   virtual void update(ConstraintPtr theConstraint = ConstraintPtr());
43
44   /// \brief Tries to remove constraint
45   /// \return \c false, if current constraint contains another SketchPlugin constraints (like for multiple coincidence)
46   virtual bool remove(ConstraintPtr theConstraint = ConstraintPtr());
47
48   /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities
49   void refresh();
50
51   /// \brief Returns the type of constraint
52   virtual int getType() const = 0;
53
54   /// \brief Checks the constraint is used by current object
55   virtual bool hasConstraint(ConstraintPtr theConstraint) const
56   { return theConstraint == myBaseConstraint; }
57
58   /// \brief Return list of SketchPlugin constraints attached to this object
59   virtual std::list<ConstraintPtr> constraints() const;
60
61   /// \brief Return identifier of SolveSpace entity relating to the feature
62   Slvs_hEntity getId(FeaturePtr theFeature) const;
63   /// \brief Return identifier of SolveSpace entity relating to the attribute
64   Slvs_hEntity getId(AttributePtr theAttribute) const;
65
66   /// \brief Shows error message
67   const std::string& error() const
68   { return myErrorMsg; }
69
70 protected:
71   /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
72   virtual void process();
73
74   /// \brief Generate list of attributes of constraint in order useful for SolveSpace constraints
75   /// \param[out] theValue      numerical characteristic of constraint (e.g. distance)
76   /// \param[out] theAttributes list of attributes to be filled
77   virtual void getAttributes(double& theValue, std::vector<Slvs_hEntity>& theAttributes);
78
79   /// \brief This method is used in derived objects to check consistence of constraint.
80   ///        E.g. the distance between line and point may be signed.
81   virtual void adjustConstraint()
82   {}
83
84   /// \brief Create or change SlveSpace entity according to the given attribute
85   /// \param[in]  theAttribute  reference to the entity to be changed
86   /// \param[out] theType       type of created entity
87   /// \return identifier of created/updated entity
88   Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
89   /// \brief Create or change SlveSpace entity according to the given attribute
90   Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
91   /// \brief Create or change SlveSpace entity according to the given feature
92   Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
93
94   /// \brief Removes the links to unused entities
95   void cleanRemovedEntities();
96
97   /// \brief Removes last error
98   void cleanErrorMsg()
99   { myErrorMsg.clear(); }
100
101 protected:
102   SketchSolver_Group* myGroup; ///< the group which contains current constraint
103   ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
104   std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
105   std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
106   std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
107   std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
108   StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
109
110   std::string myErrorMsg; ///< error message
111 };
112
113 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
114
115
116
117 /** \class   SketchSolver_ConstraintParallel
118  *  \ingroup Plugins
119  *  \brief   Convert Parallel constraint to SolveSpace structure
120  */
121 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
122 {
123 public:
124   SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
125       SketchSolver_Constraint(theConstraint)
126   {}
127
128   virtual int getType() const
129   { return SLVS_C_PARALLEL; }
130 };
131
132
133 /** \class   SketchSolver_ConstraintPerpendicular
134  *  \ingroup Plugins
135  *  \brief   Convert Perpendicular constraint to SolveSpace structure
136  */
137 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
138 {
139 public:
140   SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
141       SketchSolver_Constraint(theConstraint)
142   {}
143
144   virtual int getType() const
145   { return SLVS_C_PERPENDICULAR; }
146 };
147
148
149 /** \class   SketchSolver_ConstraintHorizontal
150  *  \ingroup Plugins
151  *  \brief   Convert Horizontal constraint to SolveSpace structure
152  */
153 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
154 {
155 public:
156   SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
157       SketchSolver_Constraint(theConstraint)
158   {}
159
160   virtual int getType() const
161   { return SLVS_C_HORIZONTAL; }
162 };
163
164
165 /** \class   SketchSolver_ConstraintVertical
166  *  \ingroup Plugins
167  *  \brief   Convert Vertical constraint to SolveSpace structure
168  */
169 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
170 {
171 public:
172   SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
173       SketchSolver_Constraint(theConstraint)
174   {}
175
176   virtual int getType() const
177   { return SLVS_C_VERTICAL; }
178 };
179
180
181 /** \class   SketchSolver_ConstraintRadius
182  *  \ingroup Plugins
183  *  \brief   Convert Radius constraint to SolveSpace structure
184  */
185 class SketchSolver_ConstraintRadius : public SketchSolver_Constraint
186 {
187 public:
188   SketchSolver_ConstraintRadius(ConstraintPtr theConstraint) :
189       SketchSolver_Constraint(theConstraint)
190   {}
191
192   virtual int getType() const
193   { return SLVS_C_DIAMETER; }
194
195   virtual void adjustConstraint()
196   {
197     Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
198     aConstraint.valA *= 2.0;
199     myStorage->updateConstraint(aConstraint);
200   }
201 };
202
203 #endif