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_FEATURES_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_FEATURES_MOVED))
81 const Model_FeatureUpdatedMessage* anUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
82 std::set< boost::shared_ptr<ModelAPI_Feature> > aFeatures = anUpdateMsg->features();
84 if (!(theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURES_MOVED)))
86 std::set< boost::shared_ptr<ModelAPI_Feature> >::iterator aFeatIter;
87 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++)
89 // Only sketches and constraints can be added by Create event
90 const std::string& aFeatureKind = (*aFeatIter)->getKind();
91 if (aFeatureKind.compare("Sketch") == 0)
93 boost::shared_ptr<SketchPlugin_Feature> aSketch =
94 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
95 changeWorkplane(aSketch);
98 boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
99 boost::dynamic_pointer_cast<SketchPlugin_Constraint>(*aFeatIter);
101 changeConstraint(aConstraint);
104 // Sketch plugin features can be only updated
105 boost::shared_ptr<SketchPlugin_Feature> aFeature =
106 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
108 updateEntity(aFeature);
113 // Solve the set of constraints
114 resolveConstraints();
116 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
118 const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
119 const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
121 // Find "Sketch" in groups. The constraint groups should be updated when an object removed from Sketch
122 std::set<std::string>::const_iterator aFGrIter;
123 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
124 if (aFGrIter->compare("Sketch") == 0)
127 if (aFGrIter != aFeatureGroups.end())
129 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
130 while (aGroupIter != myGroups.end())
132 if ((*aGroupIter)->updateGroup())
133 { // the group should be removed
135 int aShift = aGroupIter - myGroups.begin();
136 myGroups.erase(aGroupIter);
137 aGroupIter = myGroups.begin() + aShift;
145 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
147 bool aResult = true; // changed when a workplane wrongly updated
148 bool isUpdated = false;
149 // Try to update specified workplane in all groups
150 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
151 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
152 if ((*aGroupIter)->isBaseWorkplane(theSketch))
155 if (!(*aGroupIter)->updateWorkplane())
158 // If the workplane is not updated, so this is a new workplane
161 SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
162 // Verify that the group is created successfully
163 if (!aNewGroup->isBaseWorkplane(theSketch))
168 myGroups.push_back(aNewGroup);
173 bool SketchSolver_ConstraintManager::changeConstraint(
174 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
176 // Search the groups which this constraint touchs
177 std::vector<Slvs_hGroup> aGroups;
178 findGroups(theConstraint, aGroups);
180 // Process the groups list
181 if (aGroups.size() == 0)
182 { // There are no groups applicable for this constraint => create new one
183 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
184 if (!aWP) return false;
185 SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
186 if (!aGroup->changeConstraint(theConstraint))
191 myGroups.push_back(aGroup);
194 else if (aGroups.size() == 1)
195 { // Only one group => add constraint into it
196 Slvs_hGroup aGroupId = *(aGroups.begin());
197 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
198 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
199 if ((*aGroupIter)->getId() == aGroupId)
200 return (*aGroupIter)->changeConstraint(theConstraint);
202 else if (aGroups.size() > 1)
203 { // Several groups applicable for this constraint => need to merge them
204 /// \todo Implement merging of groups
207 // Something goes wrong
211 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
213 // Create list of attributes depending on type of the feature
214 std::vector<std::string> anAttrList;
215 const std::string& aFeatureKind = theFeature->getKind();
217 if (aFeatureKind.compare("SketchPoint") == 0)
218 anAttrList.push_back(POINT_ATTR_COORD);
220 else if (aFeatureKind.compare("SketchLine") == 0)
222 anAttrList.push_back(LINE_ATTR_START);
223 anAttrList.push_back(LINE_ATTR_END);
225 /// \todo Other types of features should be implemented
227 // Check changing of feature's attributes (go through the groups and search usage of the attributes)
228 std::vector<std::string>::const_iterator anAttrIter;
229 for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
231 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
232 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
234 boost::shared_ptr<ModelAPI_Attribute> anAttribute =
235 boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
236 (*aGroupIter)->updateEntityIfPossible(anAttribute);
242 void SketchSolver_ConstraintManager::findGroups(
243 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
244 std::vector<Slvs_hGroup>& theGroupIDs) const
246 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
248 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
249 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
250 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
251 theGroupIDs.push_back((*aGroupIter)->getId());
254 boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
255 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
257 // Already verified workplanes
258 std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
260 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
261 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
263 boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
264 if (aVerified.find(aWP) != aVerified.end())
267 boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
268 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
269 std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
270 std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
271 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
272 if (*anIter == theConstraint)
273 return aWP; // workplane is found
274 aVerified.insert(aWP);
277 return boost::shared_ptr<SketchPlugin_Feature>();
280 void SketchSolver_ConstraintManager::resolveConstraints()
282 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
283 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
284 (*aGroupIter)->resolveConstraints();
286 // Features may be updated => send events
287 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
292 // ========================================================
293 // ========= SketchSolver_ConstraintGroup ===============
294 // ========================================================
296 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
297 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
298 : myID(++myGroupIndexer),
303 myNeedToSolve(false),
308 myConstraints.clear();
310 // Initialize workplane
311 myWorkplane.h = SLVS_E_UNKNOWN;
312 assert(addWorkplane(theWorkplane));
315 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
319 myConstraints.clear();
320 myConstraintMap.clear();
322 // If the group with maximal identifier is deleted, decrease the indexer
323 if (myID == myGroupIndexer)
327 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
328 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
330 return theWorkplane == mySketch;
333 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
334 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
336 // Check the group is empty
337 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
340 // Go through constraint entities and verify if some of them already in the group
341 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
343 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
344 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
345 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
347 if (!aCAttrRef) continue;
348 if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
352 // Entities did not found
356 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
357 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
359 // There is no workplane yet, something wrong
360 if (myWorkplane.h == SLVS_E_UNKNOWN)
363 // Search this constraint in the current group to update it
364 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
365 aConstrMapIter = myConstraintMap.find(theConstraint);
366 std::vector<Slvs_Constraint>::iterator aConstrIter;
367 if (aConstrMapIter != myConstraintMap.end())
369 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
370 aConstrIter = myConstraints.begin() + aConstrPos;
373 // Get constraint type and verify the constraint parameters are correct
374 int aConstrType = getConstraintType(theConstraint);
375 if (aConstrType == SLVS_C_UNKNOWN ||
376 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
379 // Create constraint parameters
380 double aDistance = 0.0; // scalar value of the constraint
381 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
382 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
385 aDistance = aDistAttr->value();
386 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
388 myNeedToSolve = true;
389 aConstrIter->valA = aDistance;
393 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
394 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
396 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
397 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
398 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
399 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
401 if (!aConstrAttr) continue;
402 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
405 if (aConstrMapIter == myConstraintMap.end())
407 // Create SolveSpace constraint structure
408 Slvs_Constraint aConstraint =
409 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
410 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
411 myConstraints.push_back(aConstraint);
412 myConstraintMap[theConstraint] = aConstraint.h;
417 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
418 boost::shared_ptr<ModelAPI_Attribute> theEntity)
420 // If the entity is already in the group, try to find it
421 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
422 aEntIter = myEntityMap.find(theEntity);
423 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
424 if (aEntIter == myEntityMap.end()) // no such entity => should be created
425 aParamIter = myParams.end();
427 { // the entity already exists
428 int aEntPos = Search(aEntIter->second, myEntities);
429 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
430 aParamIter = myParams.begin() + aParamPos;
433 // Look over supported types of entities
436 boost::shared_ptr<GeomDataAPI_Point> aPoint =
437 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
440 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
441 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
442 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
444 if (aEntIter != myEntityMap.end()) // the entity already exists
445 return aEntIter->second;
448 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
449 myEntities.push_back(aPtEntity);
450 myEntityMap[theEntity] = aPtEntity.h;
455 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
456 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
459 // The 2D points are created on workplane. So, if there is no workplane yet, then error
460 if (myWorkplane.h == SLVS_E_UNKNOWN)
461 return SLVS_E_UNKNOWN;
462 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
463 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
465 if (aEntIter != myEntityMap.end()) // the entity already exists
466 return aEntIter->second;
469 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
470 myEntities.push_back(aPt2DEntity);
471 myEntityMap[theEntity] = aPt2DEntity.h;
472 return aPt2DEntity.h;
475 /// \todo Other types of entities
477 // Unsupported or wrong entity type
478 return SLVS_E_UNKNOWN;
481 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
482 boost::shared_ptr<ModelAPI_Attribute> theDirX,
483 boost::shared_ptr<ModelAPI_Attribute> theDirY,
484 boost::shared_ptr<ModelAPI_Attribute> theNorm)
486 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
487 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
488 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
489 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
490 if (!aDirX || !aDirY ||
491 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
492 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
493 return SLVS_E_UNKNOWN;
495 // quaternion parameters of normal vector
496 double qw, qx, qy, qz;
497 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
498 aDirY->x(), aDirY->y(), aDirY->z(),
500 double aNormCoord[4] = {qw, qx, qy, qz};
502 // Try to find existent normal
503 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
504 aEntIter = myEntityMap.find(theNorm);
505 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
506 if (aEntIter == myEntityMap.end()) // no such entity => should be created
507 aParamIter = myParams.end();
509 { // the entity already exists, update it
510 int aEntPos = Search(aEntIter->second, myEntities);
511 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
512 aParamIter = myParams.begin() + aParamPos;
515 // Change parameters of the normal
516 Slvs_hParam aNormParams[4];
517 for (int i = 0; i < 4; i++)
518 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
520 if (aEntIter != myEntityMap.end()) // the entity already exists
521 return aEntIter->second;
524 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
525 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
526 myEntities.push_back(aNormal);
527 myEntityMap[theNorm] = aNormal.h;
532 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
533 boost::shared_ptr<SketchPlugin_Feature> theSketch)
535 if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
536 return false; // the workplane already exists or the function parameter is not Sketch
538 mySketch = theSketch;
543 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
545 // Get parameters of workplane
546 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
547 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
548 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
549 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
550 // Transform them into SolveSpace format
551 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
552 if (!aNormalWP) return false;
553 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
554 if (!anOriginWP) return false;
559 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
560 // Workplane should be added to the list of entities
561 myEntities.push_back(myWorkplane);
567 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
568 const double& theParam,
569 std::vector<Slvs_Param>::const_iterator& thePrmIter)
571 if (thePrmIter != myParams.end())
572 { // Parameter should be updated
573 int aParamPos = thePrmIter - myParams.begin();
574 if (fabs(thePrmIter->val - theParam) > tolerance)
576 myNeedToSolve = true; // parameter is changed, need to resolve constraints
577 myParams[aParamPos].val = theParam;
580 return myParams[aParamPos].h;
583 // Newly created parameter
584 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
585 myParams.push_back(aParam);
586 myNeedToSolve = true;
587 // The list of parameters is changed, move iterator to the end of the list to avoid problems
588 thePrmIter = myParams.end();
592 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
593 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
595 const std::string& aConstraintKind = theConstraint->getKind();
596 // Constraint for coincidence of two points
597 if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
599 // Verify the constraint has only two attributes and they are points
600 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
601 int aPt3d = 0; // bit-mapped field, the same information for 3D points
602 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
604 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
605 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
606 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
608 if (!anAttr) continue;
609 // Verify the attribute is a 2D point
610 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
611 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
614 aPt2d |= (1 << indAttr);
617 // Verify the attribute is a 3D point
618 boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
619 boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
622 aPt3d |= (1 << indAttr);
625 // Attribute neither 2D nor 3D point is not supported by this type of constraint
626 return SLVS_C_UNKNOWN;
628 // The constrained points should be in first and second positions,
629 // so the expected value of aPt2d or aPt3d is 3
630 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
631 return SLVS_C_POINTS_COINCIDENT;
632 // Constraint parameters are wrong
633 return SLVS_C_UNKNOWN;
636 /// \todo Implement other kind of constrtaints
638 return SLVS_C_UNKNOWN;
641 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::resolveConstraints()
646 myConstrSolver.setGroupID(myID);
647 myConstrSolver.setParameters(myParams);
648 myConstrSolver.setEntities(myEntities);
649 myConstrSolver.setConstraints(myConstraints);
651 if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
652 { // solution succeeded, store results into correspondent attributes
653 // Obtain result into the same list of parameters
654 if (!myConstrSolver.getResult(myParams))
657 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
658 anEntIter = myEntityMap.begin();
659 for ( ; anEntIter != myEntityMap.end(); anEntIter++)
660 updateAttribute(anEntIter->first, anEntIter->second);
662 /// \todo Implement error handling
664 removeTemporaryConstraints();
665 myNeedToSolve = false;
668 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateGroup()
670 // Check for valid sketch
671 if (!mySketch->data()->isValid())
674 // Fast check for constraint validity. If all constraints are valid, no need to update the group
675 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
676 aConstrIter = myConstraintMap.rbegin();
677 bool isAllValid = true;
678 for ( ; isAllValid && aConstrIter != myConstraintMap.rend(); aConstrIter++)
679 if (!aConstrIter->first->data()->isValid())
684 // Remove invalid constraints.
685 // There only constraint will be deleted (parameters and entities) will be removed below
686 std::list< boost::shared_ptr<SketchPlugin_Constraint> > aConstrToDelete;
687 std::map<Slvs_hEntity, bool> anEntToDelete; // entities will be removed if no valid constraints use them
688 for (aConstrIter = myConstraintMap.rbegin(); aConstrIter != myConstraintMap.rend(); aConstrIter++)
690 bool isValid = aConstrIter->first->data()->isValid();
692 int aConstrPos = Search(aConstrIter->second, myConstraints);
693 if (aConstrPos < (int)myConstraints.size())
695 Slvs_hEntity aConstrEnt[] = {
696 myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
697 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
698 for (int i = 0; i < 4; i++)
699 if (aConstrEnt[i] != SLVS_E_UNKNOWN)
701 if (anEntToDelete.find(aConstrEnt[i]) == anEntToDelete.end())
702 anEntToDelete[aConstrEnt[i]] = !isValid;
703 else if (isValid) // constraint is valid => no need to remove its entities
704 anEntToDelete[aConstrEnt[i]] = false;
708 myConstraints.erase(myConstraints.begin() + aConstrPos);
709 if (aConstrIter->second == myConstrMaxID) // When the constraint with highest ID is removed, decrease indexer
711 aConstrToDelete.push_front(aConstrIter->first);
715 std::list< boost::shared_ptr<SketchPlugin_Constraint> >::iterator aDelIter;
716 for (aDelIter = aConstrToDelete.begin(); aDelIter != aConstrToDelete.end(); aDelIter++)
717 myConstraintMap.erase(*aDelIter);
719 // Remove invalid and unused entities
720 std::map<Slvs_hEntity, bool>::reverse_iterator aEDelIter;
721 for (aEDelIter = anEntToDelete.rbegin(); aEDelIter != anEntToDelete.rend(); aEDelIter++)
722 if (aEDelIter->second)
724 int anEntPos = Search(aEDelIter->first, myEntities);
725 std::vector<Slvs_Entity>::iterator aEntIter = myEntities.begin() + anEntPos;
726 // Number of parameters for the entity
728 while (aEntIter->param[aNbParams]) aNbParams++;
729 if (aNbParams == 0) continue;
730 // Decrease parameter indexer if there are deleted parameter with higher IDs
731 if (aEntIter->param[aNbParams-1] == myParamMaxID)
732 myParamMaxID -= aNbParams;
733 // Remove parameters of the entity
734 int aParamPos = Search(aEntIter->param[0], myParams);
735 myParams.erase(myParams.begin() + aParamPos,
736 myParams.begin() + aParamPos + aNbParams);
739 if (aEDelIter->first == myEntityMaxID)
741 myEntities.erase(myEntities.begin() + anEntPos);
742 // Remove such entity from myEntityMap
743 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
744 anEntMapIter = myEntityMap.begin();
745 for ( ; anEntMapIter != myEntityMap.end(); anEntMapIter++)
746 if (anEntMapIter->second == aEDelIter->first)
748 if (anEntMapIter != myEntityMap.end())
749 myEntityMap.erase(anEntMapIter);
755 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
756 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
757 const Slvs_hEntity& theEntityID)
759 // Search the position of the first parameter of the entity
760 int anEntPos = Search(theEntityID, myEntities);
761 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
763 // Look over supported types of entities
766 boost::shared_ptr<GeomDataAPI_Point> aPoint =
767 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
770 aPoint->setValue(myParams[aFirstParamPos].val,
771 myParams[aFirstParamPos+1].val,
772 myParams[aFirstParamPos+2].val);
777 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
778 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
781 aPoint2D->setValue(myParams[aFirstParamPos].val,
782 myParams[aFirstParamPos+1].val);
786 /// \todo Support other types of entities
789 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
790 boost::shared_ptr<ModelAPI_Attribute> theEntity)
792 if (myEntityMap.find(theEntity) != myEntityMap.end())
794 // If the attribute is a point and it is changed (the group needs to rebuild),
795 // probably user has dragged this point into this position,
796 // so it is necessary to add constraint which will guarantee the point will not change
798 // Store myNeedToSolve flag to verify the entity is really changed
799 bool aNeedToSolveCopy = myNeedToSolve;
800 myNeedToSolve = false;
802 changeEntity(theEntity);
804 if (myNeedToSolve) // the entity is changed
806 // Verify the entity is a point and add temporary constraint of permanency
807 boost::shared_ptr<GeomDataAPI_Point> aPoint =
808 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
809 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
810 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
811 if (aPoint || aPoint2D)
812 addTemporaryConstraintWhereDragged(theEntity);
815 // Restore flag of changes
816 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
820 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
821 boost::shared_ptr<ModelAPI_Attribute> theEntity)
823 // Find identifier of the entity
824 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
825 anEntIter = myEntityMap.find(theEntity);
827 // Create WHERE_DRAGGED constraint
828 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
829 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
830 myConstraints.push_back(aWDConstr);
831 myTempConstraints.push_back(aWDConstr.h);
834 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
836 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
837 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
839 int aConstrPos = Search(*aTmpConstrIter, myConstraints);
840 myConstraints.erase(myConstraints.begin() + aConstrPos);
842 // If the removing constraint has higher index, decrease the indexer
843 if (*aTmpConstrIter == myConstrMaxID)
846 myTempConstraints.clear();
851 // ========================================================
852 // ========= Auxiliary functions ===============
853 // ========================================================
855 template <typename T>
856 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
858 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
859 int aVecSize = theEntities.size();
860 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
862 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
865 aResIndex = aVecSize;