Salome HOME
d3509c7078f33cee6d7ab049de1f4a3adf2b3e9d
[modules/shaper.git] / src / SketchSolver / SketchSolver_Manager.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef SketchSolver_Manager_H_
22 #define SketchSolver_Manager_H_
23
24 #include <SketchSolver_Group.h>
25
26 #include <Events_Listener.h>
27
28 #include <list>
29 #include <set>
30
31 class GeomAPI_Pnt2d;
32 class GeomDataAPI_Point2D;
33 class SketchPlugin_Constraint;
34
35 /** \class   SketchSolver_Manager
36  *  \ingroup Plugins
37  *  \brief   Listens the changes of SketchPlugin features and transforms the Constraint
38  *           feature into the format understandable by sketch solver.
39  *
40  *  \remark This is a singleton.
41  */
42 class SketchSolver_Manager : public Events_Listener
43 {
44 public:
45   /** \brief Main method to create constraint manager
46    *  \return pointer to the singleton
47    */
48   static SketchSolver_Manager* instance();
49
50   /** \brief Implementation of Event Listener method
51    *  \param[in] theMessage the data of the event
52    */
53   virtual void processEvent(const std::shared_ptr<Events_Message>& theMessage);
54
55   /**
56    * The solver needs all the updated objects are transfered in one group, not one by one.
57    * This iscreases performance and avoids problems in resolve of only part of the made updates.
58    */
59   virtual bool groupMessages();
60
61 protected:
62   SketchSolver_Manager();
63   ~SketchSolver_Manager();
64
65   /** \brief Adds or updates a constraint or an entity in the suitable group
66    *  \param[in] theFeature sketch feature to be changed
67    *  \param[in] theMoved   \c true if the feature has been moved in the viewer
68    *  \return \c true if the feature changed successfully
69    */
70   bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature, bool theMoved = false);
71
72   /** \brief Move feature
73    *  \param[in] theMovedFeature dragged sketch feature
74    *  \param[in] theFromPoint    original position of the feature
75    *  \param[in] theToPoint      prefereble position of the feature (current position of the mouse)
76    *  \return \c true if the feature has been changed successfully
77    */
78   bool moveFeature(const std::shared_ptr<SketchPlugin_Feature>& theMovedFeature,
79                    const std::shared_ptr<GeomAPI_Pnt2d>& theFromPoint,
80                    const std::shared_ptr<GeomAPI_Pnt2d>& theToPoint);
81
82   /** \brief Move feature using its moved attribute
83    *  \param[in] theMovedAttribute dragged point attribute of sketch feature
84    *  \param[in] theFromPoint      original position of the moved point
85    *  \param[in] theToPoint        prefereble position (current position of the mouse)
86    *  \return \c true if the attribute owner has been changed successfully
87    */
88   bool moveAttribute(const std::shared_ptr<GeomDataAPI_Point2D>& theMovedAttribute,
89                      const std::shared_ptr<GeomAPI_Pnt2d>& theFromPoint,
90                      const std::shared_ptr<GeomAPI_Pnt2d>& theToPoint);
91
92   /** \brief Removes a constraint from the manager
93    *  \param[in] theConstraint constraint to be removed
94    *  \return \c true if the constraint removed successfully
95    */
96   bool removeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
97
98   /** \brief Goes through the list of groups and solve the constraints
99    *  \return \c true, if groups are resolved, and features should be updated 
100    *  (send the Update event)
101    */
102   bool resolveConstraints();
103
104 private:
105   /** \brief Searches group which interact with specified feature
106    *  \param[in]  theFeature  object to be found
107    *  \return Pointer to corresponding group or NULL if the group cannot be created.
108    */
109   SketchGroupPtr findGroup(std::shared_ptr<SketchPlugin_Feature> theFeature);
110
111   /// \brief Stop sending the Update event until all features updated
112   /// \return \c true, if the last flushed event is Update
113   bool stopSendUpdate() const;
114   /// \brief Allow to send the Update event
115   void allowSendUpdate() const;
116
117   /// \brief Allow send events about changing features in groups
118   void releaseFeaturesIfEventsBlocked() const;
119
120 private:
121   std::list<SketchGroupPtr> myGroups; ///< Groups of constraints
122   /// true if computation is performed and all "updates" are generated by this algo
123   /// and needs no recomputation
124   bool myIsComputed;
125 };
126
127 #endif