1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Constraint.h
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #ifndef SketchSolver_Constraint_H_
8 #define SketchSolver_Constraint_H_
10 #include "SketchSolver.h"
11 #include <SketchSolver_Storage.h>
13 #include <SketchPlugin_Constraint.h>
15 #include <ModelAPI_AttributeRefAttr.h>
20 class SketchSolver_Group;
22 /** \class SketchSolver_Constraint
24 * \brief Stores mapping between SketchPlugin and SolveSpace constraints data
26 class SketchSolver_Constraint
29 /// Default constructor
30 SketchSolver_Constraint() {}
31 /// Constructor based on SketchPlugin constraint
32 SketchSolver_Constraint(ConstraintPtr theConstraint);
35 virtual ~SketchSolver_Constraint();
37 /// \brief Initializes the storage of SolveSpace constraints
38 void setStorage(StoragePtr theStorage);
39 /// \brief Initializes group ID for this constraint
40 void setGroup(SketchSolver_Group* theGroup);
42 /// \brief Update constraint
43 virtual void update(ConstraintPtr theConstraint = ConstraintPtr());
45 /// \brief Tries to remove constraint
46 /// \return \c false, if current constraint contains another SketchPlugin constraints (like for multiple coincidence)
47 virtual bool remove(ConstraintPtr theConstraint = ConstraintPtr());
49 /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities
50 virtual void refresh();
52 /// \brief Returns the type of constraint
53 virtual int getType() const = 0;
55 /// \brief The constraint is made temporary
56 void makeTemporary() const;
58 /// \brief Checks the constraint is used by current object
59 virtual bool hasConstraint(ConstraintPtr theConstraint) const
60 { return theConstraint == myBaseConstraint; }
62 /// \brief Return list of SketchPlugin constraints attached to this object
63 virtual std::list<ConstraintPtr> constraints() const;
65 /// \brief Return identifier of SolveSpace entity relating to the feature
66 Slvs_hEntity getId(FeaturePtr theFeature) const;
67 /// \brief Return identifier of SolveSpace entity relating to the attribute
68 Slvs_hEntity getId(AttributePtr theAttribute) const;
70 /// \brief Adds a feature to constraint and create its analogue in SolveSpace
71 virtual void addFeature(FeaturePtr theFeature);
73 /// \brief Shows error message
74 const std::string& error() const
75 { return myErrorMsg; }
78 /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
79 virtual void process();
81 /// \brief Generate list of attributes of constraint in order useful for SolveSpace constraints
82 /// \param[out] theValue numerical characteristic of constraint (e.g. distance)
83 /// \param[out] theAttributes list of attributes to be filled
84 virtual void getAttributes(double& theValue, std::vector<Slvs_hEntity>& theAttributes);
86 /// \brief Verify the attributes of constraint are changed (and constraint need to rebuild)
87 /// \param[in] theConstraint constraint, which attributes should be checked (if NULL, the myBaseConstraint is used)
88 /// \return \c true if some attributes are changed
89 virtual bool checkAttributesChanged(ConstraintPtr theConstraint);
91 /// \brief This method is used in derived objects to check consistency of constraint.
92 /// E.g. the distance between line and point may be signed.
93 virtual void adjustConstraint()
96 /// \brief Create or change SlveSpace entity according to the given attribute
97 /// \param[in] theAttribute reference to the entity to be changed
98 /// \param[out] theType type of created entity
99 /// \return identifier of created/updated entity
100 Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
101 /// \brief Create or change SlveSpace entity according to the given attribute
102 Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
103 /// \brief Create or change SlveSpace entity according to the given feature
104 Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
106 /// \brief Calculate middle point on the specified entity
107 /// \param[in] theEntity arc or line
108 /// \param[in] theCoeff is a value in [0.0, 1.0] which shows the position of the point on the entity (0.0 - start point, 1.0 - end point)
109 /// \param[out] theX X coordinate of middle point
110 /// \param[out] theY Y coordinate of middle point
111 void calculateMiddlePoint(const Slvs_Entity& theEntity, double theCoeff,
112 double& theX, double& theY) const;
114 /// \brief Removes the links to unused entities
115 void cleanRemovedEntities();
117 /// \brief Removes last error
119 { myErrorMsg.clear(); }
122 /// \brief Sets error, if the attribute is not initialized
123 bool isInitialized(AttributePtr theAttribute);
126 SketchSolver_Group* myGroup; ///< the group which contains current constraint
127 ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
128 std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
129 std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
130 std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
131 std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
132 StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
134 std::string myErrorMsg; ///< error message
137 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
141 /** \class SketchSolver_ConstraintParallel
143 * \brief Convert Parallel constraint to SolveSpace structure
145 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
148 /// Constructor based on SketchPlugin constraint
149 SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
150 SketchSolver_Constraint(theConstraint)
153 virtual int getType() const
154 { return SLVS_C_PARALLEL; }
158 /** \class SketchSolver_ConstraintPerpendicular
160 * \brief Convert Perpendicular constraint to SolveSpace structure
162 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
165 /// Constructor based on SketchPlugin constraint
166 SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
167 SketchSolver_Constraint(theConstraint)
170 virtual int getType() const
171 { return SLVS_C_PERPENDICULAR; }
175 /** \class SketchSolver_ConstraintHorizontal
177 * \brief Convert Horizontal constraint to SolveSpace structure
179 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
182 /// Constructor based on SketchPlugin constraint
183 SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
184 SketchSolver_Constraint(theConstraint)
187 virtual int getType() const
188 { return SLVS_C_HORIZONTAL; }
192 /** \class SketchSolver_ConstraintVertical
194 * \brief Convert Vertical constraint to SolveSpace structure
196 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
199 /// Constructor based on SketchPlugin constraint
200 SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
201 SketchSolver_Constraint(theConstraint)
204 virtual int getType() const
205 { return SLVS_C_VERTICAL; }
209 /** \class SketchSolver_ConstraintRadius
211 * \brief Convert Radius constraint to SolveSpace structure
213 class SketchSolver_ConstraintRadius : public SketchSolver_Constraint
216 /// Constructor based on SketchPlugin constraint
217 SketchSolver_ConstraintRadius(ConstraintPtr theConstraint) :
218 SketchSolver_Constraint(theConstraint)
221 virtual int getType() const
222 { return SLVS_C_DIAMETER; }
224 virtual void adjustConstraint()
226 Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
227 aConstraint.valA *= 2.0;
228 myStorage->updateConstraint(aConstraint);