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