Salome HOME
First phase of SketchSolver refactoring
[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_IParameterWrapper.h>
14 #include <SketchSolver_ISolver.h>
15
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Feature.h>
19 #include <SketchPlugin_Constraint.h>
20
21 /** \class   SketchSolver_Storage
22  *  \ingroup Plugins
23  *  \brief   Interface to map SketchPlugin features to the entities of corresponding solver.
24  */
25 class SketchSolver_Storage
26 {
27 private:
28   SketchSolver_Storage();
29   SketchSolver_Storage(const SketchSolver_Storage&);
30   SketchSolver_Storage& operator=(const SketchSolver_Storage&);
31
32 public:
33   SketchSolver_Storage(const GroupID& theGroup)
34     : myGroupID(theGroup),
35       myNeedToResolve(false)
36   {}
37
38   /// \brief Change mapping between constraint from SketchPlugin and
39   ///        a constraint applicable for corresponding solver.
40   /// \param theConstraint       [in]   original SketchPlugin constraint
41   /// \param theSolverConstraint [in]   solver's constraints
42   SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr        theConstraint,
43                                          ConstraintWrapperPtr theSolverConstraints);
44   /// \brief Change mapping between constraint from SketchPlugin and
45   ///        the list of constraints applicable for corresponding solver.
46   /// \param theConstraint        [in]   original SketchPlugin constraint
47   /// \param theSolverConstraints [in]   list of solver's constraints
48   SKETCHSOLVER_EXPORT
49     void addConstraint(ConstraintPtr                   theConstraint,
50                        std::list<ConstraintWrapperPtr> theSolverConstraints);
51
52   /// \brief Convert feature to the form applicable for specific solver and map it
53   /// \param theFeature [in]  feature to convert
54   /// \param theGroup   [in]  id of the group where the feature should be placed
55   /// \return \c true if the feature has been created or updated
56   SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature, const GroupID& theGroup = GID_UNKNOWN);
57   /// \brief Convert attribute to the form applicable for specific solver and map it
58   /// \param theFeature [in]  feature to convert
59   /// \return \c true if the attribute has been created or updated
60   SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute, const GroupID& theGroup = GID_UNKNOWN);
61
62   /// \brief Returns constraint related to corresponding constraint
63   SKETCHSOLVER_EXPORT
64     const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
65
66   /// \brief Returns entity related to corresponding feature
67   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
68   /// \brief Returns entity related to corresponding attribute
69   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
70
71   /// \brief Return parsed sketch entity
72   const EntityWrapperPtr& sketch() const;
73   /// \brief Set parsed sketch entity.
74   /// Be careful, this method does not update fields of the storage specific for the solver.
75   /// Does not update if the sketch already exists.
76   void setSketch(const EntityWrapperPtr& theSketch);
77
78   /// \brief Mark two points as coincident
79   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
80
81   /// \brief Shows the storage has any constraint twice
82   virtual bool hasDuplicatedConstraint() const = 0;
83
84   /// \brief Removes constraint from the storage
85   /// \return \c true if the constraint and all its parameters are removed successfully
86   virtual bool removeConstraint(ConstraintPtr theConstraint) = 0;
87   /// \brief Removes feature from the storage
88   /// \return \c true if the feature and its attributes are removed successfully;
89   ///         \c false if the feature or any it attribute is used by remaining constraints.
90   virtual bool removeEntity(FeaturePtr theFeature) = 0;
91   /// \brief Removes attribute from the storage
92   /// \return \c true if the attribute is not used by remaining features and constraints
93   virtual bool removeEntity(AttributePtr theAttribute) = 0;
94
95   /// \brief Remove all features became invalid
96   SKETCHSOLVER_EXPORT void removeInvalidEntities();
97
98   /// \brief Mark specified constraint as temporary
99   virtual void setTemporary(ConstraintPtr theConstraint) = 0;
100   /// \brief Returns number of temporary constraints
101   virtual size_t nbTemporary() const = 0;
102   /// \brief Remove temporary constraints
103   /// \param theNbConstraints [in]  number of temporary constraints to be deleted
104   /// \return number of remaining temporary constraints
105   virtual size_t removeTemporary(size_t theNbConstraints = 1) = 0;
106
107   /// \brief Check whether the feature or its attributes are used by this storage
108   /// \param theFeature [in]  feature to be checked
109   /// \return \c true if the feature interacts with the storage
110   bool isInteract(const FeaturePtr& theFeature) const;
111   /// \brief Check whether the attribute is used by this storage
112   /// \param theAttribute [in]  attribute to be checked
113   /// \return \c true if the attribute interacts with the storage
114   bool isInteract(const AttributePtr& theAttribute) const;
115
116   /// \brief Check the features is not removed
117   bool isConsistent() const;
118
119   /// \brief Shows the sketch should be resolved
120   virtual bool isNeedToResolve()
121   { return myNeedToResolve; }
122   /// \brief Changes the flag of group to be resolved
123   void setNeedToResolve(bool theFlag)
124   { myNeedToResolve = theFlag; }
125
126   /// \brief Initialize solver by constraints, entities and parameters
127   virtual void initializeSolver(SolverPtr theSolver) = 0;
128
129   /// \brief Update SketchPlugin features after resolving constraints
130   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
131   virtual void refresh(bool theFixedOnly = false) const = 0;
132
133   /// \brief Check if some parameters or entities are returned
134   ///        to the current group after removing temporary constraints
135   virtual void verifyFixed() = 0;
136
137   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
138   ///        shows the distance from the start point.
139   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
140                                                 double theCoeff) = 0;
141
142 protected:
143   /// \brief Change mapping feature from SketchPlugin and
144   ///        the entity applicable for corresponding solver.
145   /// \param theFeature      [in]  original SketchPlugin feature
146   /// \param theSolverEntity [in]  solver's entity, created outside
147   SKETCHSOLVER_EXPORT
148     void addEntity(FeaturePtr       theFeature,
149                    EntityWrapperPtr theSolverEntity);
150
151   /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
152   /// \param theAttribute    [in]  original attribute
153   /// \param theSolverEntity [in]  solver's entity, created outside
154   SKETCHSOLVER_EXPORT
155     void addEntity(AttributePtr     theAttribute,
156                    EntityWrapperPtr theSolverEntity);
157
158   /// \brief Update constraint's data
159   /// \return \c true if any value is updated
160   virtual bool update(ConstraintWrapperPtr& theConstraint) = 0;
161   /// \brief Update entity's data
162   /// \return \c true if any value is updated
163   virtual bool update(EntityWrapperPtr& theEntity) = 0;
164   /// \brief Update parameter's data
165   /// \return \c true if the value of parameter is updated
166   virtual bool update(ParameterWrapperPtr& theParameter) = 0;
167
168   /// \brief Remove constraint
169   /// \return \c true if the constraint and all its parameters are removed successfully
170   virtual bool remove(ConstraintWrapperPtr theConstraint) = 0;
171   /// \brief Remove entity
172   /// \return \c true if the entity and all its parameters are removed successfully
173   virtual bool remove(EntityWrapperPtr theEntity) = 0;
174   /// \brief Remove parameter
175   /// \return \c true if the parameter has been removed
176   virtual bool remove(ParameterWrapperPtr theParameter) = 0;
177
178   /// \brief Update the group for the given entity, its sub-entities and parameters
179   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
180   /// \brief Update the group for the given parameter
181   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
182
183   /// \brief Block or unblock events when refreshing features
184   SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked) const;
185
186 private:
187   /// \brief Find the normal of the sketch
188   EntityWrapperPtr getNormal() const;
189
190 protected:
191   GroupID myGroupID; ///< identifier of the group, this storage belongs to
192
193   /// map SketchPlugin constraint to a list of solver's constraints
194   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
195   /// map SketchPlugin feature to solver's entity
196   std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
197   /// map attribute to solver's entity
198   std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
199
200   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
201 };
202
203 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
204
205 #endif