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>
16 #include <SketchPlugin_Constraint.h>
17 #include <SketchPlugin_ConstraintCoincidence.h>
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Point.h>
20 #include <SketchPlugin_Sketch.h>
27 /// Tolerance for value of parameters
28 const double tolerance = 1.e-10;
30 // Initialization of constraint manager self pointer
31 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
33 /// Global constraint manager object
34 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
36 /// This value is used to give unique index to the groups
37 static Slvs_hGroup myGroupIndexer = 0;
39 /** \brief Search the entity/parameter with specified ID in the list of elements
40 * \param[in] theEntityID unique ID of the element
41 * \param[in] theEntities list of elements
42 * \return position of the found element or -1 if the element is not found
45 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
49 // ========================================================
50 // ========= SketchSolver_ConstraintManager ===============
51 // ========================================================
52 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
55 _self = new SketchSolver_ConstraintManager();
59 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
63 // Register in event loop
64 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
65 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
66 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
67 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_MOVED));
70 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
75 void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
77 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
78 theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED) ||
79 theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED))
81 const Model_FeatureUpdatedMessage* anUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
82 std::set< boost::shared_ptr<ModelAPI_Feature> > aFeatures = anUpdateMsg->features();
84 bool isModifiedEvt = theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED);
87 std::set< boost::shared_ptr<ModelAPI_Feature> >::iterator aFeatIter;
88 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++)
90 // Only sketches and constraints can be added by Create event
91 const std::string& aFeatureKind = (*aFeatIter)->getKind();
92 if (aFeatureKind.compare("Sketch") == 0)
94 boost::shared_ptr<SketchPlugin_Feature> aSketch =
95 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
97 changeWorkplane(aSketch);
100 boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
101 boost::dynamic_pointer_cast<SketchPlugin_Constraint>(*aFeatIter);
103 changeConstraint(aConstraint);
106 // Sketch plugin features can be only updated
107 boost::shared_ptr<SketchPlugin_Feature> aFeature =
108 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
110 updateEntity(aFeature);
115 // Solve the set of constraints
116 resolveConstraints();
118 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
120 const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
121 const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
123 // Find "Sketch" in groups. The constraint groups should be updated when an object removed from Sketch
124 std::set<std::string>::const_iterator aFGrIter;
125 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
126 if (aFGrIter->compare("Sketch") == 0)
129 if (aFGrIter != aFeatureGroups.end())
131 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
132 while (aGroupIter != myGroups.end())
134 if ((*aGroupIter)->updateGroup())
135 { // the group should be removed
137 int aShift = aGroupIter - myGroups.begin();
138 myGroups.erase(aGroupIter);
139 aGroupIter = myGroups.begin() + aShift;
147 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
149 bool aResult = true; // changed when a workplane wrongly updated
150 bool isUpdated = false;
151 // Try to update specified workplane in all groups
152 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
153 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
154 if ((*aGroupIter)->isBaseWorkplane(theSketch))
157 if (!(*aGroupIter)->updateWorkplane())
160 // If the workplane is not updated, so this is a new workplane
163 SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
164 // Verify that the group is created successfully
165 if (!aNewGroup->isBaseWorkplane(theSketch))
170 myGroups.push_back(aNewGroup);
175 bool SketchSolver_ConstraintManager::changeConstraint(
176 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
178 // Search the groups which this constraint touchs
179 std::vector<Slvs_hGroup> aGroups;
180 findGroups(theConstraint, aGroups);
182 // Process the groups list
183 if (aGroups.size() == 0)
184 { // There are no groups applicable for this constraint => create new one
185 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
186 if (!aWP) return false;
187 SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
188 if (!aGroup->changeConstraint(theConstraint))
193 myGroups.push_back(aGroup);
196 else if (aGroups.size() == 1)
197 { // Only one group => add constraint into it
198 Slvs_hGroup aGroupId = *(aGroups.begin());
199 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
200 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
201 if ((*aGroupIter)->getId() == aGroupId)
202 return (*aGroupIter)->changeConstraint(theConstraint);
204 else if (aGroups.size() > 1)
205 { // Several groups applicable for this constraint => need to merge them
206 /// \todo Implement merging of groups
209 // Something goes wrong
213 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
215 // Create list of attributes depending on type of the feature
216 std::vector<std::string> anAttrList;
217 const std::string& aFeatureKind = theFeature->getKind();
219 if (aFeatureKind.compare("SketchPoint") == 0)
220 anAttrList.push_back(POINT_ATTR_COORD);
222 else if (aFeatureKind.compare("SketchLine") == 0)
224 anAttrList.push_back(LINE_ATTR_START);
225 anAttrList.push_back(LINE_ATTR_END);
227 /// \todo Other types of features should be implemented
229 // Check changing of feature's attributes (go through the groups and search usage of the attributes)
230 std::vector<std::string>::const_iterator anAttrIter;
231 for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
233 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
234 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
236 boost::shared_ptr<ModelAPI_Attribute> anAttribute =
237 boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
238 (*aGroupIter)->updateEntityIfPossible(anAttribute);
244 void SketchSolver_ConstraintManager::findGroups(
245 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
246 std::vector<Slvs_hGroup>& theGroupIDs) const
248 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
250 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
251 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
252 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
253 theGroupIDs.push_back((*aGroupIter)->getId());
256 boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
257 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
259 // Already verified workplanes
260 std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
262 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
263 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
265 boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
266 if (aVerified.find(aWP) != aVerified.end())
269 boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
270 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
271 std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
272 std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
273 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
274 if (*anIter == theConstraint)
275 return aWP; // workplane is found
276 aVerified.insert(aWP);
279 return boost::shared_ptr<SketchPlugin_Feature>();
282 void SketchSolver_ConstraintManager::resolveConstraints()
284 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
285 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
286 (*aGroupIter)->resolveConstraints();
288 // Features may be updated => send events
289 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
294 // ========================================================
295 // ========= SketchSolver_ConstraintGroup ===============
296 // ========================================================
298 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
299 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
300 : myID(++myGroupIndexer),
305 myNeedToSolve(false),
310 myConstraints.clear();
312 // Initialize workplane
313 myWorkplane.h = SLVS_E_UNKNOWN;
315 assert(addWorkplane(theWorkplane));
317 addWorkplane(theWorkplane);
321 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
325 myConstraints.clear();
326 myConstraintMap.clear();
328 // If the group with maximal identifier is deleted, decrease the indexer
329 if (myID == myGroupIndexer)
333 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
334 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
336 return theWorkplane == mySketch;
339 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
340 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
342 // Check the group is empty
343 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
346 // Go through constraint entities and verify if some of them already in the group
347 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
349 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
350 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
351 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
353 if (!aCAttrRef) continue;
354 if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
358 // Entities did not found
362 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
363 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
365 // There is no workplane yet, something wrong
366 if (myWorkplane.h == SLVS_E_UNKNOWN)
369 // Search this constraint in the current group to update it
370 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
371 aConstrMapIter = myConstraintMap.find(theConstraint);
372 std::vector<Slvs_Constraint>::iterator aConstrIter;
373 if (aConstrMapIter != myConstraintMap.end())
375 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
376 aConstrIter = myConstraints.begin() + aConstrPos;
379 // Get constraint type and verify the constraint parameters are correct
380 int aConstrType = getConstraintType(theConstraint);
381 if (aConstrType == SLVS_C_UNKNOWN ||
382 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
385 // Create constraint parameters
386 double aDistance = 0.0; // scalar value of the constraint
387 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
388 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
391 aDistance = aDistAttr->value();
392 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
394 myNeedToSolve = true;
395 aConstrIter->valA = aDistance;
399 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
400 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
402 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
403 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
404 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
405 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
407 if (!aConstrAttr) continue;
408 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
411 if (aConstrMapIter == myConstraintMap.end())
413 // Create SolveSpace constraint structure
414 Slvs_Constraint aConstraint =
415 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
416 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
417 myConstraints.push_back(aConstraint);
418 myConstraintMap[theConstraint] = aConstraint.h;
423 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
424 boost::shared_ptr<ModelAPI_Attribute> theEntity)
426 // If the entity is already in the group, try to find it
427 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
428 aEntIter = myEntityMap.find(theEntity);
429 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
430 if (aEntIter == myEntityMap.end()) // no such entity => should be created
431 aParamIter = myParams.end();
433 { // the entity already exists
434 int aEntPos = Search(aEntIter->second, myEntities);
435 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
436 aParamIter = myParams.begin() + aParamPos;
439 // Look over supported types of entities
442 boost::shared_ptr<GeomDataAPI_Point> aPoint =
443 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
446 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
447 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
448 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
450 if (aEntIter != myEntityMap.end()) // the entity already exists
451 return aEntIter->second;
454 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
455 myEntities.push_back(aPtEntity);
456 myEntityMap[theEntity] = aPtEntity.h;
461 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
462 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
465 // The 2D points are created on workplane. So, if there is no workplane yet, then error
466 if (myWorkplane.h == SLVS_E_UNKNOWN)
467 return SLVS_E_UNKNOWN;
468 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
469 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
471 if (aEntIter != myEntityMap.end()) // the entity already exists
472 return aEntIter->second;
475 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
476 myEntities.push_back(aPt2DEntity);
477 myEntityMap[theEntity] = aPt2DEntity.h;
478 return aPt2DEntity.h;
481 /// \todo Other types of entities
483 // Unsupported or wrong entity type
484 return SLVS_E_UNKNOWN;
487 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
488 boost::shared_ptr<ModelAPI_Attribute> theDirX,
489 boost::shared_ptr<ModelAPI_Attribute> theDirY,
490 boost::shared_ptr<ModelAPI_Attribute> theNorm)
492 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
493 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
494 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
495 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
496 if (!aDirX || !aDirY ||
497 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
498 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
499 return SLVS_E_UNKNOWN;
501 // quaternion parameters of normal vector
502 double qw, qx, qy, qz;
503 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
504 aDirY->x(), aDirY->y(), aDirY->z(),
506 double aNormCoord[4] = {qw, qx, qy, qz};
508 // Try to find existent normal
509 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
510 aEntIter = myEntityMap.find(theNorm);
511 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
512 if (aEntIter == myEntityMap.end()) // no such entity => should be created
513 aParamIter = myParams.end();
515 { // the entity already exists, update it
516 int aEntPos = Search(aEntIter->second, myEntities);
517 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
518 aParamIter = myParams.begin() + aParamPos;
521 // Change parameters of the normal
522 Slvs_hParam aNormParams[4];
523 for (int i = 0; i < 4; i++)
524 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
526 if (aEntIter != myEntityMap.end()) // the entity already exists
527 return aEntIter->second;
530 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
531 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
532 myEntities.push_back(aNormal);
533 myEntityMap[theNorm] = aNormal.h;
538 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
539 boost::shared_ptr<SketchPlugin_Feature> theSketch)
541 if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
542 return false; // the workplane already exists or the function parameter is not Sketch
544 mySketch = theSketch;
549 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
551 // Get parameters of workplane
552 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
553 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
554 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
555 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
556 // Transform them into SolveSpace format
557 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
558 if (!aNormalWP) return false;
559 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
560 if (!anOriginWP) return false;
565 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
566 // Workplane should be added to the list of entities
567 myEntities.push_back(myWorkplane);
573 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
574 const double& theParam,
575 std::vector<Slvs_Param>::const_iterator& thePrmIter)
577 if (thePrmIter != myParams.end())
578 { // Parameter should be updated
579 int aParamPos = thePrmIter - myParams.begin();
580 if (fabs(thePrmIter->val - theParam) > tolerance)
582 myNeedToSolve = true; // parameter is changed, need to resolve constraints
583 myParams[aParamPos].val = theParam;
586 return myParams[aParamPos].h;
589 // Newly created parameter
590 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
591 myParams.push_back(aParam);
592 myNeedToSolve = true;
593 // The list of parameters is changed, move iterator to the end of the list to avoid problems
594 thePrmIter = myParams.end();
598 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
599 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
601 const std::string& aConstraintKind = theConstraint->getKind();
602 // Constraint for coincidence of two points
603 if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
605 // Verify the constraint has only two attributes and they are points
606 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
607 int aPt3d = 0; // bit-mapped field, the same information for 3D points
608 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
610 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
611 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
612 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
614 if (!anAttr) continue;
615 // Verify the attribute is a 2D point
616 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
617 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
620 aPt2d |= (1 << indAttr);
623 // Verify the attribute is a 3D point
624 boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
625 boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
628 aPt3d |= (1 << indAttr);
631 // Attribute neither 2D nor 3D point is not supported by this type of constraint
632 return SLVS_C_UNKNOWN;
634 // The constrained points should be in first and second positions,
635 // so the expected value of aPt2d or aPt3d is 3
636 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
637 return SLVS_C_POINTS_COINCIDENT;
638 // Constraint parameters are wrong
639 return SLVS_C_UNKNOWN;
642 /// \todo Implement other kind of constrtaints
644 return SLVS_C_UNKNOWN;
647 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::resolveConstraints()
652 myConstrSolver.setGroupID(myID);
653 myConstrSolver.setParameters(myParams);
654 myConstrSolver.setEntities(myEntities);
655 myConstrSolver.setConstraints(myConstraints);
657 if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
658 { // solution succeeded, store results into correspondent attributes
659 // Obtain result into the same list of parameters
660 if (!myConstrSolver.getResult(myParams))
663 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
664 anEntIter = myEntityMap.begin();
665 for ( ; anEntIter != myEntityMap.end(); anEntIter++)
666 updateAttribute(anEntIter->first, anEntIter->second);
668 /// \todo Implement error handling
670 removeTemporaryConstraints();
671 myNeedToSolve = false;
674 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateGroup()
676 // Check for valid sketch
677 if (!mySketch->data()->isValid())
680 // Fast check for constraint validity. If all constraints are valid, no need to update the group
681 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
682 aConstrIter = myConstraintMap.rbegin();
683 bool isAllValid = true;
684 for ( ; isAllValid && aConstrIter != myConstraintMap.rend(); aConstrIter++)
685 if (!aConstrIter->first->data()->isValid())
690 // Remove invalid constraints.
691 // There only constraint will be deleted (parameters and entities) will be removed below
692 std::list< boost::shared_ptr<SketchPlugin_Constraint> > aConstrToDelete;
693 std::map<Slvs_hEntity, bool> anEntToDelete; // entities will be removed if no valid constraints use them
694 for (aConstrIter = myConstraintMap.rbegin(); aConstrIter != myConstraintMap.rend(); aConstrIter++)
696 bool isValid = aConstrIter->first->data()->isValid();
698 int aConstrPos = Search(aConstrIter->second, myConstraints);
699 if (aConstrPos < (int)myConstraints.size())
701 Slvs_hEntity aConstrEnt[] = {
702 myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
703 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
704 for (int i = 0; i < 4; i++)
705 if (aConstrEnt[i] != SLVS_E_UNKNOWN)
707 if (anEntToDelete.find(aConstrEnt[i]) == anEntToDelete.end())
708 anEntToDelete[aConstrEnt[i]] = !isValid;
709 else if (isValid) // constraint is valid => no need to remove its entities
710 anEntToDelete[aConstrEnt[i]] = false;
714 myConstraints.erase(myConstraints.begin() + aConstrPos);
715 if (aConstrIter->second == myConstrMaxID) // When the constraint with highest ID is removed, decrease indexer
717 aConstrToDelete.push_front(aConstrIter->first);
721 std::list< boost::shared_ptr<SketchPlugin_Constraint> >::iterator aDelIter;
722 for (aDelIter = aConstrToDelete.begin(); aDelIter != aConstrToDelete.end(); aDelIter++)
723 myConstraintMap.erase(*aDelIter);
725 // Remove invalid and unused entities
726 std::map<Slvs_hEntity, bool>::reverse_iterator aEDelIter;
727 for (aEDelIter = anEntToDelete.rbegin(); aEDelIter != anEntToDelete.rend(); aEDelIter++)
728 if (aEDelIter->second)
730 int anEntPos = Search(aEDelIter->first, myEntities);
731 std::vector<Slvs_Entity>::iterator aEntIter = myEntities.begin() + anEntPos;
732 // Number of parameters for the entity
734 while (aEntIter->param[aNbParams]) aNbParams++;
735 if (aNbParams == 0) continue;
736 // Decrease parameter indexer if there are deleted parameter with higher IDs
737 if (aEntIter->param[aNbParams-1] == myParamMaxID)
738 myParamMaxID -= aNbParams;
739 // Remove parameters of the entity
740 int aParamPos = Search(aEntIter->param[0], myParams);
741 myParams.erase(myParams.begin() + aParamPos,
742 myParams.begin() + aParamPos + aNbParams);
745 if (aEDelIter->first == myEntityMaxID)
747 myEntities.erase(myEntities.begin() + anEntPos);
748 // Remove such entity from myEntityMap
749 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
750 anEntMapIter = myEntityMap.begin();
751 for ( ; anEntMapIter != myEntityMap.end(); anEntMapIter++)
752 if (anEntMapIter->second == aEDelIter->first)
754 if (anEntMapIter != myEntityMap.end())
755 myEntityMap.erase(anEntMapIter);
761 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
762 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
763 const Slvs_hEntity& theEntityID)
765 // Search the position of the first parameter of the entity
766 int anEntPos = Search(theEntityID, myEntities);
767 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
769 // Look over supported types of entities
772 boost::shared_ptr<GeomDataAPI_Point> aPoint =
773 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
776 aPoint->setValue(myParams[aFirstParamPos].val,
777 myParams[aFirstParamPos+1].val,
778 myParams[aFirstParamPos+2].val);
783 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
784 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
787 aPoint2D->setValue(myParams[aFirstParamPos].val,
788 myParams[aFirstParamPos+1].val);
792 /// \todo Support other types of entities
795 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
796 boost::shared_ptr<ModelAPI_Attribute> theEntity)
798 if (myEntityMap.find(theEntity) != myEntityMap.end())
800 // If the attribute is a point and it is changed (the group needs to rebuild),
801 // probably user has dragged this point into this position,
802 // so it is necessary to add constraint which will guarantee the point will not change
804 // Store myNeedToSolve flag to verify the entity is really changed
805 bool aNeedToSolveCopy = myNeedToSolve;
806 myNeedToSolve = false;
808 changeEntity(theEntity);
810 if (myNeedToSolve) // the entity is changed
812 // Verify the entity is a point and add temporary constraint of permanency
813 boost::shared_ptr<GeomDataAPI_Point> aPoint =
814 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
815 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
816 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
817 if (aPoint || aPoint2D)
818 addTemporaryConstraintWhereDragged(theEntity);
821 // Restore flag of changes
822 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
826 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
827 boost::shared_ptr<ModelAPI_Attribute> theEntity)
829 // Find identifier of the entity
830 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
831 anEntIter = myEntityMap.find(theEntity);
833 // Create WHERE_DRAGGED constraint
834 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
835 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
836 myConstraints.push_back(aWDConstr);
837 myTempConstraints.push_back(aWDConstr.h);
840 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
842 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
843 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
845 int aConstrPos = Search(*aTmpConstrIter, myConstraints);
846 myConstraints.erase(myConstraints.begin() + aConstrPos);
848 // If the removing constraint has higher index, decrease the indexer
849 if (*aTmpConstrIter == myConstrMaxID)
852 myTempConstraints.clear();
857 // ========================================================
858 // ========= Auxiliary functions ===============
859 // ========================================================
861 template <typename T>
862 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
864 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
865 int aVecSize = theEntities.size();
866 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
868 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
871 aResIndex = aVecSize;