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))
80 const Model_FeatureUpdatedMessage* aUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
82 // Only sketches and constraints can be added by Create event
83 const std::string& aFeatureKind = aUpdateMsg->feature()->getKind();
84 if (aFeatureKind.compare("Sketch") == 0)
86 boost::shared_ptr<SketchPlugin_Feature> aSketch =
87 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
88 changeWorkplane(aSketch);
91 boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
92 boost::dynamic_pointer_cast<SketchPlugin_Constraint>(aUpdateMsg->feature());
94 changeConstraint(aConstraint);
97 // Sketch plugin features can be only updated
98 boost::shared_ptr<SketchPlugin_Feature> aFeature =
99 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
101 updateEntity(aFeature);
104 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
106 const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
108 if (aDeleteMsg->group().compare("Sketch") == 0)
110 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
111 while (aGroupIter != myGroups.end())
113 if ((*aGroupIter)->updateGroup())
114 { // the group should be removed
116 int aShift = aGroupIter - myGroups.begin();
117 myGroups.erase(aGroupIter);
118 aGroupIter = myGroups.begin() + aShift;
124 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURES_MOVED))
126 // Solve the set of constraints
127 resolveConstraints();
131 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
133 bool aResult = true; // changed when a workplane wrongly updated
134 bool isUpdated = false;
135 // Try to update specified workplane in all groups
136 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
137 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
138 if ((*aGroupIter)->isBaseWorkplane(theSketch))
141 if (!(*aGroupIter)->updateWorkplane())
144 // If the workplane is not updated, so this is a new workplane
147 SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
148 // Verify that the group is created successfully
149 if (!aNewGroup->isBaseWorkplane(theSketch))
154 myGroups.push_back(aNewGroup);
159 bool SketchSolver_ConstraintManager::changeConstraint(
160 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
162 // Search the groups which this constraint touchs
163 std::vector<Slvs_hGroup> aGroups;
164 findGroups(theConstraint, aGroups);
166 // Process the groups list
167 if (aGroups.size() == 0)
168 { // There are no groups applicable for this constraint => create new one
169 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
170 if (!aWP) return false;
171 SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
172 if (!aGroup->changeConstraint(theConstraint))
177 myGroups.push_back(aGroup);
180 else if (aGroups.size() == 1)
181 { // Only one group => add constraint into it
182 Slvs_hGroup aGroupId = *(aGroups.begin());
183 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
184 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
185 if ((*aGroupIter)->getId() == aGroupId)
186 return (*aGroupIter)->changeConstraint(theConstraint);
188 else if (aGroups.size() > 1)
189 { // Several groups applicable for this constraint => need to merge them
190 /// \todo Implement merging of groups
193 // Something goes wrong
197 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
199 // Create list of attributes depending on type of the feature
200 std::vector<std::string> anAttrList;
201 const std::string& aFeatureKind = theFeature->getKind();
203 if (aFeatureKind.compare("SketchPoint") == 0)
204 anAttrList.push_back(POINT_ATTR_COORD);
206 else if (aFeatureKind.compare("SketchLine") == 0)
208 anAttrList.push_back(LINE_ATTR_START);
209 anAttrList.push_back(LINE_ATTR_END);
211 /// \todo Other types of features should be implemented
213 // Check changing of feature's attributes (go through the groups and search usage of the attributes)
214 std::vector<std::string>::const_iterator anAttrIter;
215 for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
217 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
218 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
220 boost::shared_ptr<ModelAPI_Attribute> anAttribute =
221 boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
222 (*aGroupIter)->updateEntityIfPossible(anAttribute);
228 void SketchSolver_ConstraintManager::findGroups(
229 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
230 std::vector<Slvs_hGroup>& theGroupIDs) const
232 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
234 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
235 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
236 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
237 theGroupIDs.push_back((*aGroupIter)->getId());
240 boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
241 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
243 // Already verified workplanes
244 std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
246 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
247 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
249 boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
250 if (aVerified.find(aWP) != aVerified.end())
253 boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
254 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
255 std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
256 std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
257 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
258 if (*anIter == theConstraint)
259 return aWP; // workplane is found
260 aVerified.insert(aWP);
263 return boost::shared_ptr<SketchPlugin_Feature>();
266 void SketchSolver_ConstraintManager::resolveConstraints()
268 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
269 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
270 (*aGroupIter)->resolveConstraints();
275 // ========================================================
276 // ========= SketchSolver_ConstraintGroup ===============
277 // ========================================================
279 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
280 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
281 : myID(++myGroupIndexer),
286 myNeedToSolve(false),
291 myConstraints.clear();
293 // Initialize workplane
294 myWorkplane.h = SLVS_E_UNKNOWN;
295 assert(addWorkplane(theWorkplane));
298 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
302 myConstraints.clear();
303 myConstraintMap.clear();
305 // If the group with maximal identifier is deleted, decrease the indexer
306 if (myID == myGroupIndexer)
310 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
311 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
313 return theWorkplane == mySketch;
316 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
317 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
319 // Check the group is empty
320 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
323 // Go through constraint entities and verify if some of them already in the group
324 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
326 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
327 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
328 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
330 if (!aCAttrRef) continue;
331 if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
335 // Entities did not found
339 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
340 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
342 // There is no workplane yet, something wrong
343 if (myWorkplane.h == SLVS_E_UNKNOWN)
346 // Search this constraint in the current group to update it
347 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
348 aConstrMapIter = myConstraintMap.find(theConstraint);
349 std::vector<Slvs_Constraint>::iterator aConstrIter;
350 if (aConstrMapIter != myConstraintMap.end())
352 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
353 aConstrIter = myConstraints.begin() + aConstrPos;
356 // Get constraint type and verify the constraint parameters are correct
357 int aConstrType = getConstraintType(theConstraint);
358 if (aConstrType == SLVS_C_UNKNOWN ||
359 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
362 // Create constraint parameters
363 double aDistance = 0.0; // scalar value of the constraint
364 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
365 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
368 aDistance = aDistAttr->value();
369 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
371 myNeedToSolve = true;
372 aConstrIter->valA = aDistance;
376 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
377 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
379 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
380 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
381 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
382 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
384 if (!aConstrAttr) continue;
385 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
388 if (aConstrMapIter == myConstraintMap.end())
390 // Create SolveSpace constraint structure
391 Slvs_Constraint aConstraint =
392 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
393 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
394 myConstraints.push_back(aConstraint);
395 myConstraintMap[theConstraint] = aConstraint.h;
400 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
401 boost::shared_ptr<ModelAPI_Attribute> theEntity)
403 // If the entity is already in the group, try to find it
404 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
405 aEntIter = myEntityMap.find(theEntity);
406 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
407 if (aEntIter == myEntityMap.end()) // no such entity => should be created
408 aParamIter = myParams.end();
410 { // the entity already exists
411 int aEntPos = Search(aEntIter->second, myEntities);
412 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
413 aParamIter = myParams.begin() + aParamPos;
416 // Look over supported types of entities
419 boost::shared_ptr<GeomDataAPI_Point> aPoint =
420 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
423 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
424 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
425 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
427 if (aEntIter != myEntityMap.end()) // the entity already exists
428 return aEntIter->second;
431 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
432 myEntities.push_back(aPtEntity);
433 myEntityMap[theEntity] = aPtEntity.h;
438 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
439 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
442 // The 2D points are created on workplane. So, if there is no workplane yet, then error
443 if (myWorkplane.h == SLVS_E_UNKNOWN)
444 return SLVS_E_UNKNOWN;
445 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
446 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
448 if (aEntIter != myEntityMap.end()) // the entity already exists
449 return aEntIter->second;
452 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
453 myEntities.push_back(aPt2DEntity);
454 myEntityMap[theEntity] = aPt2DEntity.h;
455 return aPt2DEntity.h;
458 /// \todo Other types of entities
460 // Unsupported or wrong entity type
461 return SLVS_E_UNKNOWN;
464 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
465 boost::shared_ptr<ModelAPI_Attribute> theDirX,
466 boost::shared_ptr<ModelAPI_Attribute> theDirY,
467 boost::shared_ptr<ModelAPI_Attribute> theNorm)
469 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
470 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
471 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
472 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
473 if (!aDirX || !aDirY ||
474 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
475 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
476 return SLVS_E_UNKNOWN;
478 // quaternion parameters of normal vector
479 double qw, qx, qy, qz;
480 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
481 aDirY->x(), aDirY->y(), aDirY->z(),
483 double aNormCoord[4] = {qw, qx, qy, qz};
485 // Try to find existent normal
486 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
487 aEntIter = myEntityMap.find(theNorm);
488 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
489 if (aEntIter == myEntityMap.end()) // no such entity => should be created
490 aParamIter = myParams.end();
492 { // the entity already exists, update it
493 int aEntPos = Search(aEntIter->second, myEntities);
494 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
495 aParamIter = myParams.begin() + aParamPos;
498 // Change parameters of the normal
499 Slvs_hParam aNormParams[4];
500 for (int i = 0; i < 4; i++)
501 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
503 if (aEntIter != myEntityMap.end()) // the entity already exists
504 return aEntIter->second;
507 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
508 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
509 myEntities.push_back(aNormal);
510 myEntityMap[theNorm] = aNormal.h;
515 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
516 boost::shared_ptr<SketchPlugin_Feature> theSketch)
518 if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
519 return false; // the workplane already exists or the function parameter is not Sketch
521 mySketch = theSketch;
526 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
528 // Get parameters of workplane
529 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
530 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
531 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
532 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
533 // Transform them into SolveSpace format
534 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
535 if (!aNormalWP) return false;
536 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
537 if (!anOriginWP) return false;
542 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
543 // Workplane should be added to the list of entities
544 myEntities.push_back(myWorkplane);
550 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
551 const double& theParam,
552 std::vector<Slvs_Param>::const_iterator& thePrmIter)
554 if (thePrmIter != myParams.end())
555 { // Parameter should be updated
556 int aParamPos = thePrmIter - myParams.begin();
557 if (fabs(thePrmIter->val - theParam) > tolerance)
559 myNeedToSolve = true; // parameter is changed, need to resolve constraints
560 myParams[aParamPos].val = theParam;
563 return myParams[aParamPos].h;
566 // Newly created parameter
567 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
568 myParams.push_back(aParam);
569 myNeedToSolve = true;
570 // The list of parameters is changed, move iterator to the end of the list to avoid problems
571 thePrmIter = myParams.end();
575 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
576 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
578 const std::string& aConstraintKind = theConstraint->getKind();
579 // Constraint for coincidence of two points
580 if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
582 // Verify the constraint has only two attributes and they are points
583 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
584 int aPt3d = 0; // bit-mapped field, the same information for 3D points
585 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
587 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
588 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
589 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
591 if (!anAttr) continue;
592 // Verify the attribute is a 2D point
593 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
594 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
597 aPt2d |= (1 << indAttr);
600 // Verify the attribute is a 3D point
601 boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
602 boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
605 aPt3d |= (1 << indAttr);
608 // Attribute neither 2D nor 3D point is not supported by this type of constraint
609 return SLVS_C_UNKNOWN;
611 // The constrained points should be in first and second positions,
612 // so the expected value of aPt2d or aPt3d is 3
613 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
614 return SLVS_C_POINTS_COINCIDENT;
615 // Constraint parameters are wrong
616 return SLVS_C_UNKNOWN;
619 /// \todo Implement other kind of constrtaints
621 return SLVS_C_UNKNOWN;
624 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::resolveConstraints()
629 myConstrSolver.setGroupID(myID);
630 myConstrSolver.setParameters(myParams);
631 myConstrSolver.setEntities(myEntities);
632 myConstrSolver.setConstraints(myConstraints);
634 if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
635 { // solution succeeded, store results into correspondent attributes
636 // Obtain result into the same list of parameters
637 if (!myConstrSolver.getResult(myParams))
640 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
641 anEntIter = myEntityMap.begin();
642 for ( ; anEntIter != myEntityMap.end(); anEntIter++)
643 updateAttribute(anEntIter->first, anEntIter->second);
645 /// \todo Implement error handling
647 removeTemporaryConstraints();
648 myNeedToSolve = false;
651 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateGroup()
653 // Check for valid sketch
654 if (!mySketch->data()->isValid())
657 // Fast check for constraint validity. If all constraints are valid, no need to update the group
658 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
659 aConstrIter = myConstraintMap.rbegin();
660 bool isAllValid = true;
661 for ( ; isAllValid && aConstrIter != myConstraintMap.rend(); aConstrIter++)
662 if (!aConstrIter->first->data()->isValid())
667 // Remove invalid constraints.
668 // There only constraint will be deleted (parameters and entities) will be removed below
669 std::list< boost::shared_ptr<SketchPlugin_Constraint> > aConstrToDelete;
670 std::map<Slvs_hEntity, bool> anEntToDelete; // entities will be removed if no valid constraints use them
671 for (aConstrIter = myConstraintMap.rbegin(); aConstrIter != myConstraintMap.rend(); aConstrIter++)
673 bool isValid = aConstrIter->first->data()->isValid();
675 int aConstrPos = Search(aConstrIter->second, myConstraints);
676 if (aConstrPos < (int)myConstraints.size())
678 Slvs_hEntity aConstrEnt[] = {
679 myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
680 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
681 for (int i = 0; i < 4; i++)
682 if (aConstrEnt[i] != SLVS_E_UNKNOWN)
684 if (anEntToDelete.find(aConstrEnt[i]) == anEntToDelete.end())
685 anEntToDelete[aConstrEnt[i]] = !isValid;
686 else if (isValid) // constraint is valid => no need to remove its entities
687 anEntToDelete[aConstrEnt[i]] = false;
691 myConstraints.erase(myConstraints.begin() + aConstrPos);
692 if (aConstrIter->second == myConstrMaxID) // When the constraint with highest ID is removed, decrease indexer
694 aConstrToDelete.push_front(aConstrIter->first);
698 std::list< boost::shared_ptr<SketchPlugin_Constraint> >::iterator aDelIter;
699 for (aDelIter = aConstrToDelete.begin(); aDelIter != aConstrToDelete.end(); aDelIter++)
700 myConstraintMap.erase(*aDelIter);
702 // Remove invalid and unused entities
703 std::map<Slvs_hEntity, bool>::reverse_iterator aEDelIter;
704 for (aEDelIter = anEntToDelete.rbegin(); aEDelIter != anEntToDelete.rend(); aEDelIter++)
705 if (aEDelIter->second)
707 int anEntPos = Search(aEDelIter->first, myEntities);
708 std::vector<Slvs_Entity>::iterator aEntIter = myEntities.begin() + anEntPos;
709 // Number of parameters for the entity
711 while (aEntIter->param[aNbParams]) aNbParams++;
712 if (aNbParams == 0) continue;
713 // Decrease parameter indexer if there are deleted parameter with higher IDs
714 if (aEntIter->param[aNbParams-1] == myParamMaxID)
715 myParamMaxID -= aNbParams;
716 // Remove parameters of the entity
717 int aParamPos = Search(aEntIter->param[0], myParams);
718 myParams.erase(myParams.begin() + aParamPos,
719 myParams.begin() + aParamPos + aNbParams);
722 if (aEDelIter->first == myEntityMaxID)
724 myEntities.erase(myEntities.begin() + anEntPos);
725 // Remove such entity from myEntityMap
726 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
727 anEntMapIter = myEntityMap.begin();
728 for ( ; anEntMapIter != myEntityMap.end(); anEntMapIter++)
729 if (anEntMapIter->second == aEDelIter->first)
731 if (anEntMapIter != myEntityMap.end())
732 myEntityMap.erase(anEntMapIter);
738 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
739 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
740 const Slvs_hEntity& theEntityID)
742 // Search the position of the first parameter of the entity
743 int anEntPos = Search(theEntityID, myEntities);
744 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
746 // Look over supported types of entities
749 boost::shared_ptr<GeomDataAPI_Point> aPoint =
750 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
753 aPoint->setValue(myParams[aFirstParamPos].val,
754 myParams[aFirstParamPos+1].val,
755 myParams[aFirstParamPos+2].val);
760 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
761 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
764 aPoint2D->setValue(myParams[aFirstParamPos].val,
765 myParams[aFirstParamPos+1].val);
769 /// \todo Support other types of entities
772 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
773 boost::shared_ptr<ModelAPI_Attribute> theEntity)
775 if (myEntityMap.find(theEntity) != myEntityMap.end())
777 // If the attribute is a point and it is changed (the group needs to rebuild),
778 // probably user has dragged this point into this position,
779 // so it is necessary to add constraint which will guarantee the point will not change
781 // Store myNeedToSolve flag to verify the entity is really changed
782 bool aNeedToSolveCopy = myNeedToSolve;
783 myNeedToSolve = false;
785 changeEntity(theEntity);
787 if (myNeedToSolve) // the entity is changed
789 // Verify the entity is a point and add temporary constraint of permanency
790 boost::shared_ptr<GeomDataAPI_Point> aPoint =
791 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
792 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
793 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
794 if (aPoint || aPoint2D)
795 addTemporaryConstraintWhereDragged(theEntity);
798 // Restore flag of changes
799 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
803 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
804 boost::shared_ptr<ModelAPI_Attribute> theEntity)
806 // Find identifier of the entity
807 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
808 anEntIter = myEntityMap.find(theEntity);
810 // Create WHERE_DRAGGED constraint
811 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
812 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
813 myConstraints.push_back(aWDConstr);
814 myTempConstraints.push_back(aWDConstr.h);
817 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
819 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
820 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
822 int aConstrPos = Search(*aTmpConstrIter, myConstraints);
823 myConstraints.erase(myConstraints.begin() + aConstrPos);
825 // If the removing constraint has higher index, decrease the indexer
826 if (*aTmpConstrIter == myConstrMaxID)
829 myTempConstraints.clear();
834 // ========================================================
835 // ========= Auxiliary functions ===============
836 // ========================================================
838 template <typename T>
839 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
841 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
842 int aVecSize = theEntities.size();
843 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
845 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
848 aResIndex = aVecSize;