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