Salome HOME
Base class for constraints was updated. Constraint manager functionality was started.
[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 <SketchPlugin_Constraint.h>
11
12 // Need to be defined before including SolveSpace to avoid additional dependances on Windows platform
13 #if defined(WIN32) && !defined(HAVE_C99_INTEGER_TYPES)
14 typedef unsigned int UINT32;
15 #endif
16 #include <string.h>
17 #include <slvs.h>
18
19 #include <map>
20 #include <vector>
21
22
23 // Unknown constraint (for error reporting)
24 #define SLVS_C_UNKNOWN 0
25
26 /** \class   SketchSolver_ConstraintManager
27  *  \ingroup DataModel
28  *  \brief   Transforms the Constraint feature into the format understandable by SolveSpace library.
29  *
30  *  Constraints created for SolveSpace library will be divided into the groups.
31  *  The division order based on connectedness of the features by the constraints.
32  *  The groups may be fused or separated according to the new constraints.
33  */
34 class SketchSolver_ConstraintManager
35 {
36 public:
37   SketchSolver_ConstraintManager();
38   ~SketchSolver_ConstraintManager();
39
40   /** \brief Adds a constraint into the manager
41    *  \param[in] theConstraint constraint to be added
42    *  \return \c true if the constraint added successfully
43    */
44   SKETCHSOLVER_EXPORT bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
45
46   /** \brief Removes a constraint from the manager
47    *  \param[in] theConstraint constraint to be removed
48    *  \return \c true if the constraint removed successfully
49    */
50   SKETCHSOLVER_EXPORT bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
51
52 private:
53   class SketchSolver_ConstraintGroup;
54
55   /** \brief Searches list of groups which interact with specified constraint
56    *  \param[in]  theConstraint constraint to be found
57    *  \param[out] theGroups     list of group indexes interacted with constraint
58    */
59   void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
60                   std::vector<Slvs_hGroup>&                  theGroupIDs) const;
61
62 private:
63   std::vector<SketchSolver_ConstraintGroup> myGroups; ///< groups of constraints
64 };
65
66
67 /** \class   SketchSolver_ConstraintGroup
68  *  \ingroup DataModel
69  *  \brief   Keeps the group of constraints which based on the same entities
70  */
71 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
72 {
73 public:
74   SketchSolver_ConstraintGroup();
75   ~SketchSolver_ConstraintGroup();
76
77   /// \brief Returns group's unique identifier
78   const Slvs_hGroup& getId() const
79   {return myID;}
80
81   /** \brief Adds a constraint into the group
82    *  \param[in] theConstraint constraint to be added
83    *  \return \c true if the constraint added successfully
84    */
85   bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
86
87   /** \brief Removes a constraint into the group
88    *  \param[in] theConstraint constraint to be removed
89    *  \return \c true if the constraint removed successfully
90    */
91   bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
92
93   /** \brief Verifies the constraint uses the objects from this group
94    *  \param[in] theConstraint constraint for verification of interaction
95    *  \return \c true if the constrained objects are used in current group
96    */
97   bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
98
99 protected:
100   /** \brief Creates a workplane from the sketch parameters
101    *  \param[in] theParams list of the basic parameters of the workplane
102    *  \return \c true if success
103    */
104   bool addWorkplane(std::list< boost::shared_ptr<ModelAPI_Attribute> >& theParams);
105
106   /** \brief Adds an entity into the group
107    *  \param[in] theEntity the object of constraint
108    *  \return identifier of created entity or 0 if entity was not added
109    */
110   Slvs_hEntity addEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
111
112   /** \brief Adds a parameter into the group
113    *  \param[in] theParam parameter to be added
114    *  \return identifier of created parameter or 0 if it was not added
115    */
116   Slvs_hParam addParameter(double theParam);
117
118   /** \brief Compute constraint type according to SolveSpace identifiers
119    *  \param[in] theConstraint constraint which type should be determined
120    *  \return identifier of constraint type
121    */
122   int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
123
124 private:
125   Slvs_hGroup                  myID;            ///< the index of the group
126   Slvs_Entity                  myWorkplane;     ///< Workplane for the current group
127   std::vector<Slvs_Param>      myParams;        ///< List of parameters of the constraints
128   Slvs_hParam                  myParamMaxID;    ///< Actual maximal ID of parameters
129   std::vector<Slvs_Entity>     myEntities;      ///< List of entities of the constaints
130   Slvs_hEntity                 myEntityMaxID;   ///< Actual maximal ID of entities
131   std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
132   Slvs_hConstraint             myConstrMaxID;   ///< Actual maximal ID of constraints
133   Slvs_System                  myConstrSet;     ///< SolveSpace's set of equations obtained by constraints
134   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint> 
135                                myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
136 };
137
138 #endif