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