Salome HOME
6e53af522829e3cd5f19c5d541c075a0b114f46a
[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
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 ////  /** \brief Adds or updates a rigid constraint in the group
79 ////   *  \param[in] theConstraint constraint to be changed
80 ////   *  \return \c true if the constraint added or updated successfully
81 ////   */
82 ////  bool changeRigidConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
83 ////  /** \brief Adds or updates a mirror constraint in the group
84 ////   *  \param[in] theConstraint constraint to be changed
85 ////   *  \return \c true if the constraint added or updated successfully
86 ////   */
87 ////  bool changeMirrorConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
88 ////  /** \brief Adds or updates a fillet constraint in the group
89 ////   *  \param[in] theConstraint constraint to be changed
90 ////   *  \return \c true if the constraint added or updated successfully
91 ////   */
92 ////  bool changeFilletConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
93
94   /** \brief Updates the data corresponding the specified feature
95    *  \param[in] theFeature the feature to be updated
96    */
97   bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
98
99   /** \brief Updates the data corresponding the specified feature moved in GUI.
100    *         Additional Fixed constraints are created.
101    *  \param[in] theFeature the feature to be updated
102    */
103   void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
104
105   /** \brief Verifies the feature attributes are used in this group
106    *  \param[in] theFeature constraint or any other object for verification of interaction
107    *  \return \c true if some of attributes are used in current group
108    */
109   bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
110
111   /** \brief Verifies the specified feature is equal to the base workplane for this group
112    *  \param[in] theWorkplane the feature to be compared with base workplane
113    *  \return \c true if workplanes are the same
114    */
115   bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
116
117   /// Returns the current workplane
118   std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
119   {
120     return mySketch;
121   }
122
123   /** \brief Update parameters of workplane. Should be called when Update event is coming.
124    *  \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
125    */
126   bool updateWorkplane();
127 ////
128 ////  /** \brief If the entity is in this group it will updated
129 ////   *  \param[in] theEntity attribute, which values should update SolveSpace entity
130 ////   */
131 ////  void updateEntityIfPossible(std::shared_ptr<ModelAPI_Attribute> theEntity);
132
133   /** \brief Searches invalid features and constraints in the group and removes them
134    *  \return \c false if the group several constraints were removed
135    */
136   bool isConsistent();
137
138   /** \brief Add specified group to this one
139    *  \param[in] theGroup group of constraint to be added
140    */
141   void mergeGroups(const SketchSolver_Group& theGroup);
142
143   /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
144    *  \param[out] theCuts enlarge this list by newly created groups
145    */
146   void splitGroup(std::vector<SketchSolver_Group*>& theCuts);
147
148   /** \brief Start solution procedure if necessary and update attributes of features
149    *  \return \c false when no need to solve constraints
150    */
151   bool resolveConstraints();
152 ////
153 ////  /** \brief Searches the constraints built on the entity and emit the signal to update them
154 ////   *  \param[in] theEntity attribute of the constraint
155 ////   */
156 ////  void updateRelatedConstraints(std::shared_ptr<ModelAPI_Attribute> theEntity) const;
157 ////  /** \brief Searches the constraints built on the entity and emit the signal to update them
158 ////   *  \param[in] theFeature feature of the constraint
159 ////   */
160 ////  void updateRelatedConstraintsFeature(std::shared_ptr<ModelAPI_Feature> theFeature) const;
161 ////
162 ////  /** \brief Adds or updates an entity in the group
163 ////   *
164 ////   *  The parameters of entity will be parsed and added to the list of SolveSpace parameters.
165 ////   *  Parameters of certain entity will be placed sequentially in the list.
166 ////   *
167 ////   *  \param[in] theEntity the object of constraint
168 ////   *  \return identifier of changed entity or 0 if entity could not be changed
169 ////   */
170 ////  Slvs_hEntity changeEntity(std::shared_ptr<ModelAPI_Attribute> theEntity);
171 ////  Slvs_hEntity changeEntityFeature(std::shared_ptr<ModelAPI_Feature> theEntity);
172
173 protected:
174 ////  /** \brief Adds or updates a normal in the group
175 ////   *
176 ////   *  Normal is a special entity in SolveSpace, which defines a direction in 3D and
177 ////   *  a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
178 ////   *
179 ////   *  To define a normal there should be specified two coordinate axis
180 ////   *  on the plane transversed to created normal.
181 ////   *
182 ////   *  \param[in] theDirX first coordinate axis of the plane
183 ////   *  \param[in] theNorm attribute for the normal (used to identify newly created entity)
184 ////   *  \return identifier of created or updated normal
185 ////   */
186 ////  Slvs_hEntity changeNormal(std::shared_ptr<ModelAPI_Attribute> theDirX,
187 ////                            std::shared_ptr<ModelAPI_Attribute> theNorm);
188 ////
189 ////  /** \brief Adds or updates a parameter in the group
190 ////   *  \param[in] theParam   the value of parameter
191 ////   *  \param[in] thePrmIter the cell in the list of parameters which should be changed
192 ////   *                        (the iterator will be increased if it does not reach the end of the list)
193 ////   *  \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
194 ////   */
195 ////  Slvs_hParam changeParameter(const double& theParam,
196 ////                              std::vector<Slvs_Param>::const_iterator& thePrmIter);
197 ////
198 ////  /** \brief Removes specified entities and their parameters
199 ////   *  \param[in] theEntities  list of IDs of the entities to be removed
200 ////   */
201 ////  void removeEntitiesById(const std::set<Slvs_hEntity>& theEntities);
202
203   /** \brief Removes constraints from the group
204    *  \param[in] theConstraint constraint to be removed
205    */
206   void removeConstraint(ConstraintPtr theConstraint);
207 ////
208 ////  /** \brief Change values of attribute by parameters received from SolveSpace solver
209 ////   *  \param[in,out] theAttribute pointer to the attribute to be changed
210 ////   *  \param[in]     theEntityID  identifier of SolveSpace entity, which contains updated data
211 ////   *  \return \c true if the attribute's value has changed
212 ////   */
213 ////  bool updateAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute,
214 ////                       const Slvs_hEntity& theEntityID);
215 ////
216 ////  /// \brief Update arc of fillet to be less than 180 degree
217 ////  void updateFilletConstraints();
218 ////
219 ////  /** \brief Adds a constraint for a point which should not be changed during computations
220 ////   *  \param[in] theEntity     the base for the constraint
221 ////   *  \param[in] theAllowToFit this flag shows that the entity may be placed into 
222 ////   *                           the 'dragged' field of SolveSpace solver, so this entity 
223 ////   *                           may be changed a little during solution
224 ////   */
225 ////  void addTemporaryConstraintWhereDragged(std::shared_ptr<ModelAPI_Attribute> theEntity,
226 ////                                          bool theAllowToFit = true);
227
228   /** \brief Remove all temporary constraint after computation finished
229    *  \param[in] theRemoved  indexes of constraints to be removed. If empty, all temporary constraints should be deleted
230    */
231   void removeTemporaryConstraints();
232
233 private:
234   /** \brief Creates a workplane from the sketch parameters
235    *  \param[in] theSketch parameters of workplane are the attributes of this sketch
236    *  \return \c true if success, \c false if workplane parameters are not consistent
237    */
238   bool addWorkplane(CompositeFeaturePtr theSketch);
239 ////
240 ////  /** \brief Add the entities of constraint for points coincidence into the appropriate list
241 ////   *  \param[in] thePoint1 identifier of the first point
242 ////   *  \param[in] thePoint2 identifier of the second point
243 ////   *  \return \c true if the points are added successfully, and 
244 ////   *          \c false if the constraint is the extra one (should not be created in SolveSpace)
245 ////   */
246 ////  bool addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
247 ////
248 ////  /** \brief Change entities parameters to make them symmetric relating to the mirror line
249 ////   *  \param[in] theBase        entity to be mirrored
250 ////   *  \param[in] theMirror      a mirrored object
251 ////   *  \param[in] theMirrorLine  a mirror line
252 ////   */
253 ////  void makeMirrorEntity(const Slvs_hEntity& theBase,
254 ////                        const Slvs_hEntity& theMirror,
255 ////                        const Slvs_hEntity& theMirrorLine);
256 ////
257 ////  /** \brief Calculates middle point on line or arc
258 ////   *  \param[in]  theEntity  identifier of line or arc
259 ////   *  \param[out] theX       X value of middle point
260 ////   *  \param[out] theY       Y value of middle point
261 ////   */
262 ////  void calculateMiddlePoint(const Slvs_hEntity& theEntity,
263 ////                            double& theX, double& theY) const;
264 ////
265 //// private:
266 ////  // SolveSpace entities
267 ////  Slvs_hGroup myID;            ///< the index of the group
268 ////  Slvs_Entity myWorkplane;     ///< Workplane for the current group
269 ////  std::vector<Slvs_Param> myParams;        ///< List of parameters of the constraints
270 ////  Slvs_hParam myParamMaxID;    ///< Actual maximal ID of parameters (not equal to myParams size)
271 ////  std::vector<Slvs_Entity> myEntities;      ///< List of entities of the constaints
272 ////  std::vector<bool> myEntOfConstr; ///< Flags show that certain entity used in constraints
273 ////  Slvs_hEntity myEntityMaxID;   ///< Actual maximal ID of entities (not equal to myEntities size)
274 ////  std::vector<Slvs_Constraint> myConstraints;   ///< List of constraints in SolveSpace format
275 ////  Slvs_hConstraint myConstrMaxID;  ///< Actual maximal ID of constraints (not equal to myConstraints size)
276 ////  bool myNeedToSolve;  ///< Indicator that something changed in the group and constraint system need to be rebuilt
277 ////
278 ////  SketchSolver_Solver myConstrSolver;  ///< Solver for set of equations obtained by constraints
279 ////
280 ////  std::vector<Slvs_hParam> myTempPointWhereDragged;  ///< Parameters of one of the points which is moved by user
281 ////  Slvs_hEntity myTempPointWDrgdID;      ///< Identifier of such point
282 ////  std::list<Slvs_hConstraint> myTempConstraints;  ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
283 ////
284 ////  // SketchPlugin entities
285 ////  std::shared_ptr<ModelAPI_CompositeFeature> mySketch;        ///< Equivalent to workplane
286 ////  ConstraintMap myConstraintMap;  ///< The map between SketchPlugin and SolveSpace constraints
287 ////  std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity> myEntityAttrMap;  ///< The map between "attribute" parameters of constraints and their equivalent SolveSpace entities
288 ////  std::map<FeaturePtr, Slvs_hEntity> myEntityFeatMap;  ///< The map between "feature" parameters of constraints and their equivalent SolveSpace entities
289 ////
290 ////  // Conincident items
291 ////  std::vector<std::set<Slvs_hEntity> > myCoincidentPoints;  ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
292 ////  std::set<std::shared_ptr<SketchPlugin_Constraint> > myExtraCoincidence;  ///< Additional coincidence constraints which are not necessary (coincidence between points already done
293 ////                                                                             ///< by other constraints) but created by GUI tools. Useful when some coincidence constraints were removed
294
295 private:
296   Slvs_hGroup myID; ///< Index of the group
297   Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on
298   CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
299   ConstraintConstraintMap myConstraints; ///< List of constraints
300   std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
301
302   StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
303   FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies
304
305   SketchSolver_Solver myConstrSolver;  ///< Solver for set of equations obtained by constraints
306 };
307
308 #endif