Salome HOME
Issue #141 fix: Undo for constraints
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 // File:    SketchSolver_Constraint.cpp
2 // Created: 27 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_Constraint.h"
6 #include <SketchSolver_Solver.h>
7
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Arc.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_ConstraintLength.h>
15 #include <SketchPlugin_ConstraintParallel.h>
16 #include <SketchPlugin_ConstraintPerpendicular.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultConstruction.h>
24
25 #include <GeomDataAPI_Point.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
29
30 /// Possible types of attributes (used to determine constraint type)
31 enum AttrType
32 {
33   UNKNOWN,  // Something wrong during type determination
34   POINT2D,
35   POINT3D,
36   LINE,
37   CIRCLE,
38   ARC
39 };
40
41 /// Calculate type of the attribute
42 static AttrType typeOfAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute);
43
44 SketchSolver_Constraint::SketchSolver_Constraint()
45     : myConstraint(boost::shared_ptr<SketchPlugin_Constraint>()),
46       myType(SLVS_C_UNKNOWN),
47       myAttributesList()
48 {
49 }
50
51 SketchSolver_Constraint::SketchSolver_Constraint(
52     boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
53     : myConstraint(theConstraint),
54       myAttributesList()
55 {
56   myType = getType(myConstraint);
57 }
58
59 const int& SketchSolver_Constraint::getType(
60     boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
61 {
62   myType = SLVS_C_UNKNOWN;
63   if (!theConstraint)
64     return getType();
65
66   // Assign empty names of attributes
67   myAttributesList.clear();
68   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
69     myAttributesList.push_back(std::string());
70
71   const std::string& aConstraintKind = theConstraint->getKind();
72   // Constraint for coincidence of two points
73   if (aConstraintKind.compare(SketchPlugin_ConstraintCoincidence::ID()) == 0) {
74     int anAttrPos = 0;
75     // Verify the constraint has only two attributes and they are points
76     int aPt2d = 0;  // bit-mapped field, each bit indicates whether the attribute is 2D point
77     int aPt3d = 0;  // bit-mapped field, the same information for 3D points
78     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
79       boost::shared_ptr<ModelAPI_Attribute> anAttr = theConstraint->data()->attribute(
80           SketchPlugin_Constraint::ATTRIBUTE(indAttr));
81       if (!anAttr)
82         continue;
83       switch (typeOfAttribute(anAttr)) {
84         case POINT2D:  // the attribute is a 2D point
85           aPt2d |= (1 << indAttr);
86           myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
87           break;
88         case POINT3D:  // the attribute is a 3D point
89           aPt3d |= (1 << indAttr);
90           myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
91           break;
92         default:
93           // Attribute neither 2D nor 3D point is not supported by this type of constraint
94           return getType();
95       }
96     }
97     // The constrained points should be in first and second positions,
98     // so the expected value of aPt2d or aPt3d is 3
99     if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
100       myType = SLVS_C_POINTS_COINCIDENT;
101     // Constraint parameters are wrong
102     return getType();
103   }
104
105   // Constraint for distance between point and another entity
106   if (aConstraintKind.compare(SketchPlugin_ConstraintDistance::ID()) == 0) {
107     int aNbPoints = 0;
108     int aNbEntities = 0;
109     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
110       boost::shared_ptr<ModelAPI_Attribute> anAttr = theConstraint->data()->attribute(
111           SketchPlugin_Constraint::ATTRIBUTE(indAttr));
112       switch (typeOfAttribute(anAttr)) {
113         case POINT2D:
114         case POINT3D:
115           myAttributesList[aNbPoints++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
116           break;
117         case LINE:
118           // entities are placed starting from SketchPlugin_Constraint::ENTITY_C() attribute
119           myAttributesList[2 + aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
120           myType = SLVS_C_PT_LINE_DISTANCE;
121           break;
122       }
123     }
124     // Verify the correctness of constraint arguments
125     if (aNbPoints == 2 && aNbEntities == 0)
126       myType = SLVS_C_PT_PT_DISTANCE;
127     else if (aNbPoints != 1 || aNbEntities != 1)
128       myType = SLVS_C_UNKNOWN;
129     return getType();
130   }
131
132   // Constraint for the given length of a line
133   if (aConstraintKind.compare(SketchPlugin_ConstraintLength::ID()) == 0) {
134     int aNbLines = 0;
135     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
136       boost::shared_ptr<ModelAPI_Attribute> anAttr = theConstraint->data()->attribute(
137           SketchPlugin_Constraint::ATTRIBUTE(indAttr));
138       if (typeOfAttribute(anAttr) == LINE)
139         myAttributesList[aNbLines++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
140     }
141     if (aNbLines == 1)
142       myType = SLVS_C_PT_PT_DISTANCE;
143     return getType();
144   }
145
146   // Constraint for two parallel/perpendicular lines
147   bool isParallel = (aConstraintKind.compare(SketchPlugin_ConstraintParallel::ID()) == 0);
148   bool isPerpendicular = (aConstraintKind.compare(SketchPlugin_ConstraintPerpendicular::ID()) == 0);
149   if (isParallel || isPerpendicular) {
150     int aNbEntities = 2;  // lines in SolveSpace constraints should start from SketchPlugin_Constraint::ENTITY_C() attribute
151     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
152       boost::shared_ptr<ModelAPI_Attribute> anAttr = theConstraint->data()->attribute(
153           SketchPlugin_Constraint::ATTRIBUTE(indAttr));
154       if (typeOfAttribute(anAttr) == LINE)
155         myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
156     }
157     if (aNbEntities == 4)
158       myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR;
159     return getType();
160   }
161
162   // Constraint for radius of a circle or an arc of circle
163   if (aConstraintKind.compare(SketchPlugin_ConstraintRadius::ID()) == 0) {
164     int aNbEntities = 2;  // lines in SolveSpace constraints should started from SketchPlugin_Constraint::ENTITY_C() attribute
165     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
166       boost::shared_ptr<ModelAPI_Attribute> anAttr = theConstraint->data()->attribute(
167           SketchPlugin_Constraint::ATTRIBUTE(indAttr));
168       AttrType aType = typeOfAttribute(anAttr);
169       if (aType == CIRCLE || aType == ARC)
170         myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
171     }
172     if (aNbEntities == 3)
173       myType = SLVS_C_DIAMETER;
174     return getType();
175   }
176
177   /// \todo Implement other kind of constraints
178
179   return getType();
180 }
181
182 // ================= Auxiliary functions ==============================
183 AttrType typeOfAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute)
184 {
185   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttrRef = boost::dynamic_pointer_cast<
186       ModelAPI_AttributeRefAttr>(theAttribute);
187   if (!anAttrRef)
188     return UNKNOWN;
189
190   if (anAttrRef->isObject()) {
191     ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
192         anAttrRef->object());
193     if (!aRC)
194       return UNKNOWN;
195
196     if (aRC->shape()->isVertex())
197       return POINT3D;
198     else if (aRC->shape()->isEdge()) {
199       boost::shared_ptr<GeomAPI_Edge> anEdge = boost::dynamic_pointer_cast<GeomAPI_Edge>(
200           aRC->shape());
201       if (anEdge->isLine())
202         return LINE;
203       else if (anEdge->isCircle())
204         return CIRCLE;
205       else if (anEdge->isArc())
206         return ARC;
207     }
208   } else {
209     const std::string aType = anAttrRef->attr()->attributeType();
210     if (aType == GeomDataAPI_Point2D::type())
211       return POINT2D;
212     if (aType == GeomDataAPI_Point2D::type())
213       return POINT2D;
214   }
215
216   return UNKNOWN;
217 }
218