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