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