]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Storage.h
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 Returns parameter related to corresponding scalar attribute
72 ////  SKETCHSOLVER_EXPORT
73 ////    const ParameterWrapperPtr& parameter(const AttributeDoublePtr& theAttribute) const;
74
75   /// \brief Return parsed sketch entity
76   const EntityWrapperPtr& sketch() const;
77   /// \brief Set parsed sketch entity.
78   /// Be careful, this method does not update fields of the storage specific for the solver.
79   /// Does not update if the sketch already exists.
80   void setSketch(const EntityWrapperPtr& theSketch);
81
82   /// \brief Mark two points as coincident
83   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
84
85   /// \brief Shows the storage has any constraint twice
86   virtual bool hasDuplicatedConstraint() const = 0;
87
88   /// \brief Removes constraint from the storage
89   /// \return \c true if the constraint and all its parameters are removed successfully
90   virtual bool removeConstraint(ConstraintPtr theConstraint) = 0;
91   /// \brief Removes feature from the storage
92   /// \return \c true if the feature and its attributes are removed successfully;
93   ///         \c false if the feature or any it attribute is used by remaining constraints.
94   virtual bool removeEntity(FeaturePtr theFeature) = 0;
95   /// \brief Removes attribute from the storage
96   /// \return \c true if the attribute is not used by remaining features and constraints
97   virtual bool removeEntity(AttributePtr theAttribute) = 0;
98
99   /// \brief Remove all features became invalid
100   SKETCHSOLVER_EXPORT void removeInvalidEntities();
101
102   /// \brief Mark specified constraint as temporary
103   virtual void setTemporary(ConstraintPtr theConstraint) = 0;
104   /// \brief Returns number of temporary constraints
105   virtual size_t nbTemporary() const = 0;
106   /// \brief Remove temporary constraints
107   /// \param theNbConstraints [in]  number of temporary constraints to be deleted
108   /// \return number of remaining temporary constraints
109   virtual size_t removeTemporary(size_t theNbConstraints = 1) = 0;
110
111   /// \brief Check whether the feature or its attributes are used by this storage
112   /// \param theFeature [in]  feature to be checked
113   /// \return \c true if the feature interacts with the storage
114   bool isInteract(const FeaturePtr& theFeature) const;
115   /// \brief Check whether the attribute is used by this storage
116   /// \param theAttribute [in]  attribute to be checked
117   /// \return \c true if the attribute interacts with the storage
118   bool isInteract(const AttributePtr& theAttribute) const;
119
120   /// \brief Check the features is not removed
121   bool isConsistent() const;
122
123   /// \brief Shows the sketch should be resolved
124   virtual bool isNeedToResolve()
125   { return myNeedToResolve; }
126   /// \brief Changes the flag of group to be resolved
127   void setNeedToResolve(bool theFlag)
128   { myNeedToResolve = theFlag; }
129
130   /// \brief Initialize solver by constraints, entities and parameters
131   virtual void initializeSolver(SolverPtr theSolver) = 0;
132
133   /// \brief Update SketchPlugin features after resolving constraints
134   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
135   virtual void refresh(bool theFixedOnly = false) const = 0;
136
137   /// \brief Check if some parameters or entities are returned
138   ///        to the current group after removing temporary constraints
139   virtual void verifyFixed() = 0;
140
141   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
142   ///        shows the distance from the start point.
143   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
144                                                 double theCoeff) = 0;
145
146 protected:
147   /// \brief Change mapping feature from SketchPlugin and
148   ///        the entity applicable for corresponding solver.
149   /// \param theFeature      [in]  original SketchPlugin feature
150   /// \param theSolverEntity [in]  solver's entity, created outside
151   SKETCHSOLVER_EXPORT
152     void addEntity(FeaturePtr       theFeature,
153                    EntityWrapperPtr theSolverEntity);
154
155   /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
156   /// \param theAttribute    [in]  original attribute
157   /// \param theSolverEntity [in]  solver's entity, created outside
158   SKETCHSOLVER_EXPORT
159     void addEntity(AttributePtr     theAttribute,
160                    EntityWrapperPtr theSolverEntity);
161
162 ////  /// \brief Change mapping scalar attribute and the parameter applicable for corresponding solver.
163 ////  /// \param theValue       [in]  original attribute
164 ////  /// \param theSolverParam [in]  solver's parameter, created outside
165 ////  SKETCHSOLVER_EXPORT
166 ////    void addParameter(AttributeDoublePtr  theValue,
167 ////                      ParameterWrapperPtr theSolverParam);
168
169   /// \brief Update constraint's data
170   /// \return \c true if any value is updated
171   virtual bool update(ConstraintWrapperPtr& theConstraint) = 0;
172   /// \brief Update entity's data
173   /// \return \c true if any value is updated
174   virtual bool update(EntityWrapperPtr& theEntity) = 0;
175   /// \brief Update parameter's data
176   /// \return \c true if the value of parameter is updated
177   virtual bool update(ParameterWrapperPtr& theParameter) = 0;
178
179   /// \brief Remove constraint
180   /// \return \c true if the constraint and all its parameters are removed successfully
181   virtual bool remove(ConstraintWrapperPtr theConstraint) = 0;
182   /// \brief Remove entity
183   /// \return \c true if the entity and all its parameters are removed successfully
184   virtual bool remove(EntityWrapperPtr theEntity) = 0;
185   /// \brief Remove parameter
186   /// \return \c true if the parameter has been removed
187   virtual bool remove(ParameterWrapperPtr theParameter) = 0;
188
189   /// \brief Update the group for the given entity, its sub-entities and parameters
190   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
191   /// \brief Update the group for the given parameter
192   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
193
194   /// \brief Block or unblock events when refreshing features
195   SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked) const;
196
197 private:
198   /// \brief Find the normal of the sketch
199   EntityWrapperPtr getNormal() const;
200
201 protected:
202   GroupID myGroupID; ///< identifier of the group, this storage belongs to
203
204   /// map SketchPlugin constraint to a list of solver's constraints
205   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
206   /// map SketchPlugin feature to solver's entity
207   std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
208   /// map attribute to solver's entity
209   std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
210 ////  /// map scalar attribute to solver's parameter
211 ////  std::map<AttributeDoublePtr, ParameterWrapperPtr>         myParametersMap;
212
213   bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
214 };
215
216 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
217
218 #endif