Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Group.h
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Group_H_
8 #define SketchSolver_Group_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Constraint.h>
12 #include <SketchSolver_Storage.h>
13 #include <SketchSolver_FeatureStorage.h>
14 #include <SketchSolver_Solver.h>
15
16 #include <SketchPlugin_Constraint.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_AttributeRefList.h>
20
21 #include <memory>
22 #include <list>
23 #include <map>
24 #include <vector>
25 #include <set>
26
27 typedef std::map<ConstraintPtr, SolverConstraintPtr> ConstraintConstraintMap;
28
29 /** \class   SketchSolver_Group
30  *  \ingroup Plugins
31  *  \brief   Keeps the group of constraints which based on the same entities
32  */
33 class SketchSolver_Group
34 {
35  public:
36   /** \brief New group based on specified workplane.
37    *         Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
38    *  \remark Type of theSketch is not verified inside
39    */
40   SketchSolver_Group(std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane);
41
42   ~SketchSolver_Group();
43
44   /// \brief Returns group's unique identifier
45   inline const Slvs_hGroup& getId() const
46   {
47     return myID;
48   }
49
50   /// \brief Returns identifier of the workplane
51   inline const Slvs_hEntity& getWorkplaneId() const
52   {
53     return myWorkplaneID;
54   }
55
56   /// \brief Find the identifier of the feature, if it already exists in the group
57   Slvs_hEntity getFeatureId(FeaturePtr theFeature) const;
58   /// \brief Find the identifier of the attribute, if it already exists in the group
59   Slvs_hEntity getAttributeId(AttributePtr theAttribute) const;
60
61   /// \brief Returns true if the group has no constraints yet
62   inline bool isEmpty() const
63   {
64     return myConstraints.empty();
65   }
66
67   /// \brief Check for valid sketch data
68   inline bool isWorkplaneValid() const
69   {
70     return mySketch->data() && mySketch->data()->isValid();
71   }
72
73   /// \brief Verifies the constraint is complex, i.e. it needs another constraints to be created before
74   static bool isComplexConstraint(FeaturePtr theConstraint);
75
76   /** \brief Adds or updates a constraint in the group
77    *  \param[in] theConstraint constraint to be changed
78    *  \return \c true if the constraint added or updated successfully
79    */
80   bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
81
82   /** \brief Updates the data corresponding the specified feature
83    *  \param[in] theFeature the feature to be updated
84    */
85   bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
86
87   /** \brief Updates the data corresponding the specified feature moved in GUI.
88    *         Additional Fixed constraints are created.
89    *  \param[in] theFeature the feature to be updated
90    */
91   void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
92
93   /** \brief Verifies the feature attributes are used in this group
94    *  \param[in] theFeature constraint or any other object for verification of interaction
95    *  \return \c true if some of attributes are used in current group
96    */
97   bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
98
99   /** \brief Verifies the specified feature is equal to the base workplane for this group
100    *  \param[in] theWorkplane the feature to be compared with base workplane
101    *  \return \c true if workplanes are the same
102    */
103   bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
104
105   /// Returns the current workplane
106   std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
107   {
108     return mySketch;
109   }
110
111   /** \brief Update parameters of workplane. Should be called when Update event is coming.
112    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
113    */
114   bool updateWorkplane();
115
116   /** \brief Searches invalid features and constraints in the group and removes them
117    *  \return \c false if the group several constraints were removed
118    */
119   bool isConsistent();
120
121   /** \brief Add specified group to this one
122    *  \param[in] theGroup group of constraint to be added
123    */
124   void mergeGroups(const SketchSolver_Group& theGroup);
125
126   /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
127    *  \param[out] theCuts enlarge this list by newly created groups
128    */
129   void splitGroup(std::vector<SketchSolver_Group*>& theCuts);
130
131   /** \brief Start solution procedure if necessary and update attributes of features
132    *  \return \c false when no need to solve constraints
133    */
134   bool resolveConstraints();
135
136 protected:
137   /** \brief Removes constraints from the group
138    *  \param[in] theConstraint constraint to be removed
139    */
140   void removeConstraint(ConstraintPtr theConstraint);
141
142   /** \brief Remove all temporary constraint after computation finished
143    *  \param[in] theRemoved  indexes of constraints to be removed. If empty, all temporary constraints should be deleted
144    */
145   void removeTemporaryConstraints();
146
147 private:
148   /** \brief Creates a workplane from the sketch parameters
149    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
150    *  \return \c true if success, \c false if workplane parameters are not consistent
151    */
152   bool addWorkplane(CompositeFeaturePtr theSketch);
153
154   /// \brief Apply temporary rigid constraints for the list of features
155   void fixFeaturesList(AttributeRefListPtr theList);
156
157   /// \brief Append given constraint to th group of temporary constraints
158   void setTemporary(SolverConstraintPtr theConstraint);
159
160 private:
161   Slvs_hGroup myID; ///< Index of the group
162   Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on
163   CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
164   ConstraintConstraintMap myConstraints; ///< List of constraints
165   std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
166
167   StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
168   FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies
169
170   SketchSolver_Solver myConstrSolver;  ///< Solver for set of equations obtained by constraints
171 };
172
173 #endif