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