1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SolveSpaceSolver_Storage.h
4 // Created: 18 Mar 2015
5 // Author: Artem ZHIDKOV
7 #ifndef SolveSpaceSolver_Storage_H_
8 #define SolveSpaceSolver_Storage_H_
10 #include <SketchSolver_Storage.h>
11 #include <SolveSpaceSolver_Solver.h>
18 typedef std::list< std::set<ConstraintWrapperPtr> > SameConstraintMap;
20 /** \class SolveSpaceSolver_Storage
22 * \brief Contains all necessary data in SolveSpace format to solve a single group of constraints
24 class SolveSpaceSolver_Storage : public SketchSolver_Storage
27 SolveSpaceSolver_Storage(const GroupID& theGroup);
29 // ============= Inherited from SketchSolver_Storage =============
31 /// \brief Update constraint's data
32 /// \return \c true if any value is updated
33 virtual bool update(ConstraintWrapperPtr theConstraint);
34 /// \brief Update entity's data
35 /// \return \c true if any value is updated
36 virtual bool update(EntityWrapperPtr theEntity);
37 /// \brief Update parameter's data
38 /// \return \c true if the value of parameter is updated
39 virtual bool update(ParameterWrapperPtr theParameter);
41 /// \brief Update SketchPlugin features after resolving constraints
42 /// \param theFixedOnly [in] if \c true the fixed points will be updated only
43 virtual void refresh(bool theFixedOnly = false) const;
45 /// \brief Check if some parameters or entities are returned
46 /// to the current group after removing temporary constraints
47 virtual void verifyFixed();
49 /// \brief Mark two points as coincident
50 virtual void addCoincidentPoints(EntityWrapperPtr theMaster, EntityWrapperPtr theSlave);
52 /// \brief Calculate point on theBase entity. Value theCoeff is in [0.0 .. 1.0] and
53 /// shows the distance from the start point.
54 virtual EntityWrapperPtr calculateMiddlePoint(EntityWrapperPtr theBase, double theCoeff);
57 /// \brief Remove constraint
58 /// \return \c true if the constraint and all its parameters are removed successfully
59 virtual bool remove(ConstraintWrapperPtr theConstraint);
60 /// \brief Remove entity
61 /// \return \c true if the entity and all its parameters are removed successfully
62 virtual bool remove(EntityWrapperPtr theEntity);
63 /// \brief Remove parameter
64 /// \return \c true if the parameter has been removed
65 virtual bool remove(ParameterWrapperPtr theParameter);
67 /// \brief Remove point-point coincidence
68 bool removeCoincidence(ConstraintWrapperPtr theConstraint);
70 /// \brief Update the group for the given entity, its sub-entities and parameters
71 virtual void changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup);
72 /// \brief Update the group for the given parameter
73 virtual void changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup);
76 // ============= Own methods =============
78 /// \brief Obtain and store identifier of sketch
79 void storeWorkplane(EntityWrapperPtr theSketch);
81 /** \brief Add new parameter to the current group
82 * \param[in] theParam SolveSpace parameter
83 * \return the ID of added parameter
85 Slvs_hParam addParameter(const Slvs_Param& theParam);
86 /** \brief Updates parameter in the current group. If the ID of parameter is zero, the new item will be added
87 * \param[in] theParam SolveSpace parameter
88 * \return the ID of updated/added parameter
90 Slvs_hParam updateParameter(const Slvs_Param& theParam);
91 /** \brief Removes the parameter by its ID
92 * \param[in] theParamID index of parameter to be removed
93 * \return \c true if the parameter was successfully removed
95 bool removeParameter(const Slvs_hParam& theParamID);
96 /// \brief Returns the parameter by its ID
97 const Slvs_Param& getParameter(const Slvs_hParam& theParamID) const;
99 /** \brief Add new entity to the current group
100 * \param[in] theEntity SolveSpace entity
101 * \return the ID of added entity
103 Slvs_hEntity addEntity(const Slvs_Entity& theEntity);
104 /** \brief Updates entity in the current group. If the ID of entity is zero, the new item will be added
105 * \param[in] theEntity SolveSpace entity
106 * \return the ID of updated/added entity
108 Slvs_hEntity updateEntity(const Slvs_Entity& theEntity);
109 /** \brief Removes the entity by its ID. All parameters used in this entity,
110 * and not used in other constraints, will be removed too.
111 * \param[in] theEntityID index of entity to be removed
112 * \return \c true if the entity was successfully removed
114 bool removeEntity(const Slvs_hEntity& theEntityID);
115 /** \brief Remove all entities, which are not used in constraints
117 void removeUnusedEntities();
118 /// \brief Returns the entity by its ID
119 const Slvs_Entity& getEntity(const Slvs_hEntity& theEntityID) const;
120 /// \brief Makes a full copy of the given entity
121 Slvs_hEntity copyEntity(const Slvs_hEntity& theCopied);
122 /// \brief Copy one entity to another
123 void copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo);
124 /// \brief Check the entity is used in constraints
125 bool isUsedByConstraints(const Slvs_hEntity& theEntityID) const;
126 /// \brief Returns maximal ID of entities in this storage
127 const Slvs_hEntity& entityMaxID() const
128 { return myEntityMaxID; }
130 /** \brief Add new constraint to the current group
131 * \param[in] theConstraint SolveSpace's constraint
132 * \return the ID of added constraint
134 Slvs_hConstraint addConstraint(const Slvs_Constraint& theConstraint);
135 /** \brief Updates constraint in the current group.
136 * If the ID of constraint is zero, the new item will be added
137 * \param[in] theConstraint SolveSpace constraint
138 * \return the ID of updated/added constraint
140 Slvs_hConstraint updateConstraint(const Slvs_Constraint& theConstraint);
141 /** \brief Removes the constraint by its ID. All entities and parameters depending on this
142 * constraint, which are not used in other constraints, will be removed too.
143 * \param[in] theConstraintID index of constraint to be removed
144 * \return \c true if the constraint was successfully removed
146 bool removeConstraint(const Slvs_hConstraint& theConstraintID);
147 /// \brief Returns the constraint by its ID
148 const Slvs_Constraint& getConstraint(const Slvs_hConstraint& theConstraintID) const;
149 /// \brief Returns quantity of constraints in this storage
150 size_t nbConstraints() const
151 { return myConstraints.size(); }
153 /// \brief Shows the storage has the same constraint twice
154 virtual bool hasDuplicatedConstraint() const
155 { return myDuplicatedConstraint; }
157 /// \brief Initialize constraint solver by the entities collected by current storage
158 virtual void initializeSolver(SolverPtr theSolver);
161 /// \brief Update arc points to be on circle sharp.
162 void adjustArc(const Slvs_Entity& theArc);
164 /// \brief Replace sub-entity theSource in all features by theDest
165 void replaceInFeatures(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
166 /// \brief Replace constrained entity theSource by theDest in all constraints;
167 void replaceInConstraints(EntityWrapperPtr theSource, EntityWrapperPtr theDest);
169 /// \brief Add pair of constraints which have same representation in SolveSpace notation.
171 /// These constraints may be different and become the same after the substitution
172 /// of point coincidence.
173 void addSameConstraints(ConstraintWrapperPtr theConstraint1, ConstraintWrapperPtr theConstraint2);
175 /// \brief Search constraint equal to the given in terms of SolveSpace notation
176 bool findSameConstraint(ConstraintWrapperPtr theConstraint);
179 Slvs_hEntity myWorkplaneID; ///< identifier of workplane
181 Slvs_hParam myParamMaxID; ///< current parameter index (may differs with the number of parameters)
182 std::vector<Slvs_Param> myParameters; ///< list of parameters used in the current group of constraints (sorted by the identifier)
183 Slvs_hEntity myEntityMaxID; ///< current entity index (may differs with the number of entities)
184 std::vector<Slvs_Entity> myEntities; ///< list of entities used in the current group of constraints (sorted by the identifier)
185 Slvs_hConstraint myConstrMaxID; ///< current constraint index (may differs with the number of constraints)
186 std::vector<Slvs_Constraint> myConstraints; ///< list of constraints used in the current group (sorted by the identifier)
188 bool myDuplicatedConstraint; ///< shows the storage has same constraint twice
190 std::set<Slvs_hParam> myUpdatedParameters; ///< list of just updated parameters (cleared when isNeedToResolve() called)
192 SameConstraintMap myEqualConstraints; ///< list of groups of equal constraints