Salome HOME
Issue #995: Checkbox 'Make a copy' during translation and rotation operations
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
1 #include <SketchSolver_ConstraintMulti.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5 #include <SketchPlugin_Arc.h>
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_AttributeInteger.h>
9 #include <ModelAPI_AttributeRefAttr.h>
10 #include <ModelAPI_AttributeRefList.h>
11 #include <ModelAPI_ResultConstruction.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_XY.h>
15
16 #include <math.h>
17
18 void SketchSolver_ConstraintMulti::processEntities(const std::vector< std::vector<Slvs_hEntity> >& theEntAndCopies)
19 {
20   // Keep all objects unchanged (only initial object may be changed by user)
21   myCircsAndCopies.clear();
22   std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = theEntAndCopies.begin();
23   std::vector<Slvs_hEntity>::const_iterator aCpIt;
24   for (; anEntIt != theEntAndCopies.end(); ++anEntIt) {
25     std::vector<Slvs_hEntity> aCircs;
26     aCpIt = anEntIt->begin();
27     // Obtain initial points
28     Slvs_Entity anInitial = myStorage->getEntity(*aCpIt);
29     if (anInitial.type == SLVS_E_POINT_IN_2D || anInitial.type == SLVS_E_POINT_IN_3D)
30       myInitialPoints.insert(anInitial.h);
31     else {
32       for (int i = 0; i < 4 && anInitial.point[i] != SLVS_E_UNKNOWN; i++)
33         myInitialPoints.insert(anInitial.point[i]);
34     }
35
36     // Fix the copies
37     for (++aCpIt; aCpIt != anEntIt->end(); ++aCpIt) {
38       const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
39       std::vector<Slvs_hConstraint> aNewConstr;
40       if (anEntity.type == SLVS_E_CIRCLE) {
41         aCircs.push_back(anEntity.distance);
42         // for circles we fix only center
43         aNewConstr = myStorage->fixEntity(anEntity.point[0]);
44       } else
45         aNewConstr = myStorage->fixEntity(*aCpIt);
46       if (anEntity.type == SLVS_E_ARC_OF_CIRCLE)
47         aCircs.push_back(anEntity.h);
48       mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
49     }
50
51     if (!aCircs.empty()) {
52       if (anInitial.type == SLVS_E_CIRCLE)
53         aCircs.insert(aCircs.begin(), anInitial.distance);
54       else
55         aCircs.insert(aCircs.begin(), anInitial.h);
56       myCircsAndCopies.push_back(aCircs);
57     }
58   }
59 }
60
61 void SketchSolver_ConstraintMulti::update(ConstraintPtr theConstraint)
62 {
63   cleanErrorMsg();
64   if (!theConstraint || theConstraint == myBaseConstraint) {
65     AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
66         myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
67     AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
68     if (anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies) {
69       remove(myBaseConstraint);
70       process();
71       return;
72     }
73   }
74
75   updateLocal();
76   SketchSolver_Constraint::update();
77 }
78
79 bool SketchSolver_ConstraintMulti::remove(ConstraintPtr theConstraint)
80 {
81   cleanErrorMsg();
82   if (theConstraint && theConstraint != myBaseConstraint)
83     return false;
84   bool isFullyRemoved = true;
85   std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
86   for (; aCIter != mySlvsConstraints.end(); aCIter++)
87    isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
88   mySlvsConstraints.clear();
89
90   std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
91   for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
92     myStorage->removeEntity(aFeatIt->second);
93   myStorage->removeUnusedEntities();
94
95   std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
96
97   if (isFullyRemoved) {
98     myFeatureMap.clear();
99     myAttributeMap.clear();
100     myValueMap.clear();
101   } else
102     cleanRemovedEntities();
103
104   // Restore initial features
105   std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
106   for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
107   {
108     if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
109       continue; // the feature was not removed
110     Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
111     if (anEntity != SLVS_E_UNKNOWN)
112       myFeatureMap[aFIter->first] = anEntity;
113   }
114
115   // Clear list of rotated points
116   myPointsAndCopies.clear();
117   myInitialPoints.clear();
118
119   return true;
120 }
121
122 void SketchSolver_ConstraintMulti::addFeature(FeaturePtr theFeature)
123 {
124   SketchSolver_Constraint::addFeature(theFeature);
125
126   std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
127   if (aFeatIt == myFeatureMap.end())
128     return;
129
130   // store list of points of the feature
131   const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
132   for (int i = 0; i < 4; i++)
133     if (theEntity.point[i] != SLVS_E_UNKNOWN)
134       myPointsJustUpdated.insert(theEntity.point[i]);
135 }
136
137 void SketchSolver_ConstraintMulti::adjustConstraint()
138 {
139   if (myAdjusted)
140     return; // constraint already adjusted, don't do it once again
141
142   double aRelCoord[2]  = {0.0, 0.0}; // relative coordinates of point
143   double anAbsCoord[2] = {0.0, 0.0}; // absolute coordinates of point
144
145   std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
146   std::list<Slvs_Constraint>::const_iterator aCoIt;
147
148   // Update positions of all points to satisfy angles
149   std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
150   std::vector<Slvs_hEntity>::const_iterator aCopyIter;
151   for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
152     aCopyIter = aPointsIter->begin();
153     const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
154     for (int i = 0; i < 2; i++)
155       anAbsCoord[i] = myStorage->getParameter(anInitial.param[i]).val;
156     getRelative(anAbsCoord[0], anAbsCoord[1], aRelCoord[0], aRelCoord[1]);
157
158     // if the point is coincident with another one which is temporary fixed (moved by user),
159     // we will update its position correspondingly
160     Slvs_hConstraint aFixed;
161     for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
162       if ((aCoIt->ptA == anInitial.h && myInitialPoints.find(aCoIt->ptB) != myInitialPoints.end()) ||
163           (aCoIt->ptB == anInitial.h && myInitialPoints.find(aCoIt->ptA) != myInitialPoints.end())) {
164         Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
165         if (!myStorage->isTemporary(aFixed) &&
166             myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
167           continue; // nothing to change
168
169         const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
170         for (int i = 0; i < 2; i++) {
171           Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
172           const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
173           anInitParam.val = anOtherParam.val;
174           myStorage->updateParameter(anInitParam);
175           anAbsCoord[i] = anOtherParam.val;
176         }
177         getRelative(anAbsCoord[0], anAbsCoord[1], aRelCoord[0], aRelCoord[1]);
178       }
179     }
180
181     // update copied points
182     aCopyIter = aPointsIter->begin();
183     for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
184       // transform coordinates
185       transformRelative(aRelCoord[0], aRelCoord[1]);
186       getAbsolute(aRelCoord[0], aRelCoord[1], anAbsCoord[0], anAbsCoord[1]);
187
188       const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
189       for (int i = 0; i < 2; i++) {
190         Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
191         aParam.val = anAbsCoord[i];
192         myStorage->updateParameter(aParam);
193       }
194     }
195   }
196
197   std::list<Slvs_Constraint> aDiamConstr;
198   for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
199     aCopyIter = aPointsIter->begin();
200     const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
201     if (anInitial.type == SLVS_E_DISTANCE) {
202       const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
203       for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
204         const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
205         Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
206         aCopyRad.val = anInitRad.val;
207         myStorage->updateParameter(aCopyRad);
208       }
209     } else if (anInitial.type == SLVS_E_ARC_OF_CIRCLE) {
210       const Slvs_Entity& aCenterEnt = myStorage->getEntity(anInitial.point[0]);
211       const Slvs_Entity& aStartEnt = myStorage->getEntity(anInitial.point[1]);
212
213       if (aDiamConstr.empty())
214         aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
215       // Calculate diameter of initial arc
216       double aDiam = 0.0;
217       for (int i = 0; i < 2; i++) {
218         double d = myStorage->getParameter(aStartEnt.param[i]).val -
219                    myStorage->getParameter(aCenterEnt.param[i]).val;
220         aDiam += d * d;
221       }
222       aDiam = sqrt(aDiam) * 2.0;
223       // Update the Diameter constraints of copied arcs
224       for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
225         std::list<Slvs_Constraint>::iterator aDCIt = aDiamConstr.begin();
226         for (; aDCIt != aDiamConstr.end(); ++aDCIt)
227           if (aDCIt->entityA == *aCopyIter) {
228             aDCIt->valA = aDiam;
229             myStorage->updateConstraint(*aDCIt);
230             aDiamConstr.erase(aDCIt);
231             break;
232           }
233       }
234     }
235   }
236
237   myPointsJustUpdated.clear();
238   myAdjusted = true;
239 }
240
241 void SketchSolver_ConstraintMulti::checkCoincidence()
242 {
243   std::vector< std::vector<Slvs_hEntity> > aFilteredPoints; // points are filtered by their positions
244
245   std::vector< std::vector<Slvs_hEntity> >::const_iterator aPCIt = myPointsAndCopies.begin();
246   std::vector<Slvs_hEntity>::const_iterator aCIt;
247   for (; aPCIt != myPointsAndCopies.end(); ++aPCIt) {
248     aCIt = aPCIt->begin();
249     // Skip first element, focus the copies only
250     for (++aCIt; aCIt != aPCIt->end(); ++aCIt) {
251       std::vector< std::vector<Slvs_hEntity> >::iterator aFilterIt = aFilteredPoints.begin();
252       for (; aFilterIt != aFilteredPoints.end(); ++aFilterIt)
253         if (myStorage->isEqual(*aCIt, aFilterIt->front())) {
254           aFilterIt->push_back(*aCIt);
255           break;
256         }
257       if (aFilterIt == aFilteredPoints.end()) {
258         std::vector<Slvs_hEntity> aNewFilter(1, *aCIt);
259         aFilteredPoints.push_back(aNewFilter);
260       }
261     }
262   }
263
264   // Check the coicidence of filtered points and remove extra fixation.
265   // Also check separated points which are not fixed.
266   std::vector< std::vector<Slvs_hEntity> >::iterator aFPIt = aFilteredPoints.begin();
267   for (; aFPIt != aFilteredPoints.end(); ++aFPIt) {
268     if (aFPIt->size() <= 1)
269       continue;
270     std::vector<Slvs_hEntity>::iterator anIt1, anIt2;
271     for (anIt1 = aFPIt->begin(); anIt1 != aFPIt->end(); ++anIt1) {
272       for (anIt2 = anIt1 + 1; anIt2 != aFPIt->end(); ++anIt2) {
273         Slvs_hConstraint aFixed1, aFixed2;
274         bool isFixed1 = myStorage->isPointFixed(*anIt1, aFixed1);
275         bool isFixed2 = myStorage->isPointFixed(*anIt2, aFixed2);
276         if (myStorage->isCoincident(*anIt1, *anIt2)) {
277           if (!isFixed1 && isFixed2) {
278             Slvs_hEntity aTmp = *anIt1;
279             *anIt1 = *anIt2;
280             *anIt2 = aTmp;
281           } else if (isFixed1 && isFixed2) {
282             // remove fixing of the second point
283             myStorage->removeConstraint(aFixed2);
284             std::vector<Slvs_hConstraint>::iterator aRemoveIt = mySlvsConstraints.begin();
285             for (; aRemoveIt != mySlvsConstraints.end(); ++aRemoveIt)
286               if (*aRemoveIt == aFixed2) {
287                 mySlvsConstraints.erase(aRemoveIt);
288                 break;
289               }
290           }
291         } else {
292           bool isFixed[2] = {
293               myStorage->isPointFixed(*anIt1, aFixed1, true),
294               myStorage->isPointFixed(*anIt2, aFixed2, true)
295           };
296
297           Slvs_hEntity aPoint[2] = {*anIt1, *anIt2};
298           for (int i = 0; i < 2; i++)
299             if (!isFixed[i]) {
300               Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(),
301                   SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
302                   aPoint[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
303               aConstraint.h = myStorage->addConstraint(aConstraint);
304               mySlvsConstraints.push_back(aConstraint.h);
305             }
306         }
307       }
308     }
309   }
310 }