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_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
21 class SketchSolver_Group;
23 /** \class SketchSolver_Constraint
25 * \brief Stores mapping between SketchPlugin and SolveSpace constraints data
27 class SketchSolver_Constraint
30 /// Default constructor
31 SketchSolver_Constraint() {}
32 /// Constructor based on SketchPlugin constraint
33 SketchSolver_Constraint(ConstraintPtr theConstraint);
36 virtual ~SketchSolver_Constraint();
38 /// \brief Initializes the storage of SolveSpace constraints
39 void setStorage(StoragePtr theStorage);
40 /// \brief Initializes group ID for this constraint
41 void setGroup(SketchSolver_Group* theGroup);
43 /// \brief Update constraint
44 virtual void update(ConstraintPtr theConstraint = ConstraintPtr());
46 /// \brief Tries to remove constraint
47 /// \return \c false, if current constraint contains another SketchPlugin constraints (like for multiple coincidence)
48 virtual bool remove(ConstraintPtr theConstraint = ConstraintPtr());
50 /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities
51 virtual void refresh();
53 /// \brief Returns the type of constraint
54 virtual int getType() const = 0;
56 /// \brief The constraint is made temporary
57 void makeTemporary() const;
59 /// \brief Checks the constraint is used by current object
60 virtual bool hasConstraint(ConstraintPtr theConstraint) const
61 { return theConstraint == myBaseConstraint; }
63 /// \brief Return list of SketchPlugin constraints attached to this object
64 virtual std::list<ConstraintPtr> constraints() const;
66 /// \brief Return identifier of SolveSpace entity relating to the feature
67 Slvs_hEntity getId(FeaturePtr theFeature) const;
68 /// \brief Return identifier of SolveSpace entity relating to the attribute
69 Slvs_hEntity getId(AttributePtr theAttribute) const;
71 /// \brief Adds a feature to constraint and create its analogue in SolveSpace
72 virtual void addFeature(FeaturePtr theFeature);
74 /// \brief Shows error message
75 const std::string& error() const
76 { return myErrorMsg; }
79 /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
80 virtual void process();
82 /// \brief Generate list of attributes of constraint in order useful for SolveSpace constraints
83 /// \param[out] theValue numerical characteristic of constraint (e.g. distance)
84 /// \param[out] theAttributes list of attributes to be filled
85 virtual void getAttributes(double& theValue, std::vector<Slvs_hEntity>& theAttributes);
87 /// \brief Verify the attributes of constraint are changed (and constraint need to rebuild)
88 /// \param[in] theConstraint constraint, which attributes should be checked (if NULL, the myBaseConstraint is used)
89 /// \return \c true if some attributes are changed
90 virtual bool checkAttributesChanged(ConstraintPtr theConstraint);
92 /// \brief This method is used in derived objects to check consistency of constraint.
93 /// E.g. the distance between line and point may be signed.
94 virtual void adjustConstraint()
97 /// \brief Create or change SlveSpace entity according to the given attribute
98 /// \param[in] theAttribute reference to the entity to be changed
99 /// \param[out] theType type of created entity
100 /// \return identifier of created/updated entity
101 Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
102 /// \brief Create or change SlveSpace entity according to the given attribute
103 Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
104 /// \brief Create or change SlveSpace entity according to the given feature
105 Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
107 /// \brief Calculate middle point on the specified entity
108 /// \param[in] theEntity arc or line
109 /// \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)
110 /// \param[out] theX X coordinate of middle point
111 /// \param[out] theY Y coordinate of middle point
112 void calculateMiddlePoint(const Slvs_Entity& theEntity, double theCoeff,
113 double& theX, double& theY) const;
115 /// \brief Removes the links to unused entities
116 void cleanRemovedEntities();
118 /// \brief Removes last error
120 { myErrorMsg.clear(); }
123 /// \brief Sets error, if the attribute is not initialized
124 bool isInitialized(AttributePtr theAttribute);
127 SketchSolver_Group* myGroup; ///< the group which contains current constraint
128 ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
129 std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
130 std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
131 std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
132 std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
133 StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
135 std::string myErrorMsg; ///< error message
138 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
142 /** \class SketchSolver_ConstraintParallel
144 * \brief Convert Parallel constraint to SolveSpace structure
146 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
149 /// Constructor based on SketchPlugin constraint
150 SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
151 SketchSolver_Constraint(theConstraint)
154 virtual int getType() const
155 { return SLVS_C_PARALLEL; }
159 /** \class SketchSolver_ConstraintPerpendicular
161 * \brief Convert Perpendicular constraint to SolveSpace structure
163 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
166 /// Constructor based on SketchPlugin constraint
167 SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
168 SketchSolver_Constraint(theConstraint)
171 virtual int getType() const
172 { return SLVS_C_PERPENDICULAR; }
176 /** \class SketchSolver_ConstraintHorizontal
178 * \brief Convert Horizontal constraint to SolveSpace structure
180 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
183 /// Constructor based on SketchPlugin constraint
184 SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
185 SketchSolver_Constraint(theConstraint)
188 virtual int getType() const
189 { return SLVS_C_HORIZONTAL; }
193 /** \class SketchSolver_ConstraintVertical
195 * \brief Convert Vertical constraint to SolveSpace structure
197 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
200 /// Constructor based on SketchPlugin constraint
201 SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
202 SketchSolver_Constraint(theConstraint)
205 virtual int getType() const
206 { return SLVS_C_VERTICAL; }
210 /** \class SketchSolver_ConstraintRadius
212 * \brief Convert Radius constraint to SolveSpace structure
214 class SketchSolver_ConstraintRadius : public SketchSolver_Constraint
217 /// Constructor based on SketchPlugin constraint
218 SketchSolver_ConstraintRadius(ConstraintPtr theConstraint) :
219 SketchSolver_Constraint(theConstraint)
222 virtual int getType() const
223 { return SLVS_C_DIAMETER; }
225 virtual void adjustConstraint()
227 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
228 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
229 Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
230 aConstraint.valA = 2.0 * aValueAttr->value();
231 myStorage->updateConstraint(aConstraint);