Salome HOME
Sort constraints before adding it to the solver (issue #911)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
1 #include <SketchSolver_ConstraintMultiRotation.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_MultiRotation.h>
7
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeInteger.h>
10 #include <ModelAPI_AttributeRefAttr.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_ResultConstruction.h>
13
14 #include <GeomAPI_Dir2d.h>
15 #include <GeomAPI_XY.h>
16
17 #include <math.h>
18
19 void SketchSolver_ConstraintMultiRotation::getAttributes(
20     Slvs_hEntity& theCenter, double& theAngle,
21     std::vector< std::vector<Slvs_hEntity> >& thePoints,
22     std::vector< std::vector<Slvs_hEntity> >& theEntities)
23 {
24   DataPtr aData = myBaseConstraint->data();
25   theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
26       aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
27
28   AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
29   if (!aCenterAttr || !aCenterAttr->isInitialized()) {
30     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
31     return;
32   }
33   int aType = SLVS_E_UNKNOWN; // type of created entity
34   Slvs_hEntity anEntityID = myGroup->getAttributeId(aCenterAttr);
35   if (anEntityID == SLVS_E_UNKNOWN)
36     anEntityID = changeEntity(aCenterAttr, aType);
37   theCenter = anEntityID;
38
39   // Lists of objects and number of copies
40   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
41       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
42   myNumberOfObjects = anInitialRefList->size();
43   myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value();
44   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
45       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
46   if (!aRefList) {
47     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
48     return;
49   }
50
51   // Obtain all points of initial features and store them into separate lists
52   // containing their translated copies.
53   // Also all circles and arc collected too, because they will be constrained by equal radii.
54   FeaturePtr aFeature;
55   ResultConstructionPtr aRC;
56   static const size_t MAX_POINTS = 3;
57   std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
58   std::vector<Slvs_hEntity> anEntities;
59   std::list<ObjectPtr> anObjectList = aRefList->list();
60   std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
61   while (anObjectIter != anObjectList.end()) {
62     for (size_t i = 0; i < MAX_POINTS; ++i)
63       aPoints[i].clear();
64     anEntities.clear();
65
66     for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
67       aFeature = ModelAPI_Feature::feature(*anObjectIter);
68       if (!aFeature)
69         continue;
70       anEntityID = changeEntity(aFeature, aType);
71       anEntities.push_back(anEntityID);
72       Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
73       switch (aType) {
74       case SLVS_E_POINT_IN_2D:
75       case SLVS_E_POINT_IN_3D:
76         aPoints[0].push_back(anEntityID);
77         break;
78       case SLVS_E_LINE_SEGMENT:
79         aPoints[0].push_back(anEntity.point[0]); // start point of line
80         aPoints[1].push_back(anEntity.point[1]); // end point of line
81         break;
82       case SLVS_E_CIRCLE:
83         aPoints[0].push_back(anEntity.point[0]); // center of circle
84         break;
85       case SLVS_E_ARC_OF_CIRCLE:
86         aPoints[0].push_back(anEntity.point[0]); // center of arc
87         aPoints[1].push_back(anEntity.point[1]); // start point of arc
88         aPoints[2].push_back(anEntity.point[2]); // end point of arc
89         break;
90       default:
91         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
92         return;
93       }
94     }
95
96     for (size_t i = 0; i < MAX_POINTS; ++i)
97       if (!aPoints[i].empty())
98         thePoints.push_back(aPoints[i]);
99     if (!anEntities.empty())
100       theEntities.push_back(anEntities);
101   }
102 }
103
104 void SketchSolver_ConstraintMultiRotation::process()
105 {
106   cleanErrorMsg();
107   if (!myBaseConstraint || !myStorage || myGroup == 0) {
108     /// TODO: Put error message here
109     return;
110   }
111   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
112     update(myBaseConstraint);
113
114   std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
115   getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
116   if (!myErrorMsg.empty())
117     return;
118
119   // Set the rotation center unchanged during constraint recalculation
120   Slvs_Constraint aConstraint;
121   if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
122     aConstraint = Slvs_MakeConstraint(
123         SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
124         myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
125     aConstraint.h = myStorage->addConstraint(aConstraint);
126     mySlvsConstraints.push_back(aConstraint.h);
127   }
128
129   processEntities(anEntitiesAndCopies);
130   adjustConstraint();
131 }
132
133 void SketchSolver_ConstraintMultiRotation::updateLocal()
134 {
135   // update angle value
136   myAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
137       myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
138 }
139
140 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
141 {
142   if (fabs(myAngle) < tolerance) {
143     myStorage->setNeedToResolve(false);
144     return;
145   }
146
147   std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
148   std::list<Slvs_Constraint>::const_iterator aCoIt;
149
150   // Check overconstrained on rotation center (if it is coincident with other fixed point)
151   Slvs_hConstraint aFixedCenter;
152   if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
153     Slvs_hConstraint aFixed;
154     for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
155       if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
156           (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
157         // Un-fix the center
158         myStorage->removeConstraint(aFixedCenter);
159         std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
160         for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
161           if (*aSCIt == aFixedCenter) {
162             mySlvsConstraints.erase(aSCIt);
163             break;
164           }
165       }
166   }
167
168   // Obtain coordinates of rotation center
169   Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
170   myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
171   myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
172
173   myRotationVal[0] = sin(myAngle * PI / 180.0);
174   myRotationVal[1] = cos(myAngle * PI / 180.0);
175
176   SketchSolver_ConstraintMulti::adjustConstraint();
177 }
178
179 void SketchSolver_ConstraintMultiRotation::getRelative(
180     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
181 {
182   theRelX = theAbsX - myCenterCoord[0];
183   theRelY = theAbsY - myCenterCoord[1];
184 }
185
186 void SketchSolver_ConstraintMultiRotation::getAbsolute(
187     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
188 {
189   theAbsX = theRelX + myCenterCoord[0];
190   theAbsY = theRelY + myCenterCoord[1];
191 }
192
193 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
194 {
195   // rotate direction
196   // myRotationVal[0] = sinA, myRotationVal[1] = cosA
197   double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
198   theY = theX * myRotationVal[0] + theY * myRotationVal[1];
199   theX = aTemp;
200 }
201
202 const std::string& SketchSolver_ConstraintMultiRotation::nameNbCopies()
203 {
204   return SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID();
205 }