1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
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>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
18 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
19 : SketchSolver_Constraint(),
20 myBaseFeature(theFeature)
25 void SketchSolver_ConstraintRigid::process()
28 if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
29 /// TODO: Put error message here
32 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
33 update(myBaseConstraint);
36 std::vector<Slvs_hEntity> anEntities;
37 getAttributes(aValue, anEntities);
38 if (!myErrorMsg.empty() || (myFeatureMap.empty() && myAttributeMap.empty()))
42 if (!myFeatureMap.empty())
43 anEntID = myFeatureMap.begin()->second;
45 anEntID = myAttributeMap.begin()->second;
46 if (myStorage->isEntityFixed(anEntID, true)) {
47 myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
52 if (!myFeatureMap.empty())
53 aKind = myFeatureMap.begin()->first->getKind();
55 aKind = myAttributeMap.begin()->first->attributeType();
57 if (aKind == SketchPlugin_Line::ID()) {
58 Slvs_Entity aLine = myStorage->getEntity(anEntID);
61 else if (aKind == SketchPlugin_Arc::ID()) {
62 Slvs_Entity anArc = myStorage->getEntity(anEntID);
65 else if (aKind == SketchPlugin_Circle::ID()) {
66 Slvs_Entity aCirc = myStorage->getEntity(anEntID);
69 else if (aKind == SketchPlugin_Point::ID() || aKind == GeomDataAPI_Point2D::typeId()) {
75 void SketchSolver_ConstraintRigid::getAttributes(
77 std::vector<Slvs_hEntity>& theAttributes)
80 int aType = SLVS_E_UNKNOWN; // type of created entity
81 Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
82 if (myBaseConstraint) {
83 // Get the attribute of constraint
84 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
85 myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
86 if (!aRefAttr || !aRefAttr->isInitialized()) {
87 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
90 anEntityID = myGroup->getAttributeId(aRefAttr);
91 if (anEntityID == SLVS_E_UNKNOWN)
92 anEntityID = changeEntity(aRefAttr, aType);
94 anEntityID = myGroup->getFeatureId(myBaseFeature);
95 if (anEntityID == SLVS_E_UNKNOWN)
96 anEntityID = changeEntity(myBaseFeature, aType);
98 myFeatureMap[myBaseFeature] = anEntityID;
101 if (anEntityID == SLVS_E_UNKNOWN) {
102 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
106 // Check the entity is complex
107 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
108 if (anEntity.point[0] != SLVS_E_UNKNOWN) {
109 for (int i = 0; i < 4 && anEntity.point[i]; i++)
110 theAttributes.push_back(anEntity.point[i]);
111 } else // simple entity
112 theAttributes.push_back(anEntityID);
115 void SketchSolver_ConstraintRigid::adjustConstraint()
117 if (myFeatureMap.empty() || (
118 myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() &&
119 myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
121 FeaturePtr aFeature = myFeatureMap.begin()->first;
123 // Search radius constraints and update them
124 Slvs_Constraint aConstraint;
125 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
126 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
127 aConstraint = myStorage->getConstraint(*aCIter);
128 if (aConstraint.type != SLVS_C_DIAMETER)
130 double aRadius = 0.0;
131 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
132 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
134 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
135 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
136 aRadius = aCenter->distance(aStart);
138 aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
139 aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
142 aConstraint.valA = aRadius * 2.0;
143 *aCIter = myStorage->updateConstraint(aConstraint);
148 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
151 if (theConstraint && theConstraint != myBaseConstraint)
153 bool isFullyRemoved = true;
154 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
155 for (; aCIter != mySlvsConstraints.end(); aCIter++)
156 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
158 if (isFullyRemoved) {
159 myFeatureMap.clear();
160 myAttributeMap.clear();
163 cleanRemovedEntities();
167 void SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
169 if (thePointID == SLVS_E_UNKNOWN)
172 Slvs_Constraint aConstraint;
173 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
174 bool isFixed = myStorage->isPointFixed(thePointID, aConstrID, true);
175 bool isForceUpdate = (isFixed && !myBaseConstraint &&
176 myStorage->isTemporary(aConstrID));
177 if (!isForceUpdate) { // create new constraint
179 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
180 0.0, thePointID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
181 aConstraint.h = myStorage->addConstraint(aConstraint);
182 mySlvsConstraints.push_back(aConstraint.h);
183 if (!myBaseConstraint)
184 myStorage->addConstraintWhereDragged(aConstraint.h);
185 } else { // update already existent constraint
186 if (!isFixed || aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
188 aConstraint = myStorage->getConstraint(aConstrID);
189 aConstraint.ptA = thePointID;
190 myStorage->addConstraint(aConstraint);
191 if (!myBaseConstraint)
192 myStorage->addConstraintWhereDragged(aConstraint.h);
196 void SketchSolver_ConstraintRigid::fixLine(const Slvs_Entity& theLine)
198 Slvs_Constraint anEqual;
199 if (isAxisParallel(theLine)) {
200 // Fix one point and a line length
201 Slvs_hConstraint aFixed;
202 if (!myStorage->isPointFixed(theLine.point[0], aFixed, true) &&
203 !myStorage->isPointFixed(theLine.point[1], aFixed, true))
204 fixPoint(theLine.point[0]);
205 if (!isUsedInEqual(theLine, anEqual)) {
206 // Calculate distance between points on the line
208 for (int i = 0; i < 2; i++) {
209 Slvs_Entity aPnt = myStorage->getEntity(theLine.point[i]);
210 for (int j = 0; j < 2; j++) {
211 Slvs_Param aParam = myStorage->getParameter(aPnt.param[j]);
212 aCoords[2*i+j] = aParam.val;
215 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
216 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
218 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
219 SLVS_C_PT_PT_DISTANCE, myGroup->getWorkplaneId(), aLength,
220 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
221 aDistance.h = myStorage->addConstraint(aDistance);
222 mySlvsConstraints.push_back(aDistance.h);
226 else if (isUsedInEqual(theLine, anEqual)) {
227 // Check another entity of Equal is already fixed
228 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
229 if (myStorage->isEntityFixed(anOtherEntID, true)) {
230 // Fix start point of the line (if end point is not fixed yet) ...
231 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
232 bool isFixed = myStorage->isPointFixed(theLine.point[1], anEndFixedID, true);
233 if (isFixed == SLVS_E_UNKNOWN)
234 fixPoint(theLine.point[0]);
235 // ... and create fixed point lying on this line
236 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
237 // Firstly, search already fixed point on line
238 bool isPonLineFixed = false;
239 Slvs_hEntity aFixedPoint;
240 std::list<Slvs_Constraint> aPonLineList = myStorage->getConstraintsByType(SLVS_C_PT_ON_LINE);
241 std::list<Slvs_Constraint>::const_iterator aPLIter = aPonLineList.begin();
242 for (; aPLIter != aPonLineList.end() && !isPonLineFixed; aPLIter++)
243 if (aPLIter->entityA == theLine.h) {
244 isPonLineFixed = myStorage->isPointFixed(aPLIter->ptA, anEndFixedID);
245 aFixedPoint = aPLIter->ptA;
248 if (isPonLineFixed) { // update existent constraint
249 myStorage->copyEntity(aPointToCopy, aFixedPoint);
250 } else { // create new constraint
251 Slvs_hEntity aCopied = myStorage->copyEntity(aPointToCopy);
252 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_PT_ON_LINE,
253 myGroup->getWorkplaneId(), 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
254 aPonLine.h = myStorage->addConstraint(aPonLine);
255 mySlvsConstraints.push_back(aPonLine.h);
262 for (int i = 0; i < 2; i++)
263 fixPoint(theLine.point[i]);
266 void SketchSolver_ConstraintRigid::fixCircle(const Slvs_Entity& theCircle)
268 bool isFixRadius = true;
269 // Verify the arc is under Equal constraint
270 Slvs_Constraint anEqual;
271 if (isUsedInEqual(theCircle, anEqual)) {
272 // Check another entity of Equal is already fixed
273 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
274 if (myStorage->isEntityFixed(anOtherEntID, true))
278 fixPoint(theCircle.point[0]);
281 // Search the radius is already fixed
282 std::list<Slvs_Constraint> aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
283 std::list<Slvs_Constraint>::const_iterator aDiamIter = aDiamConstr.begin();
284 for (; aDiamIter != aDiamConstr.end(); aDiamIter++)
285 if (aDiamIter->entityA == theCircle.h)
288 // Fix radius of a circle
289 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
290 myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
291 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
292 myGroup->getWorkplaneId(), aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
293 myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
294 aFixedR.h = myStorage->addConstraint(aFixedR);
295 mySlvsConstraints.push_back(aFixedR.h);
299 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
301 bool isFixRadius = true;
302 std::list<Slvs_hEntity> aPointsToFix;
303 aPointsToFix.push_back(theArc.point[1]);
304 aPointsToFix.push_back(theArc.point[2]);
306 // Verify the arc is under Equal constraint
307 Slvs_Constraint anEqual;
308 if (isUsedInEqual(theArc, anEqual)) {
309 // Check another entity of Equal is already fixed
310 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
311 if (myStorage->isEntityFixed(anOtherEntID, true)) {
313 Slvs_Entity anOtherEntity = myStorage->getEntity(anOtherEntID);
314 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
315 aPointsToFix.pop_back();
316 aPointsToFix.push_back(theArc.point[0]);
321 Slvs_hConstraint aConstrID;
322 int aNbPointsToFix = 2; // number of fixed points for the arc
323 if (myStorage->isPointFixed(theArc.point[0], aConstrID, true))
327 FeaturePtr aFeature = myFeatureMap.begin()->first;
328 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
329 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
330 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
331 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
332 double aRadius = aCenter->distance(aStart);
334 // Update end point of the arc to be on a curve
335 std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
336 aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
337 double aDistance = anEnd->distance(aCenter);
338 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
339 if (aDistance < tolerance)
340 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
342 aDir = aDir->multiplied(aRadius / aDistance);
343 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
344 Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
345 for (int i = 0; i < 2; i++) {
346 Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
348 myStorage->updateParameter(aParam);
351 std::list<Slvs_hEntity>::iterator aPtIt = aPointsToFix.begin();
352 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
356 // Fix radius of the arc
357 bool isExists = false;
358 std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
359 std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
360 for (; anIt != aDiamConstraints.end() && !isExists; anIt++)
361 if (anIt->entityA == theArc.h)
364 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
365 myGroup->getWorkplaneId(), aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
366 myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
367 aFixedR.h = myStorage->addConstraint(aFixedR);
368 mySlvsConstraints.push_back(aFixedR.h);
369 if (!myBaseConstraint)
370 myStorage->addConstraintWhereDragged(aFixedR.h);
375 bool SketchSolver_ConstraintRigid::isUsedInEqual(
376 const Slvs_Entity& theEntity, Slvs_Constraint& theEqual) const
378 // Check the entity is used in Equal constraint
379 std::list<Slvs_Constraint> anEqualConstr = myStorage->getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
380 std::list<Slvs_Constraint> anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN);
381 anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
382 anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
383 anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
385 std::list<Slvs_Constraint>::const_iterator anEqIter = anEqualConstr.begin();
386 for (; anEqIter != anEqualConstr.end(); anEqIter++)
387 if (anEqIter->entityA == theEntity.h || anEqIter->entityB == theEntity.h) {
388 theEqual = *anEqIter;
394 bool SketchSolver_ConstraintRigid::isAxisParallel(const Slvs_Entity& theEntity) const
396 std::list<Slvs_Constraint> aConstr = myStorage->getConstraintsByType(SLVS_C_HORIZONTAL);
397 std::list<Slvs_Constraint> aVert = myStorage->getConstraintsByType(SLVS_C_VERTICAL);
398 aConstr.insert(aConstr.end(), aVert.begin(), aVert.end());
400 std::list<Slvs_Constraint>::const_iterator anIter = aConstr.begin();
401 for (; anIter != aConstr.end(); anIter++)
402 if (anIter->entityA == theEntity.h)