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