1 // File: SketchSolver_ConstraintManager.cpp
2 // Created: 08 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_ConstraintManager.h"
7 #include <Events_Loop.h>
8 #include <GeomDataAPI_Dir.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Data.h>
14 #include <Model_Events.h>
15 #include <SketchPlugin_Constraint.h>
16 #include <SketchPlugin_ConstraintCoincidence.h>
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Point.h>
19 #include <SketchPlugin_Sketch.h>
23 /// Tolerance for value of parameters
24 const double tolerance = 1.e-10;
26 // Initialization of constraint manager self pointer
27 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
29 /// Global constaint manager object
30 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
32 /// This value is used to give unique index to the groups
33 static Slvs_hGroup myGroupIndexer = 0;
35 /** \brief Search the entity/parameter with specified ID in the list of elements
36 * \param[in] theEntityID unique ID of the element
37 * \param[in] theEntities list of elements
38 * \return position of the found element or -1 if the element is not found
41 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
45 // ========================================================
46 // ========= SketchSolver_ConstraintManager ===============
47 // ========================================================
48 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
51 _self = new SketchSolver_ConstraintManager();
55 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
59 // Register in event loop
60 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
61 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
62 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
65 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
70 void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
72 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
73 theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED))
75 const Model_FeatureUpdatedMessage* aUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
77 // Only sketches and constraints can be added by Create event
78 boost::shared_ptr<SketchPlugin_Sketch> aSketch =
79 boost::dynamic_pointer_cast<SketchPlugin_Sketch>(aUpdateMsg->feature());
82 changeWorkplane(aSketch);
85 boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
86 boost::dynamic_pointer_cast<SketchPlugin_Constraint>(aUpdateMsg->feature());
89 changeConstraint(aConstraint);
91 // Solve the set of constraints
96 /// \todo Implement feature update handling
97 boost::shared_ptr<SketchPlugin_Feature> aFeature =
98 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
101 updateEntity(aFeature);
103 // Solve the set of constraints
104 ResolveConstraints();
107 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
109 const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
110 /// \todo Implement deleting objects on event
114 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch)
116 bool aResult = true; // changed when a workplane wrongly updated
117 bool isUpdated = false;
118 // Try to update specified workplane in all groups
119 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
120 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
121 if ((*aGroupIter)->isBaseWorkplane(theSketch))
124 if (!(*aGroupIter)->updateWorkplane())
127 // If the workplane is not updated, so this is a new workplane
130 SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
131 // Verify that the group is created successfully
132 if (!aNewGroup->isBaseWorkplane(theSketch))
137 myGroups.push_back(aNewGroup);
142 bool SketchSolver_ConstraintManager::changeConstraint(
143 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
145 // Search the groups which this constraint touchs
146 std::vector<Slvs_hGroup> aGroups;
147 findGroups(theConstraint, aGroups);
149 // Process the groups list
150 if (aGroups.size() == 0)
151 { // There are no groups applicable for this constraint => create new one
152 boost::shared_ptr<SketchPlugin_Sketch> aWP = findWorkplaneForConstraint(theConstraint);
153 if (!aWP) return false;
154 SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
155 if (!aGroup->changeConstraint(theConstraint))
160 myGroups.push_back(aGroup);
163 else if (aGroups.size() == 1)
164 { // Only one group => add constraint into it
165 Slvs_hGroup aGroupId = *(aGroups.begin());
166 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
167 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
168 if ((*aGroupIter)->getId() == aGroupId)
169 return (*aGroupIter)->changeConstraint(theConstraint);
171 else if (aGroups.size() > 1)
172 { // Several groups applicable for this constraint => need to merge them
173 /// \todo Implement merging of groups
176 // Something goes wrong
180 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
182 // Create list of attributes depending on type of the feature
183 std::vector<std::string> anAttrList;
185 boost::shared_ptr<SketchPlugin_Point> aPoint =
186 boost::dynamic_pointer_cast<SketchPlugin_Point>(theFeature);
188 anAttrList.push_back(POINT_ATTR_COORD);
190 boost::shared_ptr<SketchPlugin_Line> aLine =
191 boost::dynamic_pointer_cast<SketchPlugin_Line>(theFeature);
194 anAttrList.push_back(LINE_ATTR_START);
195 anAttrList.push_back(LINE_ATTR_END);
197 /// \todo Other types of features should be implemented
199 // Check changing of feature's attributes (go through the groups and search usage of the attributes)
200 std::vector<std::string>::const_iterator anAttrIter;
201 for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
203 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
204 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
206 boost::shared_ptr<ModelAPI_Attribute> anAttribute =
207 boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
208 (*aGroupIter)->updateEntityIfPossible(anAttribute);
214 void SketchSolver_ConstraintManager::findGroups(
215 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
216 std::vector<Slvs_hGroup>& theGroupIDs) const
218 boost::shared_ptr<SketchPlugin_Sketch> aWP = findWorkplaneForConstraint(theConstraint);
220 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
221 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
222 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
223 theGroupIDs.push_back((*aGroupIter)->getId());
226 boost::shared_ptr<SketchPlugin_Sketch> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
227 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
229 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
230 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
232 boost::shared_ptr<SketchPlugin_Sketch> aWP = (*aGroupIter)->getWorkplane();
233 boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
234 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
235 std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
236 std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
237 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
238 if (*anIter == theConstraint)
239 return aWP; // workplane is found
242 return boost::shared_ptr<SketchPlugin_Sketch>();
245 void SketchSolver_ConstraintManager::ResolveConstraints()
247 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
248 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
249 (*aGroupIter)->ResolveConstraints();
254 // ========================================================
255 // ========= SketchSolver_ConstraintGroup ===============
256 // ========================================================
258 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
259 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane)
260 : myID(++myGroupIndexer),
265 myNeedToSolve(false),
270 myConstraints.clear();
272 // Initialize workplane
273 myWorkplane.h = SLVS_E_UNKNOWN;
274 addWorkplane(theWorkplane);
277 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
281 myConstraints.clear();
282 myConstraintMap.clear();
284 // If the group with maximal identifier is deleted, decrease the indexer
285 if (myID == myGroupIndexer)
289 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
290 boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const
292 return theWorkplane == mySketch;
295 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
296 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
298 // Check the group is empty
299 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
302 /// \todo Should be implemented
306 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
307 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
309 // There is no workplane yet, something wrong
310 if (myWorkplane.h == SLVS_E_UNKNOWN)
313 // Search this constraint in the current group to update it
314 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
315 aConstrMapIter = myConstraintMap.find(theConstraint);
316 std::vector<Slvs_Constraint>::iterator aConstrIter;
317 if (aConstrMapIter != myConstraintMap.end())
319 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
320 aConstrIter = myConstraints.begin() + aConstrPos;
323 // Get constraint type and verify the constraint parameters are correct
324 int aConstrType = getConstraintType(theConstraint);
325 if (aConstrType == SLVS_C_UNKNOWN ||
326 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
329 // Create constraint parameters
330 double aDistance = 0.0; // scalar value of the constraint
331 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
332 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
335 aDistance = aDistAttr->value();
336 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
338 myNeedToSolve = true;
339 aConstrIter->valA = aDistance;
343 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
344 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
346 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
347 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
348 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
349 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
351 if (!aConstrAttr) continue;
352 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
355 if (aConstrMapIter == myConstraintMap.end())
357 // Create SolveSpace constraint structure
358 Slvs_Constraint aConstraint =
359 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
360 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
361 myConstraints.push_back(aConstraint);
362 myConstraintMap[theConstraint] = aConstraint.h;
367 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
368 boost::shared_ptr<ModelAPI_Attribute> theEntity)
370 // If the entity is already in the group, try to find it
371 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
372 aEntIter = myEntityMap.find(theEntity);
373 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
374 if (aEntIter == myEntityMap.end()) // no such entity => should be created
375 aParamIter = myParams.end();
377 { // the entity already exists
378 int aEntPos = Search(aEntIter->second, myEntities);
379 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
380 aParamIter = myParams.begin() + aParamPos;
383 // Look over supported types of entities
386 boost::shared_ptr<GeomDataAPI_Point> aPoint =
387 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
390 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
391 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
392 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
394 if (aEntIter != myEntityMap.end()) // the entity already exists
395 return aEntIter->second;
398 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
399 myEntities.push_back(aPtEntity);
400 myEntityMap[theEntity] = aPtEntity.h;
405 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
406 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
409 // The 2D points are created on workplane. So, if there is no workplane yet, then error
410 if (myWorkplane.h == SLVS_E_UNKNOWN)
411 return SLVS_E_UNKNOWN;
412 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
413 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
415 if (aEntIter != myEntityMap.end()) // the entity already exists
416 return aEntIter->second;
419 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
420 myEntities.push_back(aPt2DEntity);
421 myEntityMap[theEntity] = aPt2DEntity.h;
422 return aPt2DEntity.h;
425 /// \todo Other types of entities
427 // Unsupported or wrong entity type
428 return SLVS_E_UNKNOWN;
431 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
432 boost::shared_ptr<ModelAPI_Attribute> theDirX,
433 boost::shared_ptr<ModelAPI_Attribute> theDirY,
434 boost::shared_ptr<ModelAPI_Attribute> theNorm)
436 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
437 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
438 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
439 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
440 if (!aDirX || !aDirY ||
441 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
442 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
443 return SLVS_E_UNKNOWN;
445 // quaternion parameters of normal vector
446 double qw, qx, qy, qz;
447 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
448 aDirY->x(), aDirY->y(), aDirY->z(),
450 double aNormCoord[4] = {qw, qx, qy, qz};
452 // Try to find existent normal
453 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
454 aEntIter = myEntityMap.find(theNorm);
455 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
456 if (aEntIter == myEntityMap.end()) // no such entity => should be created
457 aParamIter = myParams.end();
459 { // the entity already exists, update it
460 int aEntPos = Search(aEntIter->second, myEntities);
461 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
462 aParamIter = myParams.begin() + aParamPos;
465 // Change parameters of the normal
466 Slvs_hParam aNormParams[4];
467 for (int i = 0; i < 4; i++)
468 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
470 if (aEntIter != myEntityMap.end()) // the entity already exists
471 return aEntIter->second;
474 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
475 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
476 myEntities.push_back(aNormal);
477 myEntityMap[theNorm] = aNormal.h;
482 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
483 boost::shared_ptr<SketchPlugin_Sketch> theSketch)
486 return false; // the workplane already exists
488 mySketch = theSketch;
489 return updateWorkplane();
492 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
494 // Get parameters of workplane
495 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
496 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
497 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
498 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
499 // Transform them into SolveSpace format
500 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
501 if (!aNormalWP) return false;
502 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
503 if (!anOriginWP) return false;
508 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
509 // Workplane should be added to the list of entities
510 myEntities.push_back(myWorkplane);
516 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
517 const double& theParam,
518 std::vector<Slvs_Param>::const_iterator& thePrmIter)
520 if (thePrmIter != myParams.end())
521 { // Parameter should be updated
522 int aParamPos = thePrmIter - myParams.begin();
523 if (fabs(thePrmIter->val - theParam) > tolerance)
525 myNeedToSolve = true; // parameter is changed, need to resolve constraints
526 myParams[aParamPos].val = theParam;
529 return myParams[aParamPos].h;
532 // Newly created parameter
533 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
534 myParams.push_back(aParam);
535 myNeedToSolve = true;
536 // The list of parameters is changed, move iterator to the end of the list to avoid problems
537 thePrmIter = myParams.end();
541 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
542 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
544 // Constraint for coincidence of two points
545 boost::shared_ptr<SketchPlugin_ConstraintCoincidence> aPtEquiv =
546 boost::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidence>(theConstraint);
549 // Verify the constraint has only two attributes and they are points
550 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
551 int aPt3d = 0; // bit-mapped field, the same information for 3D points
552 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
554 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
555 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
556 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
558 if (!anAttr) continue;
559 // Verify the attribute is a 2D point
560 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
561 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
564 aPt2d |= (1 << indAttr);
567 // Verify the attribute is a 3D point
568 boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
569 boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
572 aPt3d |= (1 << indAttr);
575 // Attribute neither 2D nor 3D point is not supported by this type of constraint
576 return SLVS_C_UNKNOWN;
578 // The constrained points should be in first and second positions,
579 // so the expected value of aPt2d or aPt3d is 3
580 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
581 return SLVS_C_POINTS_COINCIDENT;
582 // Constraint parameters are wrong
583 return SLVS_C_UNKNOWN;
586 /// \todo Implement other kind of constrtaints
588 return SLVS_C_UNKNOWN;
591 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::ResolveConstraints()
596 myConstrSolver.setGroupID(myID);
597 myConstrSolver.setParameters(myParams);
598 myConstrSolver.setEntities(myEntities);
599 myConstrSolver.setConstraints(myConstraints);
601 if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
602 { // solution succeeded, store results into correspondent attributes
603 // Obtain result into the same list of parameters
604 if (!myConstrSolver.getResult(myParams))
607 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
608 anEntIter = myEntityMap.begin();
609 for ( ; anEntIter != myEntityMap.end(); anEntIter++)
610 updateAttribute(anEntIter->first, anEntIter->second);
612 /// \todo Implement error handling
614 removeTemporaryConstraints();
615 myNeedToSolve = false;
619 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
620 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
621 const Slvs_hEntity& theEntityID)
623 // Search the position of the first parameter of the entity
624 int anEntPos = Search(theEntityID, myEntities);
625 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
627 // Look over supported types of entities
630 boost::shared_ptr<GeomDataAPI_Point> aPoint =
631 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
634 aPoint->setValue(myParams[aFirstParamPos].val,
635 myParams[aFirstParamPos+1].val,
636 myParams[aFirstParamPos+2].val);
641 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
642 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
645 aPoint2D->setValue(myParams[aFirstParamPos].val,
646 myParams[aFirstParamPos+1].val);
650 /// \todo Support other types of entities
653 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
654 boost::shared_ptr<ModelAPI_Attribute> theEntity)
656 if (myEntityMap.find(theEntity) != myEntityMap.end())
658 // If the attribute is a point and it is changed (the group needs to rebuild),
659 // probably user has dragged this point into this position,
660 // so it is necessary to add constraint which will quarantee the point will not change
662 // Store myNeedToSolve flag to verify the entity is really changed
663 bool aNeedToSolveCopy = myNeedToSolve;
664 myNeedToSolve = false;
666 changeEntity(theEntity);
668 if (myNeedToSolve) // the entity is changed
670 // Verify the entity is a point and add temporary constraint of permanency
671 boost::shared_ptr<GeomDataAPI_Point> aPoint =
672 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
673 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
674 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
675 if (aPoint || aPoint2D)
676 addTemporaryConstraintWhereDragged(theEntity);
679 // Restore flag of changes
680 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
684 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
685 boost::shared_ptr<ModelAPI_Attribute> theEntity)
687 // Find identifier of the entity
688 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
689 anEntIter = myEntityMap.find(theEntity);
691 // Create WHERE_DRAGGED constraint
692 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
693 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
694 myConstraints.push_back(aWDConstr);
695 myTempConstraints.push_back(aWDConstr.h);
698 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
700 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
701 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
703 int aConstrPos = Search(*aTmpConstrIter, myConstraints);
704 myConstraints.erase(myConstraints.begin() + aConstrPos);
706 // If the removing constraint has higher index, decrease the indexer
707 if (*aTmpConstrIter == myConstrMaxID)
710 myTempConstraints.clear();
715 // ========================================================
716 // ========= Auxiliary functions ===============
717 // ========================================================
719 template <typename T>
720 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
722 std::vector<T>::const_iterator aEntIter = theEntities.begin() + theEntityID - 1;
723 while (aEntIter != theEntities.end() && aEntIter->h > theEntityID)
725 while (aEntIter != theEntities.end() && aEntIter->h < theEntityID)
727 if (aEntIter == theEntities.end())
729 return aEntIter - theEntities.begin();