]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintGroup.h
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintGroup.h
1 // File:    SketchSolver_ConstraintGroup.h
2 // Created: 27 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #ifndef SketchSolver_ConstraintGroup_Headerfile
6 #define SketchSolver_ConstraintGroup_Headerfile
7
8 #include "SketchSolver.h"
9 #include <SketchSolver_Solver.h>
10
11 #include <SketchPlugin_Constraint.h>
12
13 #include <list>
14 #include <map>
15 #include <vector>
16 #include <set>
17
18
19 /** \class   SketchSolver_ConstraintGroup
20  *  \ingroup DataModel
21  *  \brief   Keeps the group of constraints which based on the same entities
22  */
23 class SketchSolver_ConstraintGroup
24 {
25 public:
26   /** \brief New group based on specified workplane.
27    *         Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
28    *  \remark Type of theSketch is not verified inside
29    */
30   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane);
31
32   ~SketchSolver_ConstraintGroup();
33
34   /// \brief Returns group's unique identifier
35   inline const Slvs_hGroup& getId() const
36   {return myID;}
37
38   /// \brief Returns true if the group has no constraints yet
39   inline bool isEmpty() const
40   {return myConstraints.empty();}
41
42   /** \brief Adds or updates a constraint in the group
43    *  \param[in] theConstraint constraint to be changed
44    *  \return \c true if the constraint added or updated successfully
45    */
46   bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
47
48   /** \brief Verifies the constraint uses the objects from this group
49    *  \param[in] theConstraint constraint for verification of interaction
50    *  \return \c true if the constrained objects are used in current group
51    */
52   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
53
54   /** \brief Verifies the specified feature is equal to the base workplane for this group
55    *  \param[in] theWorkplane the feature to be compared with base workplane
56    *  \return \c true if workplanes are the same
57    */
58   bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const;
59
60   boost::shared_ptr<SketchPlugin_Feature> getWorkplane() const
61   { return mySketch; }
62
63   /** \brief Update parameters of workplane. Should be called when Update event is coming.
64    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
65    */
66   bool updateWorkplane();
67
68   /** \brief If the entity is in this group it will updated
69    *  \param[in] theEntity attribute, which values should update SolveSpace entity
70    */
71   void updateEntityIfPossible(boost::shared_ptr<ModelAPI_Attribute> theEntity);
72
73   /** \brief Searches invalid features and constraints in the group and avoids them
74    *  \return \c true if the group's sketch is invalid and the group should be removed
75    */
76   bool updateGroup();
77
78   /** \brief Add specified group to this one
79    *  \param[in] theGroup group of constraint to be added
80    */
81   void mergeGroups(const SketchSolver_ConstraintGroup& theGroup);
82
83   /** \brief Start solution procedure if necessary and update attributes of features
84    */
85   void resolveConstraints();
86
87 protected:
88   /** \brief Adds or updates an entity in the group
89    *
90    *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
91    *  Parameters of certain entity will be placed sequentially in the list.
92    *
93    *  \param[in] theEntity the object of constraint
94    *  \return identifier of changed entity or 0 if entity could not be changed
95    */
96   Slvs_hEntity changeEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
97
98   /** \brief Adds or updates a normal in the group
99    *
100    *  Normal is a special entity in SolveSpace, which defines a direction in 3D and
101    *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
102    *
103    *  To define a normal there should be specified two coordinate axis
104    *  on the plane transversed to created normal.
105    *
106    *  \param[in] theDirX first coordinate axis of the plane
107    *  \param[in] theDirY second coordinate axis of the plane
108    *  \param[in] theNorm attribute for the normal (used to identify newly created entity)
109    *  \return identifier of created or updated normal
110    */
111   Slvs_hEntity changeNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX,
112                             boost::shared_ptr<ModelAPI_Attribute> theDirY,
113                             boost::shared_ptr<ModelAPI_Attribute> theNorm);
114
115   /** \brief Adds or updates a parameter in the group
116    *  \param[in] theParam   the value of parameter
117    *  \param[in] thePrmIter the cell in the list of parameters which should be changed
118    *                        (the iterator will be increased if it does not reach the end of the list)
119    *  \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
120    */
121   Slvs_hParam changeParameter(const double& theParam,
122                               std::vector<Slvs_Param>::const_iterator& thePrmIter);
123
124   /** \brief Change values of attribute by parameters received from SolveSpace solver
125    *  \param[in,out] theAttribute pointer to the attribute to be changed
126    *  \param[in]     theEntityID  identifier of SolveSpace entity, which contains updated data
127    */
128   void updateAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID);
129
130   /** \brief Adds a constraint for a point which should not be changed during computations
131    *  \param[in] theEntity the base for the constraint
132    */
133   void addTemporaryConstraintWhereDragged(boost::shared_ptr<ModelAPI_Attribute> theEntity);
134
135   /** \brief Remove all temporary constraint after computation finished
136    */
137   void removeTemporaryConstraints();
138
139 private:
140   /** \brief Creates a workplane from the sketch parameters
141    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
142    *  \return \c true if success, \c false if workplane parameters are not consistent
143    */
144   bool addWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch);
145
146 private:
147   // SolveSpace entities
148   Slvs_hGroup                  myID;            ///< the index of the group
149   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
150   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
151   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
152   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
153   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
154   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
155   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints (not equal to myConstraints size)
156   bool                         myNeedToSolve;   ///< Indicator that something changed in the group and constraint system need to be rebuilt
157
158   SketchSolver_Solver          myConstrSolver;  ///< Solver for set of equations obtained by constraints
159
160   std::vector<Slvs_hParam>     myTempPointWhereDragged; ///< Parameters of one of the points which is moved by user
161   Slvs_hEntity                 myTempPointWDrgdID;      ///< Identifier of such point
162   std::list<Slvs_hConstraint>  myTempConstraints;       ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
163                                                         //   NOTE: First ID in the list corresponds to myTempPointWhereDragged parameters and does not added to myConstraints list
164
165   std::vector< std::set<Slvs_hEntity> >
166                                myCoincidentPoints; ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
167
168   // SketchPlugin entities
169   boost::shared_ptr<SketchPlugin_Feature>
170                                mySketch;        ///< Equivalent to workplane
171   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>
172                                myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
173   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
174                                myEntityMap;     ///< The map between parameters of constraints and their equivalent SolveSpace entities
175 };
176
177 #endif