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>
19 #include <SketchPlugin_Arc.h>
20 #include <SketchPlugin_Circle.h>
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Point.h>
23 #include <SketchPlugin_Sketch.h>
28 /// Tolerance for value of parameters
29 const double tolerance = 1.e-10;
31 // Initialization of constraint manager self pointer
32 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
34 /// Global constraint manager object
35 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
37 /// This value is used to give unique index to the groups
38 static Slvs_hGroup myGroupIndexer = 0;
40 /** \brief Search the entity/parameter with specified ID in the list of elements
41 * \param[in] theEntityID unique ID of the element
42 * \param[in] theEntities list of elements
43 * \return position of the found element or -1 if the element is not found
46 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
50 // ========================================================
51 // ========= SketchSolver_ConstraintManager ===============
52 // ========================================================
53 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
56 _self = new SketchSolver_ConstraintManager();
60 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
64 // Register in event loop
65 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
66 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
67 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
68 Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_MOVED));
71 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
76 // ============================================================================
77 // Function: processEvent
78 // Class: SketchSolver_PluginManager
79 // Purpose: listen the event loop and process the message
80 // ============================================================================
81 void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
83 if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
84 theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED) ||
85 theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED))
87 const Model_FeatureUpdatedMessage* anUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
88 std::set< boost::shared_ptr<ModelAPI_Feature> > aFeatures = anUpdateMsg->features();
90 bool isModifiedEvt = theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED);
93 std::set< boost::shared_ptr<ModelAPI_Feature> >::iterator aFeatIter;
94 for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++)
96 // Only sketches and constraints can be added by Create event
97 const std::string& aFeatureKind = (*aFeatIter)->getKind();
98 if (aFeatureKind.compare("Sketch") == 0)
100 boost::shared_ptr<SketchPlugin_Feature> aSketch =
101 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
103 changeWorkplane(aSketch);
106 boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
107 boost::dynamic_pointer_cast<SketchPlugin_Constraint>(*aFeatIter);
109 changeConstraint(aConstraint);
112 // Sketch plugin features can be only updated
113 boost::shared_ptr<SketchPlugin_Feature> aFeature =
114 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
116 updateEntity(aFeature);
121 // Solve the set of constraints
122 resolveConstraints();
124 else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
126 const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
127 const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
129 // Find "Sketch" in groups. The constraint groups should be updated when an object removed from Sketch
130 std::set<std::string>::const_iterator aFGrIter;
131 for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
132 if (aFGrIter->compare("Sketch") == 0)
135 if (aFGrIter != aFeatureGroups.end())
137 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
138 while (aGroupIter != myGroups.end())
140 if ((*aGroupIter)->updateGroup())
141 { // the group should be removed
143 int aShift = aGroupIter - myGroups.begin();
144 myGroups.erase(aGroupIter);
145 aGroupIter = myGroups.begin() + aShift;
153 // ============================================================================
154 // Function: changeWorkplane
155 // Class: SketchSolver_PluginManager
156 // Purpose: update workplane by given parameters of the sketch
157 // ============================================================================
158 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
160 bool aResult = true; // changed when a workplane wrongly updated
161 bool isUpdated = false;
162 // Try to update specified workplane in all groups
163 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
164 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
165 if ((*aGroupIter)->isBaseWorkplane(theSketch))
168 if (!(*aGroupIter)->updateWorkplane())
171 // If the workplane is not updated, so this is a new workplane
174 SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
175 // Verify that the group is created successfully
176 if (!aNewGroup->isBaseWorkplane(theSketch))
181 myGroups.push_back(aNewGroup);
186 // ============================================================================
187 // Function: changeConstraint
188 // Class: SketchSolver_PluginManager
189 // Purpose: create/update the constraint and place it into appropriate group
190 // ============================================================================
191 bool SketchSolver_ConstraintManager::changeConstraint(
192 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
194 // Search the groups which this constraint touches
195 std::set<Slvs_hGroup> aGroups;
196 findGroups(theConstraint, aGroups);
198 // Process the groups list
199 if (aGroups.size() == 0)
200 { // There are no groups applicable for this constraint => create new one
201 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
202 if (!aWP) return false;
203 SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
204 if (!aGroup->changeConstraint(theConstraint))
209 myGroups.push_back(aGroup);
212 else if (aGroups.size() == 1)
213 { // Only one group => add constraint into it
214 Slvs_hGroup aGroupId = *(aGroups.begin());
215 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
216 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
217 if ((*aGroupIter)->getId() == aGroupId)
218 return (*aGroupIter)->changeConstraint(theConstraint);
220 else if (aGroups.size() > 1)
221 { // Several groups applicable for this constraint => need to merge them
222 std::set<Slvs_hGroup>::const_iterator aGroupsIter = aGroups.begin();
224 // Search first group
225 std::vector<SketchSolver_ConstraintGroup*>::iterator aFirstGroupIter;
226 for (aFirstGroupIter = myGroups.begin(); aFirstGroupIter != myGroups.end(); aFirstGroupIter++)
227 if ((*aFirstGroupIter)->getId() == *aGroupsIter)
229 if (aFirstGroupIter == myGroups.end())
232 // Append other groups to the first one
233 std::vector<SketchSolver_ConstraintGroup*>::iterator anOtherGroupIter = aFirstGroupIter + 1;
234 for (aGroupsIter++; aGroupsIter != aGroups.end(); aGroupsIter++)
236 for ( ; anOtherGroupIter != myGroups.end(); anOtherGroupIter++)
237 if ((*anOtherGroupIter)->getId() == *aGroupsIter)
239 if (anOtherGroupIter == myGroups.end())
240 { // Group disappears
241 anOtherGroupIter = aFirstGroupIter + 1;
245 (*aFirstGroupIter)->mergeGroups(**anOtherGroupIter);
246 int aShiftFirst = aFirstGroupIter - myGroups.begin();
247 int aShiftOther = anOtherGroupIter - myGroups.begin();
248 delete *anOtherGroupIter;
249 myGroups.erase(anOtherGroupIter);
250 aFirstGroupIter = myGroups.begin() + aShiftFirst;
251 anOtherGroupIter = myGroups.begin() + aShiftOther;
254 return (*aFirstGroupIter)->changeConstraint(theConstraint);
257 // Something goes wrong
261 // ============================================================================
262 // Function: updateEntity
263 // Class: SketchSolver_PluginManager
264 // Purpose: update any element on the sketch, which is used by constraints
265 // ============================================================================
266 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
268 // Create list of attributes depending on type of the feature
269 std::vector<std::string> anAttrList;
270 const std::string& aFeatureKind = theFeature->getKind();
272 if (aFeatureKind.compare("SketchPoint") == 0)
273 anAttrList.push_back(POINT_ATTR_COORD);
275 else if (aFeatureKind.compare("SketchLine") == 0)
277 anAttrList.push_back(LINE_ATTR_START);
278 anAttrList.push_back(LINE_ATTR_END);
281 else if (aFeatureKind.compare("SketchCircle") == 0)
283 anAttrList.push_back(CIRCLE_ATTR_CENTER);
284 anAttrList.push_back(CIRCLE_ATTR_RADIUS);
287 else if (aFeatureKind.compare("SketchArc") == 0)
289 anAttrList.push_back(ARC_ATTR_CENTER);
290 anAttrList.push_back(ARC_ATTR_START);
291 anAttrList.push_back(ARC_ATTR_END);
293 /// \todo Other types of features should be implemented
295 // Check changing of feature's attributes (go through the groups and search usage of the attributes)
296 std::vector<std::string>::const_iterator anAttrIter;
297 for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
299 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
300 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
302 boost::shared_ptr<ModelAPI_Attribute> anAttribute =
303 boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
304 (*aGroupIter)->updateEntityIfPossible(anAttribute);
310 // ============================================================================
311 // Function: findGroups
312 // Class: SketchSolver_PluginManager
313 // Purpose: search groups of entities interacting with given constraint
314 // ============================================================================
315 void SketchSolver_ConstraintManager::findGroups(
316 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
317 std::set<Slvs_hGroup>& theGroupIDs) const
319 boost::shared_ptr<SketchPlugin_Feature> aWP = findWorkplaneForConstraint(theConstraint);
321 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
322 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
323 if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
324 theGroupIDs.insert((*aGroupIter)->getId());
327 // ============================================================================
328 // Function: findWorkplaneForConstraint
329 // Class: SketchSolver_PluginManager
330 // Purpose: search workplane containing given constraint
331 // ============================================================================
332 boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
333 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
335 // Already verified workplanes
336 std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
338 std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
339 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
341 boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
342 if (aVerified.find(aWP) != aVerified.end())
345 boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
346 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
347 std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
348 std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
349 for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
350 if (*anIter == theConstraint)
351 return aWP; // workplane is found
352 aVerified.insert(aWP);
355 return boost::shared_ptr<SketchPlugin_Feature>();
358 // ============================================================================
359 // Function: resolveConstraints
360 // Class: SketchSolver_PluginManager
361 // Purpose: change entities according to available constraints
362 // ============================================================================
363 void SketchSolver_ConstraintManager::resolveConstraints()
365 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
366 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
367 (*aGroupIter)->resolveConstraints();
369 // Features may be updated => send events
370 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
375 // ========================================================
376 // ========= SketchSolver_ConstraintGroup ===============
377 // ========================================================
379 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
380 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
381 : myID(++myGroupIndexer),
386 myNeedToSolve(false),
391 myConstraints.clear();
393 // Initialize workplane
394 myWorkplane.h = SLVS_E_UNKNOWN;
396 assert(addWorkplane(theWorkplane));
398 addWorkplane(theWorkplane);
402 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
406 myConstraints.clear();
407 myConstraintMap.clear();
409 // If the group with maximal identifier is deleted, decrease the indexer
410 if (myID == myGroupIndexer)
414 // ============================================================================
415 // Function: isBaseWorkplane
416 // Class: SketchSolver_ConstraintGroup
417 // Purpose: verify the group is based on the given workplane
418 // ============================================================================
419 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
420 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
422 return theWorkplane == mySketch;
425 // ============================================================================
426 // Function: isInteract
427 // Class: SketchSolver_ConstraintGroup
428 // Purpose: verify are there any entities in the group used by given constraint
429 // ============================================================================
430 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
431 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
433 // Check the group is empty
434 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
437 // Go through constraint entities and verify if some of them already in the group
438 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
440 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
441 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
442 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
444 if (!aCAttrRef) continue;
445 if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
449 // Entities did not found
453 // ============================================================================
454 // Function: changeConstraint
455 // Class: SketchSolver_ConstraintGroup
456 // Purpose: create/update the constraint in the group
457 // ============================================================================
458 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
459 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
461 // There is no workplane yet, something wrong
462 if (myWorkplane.h == SLVS_E_UNKNOWN)
465 // Search this constraint in the current group to update it
466 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
467 aConstrMapIter = myConstraintMap.find(theConstraint);
468 std::vector<Slvs_Constraint>::iterator aConstrIter;
469 if (aConstrMapIter != myConstraintMap.end())
471 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
472 aConstrIter = myConstraints.begin() + aConstrPos;
475 // Get constraint type and verify the constraint parameters are correct
476 int aConstrType = getConstraintType(theConstraint);
477 if (aConstrType == SLVS_C_UNKNOWN ||
478 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
481 // Create constraint parameters
482 double aDistance = 0.0; // scalar value of the constraint
483 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
484 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
487 aDistance = aDistAttr->value();
488 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
490 myNeedToSolve = true;
491 aConstrIter->valA = aDistance;
495 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
496 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
498 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
499 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
500 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
501 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
503 if (!aConstrAttr) continue;
504 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
507 if (aConstrMapIter == myConstraintMap.end())
509 // Create SolveSpace constraint structure
510 Slvs_Constraint aConstraint =
511 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
512 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
513 myConstraints.push_back(aConstraint);
514 myConstraintMap[theConstraint] = aConstraint.h;
519 // ============================================================================
520 // Function: changeEntity
521 // Class: SketchSolver_ConstraintGroup
522 // Purpose: create/update the element affected by any constraint
523 // ============================================================================
524 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
525 boost::shared_ptr<ModelAPI_Attribute> theEntity)
527 // If the entity is already in the group, try to find it
528 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
529 aEntIter = myEntityMap.find(theEntity);
530 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
531 if (aEntIter == myEntityMap.end()) // no such entity => should be created
532 aParamIter = myParams.end();
534 { // the entity already exists
535 int aEntPos = Search(aEntIter->second, myEntities);
536 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
537 aParamIter = myParams.begin() + aParamPos;
540 // Look over supported types of entities
543 boost::shared_ptr<GeomDataAPI_Point> aPoint =
544 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
547 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
548 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
549 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
551 if (aEntIter != myEntityMap.end()) // the entity already exists
552 return aEntIter->second;
555 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
556 myEntities.push_back(aPtEntity);
557 myEntityMap[theEntity] = aPtEntity.h;
561 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
562 if (myWorkplane.h == SLVS_E_UNKNOWN)
563 return SLVS_E_UNKNOWN;
566 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
567 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
570 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
571 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
573 if (aEntIter != myEntityMap.end()) // the entity already exists
574 return aEntIter->second;
577 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
578 myEntities.push_back(aPt2DEntity);
579 myEntityMap[theEntity] = aPt2DEntity.h;
580 return aPt2DEntity.h;
583 // Scalar value (used for the distance entities)
584 boost::shared_ptr<ModelAPI_AttributeDouble> aScalar =
585 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
588 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
590 if (aEntIter != myEntityMap.end()) // the entity already exists
591 return aEntIter->second;
594 Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
595 myEntities.push_back(aDistance);
596 myEntityMap[theEntity] = aDistance.h;
600 // SketchPlugin features
601 boost::shared_ptr<SketchPlugin_Feature> aFeature =
602 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theEntity);
604 { // Verify the feature by its kind
605 const std::string& aFeatureKind = aFeature->getKind();
608 if (aFeatureKind.compare("SketchLine") == 0)
610 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(LINE_ATTR_START));
611 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(LINE_ATTR_END));
613 if (aEntIter != myEntityMap.end()) // the entity already exists
614 return aEntIter->second;
617 Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
618 myEntities.push_back(aLineEntity);
619 myEntityMap[theEntity] = aLineEntity.h;
620 return aLineEntity.h;
623 else if (aFeatureKind.compare("SketchCircle") == 0)
625 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_CENTER));
626 Slvs_hEntity aRadius = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
628 if (aEntIter != myEntityMap.end()) // the entity already exists
629 return aEntIter->second;
632 Slvs_Entity aCircleEntity =
633 Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter, myWorkplane.normal, aRadius);
634 myEntities.push_back(aCircleEntity);
635 myEntityMap[theEntity] = aCircleEntity.h;
636 return aCircleEntity.h;
639 else if (aFeatureKind.compare("SketchArc") == 0)
641 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(ARC_ATTR_CENTER));
642 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(ARC_ATTR_START));
643 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(ARC_ATTR_END));
645 if (aEntIter != myEntityMap.end()) // the entity already exists
646 return aEntIter->second;
648 Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID,
649 myWorkplane.h, myWorkplane.normal, aCenter, aStart, aEnd);
650 myEntities.push_back(anArcEntity);
651 myEntityMap[theEntity] = anArcEntity.h;
652 return anArcEntity.h;
654 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
655 else if (aFeatureKind.compare("SketchPoint") == 0)
657 Slvs_hEntity aPoint = changeEntity(aFeature->data()->attribute(POINT_ATTR_COORD));
659 if (aEntIter != myEntityMap.end()) // the entity already exists
660 return aEntIter->second;
662 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
663 myEntityMap[theEntity] = aPoint;
668 /// \todo Other types of entities
670 // Unsupported or wrong entity type
671 return SLVS_E_UNKNOWN;
674 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
675 boost::shared_ptr<ModelAPI_Attribute> theDirX,
676 boost::shared_ptr<ModelAPI_Attribute> theDirY,
677 boost::shared_ptr<ModelAPI_Attribute> theNorm)
679 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
680 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
681 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
682 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
683 if (!aDirX || !aDirY ||
684 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
685 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
686 return SLVS_E_UNKNOWN;
688 // quaternion parameters of normal vector
689 double qw, qx, qy, qz;
690 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
691 aDirY->x(), aDirY->y(), aDirY->z(),
693 double aNormCoord[4] = {qw, qx, qy, qz};
695 // Try to find existent normal
696 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
697 aEntIter = myEntityMap.find(theNorm);
698 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
699 if (aEntIter == myEntityMap.end()) // no such entity => should be created
700 aParamIter = myParams.end();
702 { // the entity already exists, update it
703 int aEntPos = Search(aEntIter->second, myEntities);
704 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
705 aParamIter = myParams.begin() + aParamPos;
708 // Change parameters of the normal
709 Slvs_hParam aNormParams[4];
710 for (int i = 0; i < 4; i++)
711 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
713 if (aEntIter != myEntityMap.end()) // the entity already exists
714 return aEntIter->second;
717 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
718 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
719 myEntities.push_back(aNormal);
720 myEntityMap[theNorm] = aNormal.h;
725 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
726 boost::shared_ptr<SketchPlugin_Feature> theSketch)
728 if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
729 return false; // the workplane already exists or the function parameter is not Sketch
731 mySketch = theSketch;
736 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
738 // Get parameters of workplane
739 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
740 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
741 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
742 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
743 // Transform them into SolveSpace format
744 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
745 if (!aNormalWP) return false;
746 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
747 if (!anOriginWP) return false;
752 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
753 // Workplane should be added to the list of entities
754 myEntities.push_back(myWorkplane);
760 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
761 const double& theParam,
762 std::vector<Slvs_Param>::const_iterator& thePrmIter)
764 if (thePrmIter != myParams.end())
765 { // Parameter should be updated
766 int aParamPos = thePrmIter - myParams.begin();
767 if (fabs(thePrmIter->val - theParam) > tolerance)
769 myNeedToSolve = true; // parameter is changed, need to resolve constraints
770 myParams[aParamPos].val = theParam;
773 return myParams[aParamPos].h;
776 // Newly created parameter
777 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
778 myParams.push_back(aParam);
779 myNeedToSolve = true;
780 // The list of parameters is changed, move iterator to the end of the list to avoid problems
781 thePrmIter = myParams.end();
785 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
786 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
788 const std::string& aConstraintKind = theConstraint->getKind();
789 // Constraint for coincidence of two points
790 if (aConstraintKind.compare("SketchConstraintCoincidence") == 0)
792 // Verify the constraint has only two attributes and they are points
793 int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point
794 int aPt3d = 0; // bit-mapped field, the same information for 3D points
795 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
797 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
798 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
799 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
801 if (!anAttr) continue;
802 // Verify the attribute is a 2D point
803 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
804 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
807 aPt2d |= (1 << indAttr);
810 // Verify the attribute is a 3D point
811 boost::shared_ptr<GeomDataAPI_Point> aPoint3D =
812 boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
815 aPt3d |= (1 << indAttr);
818 // Attribute neither 2D nor 3D point is not supported by this type of constraint
819 return SLVS_C_UNKNOWN;
821 // The constrained points should be in first and second positions,
822 // so the expected value of aPt2d or aPt3d is 3
823 if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
824 return SLVS_C_POINTS_COINCIDENT;
825 // Constraint parameters are wrong
826 return SLVS_C_UNKNOWN;
829 /// \todo Implement other kind of constrtaints
831 return SLVS_C_UNKNOWN;
834 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::resolveConstraints()
839 myConstrSolver.setGroupID(myID);
840 myConstrSolver.setParameters(myParams);
841 myConstrSolver.setEntities(myEntities);
842 myConstrSolver.setConstraints(myConstraints);
844 if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
845 { // solution succeeded, store results into correspondent attributes
846 // Obtain result into the same list of parameters
847 if (!myConstrSolver.getResult(myParams))
850 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
851 anEntIter = myEntityMap.begin();
852 for ( ; anEntIter != myEntityMap.end(); anEntIter++)
853 updateAttribute(anEntIter->first, anEntIter->second);
855 /// \todo Implement error handling
857 removeTemporaryConstraints();
858 myNeedToSolve = false;
861 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::mergeGroups(
862 const SketchSolver_ConstraintGroup& theGroup)
864 // If specified group is empty, no need to merge
865 if (theGroup.myConstraintMap.size() == 0)
868 // NOTE: The possibility, that some elements are placed into both groups, is around 0,
869 // so the objects should be copied with changing the indexes
871 // Maps between old and new indexes of SolveSpace elements:
872 std::map<Slvs_hParam, Slvs_hParam> aParamMap;
873 std::map<Slvs_hEntity, Slvs_hEntity> anEntityMap;
874 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
876 // Go through copying constraints
877 std::vector<Slvs_Constraint>::const_iterator aConstrIter = theGroup.myConstraints.begin();
878 for ( ; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
880 Slvs_Constraint aConstraintCopy = *aConstrIter;
881 // Go through constraint entities
882 Slvs_hEntity* anEntities[CONSTRAINT_ATTR_SIZE] = {
883 &(aConstraintCopy.ptA), &(aConstraintCopy.ptB),
884 &(aConstraintCopy.entityA), &(aConstraintCopy.entityB)
886 for (int indEnt = 0; indEnt < CONSTRAINT_ATTR_SIZE; indEnt++)
888 if (*(anEntities[indEnt]) == 0)
890 if (anEntityMap.find(*(anEntities[indEnt])) != anEntityMap.end())
891 { // entity is already copied
892 *(anEntities[indEnt]) = anEntityMap[*(anEntities[indEnt])];
897 Slvs_Entity anEntityCopy = theGroup.myEntities[Search(*(anEntities[indEnt]), theGroup.myEntities)];
898 // Go through entity parameters
899 const int aNbEntParams = 4; // maximal number of entity parameters
900 for (int indPrm = 0; indPrm < aNbEntParams; indPrm++)
902 if (anEntityCopy.param[indPrm] == 0)
904 if (aParamMap.find(anEntityCopy.param[indPrm]) != aParamMap.end())
906 anEntityCopy.param[indPrm] = aParamMap[anEntityCopy.param[indPrm]];
910 Slvs_Param aParamCopy = theGroup.myParams[Search(anEntityCopy.param[indPrm], theGroup.myParams)];
911 aParamMap[aParamCopy.h] = ++myParamMaxID;
912 aParamCopy.h = myParamMaxID;
913 myParams.push_back(aParamCopy);
916 anEntityMap[anEntityCopy.h] = ++myEntityMaxID;
917 anEntityCopy.h = myEntityMaxID;
918 myEntities.push_back(anEntityCopy);
919 *(anEntities[indEnt]) = anEntityCopy.h;
922 aConstraintCopy.h = ++myConstrMaxID;
923 myConstraints.push_back(aConstraintCopy);
924 aConstrMap[aConstrIter->h] = aConstraintCopy.h;
927 // Append maps of SketchPlugin to SolveSpace parameters
928 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
929 aSPConstrMapIter = theGroup.myConstraintMap.begin();
930 for ( ; aSPConstrMapIter!= theGroup.myConstraintMap.end(); aSPConstrMapIter++)
932 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(aSPConstrMapIter->second);
933 if (aFind != aConstrMap.end())
934 myConstraintMap[aSPConstrMapIter->first] = aFind->second;
937 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
938 aSPEntMapIter = theGroup.myEntityMap.begin();
939 for ( ; aSPEntMapIter != theGroup.myEntityMap.end(); aSPEntMapIter++) {
940 std::map<Slvs_hEntity, Slvs_hEntity>::iterator aFind = anEntityMap.find(aSPEntMapIter->second);
941 if (aFind != anEntityMap.end())
942 myEntityMap[aSPEntMapIter->first] = aFind->second;
945 // Add temporary constraints
946 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
947 for ( ; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++)
949 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(*aTempConstrIter);
950 if (aFind != aConstrMap.end())
951 myTempConstraints.push_back(aFind->second);
953 myTempConstraints.sort();
955 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
958 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateGroup()
960 // Check for valid sketch
961 if (!mySketch->data()->isValid())
964 // Fast check for constraint validity. If all constraints are valid, no need to update the group
965 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
966 aConstrIter = myConstraintMap.rbegin();
967 bool isAllValid = true;
968 for ( ; isAllValid && aConstrIter != myConstraintMap.rend(); aConstrIter++)
969 if (!aConstrIter->first->data()->isValid())
974 // Remove invalid constraints.
975 // There only constraint will be deleted (parameters and entities) will be removed below
976 std::list< boost::shared_ptr<SketchPlugin_Constraint> > aConstrToDelete;
977 std::map<Slvs_hEntity, bool> anEntToDelete; // entities will be removed if no valid constraints use them
978 for (aConstrIter = myConstraintMap.rbegin(); aConstrIter != myConstraintMap.rend(); aConstrIter++)
980 bool isValid = aConstrIter->first->data()->isValid();
982 int aConstrPos = Search(aConstrIter->second, myConstraints);
983 if (aConstrPos < (int)myConstraints.size())
985 Slvs_hEntity aConstrEnt[] = {
986 myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
987 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
988 for (int i = 0; i < 4; i++)
989 if (aConstrEnt[i] != SLVS_E_UNKNOWN)
991 if (anEntToDelete.find(aConstrEnt[i]) == anEntToDelete.end())
992 anEntToDelete[aConstrEnt[i]] = !isValid;
993 else if (isValid) // constraint is valid => no need to remove its entities
994 anEntToDelete[aConstrEnt[i]] = false;
998 myConstraints.erase(myConstraints.begin() + aConstrPos);
999 if (aConstrIter->second == myConstrMaxID) // When the constraint with highest ID is removed, decrease indexer
1001 aConstrToDelete.push_front(aConstrIter->first);
1005 std::list< boost::shared_ptr<SketchPlugin_Constraint> >::iterator aDelIter;
1006 for (aDelIter = aConstrToDelete.begin(); aDelIter != aConstrToDelete.end(); aDelIter++)
1007 myConstraintMap.erase(*aDelIter);
1009 // Remove invalid and unused entities
1010 std::map<Slvs_hEntity, bool>::reverse_iterator aEDelIter;
1011 for (aEDelIter = anEntToDelete.rbegin(); aEDelIter != anEntToDelete.rend(); aEDelIter++)
1012 if (aEDelIter->second)
1014 int anEntPos = Search(aEDelIter->first, myEntities);
1015 std::vector<Slvs_Entity>::iterator aEntIter = myEntities.begin() + anEntPos;
1016 // Number of parameters for the entity
1018 while (aEntIter->param[aNbParams]) aNbParams++;
1019 if (aNbParams == 0) continue;
1020 // Decrease parameter indexer if there are deleted parameter with higher IDs
1021 if (aEntIter->param[aNbParams-1] == myParamMaxID)
1022 myParamMaxID -= aNbParams;
1023 // Remove parameters of the entity
1024 int aParamPos = Search(aEntIter->param[0], myParams);
1025 myParams.erase(myParams.begin() + aParamPos,
1026 myParams.begin() + aParamPos + aNbParams);
1029 if (aEDelIter->first == myEntityMaxID)
1031 myEntities.erase(myEntities.begin() + anEntPos);
1032 // Remove such entity from myEntityMap
1033 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1034 anEntMapIter = myEntityMap.begin();
1035 for ( ; anEntMapIter != myEntityMap.end(); anEntMapIter++)
1036 if (anEntMapIter->second == aEDelIter->first)
1038 if (anEntMapIter != myEntityMap.end())
1039 myEntityMap.erase(anEntMapIter);
1045 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
1046 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
1047 const Slvs_hEntity& theEntityID)
1049 // Search the position of the first parameter of the entity
1050 int anEntPos = Search(theEntityID, myEntities);
1051 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
1053 // Look over supported types of entities
1056 boost::shared_ptr<GeomDataAPI_Point> aPoint =
1057 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
1060 aPoint->setValue(myParams[aFirstParamPos].val,
1061 myParams[aFirstParamPos+1].val,
1062 myParams[aFirstParamPos+2].val);
1067 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1068 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
1071 aPoint2D->setValue(myParams[aFirstParamPos].val,
1072 myParams[aFirstParamPos+1].val);
1076 /// \todo Support other types of entities
1079 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
1080 boost::shared_ptr<ModelAPI_Attribute> theEntity)
1082 if (myEntityMap.find(theEntity) != myEntityMap.end())
1084 // If the attribute is a point and it is changed (the group needs to rebuild),
1085 // probably user has dragged this point into this position,
1086 // so it is necessary to add constraint which will guarantee the point will not change
1088 // Store myNeedToSolve flag to verify the entity is really changed
1089 bool aNeedToSolveCopy = myNeedToSolve;
1090 myNeedToSolve = false;
1092 changeEntity(theEntity);
1094 if (myNeedToSolve) // the entity is changed
1096 // Verify the entity is a point and add temporary constraint of permanency
1097 boost::shared_ptr<GeomDataAPI_Point> aPoint =
1098 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
1099 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1100 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
1101 if (aPoint || aPoint2D)
1102 addTemporaryConstraintWhereDragged(theEntity);
1105 // Restore flag of changes
1106 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
1110 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
1111 boost::shared_ptr<ModelAPI_Attribute> theEntity)
1113 // Find identifier of the entity
1114 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
1115 anEntIter = myEntityMap.find(theEntity);
1117 // Create WHERE_DRAGGED constraint
1118 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1119 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1120 myConstraints.push_back(aWDConstr);
1121 myTempConstraints.push_back(aWDConstr.h);
1124 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
1126 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1127 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
1129 int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1130 myConstraints.erase(myConstraints.begin() + aConstrPos);
1132 // If the removing constraint has higher index, decrease the indexer
1133 if (*aTmpConstrIter == myConstrMaxID)
1136 myTempConstraints.clear();
1141 // ========================================================
1142 // ========= Auxiliary functions ===============
1143 // ========================================================
1145 template <typename T>
1146 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1148 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1149 int aVecSize = theEntities.size();
1150 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1152 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
1154 if (aResIndex == -1)
1155 aResIndex = aVecSize;