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