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"
10 #include <Events_Listener.h>
11 #include <SketchPlugin_Constraint.h>
13 // Need to be defined before including SolveSpace to avoid additional dependances on Windows platform
14 #if defined(WIN32) && !defined(HAVE_C99_INTEGER_TYPES)
15 typedef unsigned int UINT32;
24 // Unknown constraint (for error reporting)
25 #define SLVS_C_UNKNOWN 0
27 /** \class SketchSolver_ConstraintManager
29 * \brief Listens the changes of SketchPlugin features and transforms the Constraint
30 * feature into the format understandable by SolveSpace library.
32 * Constraints created for SolveSpace library are divided into the groups.
33 * The division order based on connectedness of the features by the constraints.
34 * The groups may be fused or separated according to the new constraints.
36 * \remark This is a singleton.
38 class SketchSolver_ConstraintManager : public Events_Listener
41 /** \brief Main method to create constraint manager
42 * \return pointer to the singleton
44 static SketchSolver_ConstraintManager* Instance();
46 /** \brief Implementation of Event Listener method
47 * \param[in] theMessage the data of the event
49 virtual void processEvent(const Events_Message* theMessage);
52 SketchSolver_ConstraintManager();
53 ~SketchSolver_ConstraintManager();
55 /** \brief Adds a constraint into the manager
56 * \param[in] theConstraint constraint to be added
57 * \return \c true if the constraint added successfully
59 bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
61 /** \brief Removes a constraint from the manager
62 * \param[in] theConstraint constraint to be removed
63 * \return \c true if the constraint removed successfully
65 bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
67 /** \brief Updates a constraint
68 * \param[in] theConstraint constraint to be updated
69 * \return \c true if the constraint was updated
71 bool updateConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
73 /** \brief Adds a workplane into the manager
74 * \param[in] theSketch the feature to create workplane
75 * \return \c true if the workplane added successfully
77 bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
79 /** \brief Removes a workplane from the manager.
80 * All groups based on such workplane will be removed too.
81 * \param[in] theSketch the feature to be removed
82 * \return \c true if the workplane removed successfully
84 bool removeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
86 /** \brief Updates a workplane
87 * \param[in] theSketch workplane to be updated
88 * \return \c true if the workplane was updated
90 bool updateWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
92 /** \brief Updates entity which is neither workplane nor constraint
93 * \param[in] theFeature entity to be updated
94 * \return \c true if the entity updated successfully
96 bool updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
99 class SketchSolver_ConstraintGroup;
101 /** \brief Searches list of groups which interact with specified constraint
102 * \param[in] theConstraint constraint to be found
103 * \param[out] theGroups list of group indexes interacted with constraint
105 void findGroups(boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
106 std::vector<Slvs_hGroup>& theGroupIDs) const;
108 /** \brief Searches in the list of groups the workplane which constains specified constraint
109 * \param[in] theConstraint constraint to be found
110 * \return workplane contains the constraint
112 boost::shared_ptr<SketchPlugin_Sketch> findWorkplaneForConstraint(
113 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
116 static SketchSolver_ConstraintManager* _self; ///< Self pointer to implement singleton functionality
117 std::vector<SketchSolver_ConstraintGroup> myGroups; ///< Groups of constraints
121 /** \class SketchSolver_ConstraintGroup
123 * \brief Keeps the group of constraints which based on the same entities
125 class SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup
128 /** \brief New group based on specified workplane
130 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
132 ~SketchSolver_ConstraintGroup();
134 /// \brief Returns group's unique identifier
135 const Slvs_hGroup& getId() const
138 /** \brief Adds a constraint into the group
139 * \param[in] theConstraint constraint to be added
140 * \return \c true if the constraint added successfully
142 bool addConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
144 /** \brief Removes a constraint into the group
145 * \param[in] theConstraint constraint to be removed
146 * \return \c true if the constraint removed successfully
148 bool removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
150 /** \brief Verifies the constraint uses the objects from this group
151 * \param[in] theConstraint constraint for verification of interaction
152 * \return \c true if the constrained objects are used in current group
154 bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
156 /** \brief Verifies the specified workplane is the same as a base workplane for this group
157 * \param[in] theWorkplane workplane to be compared
158 * \return \c true if workplanes are the same
160 bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const;
162 boost::shared_ptr<SketchPlugin_Sketch> getWorkplane() const
165 /** \brief Update parameters of workplane. Should be called when Update event is coming
166 * \param[in] theWorkplane workplane to be updated
167 * \return \c true if workplane updated successfully
169 bool updateWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane);
172 /** \brief Adds an entity into the group
174 * The parameters of entity will be parsed and added to the list of SolveSpace parameters.
175 * Parameters of certain entity will be placed sequentially in the list.
177 * \param[in] theEntity the object of constraint
178 * \return identifier of created entity or 0 if entity was not added
180 Slvs_hEntity addEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
182 /** \brief Adds a normal into the group
184 * Normal is a special entity in SolveSpace, which defines a direction in 3D and
185 * a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
187 * To define a normal there should be specified two coordinate axis
188 * on the plane transversed to created normal.
190 * \param[in] theDirX first coordinate axis of the plane
191 * \param[in] theDirY second coordinate axis of the plane
192 * \return identifier of created normal
194 Slvs_hEntity addNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX,
195 boost::shared_ptr<ModelAPI_Attribute> theDirY);
197 /** \brief Adds a parameter into the group
198 * \param[in] theParam parameter to be added
199 * \return identifier of created parameter or 0 if it was not added
201 Slvs_hParam addParameter(double theParam);
203 /** \brief Compute constraint type according to SolveSpace identifiers
204 * and verify that constraint parameters are correct
205 * \param[in] theConstraint constraint which type should be determined
206 * \return identifier of constraint type or SLVS_C_UNKNOWN if the type is wrong
208 int getConstraintType(const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const;
211 /** \brief Creates a workplane from the sketch parameters
212 * \param[in] theSketch parameters of workplane are the attributes of this sketch
213 * \return \c true if success
215 bool addWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch);
218 // SolveSpace entities
219 Slvs_hGroup myID; ///< the index of the group
220 Slvs_Entity myWorkplane; ///< Workplane for the current group
221 std::vector<Slvs_Param> myParams; ///< List of parameters of the constraints
222 Slvs_hParam myParamMaxID; ///< Actual maximal ID of parameters (not equal to myParams size)
223 std::vector<Slvs_Entity> myEntities; ///< List of entities of the constaints
224 Slvs_hEntity myEntityMaxID; ///< Actual maximal ID of entities (not equal to myEntities size)
225 std::vector<Slvs_Constraint> myConstraints; ///< List of constraints in SolveSpace format
226 Slvs_hConstraint myConstrMaxID; ///< Actual maximal ID of constraints (not equal to myConstraints size)
227 Slvs_System myConstrSet; ///< SolveSpace's set of equations obtained by constraints
229 // SketchPlugin entities
230 boost::shared_ptr<SketchPlugin_Sketch> mySketch; ///< Equivalent to workplane
231 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_Constraint>
232 myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints