1 // File: SketchSolver_Constraint.cpp
2 // Created: 27 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_Constraint.h"
6 #include <SketchSolver_Solver.h>
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>
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>
25 #include <GeomDataAPI_Point.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomAPI_Edge.h>
28 #include <GeomAPI_Shape.h>
30 /// Possible types of attributes (used to determine constraint type)
33 UNKNOWN, // Something wrong during type determination
41 /// Calculate type of the attribute
42 static AttrType typeOfAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute);
46 SketchSolver_Constraint::SketchSolver_Constraint()
47 : myConstraint(boost::shared_ptr<SketchPlugin_Constraint>()),
48 myType(SLVS_C_UNKNOWN),
53 SketchSolver_Constraint::SketchSolver_Constraint(
54 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
55 : myConstraint(theConstraint),
58 myType = getType(myConstraint);
61 const int& SketchSolver_Constraint::getType(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
63 myType = SLVS_C_UNKNOWN;
67 // Assign empty names of attributes
68 myAttributesList.clear();
69 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
70 myAttributesList.push_back(std::string());
72 const std::string& aConstraintKind = theConstraint->getKind();
73 // Constraint for coincidence of two points
74 if (aConstraintKind.compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
77 // Verify the constraint has only two attributes and they are points
78 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
79 int aPt3d = 0; // bit-mapped field, the same information for 3D points
80 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
82 boost::shared_ptr<ModelAPI_Attribute> anAttr =
83 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
84 switch (typeOfAttribute(anAttr))
86 case POINT2D: // the attribute is a 2D point
87 aPt2d |= (1 << indAttr);
88 myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
90 case POINT3D: // the attribute is a 3D point
91 aPt3d |= (1 << indAttr);
92 myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
95 // Attribute neither 2D nor 3D point is not supported by this type of constraint
99 // The constrained points should be in first and second positions,
100 // so the expected value of aPt2d or aPt3d is 3
101 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
102 myType = SLVS_C_POINTS_COINCIDENT;
103 // Constraint parameters are wrong
107 // Constraint for distance between point and another entity
108 if (aConstraintKind.compare(SketchPlugin_ConstraintDistance::ID()) == 0)
112 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
114 boost::shared_ptr<ModelAPI_Attribute> anAttr =
115 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
116 switch (typeOfAttribute(anAttr))
120 myAttributesList[aNbPoints++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
123 // entities are placed starting from SketchPlugin_Constraint::ENTITY_C() attribute
124 myAttributesList[2 + aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
125 myType = SLVS_C_PT_LINE_DISTANCE;
129 // Verify the correctness of constraint arguments
130 if (aNbPoints == 2 && aNbEntities ==0)
131 myType = SLVS_C_PT_PT_DISTANCE;
132 else if (aNbPoints != 1 || aNbEntities != 1)
133 myType = SLVS_C_UNKNOWN;
137 // Constraint for the given length of a line
138 if (aConstraintKind.compare(SketchPlugin_ConstraintLength::ID()) == 0)
141 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
143 boost::shared_ptr<ModelAPI_Attribute> anAttr =
144 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
145 if (typeOfAttribute(anAttr) == LINE)
146 myAttributesList[aNbLines++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
149 myType = SLVS_C_PT_PT_DISTANCE;
153 // Constraint for two parallel/perpendicular lines
154 bool isParallel = (aConstraintKind.compare(SketchPlugin_ConstraintParallel::ID()) == 0);
155 bool isPerpendicular = (aConstraintKind.compare(SketchPlugin_ConstraintPerpendicular::ID()) == 0);
156 if (isParallel || isPerpendicular)
158 int aNbEntities = 2; // lines in SolveSpace constraints should start from SketchPlugin_Constraint::ENTITY_C() attribute
159 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
161 boost::shared_ptr<ModelAPI_Attribute> anAttr =
162 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
163 if (typeOfAttribute(anAttr) == LINE)
164 myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
166 if (aNbEntities == 4)
167 myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR;
171 // Constraint for radius of a circle or an arc of circle
172 if (aConstraintKind.compare(SketchPlugin_ConstraintRadius::ID()) == 0)
174 int aNbEntities = 2; // lines in SolveSpace constraints should started from SketchPlugin_Constraint::ENTITY_C() attribute
175 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
177 boost::shared_ptr<ModelAPI_Attribute> anAttr =
178 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
179 AttrType aType = typeOfAttribute(anAttr);
180 if (aType == CIRCLE || aType == ARC)
181 myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
183 if (aNbEntities == 3)
184 myType = SLVS_C_DIAMETER;
188 /// \todo Implement other kind of constraints
194 // ================= Auxiliary functions ==============================
195 AttrType typeOfAttribute(boost::shared_ptr<ModelAPI_Attribute> theAttribute)
197 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttrRef =
198 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
199 if (!anAttrRef) return UNKNOWN;
201 if (anAttrRef->isObject())
203 ResultConstructionPtr aRC =
204 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anAttrRef->object());
205 if (!aRC) return UNKNOWN;
207 if (aRC->shape()->isVertex())
209 else if (aRC->shape()->isEdge())
211 boost::shared_ptr<GeomAPI_Edge> anEdge =
212 boost::dynamic_pointer_cast<GeomAPI_Edge>(aRC->shape());
213 if (anEdge->isLine())
215 else if (anEdge->isCircle())
217 else if (anEdge->isArc())
223 const std::string aType = anAttrRef->attr()->attributeType();
224 if (aType == GeomDataAPI_Point2D::type())
226 if (aType == GeomDataAPI_Point2D::type())