1 // File: SketchSolver_ConstraintGroup.h
2 // Created: 27 May 2014
3 // Author: Artem ZHIDKOV
5 #ifndef SketchSolver_ConstraintGroup_H_
6 #define SketchSolver_ConstraintGroup_H_
8 #include "SketchSolver.h"
9 #include <SketchSolver_Solver.h>
11 #include <SketchPlugin_Constraint.h>
12 #include <ModelAPI_Data.h>
20 /** \class SketchSolver_ConstraintGroup
22 * \brief Keeps the group of constraints which based on the same entities
24 class SketchSolver_ConstraintGroup
27 /** \brief New group based on specified workplane.
28 * Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
29 * \remark Type of theSketch is not verified inside
31 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane);
33 ~SketchSolver_ConstraintGroup();
35 /// \brief Returns group's unique identifier
36 inline const Slvs_hGroup& getId() const
39 /// \brief Returns true if the group has no constraints yet
40 inline bool isEmpty() const
41 {return myConstraints.empty();}
43 /// \brief Check for valid sketch data
44 inline bool isWorkplaneValid() const
45 {return mySketch->data()->isValid();}
47 /** \brief Adds or updates a constraint in the group
48 * \param[in] theConstraint constraint to be changed
49 * \return \c true if the constraint added or updated successfully
51 bool changeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
53 /** \brief Verifies the constraint uses the objects from this group
54 * \param[in] theConstraint constraint for verification of interaction
55 * \return \c true if the constrained objects are used in current group
57 bool isInteract(boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const;
59 /** \brief Verifies the specified feature is equal to the base workplane for this group
60 * \param[in] theWorkplane the feature to be compared with base workplane
61 * \return \c true if workplanes are the same
63 bool isBaseWorkplane(boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const;
65 boost::shared_ptr<SketchPlugin_Feature> getWorkplane() const
68 /** \brief Update parameters of workplane. Should be called when Update event is coming.
69 * \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
71 bool updateWorkplane();
73 /** \brief If the entity is in this group it will updated
74 * \param[in] theEntity attribute, which values should update SolveSpace entity
76 void updateEntityIfPossible(boost::shared_ptr<ModelAPI_Attribute> theEntity);
78 /** \brief Searches invalid features and constraints in the group and avoids them
79 * \return \c true if the group several constraints were removed
83 /** \brief Add specified group to this one
84 * \param[in] theGroup group of constraint to be added
86 void mergeGroups(const SketchSolver_ConstraintGroup& theGroup);
88 /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
89 * \param[out] theCuts enlarge this list by newly created groups
91 void splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts);
93 /** \brief Start solution procedure if necessary and update attributes of features
95 void resolveConstraints();
97 /** \brief Searches the constraints built on the entity and emit the signal to update them
98 * \param[in] theEntity attribute of the constraint
100 void updateRelatedConstraints(boost::shared_ptr<ModelAPI_Attribute> theEntity) const;
101 void updateRelatedConstraints(boost::shared_ptr<ModelAPI_Feature> theFeature) const;
104 /** \brief Adds or updates an entity in the group
106 * The parameters of entity will be parsed and added to the list of SolveSpace parameters.
107 * Parameters of certain entity will be placed sequentially in the list.
109 * \param[in] theEntity the object of constraint
110 * \return identifier of changed entity or 0 if entity could not be changed
112 Slvs_hEntity changeEntity(boost::shared_ptr<ModelAPI_Attribute> theEntity);
113 Slvs_hEntity changeEntity(FeaturePtr theEntity);
115 /** \brief Adds or updates a normal in the group
117 * Normal is a special entity in SolveSpace, which defines a direction in 3D and
118 * a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
120 * To define a normal there should be specified two coordinate axis
121 * on the plane transversed to created normal.
123 * \param[in] theDirX first coordinate axis of the plane
124 * \param[in] theDirY second coordinate axis of the plane
125 * \param[in] theNorm attribute for the normal (used to identify newly created entity)
126 * \return identifier of created or updated normal
128 Slvs_hEntity changeNormal(boost::shared_ptr<ModelAPI_Attribute> theDirX,
129 boost::shared_ptr<ModelAPI_Attribute> theDirY,
130 boost::shared_ptr<ModelAPI_Attribute> theNorm);
132 /** \brief Adds or updates a parameter in the group
133 * \param[in] theParam the value of parameter
134 * \param[in] thePrmIter the cell in the list of parameters which should be changed
135 * (the iterator will be increased if it does not reach the end of the list)
136 * \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
138 Slvs_hParam changeParameter(const double& theParam,
139 std::vector<Slvs_Param>::const_iterator& thePrmIter);
141 /** \brief Removes constraints from the group
142 * \param[in] theConstraint constraint to be removed
144 void removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint);
146 /** \brief Change values of attribute by parameters received from SolveSpace solver
147 * \param[in,out] theAttribute pointer to the attribute to be changed
148 * \param[in] theEntityID identifier of SolveSpace entity, which contains updated data
149 * \return \c true if the attribute's value has changed
151 bool updateAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID);
153 /** \brief Adds a constraint for a point which should not be changed during computations
154 * \param[in] theEntity the base for the constraint
156 void addTemporaryConstraintWhereDragged(boost::shared_ptr<ModelAPI_Attribute> theEntity);
158 /** \brief Remove all temporary constraint after computation finished
160 void removeTemporaryConstraints();
163 /** \brief Creates a workplane from the sketch parameters
164 * \param[in] theSketch parameters of workplane are the attributes of this sketch
165 * \return \c true if success, \c false if workplane parameters are not consistent
167 bool addWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch);
169 /** \brief Add the entities of constraint for points coincidence into the appropriate list
170 * \param[in] thePoint1 identifier of the first point
171 * \param[in] thePoint2 identifier of the second point
172 * \return \c true if the points are added successfully, and
173 * \c false if the constraint is the extra one (should not be created in SolveSpace)
175 bool addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
177 /** \brief Verifies and changes parameters of constriant,
178 * e.g. sign of the distance between line and point
179 * \param[in,out] theConstraint SolveSpace constraint to be verified
181 void checkConstraintConsistence(Slvs_Constraint& theConstraint);
184 // SolveSpace entities
185 Slvs_hGroup myID; ///< the index of the group
186 Slvs_Entity myWorkplane; ///< Workplane for the current group
187 std::vector<Slvs_Param> myParams; ///< List of parameters of the constraints
188 Slvs_hParam myParamMaxID; ///< Actual maximal ID of parameters (not equal to myParams size)
189 std::vector<Slvs_Entity> myEntities; ///< List of entities of the constaints
190 Slvs_hEntity myEntityMaxID; ///< Actual maximal ID of entities (not equal to myEntities size)
191 std::vector<Slvs_Constraint> myConstraints; ///< List of constraints in SolveSpace format
192 Slvs_hConstraint myConstrMaxID; ///< Actual maximal ID of constraints (not equal to myConstraints size)
193 bool myNeedToSolve; ///< Indicator that something changed in the group and constraint system need to be rebuilt
195 SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints
197 std::vector<Slvs_hParam> myTempPointWhereDragged; ///< Parameters of one of the points which is moved by user
198 Slvs_hEntity myTempPointWDrgdID; ///< Identifier of such point
199 std::list<Slvs_hConstraint> myTempConstraints; ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
201 // SketchPlugin entities
202 boost::shared_ptr<SketchPlugin_Feature>
203 mySketch; ///< Equivalent to workplane
204 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>
205 myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
206 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>
207 myEntityAttrMap; ///< The map between "attribute" parameters of constraints and their equivalent SolveSpace entities
208 std::map<FeaturePtr, Slvs_hEntity>
209 myEntityFeatMap; ///< The map between "feature" parameters of constraints and their equivalent SolveSpace entities
212 std::vector< std::set<Slvs_hEntity> >
213 myCoincidentPoints; ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
214 std::set< boost::shared_ptr<SketchPlugin_Constraint> >
215 myExtraCoincidence; ///< Additional coincidence constraints which are not necessary (coincidence between points already done
216 ///< by other constraints) but created by GUI tools. Useful when some coincidence constraints were removed