Salome HOME
First phase of SketchSolver refactoring
[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_ISolver.h>
14
15 #include <SketchPlugin_Constraint.h>
16 ////#include <ModelAPI_Data.h>
17 #include <ModelAPI_Feature.h>
18 ////#include <ModelAPI_AttributeRefList.h>
19
20 #include <memory>
21 #include <list>
22 #include <map>
23 #include <set>
24
25 typedef std::map<ConstraintPtr, SolverConstraintPtr> ConstraintConstraintMap;
26
27 /** \class   SketchSolver_Group
28  *  \ingroup Plugins
29  *  \brief   Keeps the group of constraints which based on the same entities
30  */
31 class SketchSolver_Group
32 {
33  public:
34   /** \brief New group based on specified workplane.
35    *         Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
36    *  \remark Type of theSketch is not verified inside
37    */
38   SketchSolver_Group(std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane);
39
40   ~SketchSolver_Group();
41
42   /// \brief Returns group's unique identifier
43   inline const GroupID& getId() const
44   {
45     return myID;
46   }
47
48   /// \brief Returns identifier of the workplane
49   inline const EntityID& getWorkplaneId() const
50   {
51     return myWorkplaneID;
52   }
53
54   /// \brief Returns true if the group has no constraints yet
55   inline bool isEmpty() const
56   {
57     return myConstraints.empty();
58   }
59
60   /// \brief Check for valid sketch data
61   inline bool isWorkplaneValid() const
62   {
63     return mySketch->data() && mySketch->data()->isValid();
64   }
65
66   /// \brief Verifies the constraint is complex, i.e. it needs another constraints to be created before
67   static bool isComplexConstraint(FeaturePtr theConstraint);
68
69   /** \brief Adds or updates a constraint in the group
70    *  \param[in] theConstraint constraint to be changed
71    *  \return \c true if the constraint added or updated successfully
72    */
73   bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
74
75   /** \brief Updates the data corresponding the specified feature
76    *  \param[in] theFeature the feature to be updated
77    */
78   bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
79
80   /** \brief Updates the data corresponding the specified feature moved in GUI.
81    *         Additional Fixed constraints are created.
82    *  \param[in] theFeature the feature to be updated
83    */
84   void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
85
86   /** \brief Verifies the feature attributes are used in this group
87    *  \param[in] theFeature constraint or any other object for verification of interaction
88    *  \return \c true if some of attributes are used in current group
89    */
90   bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
91
92   /** \brief Verifies the specified feature is equal to the base workplane for this group
93    *  \param[in] theWorkplane the feature to be compared with base workplane
94    *  \return \c true if workplanes are the same
95    */
96   bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
97
98   /// Returns the current workplane
99   std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
100   {
101     return mySketch;
102   }
103
104   /** \brief Update parameters of workplane. Should be called when Update event is coming.
105    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
106    */
107   bool updateWorkplane();
108
109   /** \brief Searches invalid features and constraints in the group and removes them
110    *  \return \c false if the group several constraints were removed
111    */
112   bool isConsistent();
113
114   /** \brief Add specified group to this one
115    *  \param[in] theGroup group of constraint to be added
116    */
117   void mergeGroups(const SketchSolver_Group& theGroup);
118
119   /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
120    *  \param[out] theCuts enlarge this list by newly created groups
121    */
122   void splitGroup(std::list<SketchSolver_Group*>& theCuts);
123
124   /** \brief Start solution procedure if necessary and update attributes of features
125    *  \return \c false when no need to solve constraints
126    */
127   bool resolveConstraints();
128
129   /** \brief Collect all features applicable for the sketch
130    *  \param theObjects  list of features
131    *  \return list of bolted and sorted features
132    */
133   static std::list<FeaturePtr> selectApplicableFeatures(const std::set<ObjectPtr>& theObjects);
134
135 protected:
136   /** \brief Removes constraints from the group
137    *  \param[in] theConstraint constraint to be removed
138    */
139   void removeConstraint(ConstraintPtr theConstraint);
140
141   /// \brief Remove all temporary constraint after computation finished
142   void removeTemporaryConstraints();
143
144 private:
145   /** \brief Creates a workplane from the sketch parameters
146    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
147    *  \return \c true if success, \c false if workplane parameters are not consistent
148    */
149   bool addWorkplane(CompositeFeaturePtr theSketch);
150
151   /// \brief Append given constraint to the group of temporary constraints
152   void setTemporary(SolverConstraintPtr theConstraint);
153
154   /// \brief Verifies is the feature valid
155   bool checkFeatureValidity(FeaturePtr theFeature);
156
157   /// \brief Update just changed constraints
158   void updateConstraints();
159
160   /// \brief Update Multi-Translation/-Rotation constraints due to multi coincidence appears/disappears
161   void notifyMultiConstraints();
162
163 private:
164   GroupID  myID; ///< Index of the group
165   EntityID myWorkplaneID; ///< Index of workplane, the group is based on
166   CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
167   ConstraintConstraintMap myConstraints; ///< List of constraints
168   std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
169   std::map<AttributePtr, SolverConstraintPtr> myParametricConstraints; ///< List of parametric constraints
170   std::set<ConstraintPtr> myChangedConstraints; ///< List of just updated constraints
171
172   StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
173
174   SolverPtr mySketchSolver;  ///< Solver for set of equations obtained by constraints
175
176   bool myPrevSolved; ///< Indicates that previous solving was done correctly
177 };
178
179 #endif