]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintRigid.cpp
Salome HOME
Fit incorrect movement of a line which boundary coincident to a point from another...
[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
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomAPI_XY.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <ModelAPI_AttributeDouble.h>
13
14 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
15   : SketchSolver_Constraint(),
16     myBaseFeature(theFeature)
17 {
18   process();
19 }
20
21 void SketchSolver_ConstraintRigid::process()
22 {
23   cleanErrorMsg();
24   if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
25     /// TODO: Put error message here
26     return;
27   }
28   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
29     update(myBaseConstraint);
30
31   double aValue;
32   std::vector<Slvs_hEntity> anEntities;
33   getAttributes(aValue, anEntities);
34   if (!myErrorMsg.empty())
35     return;
36
37   Slvs_Constraint aConstraint;
38   std::vector<Slvs_hConstraint>::iterator aConstrIter = mySlvsConstraints.begin();
39   bool isEmpty = aConstrIter == mySlvsConstraints.end();
40
41   // Check the fixed entity is an arc
42   if (isEmpty) {
43     if (!myFeatureMap.empty() && myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
44       Slvs_Entity anArc = myStorage->getEntity(myFeatureMap.begin()->second);
45       fixArc(anArc);
46       return;
47     }
48   }
49
50   std::vector<Slvs_hEntity>::const_iterator anEntIter = anEntities.begin();
51   for (; anEntIter != anEntities.end(); anEntIter++) {
52     if (*anEntIter == SLVS_E_UNKNOWN)
53       continue;
54     Slvs_hConstraint aConstrID = myStorage->isPointFixed(*anEntIter);
55     bool isForceUpdate = (aConstrID != SLVS_E_UNKNOWN && !myBaseConstraint &&
56                           myStorage->isTemporary(aConstrID));
57     if (isEmpty && !isForceUpdate) { // create new constraint
58       if (aConstrID != SLVS_E_UNKNOWN)
59         continue; // the coincident point is already fixed
60       aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
61           aValue, *anEntIter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
62       aConstraint.h = myStorage->addConstraint(aConstraint);
63       mySlvsConstraints.push_back(aConstraint.h);
64       if (!myBaseConstraint)
65         myStorage->addConstraintWhereDragged(aConstraint.h);
66     } else { // update already existent constraint
67       if (aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
68         aConstrID = *aConstrIter;
69       aConstraint = myStorage->getConstraint(aConstrID);
70       aConstraint.ptA = *anEntIter;
71       myStorage->addConstraint(aConstraint);
72       if (!myBaseConstraint)
73         myStorage->addConstraintWhereDragged(aConstraint.h);
74       if (!isEmpty) {
75         aConstrIter++;
76         isEmpty = aConstrIter == mySlvsConstraints.end();
77       }
78     }
79   }
80
81   if (!myFeatureMap.empty() && myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
82     // Fix radius of a circle
83     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
84         myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
85     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
86       aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
87     aConstraint.h = myStorage->addConstraint(aConstraint);
88     mySlvsConstraints.push_back(aConstraint.h);
89     if (!myBaseConstraint)
90       myStorage->addConstraintWhereDragged(aConstraint.h);
91   }
92 }
93
94
95 void SketchSolver_ConstraintRigid::getAttributes(
96     double& theValue,
97     std::vector<Slvs_hEntity>& theAttributes)
98 {
99   theValue = 0.0;
100   int aType = SLVS_E_UNKNOWN; // type of created entity
101   Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
102   if (myBaseConstraint) {
103     // Get the attribute of constraint
104     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
105         myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
106     if (!aRefAttr || !aRefAttr->isInitialized()) {
107       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
108       return;
109     }
110     anEntityID = myGroup->getAttributeId(aRefAttr);
111     if (anEntityID == SLVS_E_UNKNOWN)
112       anEntityID = changeEntity(aRefAttr, aType);
113   } else {
114     anEntityID = myGroup->getFeatureId(myBaseFeature);
115     if (anEntityID == SLVS_E_UNKNOWN)
116       anEntityID = changeEntity(myBaseFeature, aType);
117     else
118       myFeatureMap[myBaseFeature] = anEntityID;
119   }
120
121   if (anEntityID == SLVS_E_UNKNOWN) {
122     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
123     return;
124   }
125
126   // Check the entity is complex
127   Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
128   if (anEntity.point[0] != SLVS_E_UNKNOWN) {
129     for (int i = 0; i < 4 && anEntity.point[i]; i++)
130       theAttributes.push_back(anEntity.point[i]);
131   } else // simple entity
132     theAttributes.push_back(anEntityID);
133 }
134
135 void SketchSolver_ConstraintRigid::adjustConstraint()
136 {
137   if (myFeatureMap.empty() || (
138       myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() && 
139       myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
140     return;
141   FeaturePtr aFeature = myFeatureMap.begin()->first;
142
143   // Search radius constraints and update them
144   Slvs_Constraint aConstraint;
145   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
146   for (; aCIter != mySlvsConstraints.end(); aCIter++) {
147     aConstraint = myStorage->getConstraint(*aCIter);
148     if (aConstraint.type != SLVS_C_DIAMETER)
149       continue;
150     double aRadius = 0.0;
151     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
152       std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
153         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
154       std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
155         aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
156       aRadius = aCenter->distance(aStart);
157     } else {
158       aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
159           aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
160     }
161
162     aConstraint.valA = aRadius * 2.0;
163     *aCIter = myStorage->updateConstraint(aConstraint);
164   }
165 }
166
167
168 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
169 {
170   cleanErrorMsg();
171   if (theConstraint && theConstraint != myBaseConstraint)
172     return false;
173   bool isFullyRemoved = true;
174   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
175   for (; aCIter != mySlvsConstraints.end(); aCIter++)
176     isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
177
178   if (isFullyRemoved) {
179     myFeatureMap.clear();
180     myAttributeMap.clear();
181     myValueMap.clear();
182   } else
183     cleanRemovedEntities();
184   return true;
185 }
186
187
188 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
189 {
190   Slvs_Constraint aConstraint;
191   Slvs_hConstraint aConstrID = myStorage->isPointFixed(theArc.point[0]);
192   int aPointsToFix = 2; // number of fixed points for the arc
193   if (aConstrID != SLVS_E_UNKNOWN)
194     aPointsToFix--;
195
196   // Radius of the arc
197   FeaturePtr aFeature = myFeatureMap.begin()->first;
198   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199     aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
200   std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
201     aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
202   double aRadius = aCenter->distance(aStart);
203
204   // Update end point of the arc to be on a curve
205   std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206     aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
207   double aDistance = anEnd->distance(aCenter);
208   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
209   if (aDistance < tolerance)
210     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
211   else
212     aDir = aDir->multiplied(aRadius / aDistance);
213   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
214   Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
215   for (int i = 0; i < 2; i++) {
216     Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
217     aParam.val = xy[i];
218     myStorage->updateParameter(aParam);
219   }
220
221   for (int i = 1; aPointsToFix > 0; i++, aPointsToFix--) {
222     aConstrID = myStorage->isPointFixed(theArc.point[i]);
223     if (aConstrID != SLVS_E_UNKNOWN)
224       continue; // the coincident point is already fixed
225     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
226         0.0, theArc.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
227     aConstraint.h = myStorage->addConstraint(aConstraint);
228     mySlvsConstraints.push_back(aConstraint.h);
229     if (!myBaseConstraint)
230       myStorage->addConstraintWhereDragged(aConstraint.h);
231   }
232
233   // Fix radius of the arc
234   bool isExists = false;
235   std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
236   std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
237   for (; anIt != aDiamConstraints.end() && !isExists; anIt)
238     if (anIt->entityA == myFeatureMap.begin()->second)
239       isExists = true;
240   if (!isExists) {
241     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
242       aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
243     aConstraint.h = myStorage->addConstraint(aConstraint);
244     mySlvsConstraints.push_back(aConstraint.h);
245     if (!myBaseConstraint)
246       myStorage->addConstraintWhereDragged(aConstraint.h);
247   }
248 }
249