]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintRigid.cpp
Salome HOME
SketchSolver library refactoring
[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     if (isEmpty && !isForceUpdate) { // create new constraint
57       if (aConstrID != SLVS_E_UNKNOWN)
58         continue; // the coincident point is already fixed
59       aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
60           aValue, *anEntIter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
61       aConstraint.h = myStorage->addConstraint(aConstraint);
62       mySlvsConstraints.push_back(aConstraint.h);
63       if (!myBaseConstraint)
64         myStorage->addTemporaryConstraint(aConstraint.h);
65     } else { // update already existent constraint
66       if (aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
67         aConstrID = *aConstrIter;
68       aConstraint = myStorage->getConstraint(aConstrID);
69       aConstraint.ptA = *anEntIter;
70       myStorage->addConstraint(aConstraint);
71       if (!myBaseConstraint)
72         myStorage->addTemporaryConstraint(aConstraint.h);
73       if (!isEmpty) {
74         aConstrIter++;
75         isEmpty = aConstrIter == mySlvsConstraints.end();
76       }
77     }
78   }
79
80   if (!myFeatureMap.empty() && myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
81     // Fix radius of a circle
82     AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83         myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
84     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
85       aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
86     aConstraint.h = myStorage->addConstraint(aConstraint);
87     mySlvsConstraints.push_back(aConstraint.h);
88     if (!myBaseConstraint)
89       myStorage->addTemporaryConstraint(aConstraint.h);
90   }
91 }
92
93
94 void SketchSolver_ConstraintRigid::getAttributes(
95     double& theValue,
96     std::vector<Slvs_hEntity>& theAttributes)
97 {
98   theValue = 0.0;
99   int aType = SLVS_E_UNKNOWN; // type of created entity
100   Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
101   if (myBaseConstraint) {
102     // Get the attribute of constraint
103     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
104         myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
105     if (!aRefAttr || !aRefAttr->isInitialized()) {
106       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
107       return;
108     }
109     anEntityID = myGroup->getAttributeId(aRefAttr);
110     if (anEntityID == SLVS_E_UNKNOWN)
111       anEntityID = changeEntity(aRefAttr, aType);
112   } else {
113     anEntityID = myGroup->getFeatureId(myBaseFeature);
114     if (anEntityID == SLVS_E_UNKNOWN)
115       anEntityID = changeEntity(myBaseFeature, aType);
116   }
117
118   // Check the entity is complex
119   Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
120   if (anEntity.point[0] != SLVS_E_UNKNOWN) {
121     for (int i = 0; i < 4 && anEntity.point[i]; i++)
122       theAttributes.push_back(anEntity.point[i]);
123   } else // simple entity
124     theAttributes.push_back(anEntityID);
125 }
126
127 void SketchSolver_ConstraintRigid::adjustConstraint()
128 {
129   if (myFeatureMap.empty() || (
130       myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() && 
131       myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
132     return;
133   FeaturePtr aFeature = myFeatureMap.begin()->first;
134
135   // Search radius constraints and update them
136   Slvs_Constraint aConstraint;
137   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
138   for (; aCIter != mySlvsConstraints.end(); aCIter++) {
139     aConstraint = myStorage->getConstraint(*aCIter);
140     if (aConstraint.type != SLVS_C_DIAMETER)
141       continue;
142     double aRadius = 0.0;
143     if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
144       std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
145         aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
146       std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
147         aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
148       aRadius = aCenter->distance(aStart);
149     } else {
150       aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
151           aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
152     }
153
154     aConstraint.valA = aRadius * 2.0;
155     *aCIter = myStorage->updateConstraint(aConstraint);
156   }
157 }
158
159
160 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
161 {
162   cleanErrorMsg();
163   if (theConstraint && theConstraint != myBaseConstraint)
164     return false;
165   bool isFullyRemoved = true;
166   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
167   for (; aCIter != mySlvsConstraints.end(); aCIter++)
168     isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
169
170   if (isFullyRemoved) {
171     myFeatureMap.clear();
172     myAttributeMap.clear();
173     myValueMap.clear();
174   } else
175     cleanRemovedEntities();
176   return true;
177 }
178
179
180 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
181 {
182   Slvs_Constraint aConstraint;
183   Slvs_hConstraint aConstrID = myStorage->isPointFixed(theArc.point[0]);
184   int aPointsToFix = 2; // number of fixed points for the arc
185   if (aConstrID != SLVS_E_UNKNOWN)
186     aPointsToFix--;
187
188   // Radius of the arc
189   FeaturePtr aFeature = myFeatureMap.begin()->first;
190   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
191     aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
192   std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
193     aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
194   double aRadius = aCenter->distance(aStart);
195
196   // Update end point of the arc to be on a curve
197   std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198     aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
199   double aDistance = anEnd->distance(aCenter);
200   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
201   if (aDistance < tolerance)
202     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
203   else
204     aDir = aDir->multiplied(aRadius / aDistance);
205   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
206   Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
207   for (int i = 0; i < 2; i++) {
208     Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
209     aParam.val = xy[i];
210     myStorage->updateParameter(aParam);
211   }
212
213   for (int i = 1; aPointsToFix > 0; i++, aPointsToFix--) {
214     aConstrID = myStorage->isPointFixed(theArc.point[i]);
215     if (aConstrID != SLVS_E_UNKNOWN)
216       continue; // the coincident point is already fixed
217     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
218         0.0, theArc.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
219     aConstraint.h = myStorage->addConstraint(aConstraint);
220     mySlvsConstraints.push_back(aConstraint.h);
221     if (!myBaseConstraint)
222       myStorage->addTemporaryConstraint(aConstraint.h);
223   }
224
225   // Fix radius of the arc
226   aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
227     aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
228   aConstraint.h = myStorage->addConstraint(aConstraint);
229   mySlvsConstraints.push_back(aConstraint.h);
230   if (!myBaseConstraint)
231     myStorage->addTemporaryConstraint(aConstraint.h);
232 }
233