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 moved in GUI.
95 * Additional Fixed constraints are created.
96 * \param[in] theFeature the feature to be updated
98 void moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature);
100 /** \brief Verifies the feature attributes are used in this group
101 * \param[in] theFeature constraint or any other object for verification of interaction
102 * \return \c true if some of attributes are used in current group
104 bool isInteract(std::shared_ptr<SketchPlugin_Feature> theFeature) const;
106 /** \brief Verifies the specified feature is equal to the base workplane for this group
107 * \param[in] theWorkplane the feature to be compared with base workplane
108 * \return \c true if workplanes are the same
110 bool isBaseWorkplane(CompositeFeaturePtr theWorkplane) const;
112 /// Returns the current workplane
113 std::shared_ptr<ModelAPI_CompositeFeature> getWorkplane() const
118 /** \brief Update parameters of workplane. Should be called when Update event is coming.
119 * \return \c true if workplane updated successfully, \c false if workplane parameters are not consistent
121 bool updateWorkplane();
123 //// /** \brief If the entity is in this group it will updated
124 //// * \param[in] theEntity attribute, which values should update SolveSpace entity
126 //// void updateEntityIfPossible(std::shared_ptr<ModelAPI_Attribute> theEntity);
128 /** \brief Searches invalid features and constraints in the group and removes them
129 * \return \c false if the group several constraints were removed
133 /** \brief Add specified group to this one
134 * \param[in] theGroup group of constraint to be added
136 void mergeGroups(const SketchSolver_Group& theGroup);
138 /** \brief Cut from the group several subgroups, which are not connected to the current one by any constraint
139 * \param[out] theCuts enlarge this list by newly created groups
141 void splitGroup(std::vector<SketchSolver_Group*>& theCuts);
143 /** \brief Start solution procedure if necessary and update attributes of features
144 * \return \c false when no need to solve constraints
146 bool resolveConstraints();
148 //// /** \brief Searches the constraints built on the entity and emit the signal to update them
149 //// * \param[in] theEntity attribute of the constraint
151 //// void updateRelatedConstraints(std::shared_ptr<ModelAPI_Attribute> theEntity) const;
152 //// /** \brief Searches the constraints built on the entity and emit the signal to update them
153 //// * \param[in] theFeature feature of the constraint
155 //// void updateRelatedConstraintsFeature(std::shared_ptr<ModelAPI_Feature> theFeature) const;
157 //// /** \brief Adds or updates an entity in the group
159 //// * The parameters of entity will be parsed and added to the list of SolveSpace parameters.
160 //// * Parameters of certain entity will be placed sequentially in the list.
162 //// * \param[in] theEntity the object of constraint
163 //// * \return identifier of changed entity or 0 if entity could not be changed
165 //// Slvs_hEntity changeEntity(std::shared_ptr<ModelAPI_Attribute> theEntity);
166 //// Slvs_hEntity changeEntityFeature(std::shared_ptr<ModelAPI_Feature> theEntity);
169 //// /** \brief Adds or updates a normal in the group
171 //// * Normal is a special entity in SolveSpace, which defines a direction in 3D and
172 //// * a rotation about this direction. So, SolveSpace represents normals as unit quaternions.
174 //// * To define a normal there should be specified two coordinate axis
175 //// * on the plane transversed to created normal.
177 //// * \param[in] theDirX first coordinate axis of the plane
178 //// * \param[in] theNorm attribute for the normal (used to identify newly created entity)
179 //// * \return identifier of created or updated normal
181 //// Slvs_hEntity changeNormal(std::shared_ptr<ModelAPI_Attribute> theDirX,
182 //// std::shared_ptr<ModelAPI_Attribute> theNorm);
184 //// /** \brief Adds or updates a parameter in the group
185 //// * \param[in] theParam the value of parameter
186 //// * \param[in] thePrmIter the cell in the list of parameters which should be changed
187 //// * (the iterator will be increased if it does not reach the end of the list)
188 //// * \return identifier of changed parameter; when the parameter cannot be created, returned ID is 0
190 //// Slvs_hParam changeParameter(const double& theParam,
191 //// std::vector<Slvs_Param>::const_iterator& thePrmIter);
193 //// /** \brief Removes specified entities and their parameters
194 //// * \param[in] theEntities list of IDs of the entities to be removed
196 //// void removeEntitiesById(const std::set<Slvs_hEntity>& theEntities);
198 /** \brief Removes constraints from the group
199 * \param[in] theConstraint constraint to be removed
201 void removeConstraint(ConstraintPtr theConstraint);
203 //// /** \brief Change values of attribute by parameters received from SolveSpace solver
204 //// * \param[in,out] theAttribute pointer to the attribute to be changed
205 //// * \param[in] theEntityID identifier of SolveSpace entity, which contains updated data
206 //// * \return \c true if the attribute's value has changed
208 //// bool updateAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute,
209 //// const Slvs_hEntity& theEntityID);
211 //// /// \brief Update arc of fillet to be less than 180 degree
212 //// void updateFilletConstraints();
214 //// /** \brief Adds a constraint for a point which should not be changed during computations
215 //// * \param[in] theEntity the base for the constraint
216 //// * \param[in] theAllowToFit this flag shows that the entity may be placed into
217 //// * the 'dragged' field of SolveSpace solver, so this entity
218 //// * may be changed a little during solution
220 //// void addTemporaryConstraintWhereDragged(std::shared_ptr<ModelAPI_Attribute> theEntity,
221 //// bool theAllowToFit = true);
223 /** \brief Remove all temporary constraint after computation finished
224 * \param[in] theRemoved indexes of constraints to be removed. If empty, all temporary constraints should be deleted
226 void removeTemporaryConstraints();
229 /** \brief Creates a workplane from the sketch parameters
230 * \param[in] theSketch parameters of workplane are the attributes of this sketch
231 * \return \c true if success, \c false if workplane parameters are not consistent
233 bool addWorkplane(CompositeFeaturePtr theSketch);
235 //// /** \brief Add the entities of constraint for points coincidence into the appropriate list
236 //// * \param[in] thePoint1 identifier of the first point
237 //// * \param[in] thePoint2 identifier of the second point
238 //// * \return \c true if the points are added successfully, and
239 //// * \c false if the constraint is the extra one (should not be created in SolveSpace)
241 //// bool addCoincidentPoints(const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2);
243 //// /** \brief Verifies and changes parameters of constriant,
244 //// * e.g. sign of the distance between line and point
245 //// * \param[in,out] theConstraint SolveSpace constraint to be verified
247 //// void checkConstraintConsistence(Slvs_Constraint& theConstraint);
249 //// /** \brief Change entities parameters to make them symmetric relating to the mirror line
250 //// * \param[in] theBase entity to be mirrored
251 //// * \param[in] theMirror a mirrored object
252 //// * \param[in] theMirrorLine a mirror line
254 //// void makeMirrorEntity(const Slvs_hEntity& theBase,
255 //// const Slvs_hEntity& theMirror,
256 //// const Slvs_hEntity& theMirrorLine);
258 //// /** \brief Calculates middle point on line or arc
259 //// * \param[in] theEntity identifier of line or arc
260 //// * \param[out] theX X value of middle point
261 //// * \param[out] theY Y value of middle point
263 //// void calculateMiddlePoint(const Slvs_hEntity& theEntity,
264 //// double& theX, double& theY) const;
267 //// // SolveSpace entities
268 //// Slvs_hGroup myID; ///< the index of the group
269 //// Slvs_Entity myWorkplane; ///< Workplane for the current group
270 //// std::vector<Slvs_Param> myParams; ///< List of parameters of the constraints
271 //// Slvs_hParam myParamMaxID; ///< Actual maximal ID of parameters (not equal to myParams size)
272 //// std::vector<Slvs_Entity> myEntities; ///< List of entities of the constaints
273 //// std::vector<bool> myEntOfConstr; ///< Flags show that certain entity used in constraints
274 //// Slvs_hEntity myEntityMaxID; ///< Actual maximal ID of entities (not equal to myEntities size)
275 //// std::vector<Slvs_Constraint> myConstraints; ///< List of constraints in SolveSpace format
276 //// Slvs_hConstraint myConstrMaxID; ///< Actual maximal ID of constraints (not equal to myConstraints size)
277 //// bool myNeedToSolve; ///< Indicator that something changed in the group and constraint system need to be rebuilt
279 //// SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints
281 //// std::vector<Slvs_hParam> myTempPointWhereDragged; ///< Parameters of one of the points which is moved by user
282 //// Slvs_hEntity myTempPointWDrgdID; ///< Identifier of such point
283 //// std::list<Slvs_hConstraint> myTempConstraints; ///< The list of identifiers of temporary constraints (SLVS_C_WHERE_DRAGGED) applied for all other points moved by user
285 //// // SketchPlugin entities
286 //// std::shared_ptr<ModelAPI_CompositeFeature> mySketch; ///< Equivalent to workplane
287 //// ConstraintMap myConstraintMap; ///< The map between SketchPlugin and SolveSpace constraints
288 //// std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity> myEntityAttrMap; ///< The map between "attribute" parameters of constraints and their equivalent SolveSpace entities
289 //// std::map<FeaturePtr, Slvs_hEntity> myEntityFeatMap; ///< The map between "feature" parameters of constraints and their equivalent SolveSpace entities
291 //// // Conincident items
292 //// std::vector<std::set<Slvs_hEntity> > myCoincidentPoints; ///< Stores the lists of identifiers of coincident points (to avoid unnecessary coincidence constraints)
293 //// std::set<std::shared_ptr<SketchPlugin_Constraint> > myExtraCoincidence; ///< Additional coincidence constraints which are not necessary (coincidence between points already done
294 //// ///< by other constraints) but created by GUI tools. Useful when some coincidence constraints were removed
297 Slvs_hGroup myID; ///< Index of the group
298 Slvs_hEntity myWorkplaneID; ///< Index of workplane, the group is based on
299 CompositeFeaturePtr mySketch; ///< Sketch is equivalent to workplane
300 ConstraintConstraintMap myConstraints; ///< List of constraints
301 std::set<SolverConstraintPtr> myTempConstraints; ///< List of temporary constraints
303 StoragePtr myStorage; ///< Container for the set of SolveSpace constraints and their entities
304 FeatureStoragePtr myFeatureStorage; ///< Container for the set of SketchPlugin features and their dependencies
306 SketchSolver_Solver myConstrSolver; ///< Solver for set of equations obtained by constraints