Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchSolver / SketchSolver_Group.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_Group_H_
22 #define SketchSolver_Group_H_
23
24 #include <SketchSolver_Constraint.h>
25 #include <SketchSolver_Storage.h>
26
27 #include <PlaneGCSSolver_Solver.h>
28
29 #include <SketchPlugin_Constraint.h>
30
31 #include <memory>
32 #include <map>
33
34 class GeomAPI_Pnt2d;
35
36 typedef std::map<ConstraintPtr, SolverConstraintPtr> ConstraintConstraintMap;
37
38 /** \class   SketchSolver_Group
39  *  \ingroup Plugins
40  *  \brief   Keeps the group of constraints which placed in the same sketch
41  */
42 class SketchSolver_Group
43 {
44  public:
45   /** \brief New group based on specified workplane.
46    *         Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
47    *  \remark Type of theSketch is not verified inside
48    */
49   SketchSolver_Group(const CompositeFeaturePtr& theWorkplane);
50
51   virtual ~SketchSolver_Group();
52
53   /// \brief Returns true if the group has no constraints yet
54   inline bool isEmpty() const
55   {
56     return myConstraints.empty() && myTempConstraints.empty();
57   }
58
59   /// \brief Check for valid sketch data
60   inline bool isWorkplaneValid() const
61   {
62     return mySketch->data() && mySketch->data()->isValid();
63   }
64
65   /** \brief Adds or updates a constraint in the group
66    *  \param[in] theConstraint constraint to be changed
67    *  \return \c true if the constraint added or updated successfully
68    */
69   bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
70
71   /** \brief Updates the data corresponding the specified feature
72    *  \param[in] theFeature the feature to be updated
73    */
74   bool updateFeature(FeaturePtr theFeature);
75
76   /** \brief Updates the data corresponding the specified feature moved in GUI.
77    *         Special kind of Fixed constraints is created.
78    *  \param[in] theFeature the feature to be updated
79    *  \param[in] theFrom    start point of the movement
80    *  \param[in] theTo      final point of the movement
81    *  \return \c true, if the feature is really moved
82    */
83   bool moveFeature(FeaturePtr theFeature,
84                    const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
85                    const std::shared_ptr<GeomAPI_Pnt2d>& theTo);
86   /** \brief Updates the data corresponding the specified point moved in GUI.
87    *         Special kind of Fixed constraints is created.
88    *  \param[in] thePoint the attribute to be updated
89    *  \param[in] theFrom  start point of the movement
90    *  \param[in] theTo    final point of the movement
91    *  \return \c true, if the attribute is really moved
92    */
93   bool movePoint(AttributePtr thePoint,
94                  const std::shared_ptr<GeomAPI_Pnt2d>& theFrom,
95                  const std::shared_ptr<GeomAPI_Pnt2d>& theTo);
96
97   /// Returns the current workplane
98   inline const CompositeFeaturePtr& getWorkplane() const
99   {
100     return mySketch;
101   }
102
103   /** \brief Searches invalid features and constraints in the group and removes them
104    *  \return \c false if the group several constraints were removed
105    */
106   void repairConsistency();
107
108   /** \brief Start solution procedure if necessary and update attributes of features
109    *  \return \c false when no need to solve constraints
110    */
111   bool resolveConstraints();
112
113   /// \brief Block or unblock events sent by features in this group
114   void blockEvents(bool isBlocked);
115
116 private:
117   /// \biref Verify constraints have not been removed
118   bool areConstraintsValid() const;
119
120   /** \brief Removes constraints from the group
121    *  \param[in] theConstraint constraint to be removed
122    */
123   void removeConstraint(ConstraintPtr theConstraint);
124
125   /// \brief Remove all temporary constraints after the computation finished
126   void removeTemporaryConstraints();
127
128   /// \brief Append given constraint to the group of temporary constraints
129   void setTemporary(SolverConstraintPtr theConstraint);
130
131   /// \brief Compute DoF of the sketch and set corresponding field
132   void computeDoF();
133
134 private:
135   CompositeFeaturePtr mySketch; ///< Sketch for this group
136   ConstraintConstraintMap myConstraints; ///< List of constraints
137   std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
138
139   StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
140   SolverPtr mySketchSolver;  ///< Solver for set of equations obtained by constraints
141
142   /// Result of previous solution of the set of constraints
143   PlaneGCSSolver_Solver::SolveStatus myPrevResult;
144   std::set<ObjectPtr>      myConflictingConstraints; ///< List of conflicting constraints
145
146   int  myDOF; ///< degrees of freedom of the current sketch
147
148   bool myIsEventsBlocked; ///< shows the events are blocked for this group
149
150   int myMultiConstraintUpdateStack; ///< depth of the stack updating "Multi" constraints
151 };
152
153 typedef std::shared_ptr<SketchSolver_Group> SketchGroupPtr;
154
155 #endif