Salome HOME
Add tools
[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   myAdjusted = false;
130   processEntities(anEntitiesAndCopies);
131   adjustConstraint();
132 }
133
134 void SketchSolver_ConstraintMultiRotation::updateLocal()
135 {
136   double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
137       myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
138   if (fabs(myAngle - aValue) > tolerance)
139     myAdjusted = false;
140   // update angle value
141   myAngle = aValue;
142 }
143
144 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
145 {
146   if (fabs(myAngle) < tolerance) {
147     myStorage->setNeedToResolve(false);
148     return;
149   }
150   if (myAdjusted)
151     return;
152
153   std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
154   std::list<Slvs_Constraint>::const_iterator aCoIt;
155
156   // Check overconstrained on rotation center (if it is coincident with other fixed point)
157   Slvs_hConstraint aFixedCenter;
158   if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
159     Slvs_hConstraint aFixed;
160     for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
161       if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
162           (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
163         // Un-fix the center
164         myStorage->removeConstraint(aFixedCenter);
165         std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
166         for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
167           if (*aSCIt == aFixedCenter) {
168             mySlvsConstraints.erase(aSCIt);
169             break;
170           }
171       }
172   }
173
174   // Obtain coordinates of rotation center
175   Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
176   myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
177   myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
178
179   myRotationVal[0] = sin(myAngle * PI / 180.0);
180   myRotationVal[1] = cos(myAngle * PI / 180.0);
181
182   SketchSolver_ConstraintMulti::adjustConstraint();
183 }
184
185 void SketchSolver_ConstraintMultiRotation::getRelative(
186     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
187 {
188   theRelX = theAbsX - myCenterCoord[0];
189   theRelY = theAbsY - myCenterCoord[1];
190 }
191
192 void SketchSolver_ConstraintMultiRotation::getAbsolute(
193     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
194 {
195   theAbsX = theRelX + myCenterCoord[0];
196   theAbsY = theRelY + myCenterCoord[1];
197 }
198
199 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
200 {
201   // rotate direction
202   // myRotationVal[0] = sinA, myRotationVal[1] = cosA
203   double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
204   theY = theX * myRotationVal[0] + theY * myRotationVal[1];
205   theX = aTemp;
206 }
207
208 const std::string& SketchSolver_ConstraintMultiRotation::nameNbCopies()
209 {
210   return SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID();
211 }