1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Storage.h
4 // Created: 30 Nov 2015
5 // Author: Artem ZHIDKOV
7 #ifndef SketchSolver_Storage_H_
8 #define SketchSolver_Storage_H_
10 #include <SketchSolver.h>
11 #include <SketchSolver_IConstraintWrapper.h>
12 #include <SketchSolver_IEntityWrapper.h>
13 #include <SketchSolver_IParameterWrapper.h>
14 #include <SketchSolver_ISolver.h>
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Feature.h>
19 #include <SketchPlugin_Constraint.h>
21 class SketchSolver_ConstraintDistance;
22 class SketchSolver_ConstraintFixedArcRadius;
23 typedef std::map<EntityWrapperPtr, std::set<EntityWrapperPtr> > CoincidentPointsMap;
26 /** \class SketchSolver_Storage
28 * \brief Interface to map SketchPlugin features to the entities of corresponding solver.
30 class SketchSolver_Storage
33 SketchSolver_Storage();
34 SketchSolver_Storage(const SketchSolver_Storage&);
35 SketchSolver_Storage& operator=(const SketchSolver_Storage&);
38 SketchSolver_Storage(const GroupID& theGroup)
39 : myGroupID(theGroup),
40 myNeedToResolve(false),
41 myEventsBlocked(false),
45 /// \brief Change mapping between constraint from SketchPlugin and
46 /// a constraint applicable for corresponding solver.
47 /// \param theConstraint [in] original SketchPlugin constraint
48 /// \param theSolverConstraint [in] solver's constraints
49 SKETCHSOLVER_EXPORT void addConstraint(ConstraintPtr theConstraint,
50 ConstraintWrapperPtr theSolverConstraints);
51 /// \brief Change mapping between constraint from SketchPlugin and
52 /// the list of constraints applicable for corresponding solver.
53 /// \param theConstraint [in] original SketchPlugin constraint
54 /// \param theSolverConstraints [in] list of solver's constraints
55 SKETCHSOLVER_EXPORT virtual
56 void addConstraint(ConstraintPtr theConstraint,
57 std::list<ConstraintWrapperPtr> theSolverConstraints);
59 /// \brief Convert feature to the form applicable for specific solver and map it
60 /// \param theFeature [in] feature to convert
61 /// \param theGroup [in] id of the group where the feature should be placed
62 /// \return \c true if the feature has been created or updated
63 SKETCHSOLVER_EXPORT bool update(FeaturePtr theFeature, const GroupID& theGroup = GID_UNKNOWN);
64 /// \brief Convert attribute to the form applicable for specific solver and map it
65 /// \param theFeature [in] feature to convert
66 /// \return \c true if the attribute has been created or updated
67 SKETCHSOLVER_EXPORT bool update(AttributePtr theAttribute, const GroupID& theGroup = GID_UNKNOWN);
69 /// \brief Returns constraint related to corresponding constraint
71 const std::list<ConstraintWrapperPtr>& constraint(const ConstraintPtr& theConstraint) const;
73 /// \brief Returns entity related to corresponding feature
74 SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const FeaturePtr& theFeature) const;
75 /// \brief Returns entity related to corresponding attribute
76 SKETCHSOLVER_EXPORT const EntityWrapperPtr& entity(const AttributePtr& theAttribute) const;
78 /// \brief Return parsed sketch entity
79 const EntityWrapperPtr& sketch() const;
80 /// \brief Set parsed sketch entity.
81 /// Be careful, this method does not update fields of the storage specific for the solver.
82 /// Does not update if the sketch already exists.
83 void setSketch(const EntityWrapperPtr& theSketch);
85 /// \brief Mark two points as coincident
86 virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave) = 0;
88 /// \brief Shows the storage has any constraint twice
89 virtual bool hasDuplicatedConstraint() const = 0;
91 /// \brief Removes constraint from the storage
92 /// \return \c true if the constraint and all its parameters are removed successfully
93 SKETCHSOLVER_EXPORT bool removeConstraint(ConstraintPtr theConstraint);
94 /// \brief Removes feature from the storage
95 /// \return \c true if the feature and its attributes are removed successfully;
96 /// \c false if the feature or any it attribute is used by remaining constraints.
97 SKETCHSOLVER_EXPORT bool removeEntity(FeaturePtr theFeature);
98 /// \brief Removes attribute from the storage
99 /// \return \c true if the attribute is not used by remaining features and constraints
100 SKETCHSOLVER_EXPORT bool removeEntity(AttributePtr theAttribute);
102 /// \brief Remove all features became invalid
103 SKETCHSOLVER_EXPORT void removeInvalidEntities();
105 /// \brief Check whether the feature or its attributes are used by this storage
106 /// \param theFeature [in] feature to be checked
107 /// \return \c true if the feature interacts with the storage
108 bool isInteract(const FeaturePtr& theFeature) const;
109 /// \brief Check whether the attribute is used by this storage
110 /// \param theAttribute [in] attribute to be checked
111 /// \return \c true if the attribute interacts with the storage
112 bool isInteract(const AttributePtr& theAttribute) const;
114 /// \brief Check the features is not removed
115 bool isConsistent() const;
117 /// \brief Check the storage has constraints
119 { return myConstraintMap.empty(); }
121 /// \brief Check the entity is fixed.
122 /// If the point is under verification, all coincident points are checked too.
123 SKETCHSOLVER_EXPORT bool isFixed(EntityWrapperPtr theEntity) const;
125 /// \brief Shows the sketch should be resolved
126 virtual bool isNeedToResolve()
127 { return myNeedToResolve; }
128 /// \brief Changes the flag of group to be resolved
129 void setNeedToResolve(bool theFlag)
130 { myNeedToResolve = theFlag; }
132 /// \brief Initialize solver by constraints, entities and parameters
133 virtual void initializeSolver(SolverPtr theSolver) = 0;
134 /// \brief Return list of conflicting constraints
135 std::set<ObjectPtr> getConflictingConstraints(SolverPtr theSolver) const;
137 /// \brief Update SketchPlugin features after resolving constraints
138 /// \param theFixedOnly [in] if \c true the fixed points will be updated only
139 virtual void refresh(bool theFixedOnly = false) const = 0;
141 /// \brief Check if some parameters or entities are returned
142 /// to the current group after removing temporary constraints
143 virtual void verifyFixed() = 0;
145 /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
146 /// shows the distance from the start point.
147 virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase,
148 double theCoeff) = 0;
150 /// \brief Block or unblock events when refreshing features
151 SKETCHSOLVER_EXPORT void blockEvents(bool isBlocked);
152 /// \brief Shows the events are blocked for the features in the storage
153 bool isEventsBlocked() const
154 { return myEventsBlocked; }
157 /// \brief Change mapping feature from SketchPlugin and
158 /// the entity applicable for corresponding solver.
159 /// \param theFeature [in] original SketchPlugin feature
160 /// \param theSolverEntity [in] solver's entity, created outside
162 void addEntity(FeaturePtr theFeature,
163 EntityWrapperPtr theSolverEntity);
165 /// \brief Change mapping attribute of a feature and the entity applicable for corresponding solver.
166 /// \param theAttribute [in] original attribute
167 /// \param theSolverEntity [in] solver's entity, created outside
169 void addEntity(AttributePtr theAttribute,
170 EntityWrapperPtr theSolverEntity);
172 /// \brief Update constraint's data
173 /// \return \c true if any value is updated
174 virtual bool update(ConstraintWrapperPtr theConstraint) = 0;
175 /// \brief Update entity's data
176 /// \return \c true if any value is updated
177 virtual bool update(EntityWrapperPtr theEntity) = 0;
178 /// \brief Update parameter's data
179 /// \return \c true if the value of parameter is updated
180 virtual bool update(ParameterWrapperPtr theParameter) = 0;
182 /// \brief Remove constraint
183 /// \return \c true if the constraint and all its parameters are removed successfully
184 SKETCHSOLVER_EXPORT virtual bool remove(ConstraintWrapperPtr theConstraint);
185 /// \brief Remove entity
186 /// \return \c true if the entity and all its parameters are removed successfully
187 SKETCHSOLVER_EXPORT virtual bool remove(EntityWrapperPtr theEntity);
188 /// \brief Remove parameter
189 /// \return \c true if the parameter has been removed
190 virtual bool remove(ParameterWrapperPtr theParameter) = 0;
192 /// \brief Update the group for the given entity, its sub-entities and parameters
193 virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup) = 0;
194 /// \brief Update the group for the given parameter
195 virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup) = 0;
197 /// \brief Verify the feature or any its attribute is used by constraint
198 SKETCHSOLVER_EXPORT bool isUsed(FeaturePtr theFeature) const;
199 /// \brief Verify the attribute is used by constraint
200 SKETCHSOLVER_EXPORT bool isUsed(AttributePtr theAttirubute) const;
202 /// \brief Find arcs without corresponding entity applicable for the solver and build them
203 SKETCHSOLVER_EXPORT void processArcs();
206 /// \brief Find the normal of the sketch
207 EntityWrapperPtr getNormal() const;
210 GroupID myGroupID; ///< identifier of the group, this storage belongs to
211 bool myNeedToResolve; ///< parameters are changed and group needs to be resolved
212 bool myEventsBlocked; ///< indicates that features do not send events
213 bool myExistArc; ///< the storage has any point of arc but not full arc, need to add it
215 /// map SketchPlugin constraint to a list of solver's constraints
216 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> > myConstraintMap;
217 /// map SketchPlugin feature to solver's entity
218 std::map<FeaturePtr, EntityWrapperPtr> myFeatureMap;
219 /// map attribute to solver's entity
220 std::map<AttributePtr, EntityWrapperPtr> myAttributeMap;
222 CoincidentPointsMap myCoincidentPoints; ///< lists of coincident points (first is a master point, second is a set of slaves)
224 // to be able to update entities from constraints
225 friend class SketchSolver_ConstraintDistance;
226 friend class SketchSolver_ConstraintFixedArcRadius;
229 typedef std::shared_ptr<SketchSolver_Storage> StoragePtr;