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