]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintRigid.cpp
Salome HOME
d13285362b5ced69a2ee013d56a4f4c512f85d98
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintRigid.cpp
1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_Circle.h>
7 #include <SketchPlugin_ConstraintRigid.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
15
16 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
17   : SketchSolver_Constraint(),
18     myBaseFeature(theFeature)
19 {
20   process();
21 }
22
23 void SketchSolver_ConstraintRigid::process()
24 {
25   cleanErrorMsg();
26   if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
27     /// TODO: Put error message here
28     return;
29   }
30   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
31     update(myBaseConstraint);
32
33   double aValue;
34   std::vector<Slvs_hEntity> anEntities;
35   getAttributes(aValue, anEntities);
36   if (!myErrorMsg.empty() || myFeatureMap.empty())
37     return;
38
39   Slvs_hEntity anEntID = myFeatureMap.begin()->second;
40   if (myStorage->isEntityFixed(anEntID, true)) {
41     myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
42     return;
43   }
44
45   if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Line::ID()) {
46     Slvs_Entity aLine = myStorage->getEntity(anEntID);
47     fixLine(aLine);
48   }
49   else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
50     Slvs_Entity anArc = myStorage->getEntity(anEntID);
51     fixArc(anArc);
52   }
53   else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
54     Slvs_Entity aCirc = myStorage->getEntity(anEntID);
55     fixCircle(aCirc);
56   }
57   else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Point::ID()) {
58     fixPoint(anEntID);
59   }
60 }
61
62
63 void SketchSolver_ConstraintRigid::getAttributes(
64     double& theValue,
65     std::vector<Slvs_hEntity>& theAttributes)
66 {
67   theValue = 0.0;
68   int aType = SLVS_E_UNKNOWN; // type of created entity
69   Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
70   if (myBaseConstraint) {
71     // Get the attribute of constraint
72     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
73         myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
74     if (!aRefAttr || !aRefAttr->isInitialized()) {
75       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
76       return;
77     }
78     anEntityID = myGroup->getAttributeId(aRefAttr);
79     if (anEntityID == SLVS_E_UNKNOWN)
80       anEntityID = changeEntity(aRefAttr, aType);
81   } else {
82     anEntityID = myGroup->getFeatureId(myBaseFeature);
83     if (anEntityID == SLVS_E_UNKNOWN)
84       anEntityID = changeEntity(myBaseFeature, aType);
85     else
86       myFeatureMap[myBaseFeature] = anEntityID;
87   }
88
89   if (anEntityID == SLVS_E_UNKNOWN) {
90     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
91     return;
92   }
93
94   // Check the entity is complex
95   Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
96   if (anEntity.point[0] != SLVS_E_UNKNOWN) {
97     for (int i = 0; i < 4 && anEntity.point[i]; i++)
98       theAttributes.push_back(anEntity.point[i]);
99   } else // simple entity
100     theAttributes.push_back(anEntityID);
101 }
102
103 void SketchSolver_ConstraintRigid::adjustConstraint()
104 {
105   if (myFeatureMap.empty() || (
106       myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() && 
107       myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
108     return;
109   FeaturePtr aFeature = myFeatureMap.begin()->first;
110
111   // Search radius constraints and update them
112   Slvs_Constraint aConstraint;
113   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
114   for (; aCIter != mySlvsConstraints.end(); aCIter++) {
115     aConstraint = myStorage->getConstraint(*aCIter);
116     if (aConstraint.type != SLVS_C_DIAMETER)
117       continue;
118     double aRadius = 0.0;
119     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
120       std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
122       std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123         aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
124       aRadius = aCenter->distance(aStart);
125     } else {
126       aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
127           aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
128     }
129
130     aConstraint.valA = aRadius * 2.0;
131     *aCIter = myStorage->updateConstraint(aConstraint);
132   }
133 }
134
135
136 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
137 {
138   cleanErrorMsg();
139   if (theConstraint && theConstraint != myBaseConstraint)
140     return false;
141   bool isFullyRemoved = true;
142   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
143   for (; aCIter != mySlvsConstraints.end(); aCIter++)
144     isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
145
146   if (isFullyRemoved) {
147     myFeatureMap.clear();
148     myAttributeMap.clear();
149     myValueMap.clear();
150   } else
151     cleanRemovedEntities();
152   return true;
153 }
154
155 void SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
156 {
157   if (thePointID == SLVS_E_UNKNOWN)
158     return;
159
160   Slvs_Constraint aConstraint;
161   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
162   bool isFixed = myStorage->isPointFixed(thePointID, aConstrID, true);
163   bool isForceUpdate = (isFixed && !myBaseConstraint &&
164                         myStorage->isTemporary(aConstrID));
165   if (!isForceUpdate) { // create new constraint
166     if (isFixed) return;
167     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
168         0.0, thePointID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
169     aConstraint.h = myStorage->addConstraint(aConstraint);
170     mySlvsConstraints.push_back(aConstraint.h);
171     if (!myBaseConstraint)
172       myStorage->addConstraintWhereDragged(aConstraint.h);
173   } else { // update already existent constraint
174     if (!isFixed || aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
175       return;
176     aConstraint = myStorage->getConstraint(aConstrID);
177     aConstraint.ptA = thePointID;
178     myStorage->addConstraint(aConstraint);
179     if (!myBaseConstraint)
180       myStorage->addConstraintWhereDragged(aConstraint.h);
181   }
182 }
183
184 void SketchSolver_ConstraintRigid::fixLine(const Slvs_Entity& theLine)
185 {
186   Slvs_Constraint anEqual;
187   if (isUsedInEqual(theLine, anEqual)) {
188     // Check another entity of Equal is already fixed
189     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
190     if (myStorage->isEntityFixed(anOtherEntID, true)) {
191       // Fix start point of the line (if end point is not fixed yet) ...
192       Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
193       bool isFixed = myStorage->isPointFixed(theLine.point[1], anEndFixedID, true);
194       if (isFixed == SLVS_E_UNKNOWN)
195         fixPoint(theLine.point[0]);
196       // ...  and create fixed point lying on this line
197       Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
198       // Firstly, search already fixed point on line
199       bool isPonLineFixed = false;
200       Slvs_hEntity aFixedPoint;
201       std::list<Slvs_Constraint> aPonLineList = myStorage->getConstraintsByType(SLVS_C_PT_ON_LINE);
202       std::list<Slvs_Constraint>::const_iterator aPLIter = aPonLineList.begin();
203       for (; aPLIter != aPonLineList.end() && !isPonLineFixed; aPLIter++)
204         if (aPLIter->entityA == theLine.h) {
205           isPonLineFixed = myStorage->isPointFixed(aPLIter->ptA, anEndFixedID);
206           aFixedPoint = aPLIter->ptA;
207         }
208
209       if (isPonLineFixed) { // update existent constraint
210         myStorage->copyEntity(aPointToCopy, aFixedPoint);
211       } else { // create new constraint
212         Slvs_hEntity aCopied = myStorage->copyEntity(aPointToCopy);
213         Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_PT_ON_LINE,
214             myGroup->getWorkplaneId(), 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
215         aPonLine.h = myStorage->addConstraint(aPonLine);
216         mySlvsConstraints.push_back(aPonLine.h);
217         fixPoint(aCopied);
218       }
219       return;
220     }
221   }
222
223   for (int i = 0; i < 2; i++)
224     fixPoint(theLine.point[i]);
225 }
226
227 void SketchSolver_ConstraintRigid::fixCircle(const Slvs_Entity& theCircle)
228 {
229   bool isFixRadius = true;
230   // Verify the arc is under Equal constraint
231   Slvs_Constraint anEqual;
232   if (isUsedInEqual(theCircle, anEqual)) {
233     // Check another entity of Equal is already fixed
234     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
235     if (myStorage->isEntityFixed(anOtherEntID, true))
236       isFixRadius = false;
237   }
238
239   fixPoint(theCircle.point[0]);
240
241   if (isFixRadius) {
242     // Search the radius is already fixed
243     std::list<Slvs_Constraint> aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
244     std::list<Slvs_Constraint>::const_iterator aDiamIter = aDiamConstr.begin();
245     for (; aDiamIter != aDiamConstr.end(); aDiamIter++)
246       if (aDiamIter->entityA == theCircle.h)
247         return;
248
249     // Fix radius of a circle
250     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
251         myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
252     Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
253         myGroup->getWorkplaneId(), aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
254         myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
255     aFixedR.h = myStorage->addConstraint(aFixedR);
256     mySlvsConstraints.push_back(aFixedR.h);
257   }
258 }
259
260 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
261 {
262   bool isFixRadius = true;
263   std::list<Slvs_hEntity> aPointsToFix;
264   aPointsToFix.push_back(theArc.point[1]);
265   aPointsToFix.push_back(theArc.point[2]);
266
267   // Verify the arc is under Equal constraint
268   Slvs_Constraint anEqual;
269   if (isUsedInEqual(theArc, anEqual)) {
270     // Check another entity of Equal is already fixed
271     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
272     if (myStorage->isEntityFixed(anOtherEntID, true)) {
273       isFixRadius = false;
274       Slvs_Entity anOtherEntity = myStorage->getEntity(anOtherEntID);
275       if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
276         aPointsToFix.pop_back();
277         aPointsToFix.push_back(theArc.point[0]);
278       }
279     }
280   }
281
282   Slvs_hConstraint aConstrID;
283   int aNbPointsToFix = 2; // number of fixed points for the arc
284   if (myStorage->isPointFixed(theArc.point[0], aConstrID, true))
285     aNbPointsToFix--;
286
287   // Radius of the arc
288   FeaturePtr aFeature = myFeatureMap.begin()->first;
289   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
290     aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
291   std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
292     aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
293   double aRadius = aCenter->distance(aStart);
294
295   // Update end point of the arc to be on a curve
296   std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
297     aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
298   double aDistance = anEnd->distance(aCenter);
299   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
300   if (aDistance < tolerance)
301     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
302   else
303     aDir = aDir->multiplied(aRadius / aDistance);
304   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
305   Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
306   for (int i = 0; i < 2; i++) {
307     Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
308     aParam.val = xy[i];
309     myStorage->updateParameter(aParam);
310   }
311
312   std::list<Slvs_hEntity>::iterator aPtIt = aPointsToFix.begin();
313   for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
314     fixPoint(*aPtIt);
315
316   if (isFixRadius) {
317     // Fix radius of the arc
318     bool isExists = false;
319     std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
320     std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
321     for (; anIt != aDiamConstraints.end() && !isExists; anIt++)
322       if (anIt->entityA == theArc.h)
323         isExists = true;
324     if (!isExists) {
325       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
326           myGroup->getWorkplaneId(), aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
327           myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
328       aFixedR.h = myStorage->addConstraint(aFixedR);
329       mySlvsConstraints.push_back(aFixedR.h);
330       if (!myBaseConstraint)
331         myStorage->addConstraintWhereDragged(aFixedR.h);
332     }
333   }
334 }
335
336 bool SketchSolver_ConstraintRigid::isUsedInEqual(
337     const Slvs_Entity& theEntity, Slvs_Constraint& theEqual) const
338 {
339   // Check the entity is used in Equal constraint
340   std::list<Slvs_Constraint> anEqualConstr = myStorage->getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
341   std::list<Slvs_Constraint> anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN);
342   anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
343   anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
344   anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
345
346   std::list<Slvs_Constraint>::const_iterator anEqIter = anEqualConstr.begin();
347   for (; anEqIter != anEqualConstr.end(); anEqIter++)
348     if (anEqIter->entityA == theEntity.h || anEqIter->entityB == theEntity.h) {
349       theEqual = *anEqIter;
350       return true;
351     }
352   return false;
353 }
354