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