Salome HOME
Managing of several groups with conflicting constraints on the same sketch plane
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Manager.h
4 // Created: 08 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Manager_H_
8 #define SketchSolver_Manager_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Group.h>
12 #include <SketchSolver_Builder.h>
13
14 #include <Events_Listener.h>
15 #include <SketchPlugin_Constraint.h>
16
17 #include <list>
18 #include <set>
19
20 /** \class   SketchSolver_Manager
21  *  \ingroup Plugins
22  *  \brief   Listens the changes of SketchPlugin features and transforms the Constraint
23  *           feature into the format understandable by SolveSpace library.
24  *
25  *  Constraints created for SolveSpace library are divided into the groups.
26  *  The division order based on connectedness of the features by the constraints.
27  *  The groups may be fused or separated according to the new constraints.
28  *
29  *  \remark This is a singleton.
30  */
31 class SketchSolver_Manager : public Events_Listener
32 {
33 public:
34   /** \brief Main method to create constraint manager
35    *  \return pointer to the singleton
36    */
37   SKETCHSOLVER_EXPORT static SketchSolver_Manager* instance();
38
39   /** \brief Implementation of Event Listener method
40    *  \param[in] theMessage the data of the event
41    */
42   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
43
44   /// \brief Initialize builder for solver's data structure entities
45   /// \param theBuilder [in]  solver's specific builder
46   SKETCHSOLVER_EXPORT void setBuilder(BuilderPtr theBuilder);
47   /// \brief Returns the builder specific for the solver
48   BuilderPtr builder();
49
50 protected:
51   SketchSolver_Manager();
52   ~SketchSolver_Manager();
53
54   /** \brief Adds or updates a constraint or an entity in the suitable group
55    *  \param[in] theFeature sketch feature to be changed
56    *  \return \c true if the feature changed successfully
57    */
58   bool changeFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
59
60   /** \brief Removes a constraint from the manager
61    *  \param[in] theConstraint constraint to be removed
62    *  \return \c true if the constraint removed successfully
63    */
64   bool removeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
65
66   /** \brief Adds or updates a workplane in the manager
67    *  \param[in] theSketch the feature to create or update workplane
68    *  \return \c true if the workplane changed successfully
69    *  \remark Type of theSketch is not verified inside
70    */
71   bool changeWorkplane(CompositeFeaturePtr theSketch);
72
73   /** \brief Removes a workplane from the manager.
74    *         All groups based on such workplane will be removed too.
75    *  \param[in] theSketch the feature to be removed
76    *  \return \c true if the workplane removed successfully
77    */
78   bool removeWorkplane(std::shared_ptr<SketchPlugin_Sketch> theSketch);
79
80   /** \brief Updates entity which is moved in GUI
81    *  \param[in] theFeature entity to be updated
82    */
83   void moveEntity(std::shared_ptr<SketchPlugin_Feature> theFeature);
84
85   /** \brief Goes through the list of groups and solve the constraints
86    *  \param[in] theGroups  list of groups to be resolved (if empty list, all groups are resolved)
87    *  \return \c true, if groups are resolved, and features should be updated (send the Update event)
88    */
89   bool resolveConstraints(const std::list<SketchSolver_Group*>& theGroups = std::list<SketchSolver_Group*>());
90
91 private:
92   /** \brief Searches list of groups which interact with specified feature
93    *  \param[in]  theFeature  object to be found
94    *  \param[out] theGroups   list of group indexes interacted with the feature
95    */
96   void findGroups(std::shared_ptr<SketchPlugin_Feature> theFeature,
97                   std::set<GroupID>& theGroupIDs) const;
98
99   /** \brief Searches in the list of groups the workplane which contains specified feature
100    *  \param[in] theFeature object to be found
101    *  \return workplane containing the feature
102    */
103   std::shared_ptr<ModelAPI_CompositeFeature> findWorkplane(
104       std::shared_ptr<SketchPlugin_Feature> theFeature) const;
105
106   /// \brief Stop sending the Update event until all features updated
107   /// \return \c true, if the last flushed event is Update
108   bool stopSendUpdate() const;
109   /// \brief Allow to send the Update event
110   void allowSendUpdate() const;
111
112   /// \brief If the message shows that any group is repaired after conflicting,
113   ///        find other groups on the same sketch, which have conflicts.
114   void checkConflictingConstraints(const std::shared_ptr<Events_Message>& theMessage);
115
116 private:
117   static SketchSolver_Manager*     mySelf;    ///< Self pointer to implement singleton functionality
118   std::list<SketchSolver_Group*>   myGroups;  ///< Groups of constraints
119   BuilderPtr                       myBuilder; ///< Builder for solver's entities
120   /// true if computation is performed and all "updates" are generated by this algo
121   /// and needs no recomputation
122   bool myIsComputed;
123 };
124
125 #endif