1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Group.h
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #ifndef SketchSolver_Group_H_
8 #define SketchSolver_Group_H_
10 #include "SketchSolver.h"
11 #include <SketchSolver_Constraint.h>
12 #include <SketchSolver_Storage.h>
13 #include <SketchSolver_FeatureStorage.h>
14 #include <SketchSolver_Solver.h>
16 #include <SketchPlugin_Constraint.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Feature.h>
27 typedef std::map<ConstraintPtr, SolverConstraintPtr> ConstraintConstraintMap;
29 /** \class SketchSolver_Group
31 * \brief Keeps the group of constraints which based on the same entities
33 class SketchSolver_Group
36 /** \brief New group based on specified workplane.
37 * Throws an exception if theWorkplane is not an object of SketchPlugin_Sketch type
38 * \remark Type of theSketch is not verified inside
40 SketchSolver_Group(std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane);
42 ~SketchSolver_Group();
44 /// \brief Returns group's unique identifier
45 inline const Slvs_hGroup& getId() const
50 /// \brief Returns identifier of the workplane
51 inline const Slvs_hEntity& getWorkplaneId() const
56 /// \brief Find the identifier of the feature, if it already exists in the group
57 Slvs_hEntity getFeatureId(FeaturePtr theFeature) const;
58 /// \brief Find the identifier of the attribute, if it already exists in the group
59 Slvs_hEntity getAttributeId(AttributePtr theAttribute) const;
61 /// \brief Returns true if the group has no constraints yet
62 inline bool isEmpty() const
64 return myConstraints.empty();
67 /// \brief Check for valid sketch data
68 inline bool isWorkplaneValid() const
70 return mySketch->data() && mySketch->data()->isValid();
73 /** \brief Adds or updates a constraint in the group
74 * \param[in] theConstraint constraint to be changed
75 * \return \c true if the constraint added or updated successfully
77 bool changeConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
78 //// /** \brief Adds or updates a rigid constraint in the group
79 //// * \param[in] theConstraint constraint to be changed
80 //// * \return \c true if the constraint added or updated successfully
82 //// bool changeRigidConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
83 //// /** \brief Adds or updates a mirror constraint in the group
84 //// * \param[in] theConstraint constraint to be changed
85 //// * \return \c true if the constraint added or updated successfully
87 //// bool changeMirrorConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
88 //// /** \brief Adds or updates a fillet constraint in the group
89 //// * \param[in] theConstraint constraint to be changed
90 //// * \return \c true if the constraint added or updated successfully
92 //// bool changeFilletConstraint(std::shared_ptr<SketchPlugin_Constraint> theConstraint);
94 /** \brief Updates the data corresponding the specified feature
95 * \param[in] theFeature the feature to be updated
97 bool updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
99 /** \brief Updates the data corresponding the specified feature moved in GUI.
100 * Additional Fixed constraints are created.
101 * \param[in] theFeature the feature to be updated
103 void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
105 /** \brief Verifies the feature attributes are used in this group
106 * \param[in] theFeature constraint or any other object for verification of interaction
107 * \return \c true if some of attributes are used in current group
109 bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
111 /** \brief Verifies the specified feature is equal to the base workplane for this group
112 * \param[in] theWorkplane the feature to be compared with base workplane
113 * \return \c true if workplanes are the same
115 bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
117 /// Returns the current workplane
118 std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
123 /** \brief Update parameters of workplane. Should be called when Update event is coming.
124 * \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
126 bool updateWorkplane();
128 //// /** \brief If the entity is in this group it will updated
129 //// * \param[in] theEntity attribute, which values should update SolveSpace entity
131 //// void updateEntityIfPossible(std::shared_ptr<ModelAPI_Attribute> theEntity);
133 /** \brief Searches invalid features and constraints in the group and removes them
134 * \return \c false if the group several constraints were removed
138 /** \brief Add specified group to this one
139 * \param[in] theGroup group of constraint to be added
141 void mergeGroups(const SketchSolver_Group& theGroup);
143 /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
144 * \param[out] theCuts enlarge this list by newly created groups
146 void splitGroup(std::vector<SketchSolver_Group*>& theCuts);
148 /** \brief Start solution procedure if necessary and update attributes of features
149 * \return \c false when no need to solve constraints
151 bool resolveConstraints();
153 //// /** \brief Searches the constraints built on the entity and emit the signal to update them
154 //// * \param[in] theEntity attribute of the constraint
156 //// void updateRelatedConstraints(std::shared_ptr<ModelAPI_Attribute> theEntity) const;
157 //// /** \brief Searches the constraints built on the entity and emit the signal to update them
158 //// * \param[in] theFeature feature of the constraint
160 //// void updateRelatedConstraintsFeature(std::shared_ptr<ModelAPI_Feature> theFeature) const;
162 //// /** \brief Adds or updates an entity in the group
164 //// * The parameters of entity will be parsed and added to the list of SolveSpace parameters.
165 //// * Parameters of certain entity will be placed sequentially in the list.
167 //// * \param[in] theEntity the object of constraint
168 //// * \return identifier of changed entity or 0 if entity could not be changed
170 //// Slvs_hEntity changeEntity(std::shared_ptr<ModelAPI_Attribute> theEntity);
171 //// Slvs_hEntity changeEntityFeature(std::shared_ptr<ModelAPI_Feature> theEntity);
174 //// /** \brief Adds or updates a normal in the group
176 //// * Normal is a special entity in SolveSpace, which defines a direction in 3D and
177 //// * a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
179 //// * To define a normal there should be specified two coordinate axis
180 //// * on the plane transversed to created normal.
182 //// * \param[in] theDirX first coordinate axis of the plane
183 //// * \param[in] theNorm attribute for the normal (used to identify newly created entity)
184 //// * \return identifier of created or updated normal
186 //// Slvs_hEntity changeNormal(std::shared_ptr<ModelAPI_Attribute> theDirX,
187 //// std::shared_ptr<ModelAPI_Attribute> theNorm);
189 //// /** \brief Adds or updates a parameter in the group
190 //// * \param[in] theParam the value of parameter
191 //// * \param[in] thePrmIter the cell in the list of parameters which should be changed
192 //// * (the iterator will be increased if it does not reach the end of the list)
193 //// * \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
195 //// Slvs_hParam changeParameter(const double& theParam,
196 //// std::vector<Slvs_Param>::const_iterator& thePrmIter);
198 //// /** \brief Removes specified entities and their parameters
199 //// * \param[in] theEntities list of IDs of the entities to be removed
201 //// void removeEntitiesById(const std::set<Slvs_hEntity>& theEntities);
203 /** \brief Removes constraints from the group
204 * \param[in] theConstraint constraint to be removed
206 void removeConstraint(ConstraintPtr theConstraint);
208 //// /** \brief Change values of attribute by parameters received from SolveSpace solver
209 //// * \param[in,out] theAttribute pointer to the attribute to be changed
210 //// * \param[in] theEntityID identifier of SolveSpace entity, which contains updated data
211 //// * \return \c true if the attribute's value has changed
213 //// bool updateAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute,
214 //// const Slvs_hEntity& theEntityID);
216 //// /// \brief Update arc of fillet to be less than 180 degree
217 //// void updateFilletConstraints();
219 //// /** \brief Adds a constraint for a point which should not be changed during computations
220 //// * \param[in] theEntity the base for the constraint
221 //// * \param[in] theAllowToFit this flag shows that the entity may be placed into
222 //// * the 'dragged' field of SolveSpace solver, so this entity
223 //// * may be changed a little during solution
225 //// void addTemporaryConstraintWhereDragged(std::shared_ptr<ModelAPI_Attribute> theEntity,
226 //// bool theAllowToFit = true);
228 /** \brief Remove all temporary constraint after computation finished
229 * \param[in] theRemoved indexes of constraints to be removed. If empty, all temporary constraints should be deleted
231 void removeTemporaryConstraints();
234 /** \brief Creates a workplane from the sketch parameters
235 * \param[in] theSketch parameters of workplane are the attributes of this sketch
236 * \return \c true if success, \c false if workplane parameters are not consistent
238 bool addWorkplane(CompositeFeaturePtr theSketch);
240 //// /** \brief Add the entities of constraint for points coincidence into the appropriate list
241 //// * \param[in] thePoint1 identifier of the first point
242 //// * \param[in] thePoint2 identifier of the second point
243 //// * \return \c true if the points are added successfully, and
244 //// * \c false if the constraint is the extra one (should not be created in SolveSpace)
246 //// bool addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
248 //// /** \brief Change entities parameters to make them symmetric relating to the mirror line
249 //// * \param[in] theBase entity to be mirrored
250 //// * \param[in] theMirror a mirrored object
251 //// * \param[in] theMirrorLine a mirror line
253 //// void makeMirrorEntity(const Slvs_hEntity& theBase,
254 //// const Slvs_hEntity& theMirror,
255 //// const Slvs_hEntity& theMirrorLine);
257 //// /** \brief Calculates middle point on line or arc
258 //// * \param[in] theEntity identifier of line or arc
259 //// * \param[out] theX X value of middle point
260 //// * \param[out] theY Y value of middle point
262 //// void calculateMiddlePoint(const Slvs_hEntity& theEntity,
263 //// double& theX, double& theY) const;
266 //// // SolveSpace entities
267 //// Slvs_hGroup myID; ///< the index of the group
268 //// Slvs_Entity myWorkplane; ///< Workplane for the current group
269 //// std::vector<Slvs_Param> myParams; ///< List of parameters of the constraints
270 //// Slvs_hParam myParamMaxID; ///< Actual maximal ID of parameters (not equal to myParams size)
271 //// std::vector<Slvs_Entity> myEntities; ///< List of entities of the constaints
272 //// std::vector<bool> myEntOfConstr; ///< Flags show that certain entity used in constraints
273 //// Slvs_hEntity myEntityMaxID; ///< Actual maximal ID of entities (not equal to myEntities size)
274 //// std::vector<Slvs_Constraint> myConstraints; ///< List of constraints in SolveSpace format
275 //// Slvs_hConstraint myConstrMaxID; ///< Actual maximal ID of constraints (not equal to myConstraints size)
276 //// bool myNeedToSolve; ///< Indicator that something changed in the group and constraint system need to be rebuilt
278 //// SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints
280 //// std::vector<Slvs_hParam> myTempPointWhereDragged; ///< Parameters of one of the points which is moved by user
281 //// Slvs_hEntity myTempPointWDrgdID; ///< Identifier of such point
282 //// std::list<Slvs_hConstraint> myTempConstraints; ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
284 //// // SketchPlugin entities
285 //// std::shared_ptr<ModelAPI_CompositeFeature> mySketch; ///< Equivalent to workplane
286 //// ConstraintMap myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
287 //// std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity> myEntityAttrMap; ///< The map between "attribute" parameters of constraints and their equivalent SolveSpace entities
288 //// std::map<FeaturePtr, Slvs_hEntity> myEntityFeatMap; ///< The map between "feature" parameters of constraints and their equivalent SolveSpace entities
290 //// // Conincident items
291 //// std::vector<std::set<Slvs_hEntity> > myCoincidentPoints; ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
292 //// std::set<std::shared_ptr<SketchPlugin_Constraint> > myExtraCoincidence; ///< Additional coincidence constraints which are not necessary (coincidence between points already done
293 //// ///< by other constraints) but created by GUI tools. Useful when some coincidence constraints were removed
296 Slvs_hGroup myID; ///< Index of the group
297 Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on
298 CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
299 ConstraintConstraintMap myConstraints; ///< List of constraints
300 std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
302 StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
303 FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies
305 SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints