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 SketchSolver_Constraint(ConstraintPtr theConstraint);
34 virtual ~SketchSolver_Constraint();
36 /// \brief Initializes the storage of SolveSpace constraints
37 void setStorage(StoragePtr theStorage);
38 /// \brief Initializes group ID for this constraint
39 void setGroup(SketchSolver_Group* theGroup);
41 /// \brief Update constraint
42 virtual void update(ConstraintPtr theConstraint = ConstraintPtr());
44 /// \brief Tries to remove constraint
45 /// \return \c false, if current constraint contains another SketchPlugin constraints (like for multiple coincidence)
46 virtual bool remove(ConstraintPtr theConstraint = ConstraintPtr());
48 /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities
51 /// \brief Returns the type of constraint
52 virtual int getType() const = 0;
54 /// \brief Checks the constraint is used by current object
55 virtual bool hasConstraint(ConstraintPtr theConstraint) const
56 { return theConstraint == myBaseConstraint; }
58 /// \brief Return list of SketchPlugin constraints attached to this object
59 virtual std::list<ConstraintPtr> constraints() const;
61 /// \brief Return identifier of SolveSpace entity relating to the feature
62 Slvs_hEntity getId(FeaturePtr theFeature) const;
63 /// \brief Return identifier of SolveSpace entity relating to the attribute
64 Slvs_hEntity getId(AttributePtr theAttribute) const;
66 /// \brief Shows error message
67 const std::string& error() const
68 { return myErrorMsg; }
71 /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
72 virtual void process();
74 /// \brief Generate list of attributes of constraint in order useful for SolveSpace constraints
75 /// \param[out] theValue numerical characteristic of constraint (e.g. distance)
76 /// \param[out] theAttributes list of attributes to be filled
77 virtual void getAttributes(double& theValue, std::vector<Slvs_hEntity>& theAttributes);
79 /// \brief This method is used in derived objects to check consistence of constraint.
80 /// E.g. the distance between line and point may be signed.
81 virtual void adjustConstraint()
84 /// \brief Create or change SlveSpace entity according to the given attribute
85 /// \param[in] theAttribute reference to the entity to be changed
86 /// \param[out] theType type of created entity
87 /// \return identifier of created/updated entity
88 Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
89 /// \brief Create or change SlveSpace entity according to the given attribute
90 Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
91 /// \brief Create or change SlveSpace entity according to the given feature
92 Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
94 /// \brief Calculate middle point on the specified entity
95 /// \param[in] theEntity arc or line
96 /// \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)
97 /// \param[out] theX X coordinate of middle point
98 /// \param[out] theY Y coordinate of middle point
99 void calculateMiddlePoint(const Slvs_Entity& theEntity, double theCoeff,
100 double& theX, double& theY) const;
102 /// \brief Removes the links to unused entities
103 void cleanRemovedEntities();
105 /// \brief Removes last error
107 { myErrorMsg.clear(); }
110 /// \brief Sets error, if the attribute is not initialized
111 bool isInitialized(AttributePtr theAttribute);
114 SketchSolver_Group* myGroup; ///< the group which contains current constraint
115 ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
116 std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
117 std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
118 std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
119 std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
120 StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
122 std::string myErrorMsg; ///< error message
125 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
129 /** \class SketchSolver_ConstraintParallel
131 * \brief Convert Parallel constraint to SolveSpace structure
133 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
136 SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
137 SketchSolver_Constraint(theConstraint)
140 virtual int getType() const
141 { return SLVS_C_PARALLEL; }
145 /** \class SketchSolver_ConstraintPerpendicular
147 * \brief Convert Perpendicular constraint to SolveSpace structure
149 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
152 SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
153 SketchSolver_Constraint(theConstraint)
156 virtual int getType() const
157 { return SLVS_C_PERPENDICULAR; }
161 /** \class SketchSolver_ConstraintHorizontal
163 * \brief Convert Horizontal constraint to SolveSpace structure
165 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
168 SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
169 SketchSolver_Constraint(theConstraint)
172 virtual int getType() const
173 { return SLVS_C_HORIZONTAL; }
177 /** \class SketchSolver_ConstraintVertical
179 * \brief Convert Vertical constraint to SolveSpace structure
181 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
184 SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
185 SketchSolver_Constraint(theConstraint)
188 virtual int getType() const
189 { return SLVS_C_VERTICAL; }
193 /** \class SketchSolver_ConstraintRadius
195 * \brief Convert Radius constraint to SolveSpace structure
197 class SketchSolver_ConstraintRadius : public SketchSolver_Constraint
200 SketchSolver_ConstraintRadius(ConstraintPtr theConstraint) :
201 SketchSolver_Constraint(theConstraint)
204 virtual int getType() const
205 { return SLVS_C_DIAMETER; }
207 virtual void adjustConstraint()
209 Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
210 aConstraint.valA *= 2.0;
211 myStorage->updateConstraint(aConstraint);