Salome HOME
c4dd63ddb7200c0d9a73b108d256dc0d094aed82
[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_ConstraintDistance;
22 class SketchSolver_ConstraintFixedArcRadius;
23 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
24
25
26 /** \class   SketchSolver_Storage
27  *  \ingroup Plugins
28  *  \brief   Interface to map SketchPlugin features to the entities of corresponding solver.
29  */
30 class SketchSolver_Storage
31 {
32 private:
33   SketchSolver_Storage();
34   SketchSolver_Storage(const SketchSolver_Storage&);
35   SketchSolver_Storage& operator=(const SketchSolver_Storage&);
36
37 public:
38   SketchSolver_Storage(const GroupID& theGroup)
39     : myGroupID(theGroup),
40       mySketchID(EID_UNKNOWN),
41       myNeedToResolve(false),
42       myEventsBlocked(false),
43       myExistArc(false)
44   {}
45
46   /// \brief Change mapping between constraint from SketchPlugin and
47   ///        a constraint applicable for corresponding solver.
48   /// \param theConstraint       [in]   original SketchPlugin constraint
49   /// \param theSolverConstraint [in]   solver's constraints
50   SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr        theConstraint,
51                                          ConstraintWrapperPtr theSolverConstraints);
52   /// \brief Change mapping between constraint from SketchPlugin and
53   ///        the list of constraints applicable for corresponding solver.
54   /// \param theConstraint        [in]   original SketchPlugin constraint
55   /// \param theSolverConstraints [in]   list of solver's constraints
56   SKETCHSOLVER_EXPORT virtual
57     void addConstraint(ConstraintPtr                   theConstraint,
58                        std::list<ConstraintWrapperPtr> theSolverConstraints);
59
60   /// \brief Convert feature to the form applicable for specific solver and map it
61   /// \param theFeature [in]  feature to convert
62   /// \param theGroup   [in]  id of the group where the feature should be placed
63   /// \param theForce   [in]  forced feature creation
64   /// \return \c true if the feature has been created or updated
65   SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature,
66                                   const GroupID& theGroup = GID_UNKNOWN, bool theForce = false);
67
68   /// \brief Convert attribute to the form applicable for specific solver and map it
69   /// \param theAttribute [in]  attribute to convert
70   /// \param theGroup     [in]  id of the group where the feature should be placed
71   /// \param theForce     [in]  forced feature creation
72   /// \return \c true if the attribute has been created or updated
73   SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute,
74                                   const GroupID& theGroup = GID_UNKNOWN, bool theForce = false);
75
76   /// \brief Returns constraint related to corresponding constraint
77   SKETCHSOLVER_EXPORT
78     const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
79
80   /// \brief Returns entity related to corresponding feature
81   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
82   /// \brief Returns entity related to corresponding attribute
83   SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
84
85   /// \brief Return parsed sketch entity
86   const EntityWrapperPtr& sketch() const;
87   /// \brief Set parsed sketch entity.
88   /// Be careful, this method does not update fields of the storage specific for the solver.
89   /// Does not update if the sketch already exists.
90   void setSketch(const EntityWrapperPtr& theSketch);
91
92   /// \brief Mark two points as coincident
93   virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
94
95   /// \brief Shows the storage has any constraint twice
96   virtual bool hasDuplicatedConstraint() const = 0;
97
98   /// \brief Removes constraint from the storage
99   /// \return \c true if the constraint and all its parameters are removed successfully
100   SKETCHSOLVER_EXPORT bool removeConstraint(ConstraintPtr theConstraint);
101   /// \brief Removes feature from the storage
102   /// \return \c true if the feature and its attributes are removed successfully;
103   ///         \c false if the feature or any it attribute is used by remaining constraints.
104   SKETCHSOLVER_EXPORT bool removeEntity(FeaturePtr theFeature);
105   /// \brief Removes attribute from the storage
106   /// \return \c true if the attribute is not used by remaining features and constraints
107   SKETCHSOLVER_EXPORT bool removeEntity(AttributePtr theAttribute);
108
109   /// \brief Remove all features became invalid
110   SKETCHSOLVER_EXPORT void removeInvalidEntities();
111
112   /// \brief Check whether the feature or its attributes are used by this storage
113   /// \param theFeature [in]  feature to be checked
114   /// \return \c true if the feature interacts with the storage
115   bool isInteract(const FeaturePtr& theFeature) const;
116   /// \brief Check whether the attribute is used by this storage
117   /// \param theAttribute [in]  attribute to be checked
118   /// \return \c true if the attribute interacts with the storage
119   bool isInteract(const AttributePtr& theAttribute) const;
120
121   /// \brief Check the features is not removed
122   bool isConsistent() const;
123
124   /// \brief Check the storage has constraints
125   bool isEmpty() const
126   { return myConstraintMap.empty(); }
127
128   /// \brief Check the entity is fixed.
129   ///        If the point is under verification, all coincident points are checked too.
130   SKETCHSOLVER_EXPORT bool isFixed(EntityWrapperPtr theEntity) const;
131
132   /// \brief Shows the sketch should be resolved
133   virtual bool isNeedToResolve()
134   { return myNeedToResolve; }
135   /// \brief Changes the flag of group to be resolved
136   void setNeedToResolve(bool theFlag)
137   { myNeedToResolve = theFlag; }
138
139   /// \brief Initialize solver by constraints, entities and parameters
140   virtual void initializeSolver(SolverPtr theSolver) = 0;
141   /// \brief Return list of conflicting constraints
142   std::set<ObjectPtr> getConflictingConstraints(SolverPtr theSolver) const;
143
144   /// \brief Update SketchPlugin features after resolving constraints
145   /// \param theFixedOnly [in]  if \c true the fixed points will be updated only
146   virtual void refresh(bool theFixedOnly = false) const = 0;
147
148   /// \brief Check if some parameters or entities are returned
149   ///        to the current group after removing temporary constraints
150   virtual void verifyFixed() = 0;
151
152   /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
153   ///        shows the distance from the start point.
154   virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
155                                                 double theCoeff) = 0;
156
157   /// \brief Block or unblock events when refreshing features
158   SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked);
159   /// \brief Shows the events are blocked for the features in the storage
160   bool isEventsBlocked() const
161   { return myEventsBlocked; }
162
163 protected:
164   /// \brief Change mapping feature from SketchPlugin and
165   ///        the entity applicable for corresponding solver.
166   /// \param theFeature      [in]  original SketchPlugin feature
167   /// \param theSolverEntity [in]  solver's entity, created outside
168   SKETCHSOLVER_EXPORT
169     void addEntity(FeaturePtr       theFeature,
170                    EntityWrapperPtr theSolverEntity);
171
172   /// \brief Change mapping attribute of a feature and the entity
173   /// applicable for corresponding solver.
174   /// \param theAttribute    [in]  original attribute
175   /// \param theSolverEntity [in]  solver's entity, created outside
176   SKETCHSOLVER_EXPORT
177     void addEntity(AttributePtr     theAttribute,
178                    EntityWrapperPtr theSolverEntity);
179
180   /// \brief Update constraint's data
181   /// \return \c true if any value is updated
182   virtual bool update(ConstraintWrapperPtr theConstraint) = 0;
183   /// \brief Update entity's data
184   /// \return \c true if any value is updated
185   virtual bool update(EntityWrapperPtr theEntity) = 0;
186   /// \brief Update parameter's data
187   /// \return \c true if the value of parameter is updated
188   virtual bool update(ParameterWrapperPtr theParameter) = 0;
189
190   /// \brief Remove constraint
191   /// \return \c true if the constraint and all its parameters are removed successfully
192   SKETCHSOLVER_EXPORT virtual bool remove(ConstraintWrapperPtr theConstraint);
193   /// \brief Remove entity
194   /// \return \c true if the entity and all its parameters are removed successfully
195   SKETCHSOLVER_EXPORT virtual bool remove(EntityWrapperPtr theEntity);
196   /// \brief Remove parameter
197   /// \return \c true if the parameter has been removed
198   virtual bool remove(ParameterWrapperPtr theParameter) = 0;
199
200   /// \brief Remove point-point coincidence
201   SKETCHSOLVER_EXPORT bool removeCoincidence(ConstraintWrapperPtr theConstraint);
202
203   /// \brief Update the group for the given entity, its sub-entities and parameters
204   virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
205   /// \brief Update the group for the given parameter
206   virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
207
208   /// \brief Verify the feature or any its attribute is used by constraint
209   SKETCHSOLVER_EXPORT bool isUsed(FeaturePtr theFeature) const;
210   /// \brief Verify the attribute is used by constraint
211   SKETCHSOLVER_EXPORT bool isUsed(AttributePtr theAttirubute) const;
212
213   /// \brief Find arcs without corresponding entity applicable for the solver and build them
214   SKETCHSOLVER_EXPORT void processArcs();
215
216   /// \brief Replace entities by others
217   void replaceEntities(const std::map<EntityWrapperPtr, EntityWrapperPtr>& theChange);
218
219 private:
220   /// \brief Find the normal of the sketch
221   EntityWrapperPtr getNormal() const;
222
223 protected:
224   EntityID mySketchID;     ///< identifier of the sketch
225   GroupID myGroupID;       ///< identifier of the group, this storage belongs to
226   bool    myNeedToResolve; ///< parameters are changed and group needs to be resolved
227   bool    myEventsBlocked; ///< indicates that features do not send events
228   bool    myExistArc;      ///< the storage has any point of arc but not full arc, need to add it
229
230   /// map SketchPlugin constraint to a list of solver's constraints
231   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
232   /// map SketchPlugin feature to solver's entity
233   std::map<FeaturePtr, EntityWrapperPtr>                    myFeatureMap;
234   /// map attribute to solver's entity
235   std::map<AttributePtr, EntityWrapperPtr>                  myAttributeMap;
236
237   /// lists of coincident points (first is a master point, second is a set of slaves)
238   CoincidentPointsMap myCoincidentPoints;
239
240   // to be able to update entities from constraints
241   friend class SketchSolver_ConstraintDistance;
242   friend class SketchSolver_ConstraintFixedArcRadius;
243 };
244
245 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;
246
247 #endif