Salome HOME
#878 Segmentation fault when setting distance on the two same points
[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_COPIES_ID())->value();
50   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
51       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
52   if (!aRefList) {
53     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
54     return;
55   }
56
57   // Obtain all points of initial features and store them into separate lists
58   // containing their translated copies.
59   // Also all circles and arc collected too, because they will be constrained by equal radii.
60   FeaturePtr aFeature;
61   ResultConstructionPtr aRC;
62   static const size_t MAX_POINTS = 3;
63   std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
64   std::vector<Slvs_hEntity> anEntities;          // list of translated entities
65   std::list<ObjectPtr> anObjectList = aRefList->list();
66   std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
67   while (anObjectIter != anObjectList.end()) {
68     for (size_t i = 0; i < MAX_POINTS; i++)
69       aPoints[i].clear();
70     anEntities.clear();
71
72     for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
73       aFeature = ModelAPI_Feature::feature(*anObjectIter);
74       if (!aFeature)
75         continue;
76       anEntityID = changeEntity(aFeature, aType);
77       anEntities.push_back(anEntityID);
78       Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
79       switch (aType) {
80       case SLVS_E_POINT_IN_2D:
81       case SLVS_E_POINT_IN_3D:
82         aPoints[0].push_back(anEntityID);
83         break;
84       case SLVS_E_LINE_SEGMENT:
85         aPoints[0].push_back(anEntity.point[0]); // start point of line
86         aPoints[1].push_back(anEntity.point[1]); // end point of line
87         break;
88       case SLVS_E_CIRCLE:
89         aPoints[0].push_back(anEntity.point[0]); // center of circle
90         break;
91       case SLVS_E_ARC_OF_CIRCLE:
92         aPoints[0].push_back(anEntity.point[0]); // center of arc
93         aPoints[1].push_back(anEntity.point[1]); // start point of arc
94         aPoints[2].push_back(anEntity.point[2]); // end point of arc
95         break;
96       default:
97         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
98         return;
99       }
100     }
101
102     for (size_t i = 0; i < MAX_POINTS; ++i)
103       if (!aPoints[i].empty())
104         thePoints.push_back(aPoints[i]);
105     if (!anEntities.empty())
106       theEntities.push_back(anEntities);
107   }
108 }
109
110 void SketchSolver_ConstraintMultiTranslation::process()
111 {
112   cleanErrorMsg();
113   if (!myBaseConstraint || !myStorage || myGroup == 0) {
114     /// TODO: Put error message here
115     return;
116   }
117   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
118     update(myBaseConstraint);
119
120   Slvs_hEntity aStartPoint, aEndPoint;
121   std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
122   getAttributes(aStartPoint, aEndPoint, myPointsAndCopies, anEntitiesAndCopies);
123   if (!myErrorMsg.empty())
124     return;
125
126   // Create translation line
127   if (myTranslationLine == SLVS_E_UNKNOWN) {
128     Slvs_Entity aTranslationLine = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
129         myGroup->getWorkplaneId(), aStartPoint, aEndPoint);
130     aTranslationLine.h = myStorage->addEntity(aTranslationLine);
131     myTranslationLine = aTranslationLine.h;
132   } else {
133     Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
134     if (aTranslationLine.point[0] != aStartPoint || aTranslationLine.point[1] != aEndPoint) {
135       aTranslationLine.point[0] = aStartPoint;
136       aTranslationLine.point[1] = aEndPoint;
137       myStorage->updateEntity(aTranslationLine);
138     }
139   }
140
141   myAdjusted = false;
142   processEntities(anEntitiesAndCopies);
143   adjustConstraint();
144 }
145
146 void SketchSolver_ConstraintMultiTranslation::adjustConstraint()
147 {
148   if (myAdjusted)
149     return;
150
151   Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
152   Slvs_hConstraint aFixed; // temporary variable
153   // Set the translation line unchanged during constraint recalculation
154   for (int i = 0; i < 2; i++) {
155     if (myStorage->isPointFixed(aTranslationLine.point[i], aFixed, true))
156       continue;
157     Slvs_Constraint aConstraint = Slvs_MakeConstraint(
158         SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
159         aTranslationLine.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
160     aConstraint.h = myStorage->addConstraint(aConstraint);
161     myStorage->addTemporaryConstraint(aConstraint.h);
162   }
163
164   // Check if the distance between point is 0, no need to resolve constraints (just wait another values)
165   double aXY[4];
166   for (int i = 0; i < 2; i++) {
167     Slvs_Entity aPnt = myStorage->getEntity(aTranslationLine.point[i]);
168     aXY[2*i] = myStorage->getParameter(aPnt.param[0]).val;
169     aXY[2*i+1] = myStorage->getParameter(aPnt.param[1]).val;
170   }
171   myDelta[0] = aXY[2] - aXY[0];
172   myDelta[1] = aXY[3] - aXY[1];
173   if (fabs(myDelta[0]) + fabs(myDelta[1]) < tolerance) {
174     myStorage->setNeedToResolve(false);
175     return;
176   }
177
178   SketchSolver_ConstraintMulti::adjustConstraint();
179 }
180
181 void SketchSolver_ConstraintMultiTranslation::getRelative(
182     double theAbsX, double theAbsY, double& theRelX, double& theRelY)
183 {
184   theRelX = theAbsX;
185   theRelY = theAbsY;
186 }
187
188 void SketchSolver_ConstraintMultiTranslation::getAbsolute(
189     double theRelX, double theRelY, double& theAbsX, double& theAbsY)
190 {
191   theAbsX = theRelX;
192   theAbsY = theRelY;
193 }
194
195 void SketchSolver_ConstraintMultiTranslation::transformRelative(double& theX, double& theY)
196 {
197   // translate coordinates
198   theX += myDelta[0];
199   theY += myDelta[1];
200 }
201
202 const std::string& SketchSolver_ConstraintMultiTranslation::nameNbCopies()
203 {
204   return SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID();
205 }