Salome HOME
Issue #435: Fix initial positions of base entities of mirror constraint
[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 Adds or updates a constraint in the group
74    *  \param[in] theConstraint constraint to be changed
75    *  \return \c true if the constraint added or updated successfully
76    */
77   bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
78
79   /** \brief Updates the data corresponding the specified feature
80    *  \param[in] theFeature the feature to be updated
81    */
82   bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
83
84   /** \brief Updates the data corresponding the specified feature moved in GUI.
85    *         Additional Fixed constraints are created.
86    *  \param[in] theFeature the feature to be updated
87    */
88   void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
89
90   /** \brief Verifies the feature attributes are used in this group
91    *  \param[in] theFeature constraint or any other object for verification of interaction
92    *  \return \c true if some of attributes are used in current group
93    */
94   bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
95
96   /** \brief Verifies the specified feature is equal to the base workplane for this group
97    *  \param[in] theWorkplane the feature to be compared with base workplane
98    *  \return \c true if workplanes are the same
99    */
100   bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
101
102   /// Returns the current workplane
103   std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
104   {
105     return mySketch;
106   }
107
108   /** \brief Update parameters of workplane. Should be called when Update event is coming.
109    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
110    */
111   bool updateWorkplane();
112
113   /** \brief Searches invalid features and constraints in the group and removes them
114    *  \return \c false if the group several constraints were removed
115    */
116   bool isConsistent();
117
118   /** \brief Add specified group to this one
119    *  \param[in] theGroup group of constraint to be added
120    */
121   void mergeGroups(const SketchSolver_Group& theGroup);
122
123   /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
124    *  \param[out] theCuts enlarge this list by newly created groups
125    */
126   void splitGroup(std::vector<SketchSolver_Group*>& theCuts);
127
128   /** \brief Start solution procedure if necessary and update attributes of features
129    *  \return \c false when no need to solve constraints
130    */
131   bool resolveConstraints();
132
133 protected:
134   /** \brief Removes constraints from the group
135    *  \param[in] theConstraint constraint to be removed
136    */
137   void removeConstraint(ConstraintPtr theConstraint);
138
139   /** \brief Remove all temporary constraint after computation finished
140    *  \param[in] theRemoved  indexes of constraints to be removed. If empty, all temporary constraints should be deleted
141    */
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 Apply temporary rigid constraints for the list of features
152   void fixFeaturesList(AttributeRefListPtr theList);
153
154 private:
155   Slvs_hGroup myID; ///< Index of the group
156   Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on
157   CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
158   ConstraintConstraintMap myConstraints; ///< List of constraints
159   std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
160
161   StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
162   FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies
163
164   SketchSolver_Solver myConstrSolver;  ///< Solver for set of equations obtained by constraints
165 };
166
167 #endif