Salome HOME
Issue #1084: parameter cyclic dependence
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiTranslation.cpp
1 #include <SketchSolver_ConstraintMultiTranslation.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_MultiTranslation.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 #include <ModelAPI_Data.h>
14
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_XY.h>
17
18 #include <math.h>
19
20
21 void SketchSolver_ConstraintMultiTranslation::getAttributes(
22     Slvs_hEntity& theStartPoint, Slvs_hEntity& theEndPoint,
23     std::vector< std::vector<Slvs_hEntity> >& thePoints,
24     std::vector< std::vector<Slvs_hEntity> >& theEntities)
25 {
26   DataPtr aData = myBaseConstraint->data();
27   AttributePtr aStartPointAttr = aData->attribute(SketchPlugin_MultiTranslation::START_POINT_ID());
28   AttributePtr aEndPointAttr = aData->attribute(SketchPlugin_MultiTranslation::END_POINT_ID());
29   if (!aStartPointAttr || !aStartPointAttr->isInitialized() ||
30       !aEndPointAttr || !aEndPointAttr->isInitialized()) {
31     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
32     return;
33   }
34
35   int aType = SLVS_E_UNKNOWN; // type of created entity
36   Slvs_hEntity anEntityID = myGroup->getAttributeId(aStartPointAttr);
37   if (anEntityID == SLVS_E_UNKNOWN)
38     anEntityID = changeEntity(aStartPointAttr, aType);
39   theStartPoint = anEntityID;
40   anEntityID = myGroup->getAttributeId(aEndPointAttr);
41   if (anEntityID == SLVS_E_UNKNOWN)
42     anEntityID = changeEntity(aEndPointAttr, aType);
43   theEndPoint = anEntityID;
44
45   // Lists of objects and number of copies
46   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
47       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
48   myNumberOfObjects = anInitialRefList->size();
49   myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiTranslation::NUMBER_OF_OBJECTS_ID())->value() - 1;
50   if (myNumberOfCopies <= 0)
51     return;
52
53   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
54       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
55   if (!aRefList) {
56     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
57     return;
58   }
59
60   // Obtain all points of initial features and store them into separate lists
61   // containing their translated copies.
62   // Also all circles and arc collected too, because they will be constrained by equal radii.
63   FeaturePtr aFeature;
64   ResultConstructionPtr aRC;
65   static const size_t MAX_POINTS = 3;
66   std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
67   std::vector<Slvs_hEntity> anEntities;          // list of translated entities
68   std::list<ObjectPtr> anObjectList = aRefList->list();
69   std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
70   while (anObjectIter != anObjectList.end()) {
71     for (size_t i = 0; i < MAX_POINTS; i++)
72       aPoints[i].clear();
73     anEntities.clear();
74
75     for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
76       aFeature = ModelAPI_Feature::feature(*anObjectIter);
77       if (!aFeature)
78         continue;
79       anEntityID = changeEntity(aFeature, aType);
80       anEntities.push_back(anEntityID);
81       Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
82       switch (aType) {
83       case SLVS_E_POINT_IN_2D:
84       case SLVS_E_POINT_IN_3D:
85         aPoints[0].push_back(anEntityID);
86         break;
87       case SLVS_E_LINE_SEGMENT:
88         aPoints[0].push_back(anEntity.point[0]); // start point of line
89         aPoints[1].push_back(anEntity.point[1]); // end point of line
90         break;
91       case SLVS_E_CIRCLE:
92         aPoints[0].push_back(anEntity.point[0]); // center of circle
93         break;
94       case SLVS_E_ARC_OF_CIRCLE:
95         aPoints[0].push_back(anEntity.point[0]); // center of arc
96         aPoints[1].push_back(anEntity.point[1]); // start point of arc
97         aPoints[2].push_back(anEntity.point[2]); // end point of arc
98         break;
99       default:
100         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
101         return;
102       }
103     }
104
105     for (size_t i = 0; i < MAX_POINTS; ++i)
106       if (!aPoints[i].empty())
107         thePoints.push_back(aPoints[i]);
108     if (!anEntities.empty())
109       theEntities.push_back(anEntities);
110   }
111 }
112
113 void SketchSolver_ConstraintMultiTranslation::process()
114 {
115   cleanErrorMsg();
116   if (!myBaseConstraint || !myStorage || myGroup == 0) {
117     /// TODO: Put error message here
118     return;
119   }
120   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
121     update(myBaseConstraint);
122
123   Slvs_hEntity aStartPoint, aEndPoint;
124   std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
125   getAttributes(aStartPoint, aEndPoint, myPointsAndCopies, anEntitiesAndCopies);
126   if (!myErrorMsg.empty())
127     return;
128
129   // Create translation line
130   if (myTranslationLine == SLVS_E_UNKNOWN) {
131     Slvs_Entity aTranslationLine = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
132         myGroup->getWorkplaneId(), aStartPoint, aEndPoint);
133     aTranslationLine.h = myStorage->addEntity(aTranslationLine);
134     myTranslationLine = aTranslationLine.h;
135   } else {
136     Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
137     if (aTranslationLine.point[0] != aStartPoint || aTranslationLine.point[1] != aEndPoint) {
138       aTranslationLine.point[0] = aStartPoint;
139       aTranslationLine.point[1] = aEndPoint;
140       myStorage->updateEntity(aTranslationLine);
141     }
142   }
143
144   myAdjusted = false;
145   processEntities(anEntitiesAndCopies);
146   adjustConstraint();
147 }
148
149 void SketchSolver_ConstraintMultiTranslation::adjustConstraint()
150 {
151   if (myAdjusted)
152     return;
153
154   Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
155   Slvs_hConstraint aFixed; // temporary variable
156   // Set the translation line unchanged during constraint recalculation
157   for (int i = 0; i < 2; i++) {
158     if (myStorage->isPointFixed(aTranslationLine.point[i], aFixed, true))
159       continue;
160     Slvs_Constraint aConstraint = Slvs_MakeConstraint(
161         SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
162         aTranslationLine.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
163     aConstraint.h = myStorage->addConstraint(aConstraint);
164     myStorage->addTemporaryConstraint(aConstraint.h);
165   }
166
167   // Check if the distance between point is 0, no need to resolve constraints (just wait another values)
168   double aXY[4];
169   for (int i = 0; i < 2; i++) {
170     Slvs_Entity aPnt = myStorage->getEntity(aTranslationLine.point[i]);
171     aXY[2*i] = myStorage->getParameter(aPnt.param[0]).val;
172     aXY[2*i+1] = myStorage->getParameter(aPnt.param[1]).val;
173   }
174   myDelta[0] = aXY[2] - aXY[0];
175   myDelta[1] = aXY[3] - aXY[1];
176   if (fabs(myDelta[0]) + fabs(myDelta[1]) < tolerance) {
177     myStorage->setNeedToResolve(false);
178     return;
179   }
180
181   SketchSolver_ConstraintMulti::adjustConstraint();
182 }
183
184 void SketchSolver_ConstraintMultiTranslation::getRelative(
185     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
186 {
187   theRelX = theAbsX;
188   theRelY = theAbsY;
189 }
190
191 void SketchSolver_ConstraintMultiTranslation::getAbsolute(
192     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
193 {
194   theAbsX = theRelX;
195   theAbsY = theRelY;
196 }
197
198 void SketchSolver_ConstraintMultiTranslation::transformRelative(double& theX, double& theY)
199 {
200   // translate coordinates
201   theX += myDelta[0];
202   theY += myDelta[1];
203 }
204
205 const std::string& SketchSolver_ConstraintMultiTranslation::nameNbObjects()
206 {
207   return SketchPlugin_MultiTranslation::NUMBER_OF_OBJECTS_ID();
208 }