1 // File: SketchSolver_ConstraintManager.h
2 // Created: 08 May 2014
3 // Author: Artem ZHIDKOV
5 #ifndef SketchSolver_ConstraintManager_Headerfile
6 #define SketchSolver_ConstraintManager_Headerfile
8 #include "SketchSolver.h"
9 #include <SketchSolver_Solver.h>
11 #include <Events_Listener.h>
12 #include <SketchPlugin_Constraint.h>
21 // Unknown constraint (for error reporting)
22 #define SLVS_C_UNKNOWN 0
24 #define SLVS_E_UNKNOWN 0
26 /** \class SketchSolver_ConstraintManager
28 * \brief Listens the changes of SketchPlugin features and transforms the Constraint
29 * feature into the format understandable by SolveSpace library.
31 * Constraints created for SolveSpace library are divided into the groups.
32 * The division order based on connectedness of the features by the constraints.
33 * The groups may be fused or separated according to the new constraints.
35 * \remark This is a singleton.
37 class SketchSolver_ConstraintManager : public Events_Listener
40 /** \brief Main method to create constraint manager
41 * \return pointer to the singleton
43 static SketchSolver_ConstraintManager* Instance();
45 /** \brief Implementation of Event Listener method
46 * \param[in] theMessage the data of the event
48 virtual void processEvent(const Events_Message* theMessage);
51 SketchSolver_ConstraintManager();
52 ~SketchSolver_ConstraintManager();
54 /** \brief Adds or updates a constraint in the suitable group
55 * \param[in] theConstraint constraint to be changed
56 * \return \c true if the constraint changed successfully
58 bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
60 /** \brief Removes a constraint from the manager
61 * \param[in] theConstraint constraint to be removed
62 * \return \c true if the constraint removed successfully
64 bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
66 /** \brief Adds or updates a workplane in the manager
67 * \param[in] theSketch the feature to create or update workplane
68 * \return \c true if the workplane cahnged successfully
70 bool changeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
72 /** \brief Removes a workplane from the manager.
73 * All groups based on such workplane will be removed too.
74 * \param[in] theSketch the feature to be removed
75 * \return \c true if the workplane removed successfully
77 bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
79 /** \brief Updates entity which is neither workplane nor constraint
80 * \param[in] theFeature entity to be updated
82 void updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
84 /** \brief Goes through the list of groups and solve the constraints
86 void ResolveConstraints();
89 class SketchSolver_ConstraintGroup;
91 /** \brief Searches list of groups which interact with specified constraint
92 * \param[in] theConstraint constraint to be found
93 * \param[out] theGroups list of group indexes interacted with constraint
95 void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
96 std::vector<Slvs_hGroup>& theGroupIDs) const;
98 /** \brief Searches in the list of groups the workplane which constains specified constraint
99 * \param[in] theConstraint constraint to be found
100 * \return workplane contains the constraint
102 boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
103 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
106 static SketchSolver_ConstraintManager* _self; ///< Self pointer to implement singleton functionality
107 std::vector<SketchSolver_ConstraintGroup*> myGroups; ///< Groups of constraints
111 /** \class SketchSolver_ConstraintGroup
113 * \brief Keeps the group of constraints which based on the same entities
115 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
118 /** \brief New group based on specified workplane
120 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
122 ~SketchSolver_ConstraintGroup();
124 /// \brief Returns group's unique identifier
125 const Slvs_hGroup& getId() const
128 /** \brief Adds or updates a constraint in the group
129 * \param[in] theConstraint constraint to be changed
130 * \return \c true if the constraint added or updated successfully
132 bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
134 /** \brief Removes a constraint into the group
135 * \param[in] theConstraint constraint to be removed
136 * \return \c true if the constraint removed successfully
138 bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
140 /** \brief Verifies the constraint uses the objects from this group
141 * \param[in] theConstraint constraint for verification of interaction
142 * \return \c true if the constrained objects are used in current group
144 bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
146 /** \brief Verifies the specified workplane is the same as a base workplane for this group
147 * \param[in] theWorkplane workplane to be compared
148 * \return \c true if workplanes are the same
150 bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
152 boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
155 /** \brief Update parameters of workplane. Should be called when Update event is coming.
156 * \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
158 bool updateWorkplane();
160 /** \brief If the entity is in this group it will updated
161 * \param[in] theEntity attribute, which values should update SolveSpace entity
163 void updateEntityIfPossible(boost::shared_ptr<ModelAPI_Attribute> theEntity);
165 /** \brief Start solution procedure if necessary and update attributes of features
167 void ResolveConstraints();
170 /** \brief Adds or updates an entity in the group
172 * The parameters of entity will be parsed and added to the list of SolveSpace parameters.
173 * Parameters of certain entity will be placed sequentially in the list.
175 * \param[in] theEntity the object of constraint
176 * \return identifier of changed entity or 0 if entity could not be changed
178 Slvs_hEntity changeEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
180 /** \brief Adds or updates a normal in the group
182 * Normal is a special entity in SolveSpace, which defines a direction in 3D and
183 * a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
185 * To define a normal there should be specified two coordinate axis
186 * on the plane transversed to created normal.
188 * \param[in] theDirX first coordinate axis of the plane
189 * \param[in] theDirY second coordinate axis of the plane
190 * \param[in] theNorm attribute for the normal (used to identify newly created entity)
191 * \return identifier of created or updated normal
193 Slvs_hEntity changeNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX,
194 boost::shared_ptr<ModelAPI_Attribute> theDirY,
195 boost::shared_ptr<ModelAPI_Attribute> theNorm);
197 /** \brief Adds or updates a parameter in the group
198 * \param[in] theParam the value of parameter
199 * \param[in] thePrmIter the cell in the list of parameters which should be changed
200 * (the iterator will be increased if it does not reach the end of the list)
201 * \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
203 Slvs_hParam changeParameter(const double& theParam,
204 std::vector<Slvs_Param>::const_iterator& thePrmIter);
206 /** \brief Compute constraint type according to SolveSpace identifiers
207 * and verify that constraint parameters are correct
208 * \param[in] theConstraint constraint which type should be determined
209 * \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
211 int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
213 /** \brief Change values of attribute by parameters received from SolveSpace solver
214 * \param[in,out] theAttribute pointer to the attribute to be changed
215 * \param[in] theEntityID identifier of SolveSpace entity, which contains updated data
217 void updateAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID);
220 /** \brief Creates a workplane from the sketch parameters
221 * \param[in] theSketch parameters of workplane are the attributes of this sketch
222 * \return \c true if success, \c false if workplane parameters are not consistent
224 bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
227 // SolveSpace entities
228 Slvs_hGroup myID; ///< the index of the group
229 Slvs_Entity myWorkplane; ///< Workplane for the current group
230 std::vector<Slvs_Param> myParams; ///< List of parameters of the constraints
231 Slvs_hParam myParamMaxID; ///< Actual maximal ID of parameters (not equal to myParams size)
232 std::vector<Slvs_Entity> myEntities; ///< List of entities of the constaints
233 Slvs_hEntity myEntityMaxID; ///< Actual maximal ID of entities (not equal to myEntities size)
234 std::vector<Slvs_Constraint> myConstraints; ///< List of constraints in SolveSpace format
235 Slvs_hConstraint myConstrMaxID; ///< Actual maximal ID of constraints (not equal to myConstraints size)
236 bool myNeedToSolve; ///< Indicator that something changed in the group and constraint system need to be rebuilt
238 SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints
240 // SketchPlugin entities
241 boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
242 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>
243 myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
244 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
245 myEntityMap; ///< The map between parameters of constraints and their equivalent SolveSpace entities