]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.h
Salome HOME
Implemented methods for adding constraints
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.h
1 // File:    SketchSolver_ConstraintManager.h
2 // Created: 08 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #ifndef SketchSolver_ConstraintManager_Headerfile
6 #define SketchSolver_ConstraintManager_Headerfile
7
8 #include "SketchSolver.h"
9
10 #include <Events_Listener.h>
11 #include <SketchPlugin_Constraint.h>
12
13 // Need to be defined before including SolveSpace to avoid additional dependances on Windows platform
14 #if defined(WIN32) && !defined(HAVE_C99_INTEGER_TYPES)
15 typedef unsigned int UINT32;
16 #endif
17 #include <string.h>
18 #include <slvs.h>
19
20 #include <map>
21 #include <vector>
22
23
24 // Unknown constraint (for error reporting)
25 #define SLVS_C_UNKNOWN 0
26
27 /** \class   SketchSolver_ConstraintManager
28  *  \ingroup DataModel
29  *  \brief   Listens the changes of SketchPlugin features and transforms the Constraint 
30  *           feature into the format understandable by SolveSpace library.
31  *
32  *  Constraints created for SolveSpace library are divided into the groups.
33  *  The division order based on connectedness of the features by the constraints.
34  *  The groups may be fused or separated according to the new constraints.
35  *
36  *  \remark This is a singleton.
37  */
38 class SketchSolver_ConstraintManager : public Events_Listener
39 {
40 public:
41   /** \brief Main method to create constraint manager
42    *  \return pointer to the singleton
43    */
44   static SketchSolver_ConstraintManager* Instance();
45
46   /** \brief Implementation of Event Listener method
47    *  \param[in] theMessage the data of the event
48    */
49   virtual void processEvent(const Events_Message* theMessage);
50
51 protected:
52   SketchSolver_ConstraintManager();
53   ~SketchSolver_ConstraintManager();
54
55   /** \brief Adds a constraint into the manager
56    *  \param[in] theConstraint constraint to be added
57    *  \return \c true if the constraint added successfully
58    */
59   bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
60
61   /** \brief Removes a constraint from the manager
62    *  \param[in] theConstraint constraint to be removed
63    *  \return \c true if the constraint removed successfully
64    */
65   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
66
67   /** \brief Updates a constraint
68    *  \param[in] theConstraint constraint to be updated
69    *  \return \c true if the constraint was updated
70    */
71   bool updateConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
72
73   /** \brief Adds a workplane into the manager
74    *  \param[in] theSketch the feature to create workplane
75    *  \return \c true if the workplane added successfully
76    */
77   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
78
79   /** \brief Removes a workplane from the manager. 
80    *         All groups based on such workplane will be removed too.
81    *  \param[in] theSketch the feature to be removed
82    *  \return \c true if the workplane removed successfully
83    */
84   bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
85
86   /** \brief Updates a workplane
87    *  \param[in] theSketch workplane to be updated
88    *  \return \c true if the workplane was updated
89    */
90   bool updateWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
91
92   /** \brief Updates entity which is neither workplane nor constraint
93    *  \param[in] theFeature entity to be updated
94    *  \return \c true if the entity updated successfully
95    */
96   bool updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
97
98 private:
99   class SketchSolver_ConstraintGroup;
100
101   /** \brief Searches list of groups which interact with specified constraint
102    *  \param[in]  theConstraint constraint to be found
103    *  \param[out] theGroups     list of group indexes interacted with constraint
104    */
105   void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
106                   std::vector<Slvs_hGroup>&                  theGroupIDs) const;
107
108   /** \brief Searches in the list of groups the workplane which constains specified constraint
109    *  \param[in] theConstraint constraint to be found
110    *  \return workplane contains the constraint
111    */
112   boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
113                   boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
114
115 private:
116   static SketchSolver_ConstraintManager*    _self;    ///< Self pointer to implement singleton functionality
117   std::vector<SketchSolver_ConstraintGroup> myGroups; ///< Groups of constraints
118 };
119
120
121 /** \class   SketchSolver_ConstraintGroup
122  *  \ingroup DataModel
123  *  \brief   Keeps the group of constraints which based on the same entities
124  */
125 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
126 {
127 public:
128   /** \brief New group based on specified workplane
129    */
130   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
131
132   ~SketchSolver_ConstraintGroup();
133
134   /// \brief Returns group's unique identifier
135   const Slvs_hGroup& getId() const
136   {return myID;}
137
138   /** \brief Adds a constraint into the group
139    *  \param[in] theConstraint constraint to be added
140    *  \return \c true if the constraint added successfully
141    */
142   bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
143
144   /** \brief Removes a constraint into the group
145    *  \param[in] theConstraint constraint to be removed
146    *  \return \c true if the constraint removed successfully
147    */
148   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
149
150   /** \brief Verifies the constraint uses the objects from this group
151    *  \param[in] theConstraint constraint for verification of interaction
152    *  \return \c true if the constrained objects are used in current group
153    */
154   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
155
156   /** \brief Verifies the specified workplane is the same as a base workplane for this group
157    *  \param[in] theWorkplane workplane to be compared
158    *  \return \c true if workplanes are the same
159    */
160   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
161
162   boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
163   { return mySketch; }
164
165 protected:
166   /** \brief Adds an entity into the group
167    *
168    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
169    *  Parameters of certain entity will be placed sequentially in the list.
170    *
171    *  \param[in] theEntity the object of constraint
172    *  \return identifier of created entity or 0 if entity was not added
173    */
174   Slvs_hEntity addEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
175
176   /** \brief Adds a normal into the group
177    *
178    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and 
179    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
180    *
181    *  To define a normal there should be specified two coordinate axis 
182    *  on the plane transversed to created normal.
183    *
184    *  \param[in] theDirX first coordinate axis of the plane
185    *  \param[in] theDirY second coordinate axis of the plane
186    *  \return identifier of created normal
187    */
188   Slvs_hEntity addNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX, 
189                          boost::shared_ptr<ModelAPI_Attribute> theDirY);
190
191   /** \brief Adds a parameter into the group
192    *  \param[in] theParam parameter to be added
193    *  \return identifier of created parameter or 0 if it was not added
194    */
195   Slvs_hParam addParameter(double theParam);
196
197   /** \brief Compute constraint type according to SolveSpace identifiers
198    *         and verify that constraint parameters are correct
199    *  \param[in] theConstraint constraint which type should be determined
200    *  \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
201    */
202   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
203
204 private:
205   /** \brief Creates a workplane from the sketch parameters
206    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
207    *  \return \c true if success
208    */
209   bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
210
211 private:
212   // SolveSpace entities
213   Slvs_hGroup                  myID;            ///< the index of the group
214   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
215   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
216   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
217   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
218   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
219   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
220   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
221   Slvs_System                  myConstrSet;     ///< SolveSpace's set of equations obtained by constraints
222
223   // SketchPlugin entities
224   boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
225   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint> 
226                                myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
227 };
228
229 #endif