Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 The constraint is made temoparary
55   void makeTemporary() const;
56
57   /// \brief Checks the constraint is used by current object
58   virtual bool hasConstraint(ConstraintPtr theConstraint) const
59   { return theConstraint == myBaseConstraint; }
60
61   /// \brief Return list of SketchPlugin constraints attached to this object
62   virtual std::list<ConstraintPtr> constraints() const;
63
64   /// \brief Return identifier of SolveSpace entity relating to the feature
65   Slvs_hEntity getId(FeaturePtr theFeature) const;
66   /// \brief Return identifier of SolveSpace entity relating to the attribute
67   Slvs_hEntity getId(AttributePtr theAttribute) const;
68
69   /// \brief Adds a feature to constraint and create its analogue in SolveSpace
70   void addFeature(FeaturePtr theFeature);
71
72   /// \brief Shows error message
73   const std::string& error() const
74   { return myErrorMsg; }
75
76 protected:
77   /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
78   virtual void process();
79
80   /// \brief Generate list of attributes of constraint in order useful for SolveSpace constraints
81   /// \param[out] theValue      numerical characteristic of constraint (e.g. distance)
82   /// \param[out] theAttributes list of attributes to be filled
83   virtual void getAttributes(double& theValue, std::vector<Slvs_hEntity>& theAttributes);
84
85   /// \brief This method is used in derived objects to check consistence of constraint.
86   ///        E.g. the distance between line and point may be signed.
87   virtual void adjustConstraint()
88   {}
89
90   /// \brief Create or change SlveSpace entity according to the given attribute
91   /// \param[in]  theAttribute  reference to the entity to be changed
92   /// \param[out] theType       type of created entity
93   /// \return identifier of created/updated entity
94   Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
95   /// \brief Create or change SlveSpace entity according to the given attribute
96   Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
97   /// \brief Create or change SlveSpace entity according to the given feature
98   Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
99
100   /// \brief Calculate middle point on the specified entity
101   /// \param[in]  theEntity  arc or line
102   /// \param[in]  theCoeff   is a value in [0.0, 1.0] which shows the position of the point on the entity (0.0 - start point, 1.0 - end point)
103   /// \param[out] theX       X coordinate of middle point
104   /// \param[out] theY       Y coordinate of middle point
105   void calculateMiddlePoint(const Slvs_Entity& theEntity, double theCoeff,
106                             double& theX, double& theY) const;
107
108   /// \brief Removes the links to unused entities
109   void cleanRemovedEntities();
110
111   /// \brief Removes last error
112   void cleanErrorMsg()
113   { myErrorMsg.clear(); }
114
115 private:
116   /// \brief Sets error, if the attribute is not initialized
117   bool isInitialized(AttributePtr theAttribute);
118
119 protected:
120   SketchSolver_Group* myGroup; ///< the group which contains current constraint
121   ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
122   std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
123   std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
124   std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
125   std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
126   StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
127
128   std::string myErrorMsg; ///< error message
129 };
130
131 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
132
133
134
135 /** \class   SketchSolver_ConstraintParallel
136  *  \ingroup Plugins
137  *  \brief   Convert Parallel constraint to SolveSpace structure
138  */
139 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
140 {
141 public:
142   SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
143       SketchSolver_Constraint(theConstraint)
144   {}
145
146   virtual int getType() const
147   { return SLVS_C_PARALLEL; }
148 };
149
150
151 /** \class   SketchSolver_ConstraintPerpendicular
152  *  \ingroup Plugins
153  *  \brief   Convert Perpendicular constraint to SolveSpace structure
154  */
155 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
156 {
157 public:
158   SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
159       SketchSolver_Constraint(theConstraint)
160   {}
161
162   virtual int getType() const
163   { return SLVS_C_PERPENDICULAR; }
164 };
165
166
167 /** \class   SketchSolver_ConstraintHorizontal
168  *  \ingroup Plugins
169  *  \brief   Convert Horizontal constraint to SolveSpace structure
170  */
171 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
172 {
173 public:
174   SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
175       SketchSolver_Constraint(theConstraint)
176   {}
177
178   virtual int getType() const
179   { return SLVS_C_HORIZONTAL; }
180 };
181
182
183 /** \class   SketchSolver_ConstraintVertical
184  *  \ingroup Plugins
185  *  \brief   Convert Vertical constraint to SolveSpace structure
186  */
187 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
188 {
189 public:
190   SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
191       SketchSolver_Constraint(theConstraint)
192   {}
193
194   virtual int getType() const
195   { return SLVS_C_VERTICAL; }
196 };
197
198
199 /** \class   SketchSolver_ConstraintRadius
200  *  \ingroup Plugins
201  *  \brief   Convert Radius constraint to SolveSpace structure
202  */
203 class SketchSolver_ConstraintRadius : public SketchSolver_Constraint
204 {
205 public:
206   SketchSolver_ConstraintRadius(ConstraintPtr theConstraint) :
207       SketchSolver_Constraint(theConstraint)
208   {}
209
210   virtual int getType() const
211   { return SLVS_C_DIAMETER; }
212
213   virtual void adjustConstraint()
214   {
215     Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
216     aConstraint.valA *= 2.0;
217     myStorage->updateConstraint(aConstraint);
218   }
219 };
220
221 #endif