1 // File: SketchSolver_ConstraintGroup.cpp
2 // Created: 27 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_ConstraintGroup.h"
7 #include <SketchSolver_Constraint.h>
9 #include <Events_Error.h>
10 #include <Events_Loop.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_ResultConstruction.h>
21 #include <SketchPlugin_Constraint.h>
22 #include <SketchPlugin_ConstraintLength.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Circle.h>
27 #include <SketchPlugin_Line.h>
28 #include <SketchPlugin_Point.h>
29 #include <SketchPlugin_Sketch.h>
34 /// Tolerance for value of parameters
35 const double tolerance = 1.e-10;
38 * Collects all sketch solver error' codes
39 * as inline static functions
40 * TODO: Move this class into a separate file
42 class SketchSolver_Error
45 /// The value parameter for the constraint
46 inline static const std::string& CONSTRAINTS()
48 static const std::string MY_ERROR_VALUE("Conflicting constraints");
49 return MY_ERROR_VALUE;
53 /// This value is used to give unique index to the groups
54 static Slvs_hGroup myGroupIndexer = 0;
56 /** \brief Search the entity/parameter with specified ID in the list of elements
57 * \param[in] theEntityID unique ID of the element
58 * \param[in] theEntities list of elements
59 * \return position of the found element or -1 if the element is not found
62 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
64 // ========================================================
65 // ========= SketchSolver_ConstraintGroup ===============
66 // ========================================================
68 SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup(
69 boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
70 : myID(++myGroupIndexer),
80 myConstraints.clear();
82 myTempConstraints.clear();
83 myTempPointWhereDragged.clear();
84 myTempPointWDrgdID = 0;
86 // Initialize workplane
87 myWorkplane.h = SLVS_E_UNKNOWN;
89 assert(addWorkplane(theWorkplane));
91 addWorkplane(theWorkplane);
95 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
99 myConstraints.clear();
100 myConstraintMap.clear();
101 myTempConstraints.clear();
102 myTempPointWhereDragged.clear();
104 // If the group with maximal identifier is deleted, decrease the indexer
105 if (myID == myGroupIndexer)
109 // ============================================================================
110 // Function: isBaseWorkplane
111 // Class: SketchSolver_ConstraintGroup
112 // Purpose: verify the group is based on the given workplane
113 // ============================================================================
114 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
115 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
117 return theWorkplane == mySketch;
120 // ============================================================================
121 // Function: isInteract
122 // Class: SketchSolver_ConstraintGroup
123 // Purpose: verify are there any entities in the group used by given constraint
124 // ============================================================================
125 bool SketchSolver_ConstraintGroup::isInteract(
126 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
128 // Check the group is empty
129 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
132 // Go through constraint entities and verify if some of them already in the group
133 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++) {
134 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef = boost::dynamic_pointer_cast<
135 ModelAPI_AttributeRefAttr>(
136 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(i)));
139 if (!aCAttrRef->isObject() && myEntityAttrMap.find(aCAttrRef->attr()) != myEntityAttrMap.end())
141 if (aCAttrRef->isObject()) { // Obtain a base feature for the object
142 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
143 aCAttrRef->object());
146 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
147 FeaturePtr aFeature = aDoc->feature(aRC);
148 if (myEntityFeatMap.find(aFeature) != myEntityFeatMap.end())
150 // search attributes of a feature to be parameters of constraint
151 std::list<boost::shared_ptr<ModelAPI_Attribute> > aFeatAttrList =
152 aFeature->data()->attributes(std::string());
153 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator aFAIter = aFeatAttrList
155 for (; aFAIter != aFeatAttrList.end(); aFAIter++)
156 if (myEntityAttrMap.find(*aFAIter) != myEntityAttrMap.end())
161 // Entities did not found
165 // ============================================================================
166 // Function: checkConstraintConsistence
167 // Class: SketchSolver_ConstraintGroup
168 // Purpose: verifies and changes parameters of the constraint
169 // ============================================================================
170 void SketchSolver_ConstraintGroup::checkConstraintConsistence(Slvs_Constraint& theConstraint)
172 if (theConstraint.type == SLVS_C_PT_LINE_DISTANCE) {
173 // Get constraint parameters and check the sign of constraint value
176 int aPtPos = Search(theConstraint.ptA, myEntities);
177 int aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
178 boost::shared_ptr<GeomAPI_XY> aPoint(
179 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
182 int aLnPos = Search(theConstraint.entityA, myEntities);
183 aPtPos = Search(myEntities[aLnPos].point[0], myEntities);
184 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
185 boost::shared_ptr<GeomAPI_XY> aStart(
186 new GeomAPI_XY(-myParams[aPtParamPos].val, -myParams[aPtParamPos + 1].val));
187 aPtPos = Search(myEntities[aLnPos].point[1], myEntities);
188 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
189 boost::shared_ptr<GeomAPI_XY> aEnd(
190 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
192 aEnd = aEnd->added(aStart);
193 aPoint = aPoint->added(aStart);
194 if (aPoint->cross(aEnd) * theConstraint.valA < 0.0)
195 theConstraint.valA *= -1.0;
199 // ============================================================================
200 // Function: changeConstraint
201 // Class: SketchSolver_ConstraintGroup
202 // Purpose: create/update the constraint in the group
203 // ============================================================================
204 bool SketchSolver_ConstraintGroup::changeConstraint(
205 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
207 // There is no workplane yet, something wrong
208 if (myWorkplane.h == SLVS_E_UNKNOWN)
211 // Search this constraint in the current group to update it
212 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrMapIter =
213 myConstraintMap.find(theConstraint);
214 std::vector<Slvs_Constraint>::iterator aConstrIter;
215 if (aConstrMapIter != myConstraintMap.end()) {
216 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
217 aConstrIter = myConstraints.begin() + aConstrPos;
220 // Get constraint type and verify the constraint parameters are correct
221 SketchSolver_Constraint aConstraint(theConstraint);
222 int aConstrType = aConstraint.getType();
223 if (aConstrType == SLVS_C_UNKNOWN
224 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
226 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
228 // Create constraint parameters
229 double aDistance = 0.0; // scalar value of the constraint
230 AttributeDoublePtr aDistAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
231 theConstraint->data()->attribute(SketchPlugin_Constraint::VALUE()));
233 aDistance = aDistAttr->value();
234 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
235 if (aConstrType == SLVS_C_DIAMETER)
237 if (aConstrMapIter != myConstraintMap.end()
238 && fabs(aConstrIter->valA - aDistance) > tolerance) {
239 myNeedToSolve = true;
240 aConstrIter->valA = aDistance;
244 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
245 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
246 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
247 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = boost::dynamic_pointer_cast<
248 ModelAPI_AttributeRefAttr>(
249 theConstraint->data()->attribute(aConstraintAttributes[indAttr]));
253 // Convert the object of the attribute to the feature
255 if (aConstrAttr->isObject() && aConstrAttr->object()) {
256 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
257 aConstrAttr->object());
260 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
261 aFeature = aDoc->feature(aRC);
264 // For the length constraint the start and end points of the line should be added to the entities list instead of line
265 if (aConstrType == SLVS_C_PT_PT_DISTANCE
266 && theConstraint->getKind().compare(SketchPlugin_ConstraintLength::ID()) == 0) {
267 Slvs_hEntity aLineEnt = changeEntity(aFeature);
268 int aEntPos = Search(aLineEnt, myEntities);
269 aConstrEnt[indAttr++] = myEntities[aEntPos].point[0];
270 aConstrEnt[indAttr++] = myEntities[aEntPos].point[1];
271 while (indAttr < CONSTRAINT_ATTR_SIZE)
272 aConstrEnt[indAttr++] = 0;
273 break; // there should be no other entities
274 } else if (aConstrAttr->isObject())
275 aConstrEnt[indAttr] = changeEntity(aFeature);
277 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
280 if (aConstrMapIter == myConstraintMap.end()) {
281 // Several points may be coincident, it is not necessary to store all constraints between them.
282 // Try to find sequence of coincident points which connects the points of new constraint
283 if (aConstrType == SLVS_C_POINTS_COINCIDENT) {
284 if (aConstrEnt[0] == aConstrEnt[1]) // no need to add self coincidence
286 if (!addCoincidentPoints(aConstrEnt[0], aConstrEnt[1])) {
287 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
292 // Create SolveSpace constraint structure
293 Slvs_Constraint aConstraint = Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType,
294 myWorkplane.h, aDistance, aConstrEnt[0],
295 aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
296 myConstraints.push_back(aConstraint);
297 myConstraintMap[theConstraint] = aConstraint.h;
298 int aConstrPos = Search(aConstraint.h, myConstraints);
299 aConstrIter = myConstraints.begin() + aConstrPos;
302 checkConstraintConsistence(*aConstrIter);
306 // ============================================================================
307 // Function: changeEntity
308 // Class: SketchSolver_ConstraintGroup
309 // Purpose: create/update the element affected by any constraint
310 // ============================================================================
311 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
312 boost::shared_ptr<ModelAPI_Attribute> theEntity)
314 // If the entity is already in the group, try to find it
315 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
316 myEntityAttrMap.find(theEntity);
317 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
318 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
319 aParamIter = myParams.end();
320 else { // the entity already exists
321 int aEntPos = Search(aEntIter->second, myEntities);
322 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
323 aParamIter = myParams.begin() + aParamPos;
325 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
327 // Look over supported types of entities
330 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
333 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
334 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
335 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
338 return aEntIter->second;
341 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
342 myEntities.push_back(aPtEntity);
343 myEntityAttrMap[theEntity] = aPtEntity.h;
347 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
348 if (myWorkplane.h == SLVS_E_UNKNOWN)
349 return SLVS_E_UNKNOWN;
352 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
353 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
355 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
356 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
359 return aEntIter->second;
362 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
363 myEntities.push_back(aPt2DEntity);
364 myEntityAttrMap[theEntity] = aPt2DEntity.h;
365 return aPt2DEntity.h;
368 // Scalar value (used for the distance entities)
369 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
371 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
374 return aEntIter->second;
377 Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
378 myEntities.push_back(aDistance);
379 myEntityAttrMap[theEntity] = aDistance.h;
383 /// \todo Other types of entities
385 // Unsupported or wrong entity type
386 return SLVS_E_UNKNOWN;
389 // ============================================================================
390 // Function: changeEntity
391 // Class: SketchSolver_ConstraintGroup
392 // Purpose: create/update the element defined by the feature affected by any constraint
393 // ============================================================================
394 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(FeaturePtr theEntity)
396 // If the entity is already in the group, try to find it
397 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aEntIter = myEntityFeatMap.find(theEntity);
398 // defines that the entity already exists
399 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
401 // SketchPlugin features
402 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
403 SketchPlugin_Feature>(theEntity);
404 if (aFeature) { // Verify the feature by its kind
405 const std::string& aFeatureKind = aFeature->getKind();
408 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0) {
409 Slvs_hEntity aStart = changeEntity(
410 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
411 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
414 return aEntIter->second;
417 Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart,
419 myEntities.push_back(aLineEntity);
420 myEntityFeatMap[theEntity] = aLineEntity.h;
421 myNeedToSolve = true;
422 return aLineEntity.h;
425 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
426 Slvs_hEntity aCenter = changeEntity(
427 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
428 Slvs_hEntity aRadius = changeEntity(
429 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
432 return aEntIter->second;
435 Slvs_Entity aCircleEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
436 myWorkplane.normal, aRadius);
437 myEntities.push_back(aCircleEntity);
438 myEntityFeatMap[theEntity] = aCircleEntity.h;
439 myNeedToSolve = true;
440 return aCircleEntity.h;
443 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
444 Slvs_hEntity aCenter = changeEntity(
445 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
446 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
447 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
450 return aEntIter->second;
452 Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
453 myWorkplane.normal, aCenter, aStart, aEnd);
454 myEntities.push_back(anArcEntity);
455 myEntityFeatMap[theEntity] = anArcEntity.h;
456 myNeedToSolve = true;
457 return anArcEntity.h;
459 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
460 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
461 Slvs_hEntity aPoint = changeEntity(
462 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
465 return aEntIter->second;
467 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
468 myEntityFeatMap[theEntity] = aPoint;
469 myNeedToSolve = true;
474 /// \todo Other types of features
476 // Unsupported or wrong entity type
477 return SLVS_E_UNKNOWN;
480 // ============================================================================
481 // Function: changeNormal
482 // Class: SketchSolver_ConstraintGroup
483 // Purpose: create/update the normal of workplane
484 // ============================================================================
485 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
486 boost::shared_ptr<ModelAPI_Attribute> theDirX, boost::shared_ptr<ModelAPI_Attribute> theDirY,
487 boost::shared_ptr<ModelAPI_Attribute> theNorm)
489 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
490 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
491 if (!aDirX || !aDirY || (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(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
499 double aNormCoord[4] = { qw, qx, qy, qz };
501 // Try to find existent normal
502 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
503 myEntityAttrMap.find(theNorm);
504 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
505 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
506 aParamIter = myParams.end();
507 else { // the entity already exists, update it
508 int aEntPos = Search(aEntIter->second, myEntities);
509 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
510 aParamIter = myParams.begin() + aParamPos;
513 // Change parameters of the normal
514 Slvs_hParam aNormParams[4];
515 for (int i = 0; i < 4; i++)
516 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
518 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
519 return aEntIter->second;
522 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
523 aNormParams[2], aNormParams[3]);
524 myEntities.push_back(aNormal);
525 myEntityAttrMap[theNorm] = aNormal.h;
529 // ============================================================================
530 // Function: addWorkplane
531 // Class: SketchSolver_ConstraintGroup
532 // Purpose: create workplane for the group
533 // ============================================================================
534 bool SketchSolver_ConstraintGroup::addWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
536 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
537 return false; // the workplane already exists or the function parameter is not Sketch
539 mySketch = theSketch;
544 // ============================================================================
545 // Function: updateWorkplane
546 // Class: SketchSolver_ConstraintGroup
547 // Purpose: update parameters of workplane
548 // ============================================================================
549 bool SketchSolver_ConstraintGroup::updateWorkplane()
551 // Get parameters of workplane
552 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
553 SketchPlugin_Sketch::DIRX_ID());
554 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
555 SketchPlugin_Sketch::DIRY_ID());
556 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
557 SketchPlugin_Sketch::NORM_ID());
558 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
559 SketchPlugin_Sketch::ORIGIN_ID());
560 // Transform them into SolveSpace format
561 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
564 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
568 if (!myWorkplane.h) {
570 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
571 // Workplane should be added to the list of entities
572 myEntities.push_back(myWorkplane);
577 // ============================================================================
578 // Function: changeParameter
579 // Class: SketchSolver_ConstraintGroup
580 // Purpose: create/update value of parameter
581 // ============================================================================
582 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
583 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
585 if (thePrmIter != myParams.end()) { // Parameter should be updated
586 int aParamPos = thePrmIter - myParams.begin();
587 if (fabs(thePrmIter->val - theParam) > tolerance) {
588 myNeedToSolve = true; // parameter is changed, need to resolve constraints
589 myParams[aParamPos].val = theParam;
592 return myParams[aParamPos].h;
595 // Newly created parameter
596 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
597 myParams.push_back(aParam);
598 myNeedToSolve = true;
599 // The list of parameters is changed, move iterator to the end of the list to avoid problems
600 thePrmIter = myParams.end();
604 // ============================================================================
605 // Function: resolveConstraints
606 // Class: SketchSolver_ConstraintGroup
607 // Purpose: solve the set of constraints for the current group
608 // ============================================================================
609 bool SketchSolver_ConstraintGroup::resolveConstraints()
614 myConstrSolver.setGroupID(myID);
615 myConstrSolver.setParameters(myParams);
616 myConstrSolver.setEntities(myEntities);
617 myConstrSolver.setConstraints(myConstraints);
618 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
620 int aResult = myConstrSolver.solve();
621 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
622 // Obtain result into the same list of parameters
623 if (!myConstrSolver.getResult(myParams))
626 // We should go through the attributes map, because only attributes have valued parameters
627 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
628 myEntityAttrMap.begin();
629 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
630 if (updateAttribute(anEntIter->first, anEntIter->second))
631 updateRelatedConstraints(anEntIter->first);
632 } else if (!myConstraints.empty())
633 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
635 removeTemporaryConstraints();
636 myNeedToSolve = false;
640 // ============================================================================
641 // Function: mergeGroups
642 // Class: SketchSolver_ConstraintGroup
643 // Purpose: append specified group to the current group
644 // ============================================================================
645 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
647 // If specified group is empty, no need to merge
648 if (theGroup.myConstraintMap.empty())
651 // Map between old and new indexes of SolveSpace constraints
652 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
654 // Add all constraints from theGroup to the current group
655 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
656 theGroup.myConstraintMap.begin();
657 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
658 if (changeConstraint(aConstrIter->first))
659 aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
661 // Add temporary constraints from theGroup
662 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
663 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
664 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
666 if (aFind != aConstrMap.end())
667 myTempConstraints.push_back(aFind->second);
670 if (myTempPointWhereDragged.empty())
671 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
672 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
673 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
674 theGroup.myEntityAttrMap.begin();
675 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
676 if (aFeatureIter->second == myTempPointWDrgdID) {
677 addTemporaryConstraintWhereDragged(aFeatureIter->first);
682 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
685 // ============================================================================
686 // Function: splitGroup
687 // Class: SketchSolver_ConstraintGroup
688 // Purpose: divide the group into several subgroups
689 // ============================================================================
690 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
692 // Divide constraints and entities into several groups
693 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
694 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
695 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
696 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
697 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
698 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
699 aConstrIter->entityB };
700 std::vector<int> anIndexes;
701 // Go through the groupped entities and find even one of entities of current constraint
702 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
703 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
704 bool isFound = false;
705 for (int i = 0; i < 4 && !isFound; i++)
706 if (aConstrEnt[i] != 0)
707 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
709 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
711 // Add new group if no one is found
712 if (anIndexes.empty()) {
713 std::set<Slvs_hEntity> aNewGrEnt;
714 for (int i = 0; i < 4; i++)
715 if (aConstrEnt[i] != 0)
716 aNewGrEnt.insert(aConstrEnt[i]);
717 std::set<Slvs_hConstraint> aNewGrConstr;
718 aNewGrConstr.insert(aConstrIter->h);
720 aGroupsEntities.push_back(aNewGrEnt);
721 aGroupsConstr.push_back(aNewGrConstr);
722 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
723 aMaxNbEntities = aGroupsEntities.size() - 1;
724 } else if (anIndexes.size() == 1) { // Add entities indexes into the found group
725 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
726 for (int i = 0; i < 4; i++)
727 if (aConstrEnt[i] != 0)
728 aGrEntIter->insert(aConstrEnt[i]);
729 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
730 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
731 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
732 } else { // There are found several connected groups, merge them
733 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
735 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
737 std::vector<int>::iterator anInd = anIndexes.begin();
738 for (++anInd; anInd != anIndexes.end(); anInd++) {
739 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
740 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
742 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
743 aMaxNbEntities = anIndexes.front();
744 // Remove merged groups
745 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
746 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
747 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
752 if (aGroupsEntities.size() <= 1)
755 // Remove the group with maximum elements as it will be left in the current group
756 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
757 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
759 // Add new groups of constraints and divide current group
760 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
761 for (int i = aGroupsEntities.size(); i > 0; i--) {
762 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
763 aNewGroups.push_back(aG);
765 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrMapIter =
766 myConstraintMap.begin();
767 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
768 while (aConstrMapIter != myConstraintMap.end()) {
769 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
770 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
771 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
772 if (aGIter->find(aConstrMapIter->second) != aGIter->end()) {
773 (*aGroup)->changeConstraint(aConstrMapIter->first);
774 removeConstraint(aConstrMapIter->first);
776 aConstrMapIter = myConstraintMap.begin();
777 for (int i = 0; i < aConstrMapPos; i++)
781 if (aGIter == aGroupsConstr.end()) {
787 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
790 // ============================================================================
791 // Function: updateGroup
792 // Class: SketchSolver_ConstraintGroup
793 // Purpose: search removed entities and constraints
794 // ============================================================================
795 bool SketchSolver_ConstraintGroup::updateGroup()
797 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator aConstrIter =
798 myConstraintMap.rbegin();
799 bool isAllValid = true;
800 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
801 while (isAllValid && aConstrIter != myConstraintMap.rend()) {
802 if (!aConstrIter->first->data() || !aConstrIter->first->data()->isValid()) {
803 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
805 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator aCopyIter =
807 removeConstraint(aCopyIter->first);
813 // Probably, need to update coincidence constraints
814 if (isCCRemoved && !myExtraCoincidence.empty()) {
815 // Make a copy, because the new list of unused constrtaints will be generated
816 std::set<boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
817 myExtraCoincidence.clear();
819 std::set<boost::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
820 for (; aCIter != anExtraCopy.end(); aCIter++)
821 if ((*aCIter)->data() && (*aCIter)->data()->isValid())
822 changeConstraint(*aCIter);
828 // ============================================================================
829 // Function: updateAttribute
830 // Class: SketchSolver_ConstraintGroup
831 // Purpose: update features of sketch after resolving constraints
832 // ============================================================================
833 bool SketchSolver_ConstraintGroup::updateAttribute(
834 boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
836 // Search the position of the first parameter of the entity
837 int anEntPos = Search(theEntityID, myEntities);
838 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
840 // Look over supported types of entities
843 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
846 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
847 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
848 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
849 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
850 myParams[aFirstParamPos + 2].val);
857 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
858 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
860 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
861 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
862 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
869 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
871 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
872 aScalar->setValue(myParams[aFirstParamPos].val);
878 /// \todo Support other types of entities
882 // ============================================================================
883 // Function: updateEntityIfPossible
884 // Class: SketchSolver_ConstraintGroup
885 // Purpose: search the entity in this group and update it
886 // ============================================================================
887 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
888 boost::shared_ptr<ModelAPI_Attribute> theEntity)
890 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
891 // If the attribute is a point and it is changed (the group needs to rebuild),
892 // probably user has dragged this point into this position,
893 // so it is necessary to add constraint which will guarantee the point will not change
895 // Store myNeedToSolve flag to verify the entity is really changed
896 bool aNeedToSolveCopy = myNeedToSolve;
897 myNeedToSolve = false;
899 changeEntity(theEntity);
901 if (myNeedToSolve) // the entity is changed
903 // Verify the entity is a point and add temporary constraint of permanency
904 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
906 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = boost::dynamic_pointer_cast<
907 GeomDataAPI_Point2D>(theEntity);
908 if (aPoint || aPoint2D)
909 addTemporaryConstraintWhereDragged(theEntity);
912 // Restore flag of changes
913 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
916 updateRelatedConstraints(theEntity);
920 // ============================================================================
921 // Function: addTemporaryConstraintWhereDragged
922 // Class: SketchSolver_ConstraintGroup
923 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
924 // which was moved by user
925 // ============================================================================
926 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
927 boost::shared_ptr<ModelAPI_Attribute> theEntity)
929 // Find identifier of the entity
930 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
931 myEntityAttrMap.find(theEntity);
932 if (anEntIter == myEntityAttrMap.end())
935 // If this is a first dragged point, its parameters should be placed
936 // into Slvs_System::dragged field to avoid system inconsistense
937 if (myTempPointWhereDragged.empty()) {
938 int anEntPos = Search(anEntIter->second, myEntities);
939 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
940 for (int i = 0; i < 4; i++, aDraggedParam++)
941 if (*aDraggedParam != 0)
942 myTempPointWhereDragged.push_back(*aDraggedParam);
943 myTempPointWDrgdID = myEntities[anEntPos].h;
947 // Get identifiers of all dragged points
948 std::set<Slvs_hEntity> aDraggedPntID;
949 aDraggedPntID.insert(myTempPointWDrgdID);
950 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
951 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
952 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
953 if (aConstrPos < myConstraints.size())
954 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
956 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
957 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
958 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
959 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
960 continue; // the entity was not found in current set
962 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
963 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
964 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
965 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
966 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
969 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
970 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
971 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
972 myConstraints.push_back(aWDConstr);
973 myTempConstraints.push_back(aWDConstr.h);
976 // ============================================================================
977 // Function: removeTemporaryConstraints
978 // Class: SketchSolver_ConstraintGroup
979 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
980 // resolving the set of constraints
981 // ============================================================================
982 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
984 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
985 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
987 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
988 if (aConstrPos >= myConstraints.size())
990 myConstraints.erase(myConstraints.begin() + aConstrPos);
992 // If the removing constraint has higher index, decrease the indexer
993 if (*aTmpConstrIter == myConstrMaxID)
996 myTempConstraints.clear();
998 // Clear basic dragged point
999 myTempPointWhereDragged.clear();
1002 // ============================================================================
1003 // Function: removeConstraint
1004 // Class: SketchSolver_ConstraintGroup
1005 // Purpose: remove constraint and all unused entities
1006 // ============================================================================
1007 void SketchSolver_ConstraintGroup::removeConstraint(
1008 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1010 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator anIterToRemove =
1011 myConstraintMap.find(theConstraint);
1012 if (anIterToRemove == myConstraintMap.end())
1015 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
1016 // Remove constraint from the map
1017 myConstraintMap.erase(anIterToRemove);
1019 // Find unused entities
1020 int aConstrPos = Search(aCnstrToRemove, myConstraints);
1021 std::set<Slvs_hEntity> anEntToRemove;
1022 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1023 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1024 for (int i = 0; i < 4; i++)
1025 if (aCnstEnt[i] != 0)
1026 anEntToRemove.insert(aCnstEnt[i]);
1027 myConstraints.erase(myConstraints.begin() + aConstrPos);
1028 if (aCnstrToRemove == myConstrMaxID)
1030 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1031 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1032 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1034 for (int i = 0; i < 4; i++)
1035 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1036 anEntToRemove.erase(aEnts[i]);
1039 if (anEntToRemove.empty())
1042 // Remove unused entities
1043 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1044 myEntityAttrMap.begin();
1045 while (anEntAttrIter != myEntityAttrMap.end()) {
1046 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1047 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1050 myEntityAttrMap.erase(aRemovedIter);
1054 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1055 while (anEntFeatIter != myEntityFeatMap.end()) {
1056 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1057 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1059 myEntityFeatMap.erase(aRemovedIter);
1063 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1064 for (; aRemIter != anEntToRemove.rend(); aRemIter++) {
1065 unsigned int anEntPos = Search(*aRemIter, myEntities);
1066 if (anEntPos >= myEntities.size())
1068 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1069 if (aParamPos >= myParams.size())
1072 while (myEntities[anEntPos].param[aNbParams] != 0)
1074 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1075 myParamMaxID -= aNbParams;
1076 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1077 if (*aRemIter == myEntityMaxID)
1079 myEntities.erase(myEntities.begin() + anEntPos);
1081 // Remove entity's ID from the lists of conincident points
1082 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1083 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1084 aCoPtIter->erase(*aRemIter);
1086 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1087 myCoincidentPoints.clear();
1090 // ============================================================================
1091 // Function: addCoincidentPoints
1092 // Class: SketchSolver_ConstraintGroup
1093 // Purpose: add coincident point the appropriate list of such points
1094 // ============================================================================
1095 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1096 const Slvs_hEntity& thePoint2)
1098 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1099 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1100 while (aCoPtIter != myCoincidentPoints.end()) {
1101 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1102 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1103 != aCoPtIter->end(), };
1104 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1106 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1107 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1108 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1109 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1110 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1111 myCoincidentPoints.erase(aCoPtIter);
1112 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1113 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1116 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1117 aFirstFound = aCoPtIter;
1122 // No points were found, need to create new set
1123 if (aFirstFound == myCoincidentPoints.end()) {
1124 std::set<Slvs_hEntity> aNewSet;
1125 aNewSet.insert(thePoint1);
1126 aNewSet.insert(thePoint2);
1127 myCoincidentPoints.push_back(aNewSet);
1133 // ============================================================================
1134 // Function: updateRelatedConstraints
1135 // Class: SketchSolver_ConstraintGroup
1136 // Purpose: emit the signal to update constraints
1137 // ============================================================================
1138 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1139 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1141 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1142 myConstraintMap.begin();
1143 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1144 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1145 ->attributes(std::string());
1147 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1148 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1149 bool isUpd = (*anAttrIter == theEntity);
1150 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1151 ModelAPI_AttributeRefAttr>(*anAttrIter);
1152 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1156 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1157 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1164 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1165 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1167 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1168 myConstraintMap.begin();
1169 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1170 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1171 ->attributes(std::string());
1173 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1174 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1175 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1176 ModelAPI_AttributeRefAttr>(*anAttrIter);
1177 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1178 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1179 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1186 // ========================================================
1187 // ========= Auxiliary functions ===============
1188 // ========================================================
1190 template<typename T>
1191 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1193 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1194 int aVecSize = theEntities.size();
1195 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1197 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1199 if (aResIndex == -1)
1200 aResIndex = aVecSize;