Salome HOME
2819132579238eddfe5ff85874f323a4d9be0e03
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.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_Storage_H_
21 #define SketchSolver_Storage_H_
22
23 #include <PlaneGCSSolver_ConstraintWrapper.h>
24 #include <PlaneGCSSolver_EntityWrapper.h>
25
26 #include <PlaneGCSSolver_Solver.h>
27 #include <PlaneGCSSolver_UpdateFeature.h>
28
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_Feature.h>
32 #include <SketchPlugin_Constraint.h>
33
34 class SketchSolver_ConstraintDistance;
35 class SketchSolver_ConstraintFixedArcRadius;
36 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
37
38
39 /** \class   SketchSolver_Storage
40  *  \ingroup Plugins
41  *  \brief   Interface to map SketchPlugin features to the entities of corresponding solver.
42  */
43 class SketchSolver_Storage
44 {
45 private:
46   SketchSolver_Storage(const SketchSolver_Storage&);
47   SketchSolver_Storage& operator=(const SketchSolver_Storage&);
48
49 public:
50   SketchSolver_Storage(SolverPtr theSolver);
51
52   /// \brief Change mapping between constraint from SketchPlugin and
53   ///        a constraint applicable for corresponding solver.
54   /// \param theConstraint       [in]   original SketchPlugin constraint
55   /// \param theSolverConstraint [in]   solver's constraint
56   virtual void addConstraint(ConstraintPtr        theConstraint,
57                              ConstraintWrapperPtr theSolverConstraint);
58
59   /// \brief Add a movement constraint which will be destroyed
60   ///        after the next solving of the set of constraints.
61   /// \param theSolverConstraint [in]  solver's constraint
62   virtual void addMovementConstraint(const ConstraintWrapperPtr& theSolverConstraint) = 0;
63
64   /// \brief Change mapping feature from SketchPlugin and
65   ///        the entity applicable for corresponding solver.
66   /// \param theFeature      [in]  original SketchPlugin feature
67   /// \param theSolverEntity [in]  solver's entity, created outside
68   void addEntity(FeaturePtr       theFeature,
69                  EntityWrapperPtr theSolverEntity);
70
71   /// \brief Change mapping attribute of a feature and the entity
72   /// applicable for corresponding solver.
73   /// \param theAttribute    [in]  original attribute
74   /// \param theSolverEntity [in]  solver's entity, created outside
75   void addEntity(AttributePtr     theAttribute,
76                  EntityWrapperPtr theSolverEntity);
77
78   /// \brief Convert feature to the form applicable for specific solver and map it
79   /// \param theFeature [in]  feature to convert
80   /// \param theForce   [in]  forced feature creation
81   /// \return \c true if the feature has been created or updated
82   virtual bool update(FeaturePtr theFeature, bool theForce = false) = 0;
83
84   /// \brief Convert attribute to the form applicable for specific solver and map it
85   /// \param theAttribute [in]  attribute to convert
86   /// \param theForce     [in]  forced feature creation
87   /// \return \c true if the attribute has been created or updated
88   virtual bool update(AttributePtr theAttribute, bool theForce = false) = 0;
89
90   /// \brief Returns constraint related to corresponding constraint
91   const ConstraintWrapperPtr& constraint(const ConstraintPtr& theConstraint) const;
92
93   /// \brief Returns entity related to corresponding feature
94   const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
95   /// \brief Returns entity related to corresponding attribute
96   const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
97
98   /// \brief Make entity external
99   virtual void makeExternal(const EntityWrapperPtr& theEntity) = 0;
100   /// \brief Make entity non-external
101   virtual void makeNonExternal(const EntityWrapperPtr& theEntity) = 0;
102
103   /// \brief Removes constraint from the storage
104   /// \return \c true if the constraint and all its parameters are removed successfully
105   virtual bool removeConstraint(ConstraintPtr theConstraint) = 0;
106   /// \brief Removes feature from the storage
107   void removeFeature(FeaturePtr theFeature);
108   /// \brief Removes attribute from the storage
109   void removeAttribute(AttributePtr theAttribute);
110
111   /// \brief Remove all features became invalid
112   virtual void removeInvalidEntities() = 0;
113
114   /// \brief Check the features have not been removed
115   bool areFeaturesValid() const;
116
117   /// \brief Check the storage has constraints
118   virtual bool isEmpty() const
119   { return myConstraintMap.empty(); }
120
121   /// \brief Shows the sketch should be resolved
122   virtual bool isNeedToResolve()
123   { return myNeedToResolve; }
124   /// \brief Changes the flag of group to be resolved
125   void setNeedToResolve(bool theFlag)
126   { myNeedToResolve = theFlag; }
127
128   /// \brief Return list of conflicting constraints
129   std::set<ObjectPtr> getConflictingConstraints(SolverPtr theSolver) const;
130
131   /// \brief Return list of features which are not fully constrained
132   virtual void getUnderconstrainedGeometry(std::set<ObjectPtr>& theFeatures) const = 0;
133
134   /// \brief Verify, the sketch contains degenerated geometry
135   ///        after resolving the set of constraints
136   /// \return STATUS_OK if the geometry is valid, STATUS_DEGENERATED otherwise.
137   virtual PlaneGCSSolver_Solver::SolveStatus checkDegeneratedGeometry() const = 0;
138
139   /// \brief Update SketchPlugin features after resolving constraints
140   virtual void refresh() const = 0;
141
142   /// \brief Block or unblock events when refreshing features
143   void blockEvents(bool isBlocked);
144
145   /// \brief Subscribe for updates of features
146   /// \param theSubscriber [in]  object which wants to revceive notifications
147   /// \param theGroup      [in]  group of updates features to be send
148   void subscribeUpdates(SketchSolver_Constraint* theSubscriber, const std::string& theGroup) const;
149   /// \brief Unsubscribe for updates of features
150   /// \param theSubscriber [in]  object which does not want to revceive notifications anymore
151   void unsubscribeUpdates(SketchSolver_Constraint* theSubscriber) const;
152
153   /// \brief Notify all subscribers about update of the feature
154   void notify(const FeaturePtr& theFeature) const;
155
156   /// \brief Make parametrization of arcs consistent.
157   ///        Forward arcs should have the last parameter greater than the first parameter.
158   ///        Reversed arcs should have the last parameter lesser than the first parameter.
159   virtual void adjustParametrizationOfArcs() = 0;
160
161 protected:
162   /// \brief Convert result to feature or attribute if theResult is linked to center of circle/arc
163   static void resultToFeatureOrAttribute(const ObjectPtr& theResult,
164                                          FeaturePtr&      theFeature,
165                                          AttributePtr&    theAttribute);
166
167 protected:
168   SolverPtr mySketchSolver; ///< Sketch solver, prepared in corresponding group
169   bool    myNeedToResolve; ///< parameters are changed and group needs to be resolved
170   bool    myEventsBlocked; ///< indicates that features do not send events
171
172   /// map SketchPlugin constraint to a list of solver's constraints
173   std::map<ConstraintPtr, ConstraintWrapperPtr> myConstraintMap;
174   /// map SketchPlugin feature to solver's entity
175   std::map<FeaturePtr, EntityWrapperPtr>        myFeatureMap;
176   /// map attribute to solver's entity
177   std::map<AttributePtr, EntityWrapperPtr>      myAttributeMap;
178
179   UpdaterPtr myUpdaters;
180 };
181
182 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
183
184 #endif