]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Constraint.h
Salome HOME
SketchSolver library refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Constraint.h
4 // Created: 27 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #ifndef SketchSolver_Constraint_H_
8 #define SketchSolver_Constraint_H_
9
10 #include "SketchSolver.h"
11 #include <SketchSolver_Storage.h>
12
13 #include <SketchPlugin_Constraint.h>
14
15 #include <ModelAPI_AttributeRefAttr.h>
16
17 #include <string>
18 #include <vector>
19
20 class SketchSolver_Group;
21
22 /** \class   SketchSolver_Constraint
23  *  \ingroup Plugins
24  *  \brief   Stores mapping between SketchPlugin and SolveSpace constraints data
25  */
26 class SketchSolver_Constraint
27 {
28 protected:
29   /// Default constructor
30   SketchSolver_Constraint() {}
31   SketchSolver_Constraint(ConstraintPtr theConstraint);
32
33 public:
34   virtual ~SketchSolver_Constraint();
35
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);
40
41   /// \brief Update constraint
42   virtual void update(ConstraintPtr theConstraint = ConstraintPtr());
43
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());
47
48   /// \brief Update SketchPlugin attributes using the data obtained from SolveSpace entities
49   void refresh();
50
51   /// \brief Returns the type of constraint
52   virtual int getType() const = 0;
53
54   /// \brief Checks the constraint is used by current object
55   virtual bool hasConstraint(ConstraintPtr theConstraint) const
56   { return theConstraint == myBaseConstraint; }
57
58   /// \brief Return list of SketchPlugin constraints attached to this object
59   virtual std::list<ConstraintPtr> constraints() const;
60
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;
65
66   /// \brief Shows error message
67   const std::string& error() const
68   { return myErrorMsg; }
69
70 protected:
71   /// \brief Converts SketchPlugin constraint to a list of SolveSpace constraints
72   virtual void process();
73
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);
78
79   /// \brief Create or change SlveSpace entity according to the given attribute
80   /// \param[in]  theAttribute  reference to the entity to be changed
81   /// \param[out] theType       type of created entity
82   /// \return identifier of created/updated entity
83   Slvs_hEntity changeEntity(AttributeRefAttrPtr theAttribute, int& theType);
84   /// \brief Create or change SlveSpace entity according to the given attribute
85   Slvs_hEntity changeEntity(AttributePtr theAttribute, int& theType);
86   /// \brief Create or change SlveSpace entity according to the given feature
87   Slvs_hEntity changeEntity(FeaturePtr theFeature, int& theType);
88
89   /// \brief Removes the links to unused entities
90   void cleanRemovedEntities();
91
92   /// \brief Removes last error
93   void cleanErrorMsg()
94   { myErrorMsg.clear(); }
95
96 protected:
97   SketchSolver_Group* myGroup; ///< the group which contains current constraint
98   ConstraintPtr myBaseConstraint; ///< SketchPlugin constraint
99   std::vector<Slvs_hConstraint> mySlvsConstraints; ///< list of indices of SolveSpace constraints, together which equivalent to SketchPlugin constraint
100   std::map<FeaturePtr, Slvs_hEntity> myFeatureMap; ///< map feature to the entity it represents
101   std::map<AttributePtr, Slvs_hEntity> myAttributeMap; ///< map attribute to the corresponding entity
102   std::map<AttributePtr, Slvs_hParam> myValueMap; ///< list of attributes, which represents single value (e.g. distance) used in constraint
103   StoragePtr myStorage; ///< storage, which contains all information about entities and constraints in current group
104
105   std::string myErrorMsg; ///< error message
106 };
107
108 typedef std::shared_ptr<SketchSolver_Constraint> SolverConstraintPtr;
109
110
111
112 /** \class   SketchSolver_ConstraintDistance
113  *  \ingroup Plugins
114  *  \brief   Convert distance constraint to SolveSpace structure
115  */
116 class SketchSolver_ConstraintDistance : public SketchSolver_Constraint
117 {
118 public:
119   SketchSolver_ConstraintDistance(ConstraintPtr theConstraint) :
120       SketchSolver_Constraint(theConstraint)
121   {}
122
123   virtual int getType() const
124   { return SLVS_C_PT_PT_DISTANCE; }
125 };
126
127
128 /** \class   SketchSolver_ConstraintParallel
129  *  \ingroup Plugins
130  *  \brief   Convert Parallel constraint to SolveSpace structure
131  */
132 class SketchSolver_ConstraintParallel : public SketchSolver_Constraint
133 {
134 public:
135   SketchSolver_ConstraintParallel(ConstraintPtr theConstraint) :
136       SketchSolver_Constraint(theConstraint)
137   {}
138
139   virtual int getType() const
140   { return SLVS_C_PARALLEL; }
141 };
142
143
144 /** \class   SketchSolver_ConstraintPerpendicular
145  *  \ingroup Plugins
146  *  \brief   Convert Perpendicular constraint to SolveSpace structure
147  */
148 class SketchSolver_ConstraintPerpendicular : public SketchSolver_Constraint
149 {
150 public:
151   SketchSolver_ConstraintPerpendicular(ConstraintPtr theConstraint) :
152       SketchSolver_Constraint(theConstraint)
153   {}
154
155   virtual int getType() const
156   { return SLVS_C_PERPENDICULAR; }
157 };
158
159
160 /** \class   SketchSolver_ConstraintHorizontal
161  *  \ingroup Plugins
162  *  \brief   Convert Horizontal constraint to SolveSpace structure
163  */
164 class SketchSolver_ConstraintHorizontal : public SketchSolver_Constraint
165 {
166 public:
167   SketchSolver_ConstraintHorizontal(ConstraintPtr theConstraint) :
168       SketchSolver_Constraint(theConstraint)
169   {}
170
171   virtual int getType() const
172   { return SLVS_C_HORIZONTAL; }
173 };
174
175
176 /** \class   SketchSolver_ConstraintVertical
177  *  \ingroup Plugins
178  *  \brief   Convert Vertical constraint to SolveSpace structure
179  */
180 class SketchSolver_ConstraintVertical : public SketchSolver_Constraint
181 {
182 public:
183   SketchSolver_ConstraintVertical(ConstraintPtr theConstraint) :
184       SketchSolver_Constraint(theConstraint)
185   {}
186
187   virtual int getType() const
188   { return SLVS_C_VERTICAL; }
189 };
190
191 #endif