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 {
44 /// The value parameter for the constraint
45 inline static const std::string& CONSTRAINTS()
47 static const std::string MY_ERROR_VALUE("Conflicting constraints");
48 return MY_ERROR_VALUE;
52 /// This value is used to give unique index to the groups
53 static Slvs_hGroup myGroupIndexer = 0;
55 /** \brief Search the entity/parameter with specified ID in the list of elements
56 * \param[in] theEntityID unique ID of the element
57 * \param[in] theEntities list of elements
58 * \return position of the found element or -1 if the element is not found
61 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
64 // ========================================================
65 // ========= SketchSolver_ConstraintGroup ===============
66 // ========================================================
68 SketchSolver_ConstraintGroup::
69 SketchSolver_ConstraintGroup(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++)
135 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
136 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
137 theConstraint->data()->attribute(SketchPlugin_Constraint::ATTRIBUTE(i))
139 if (!aCAttrRef) continue;
140 if (!aCAttrRef->isObject() &&
141 myEntityAttrMap.find(aCAttrRef->attr()) != myEntityAttrMap.end())
143 if (aCAttrRef->isObject())
144 { // Obtain a base feature for the object
145 ResultConstructionPtr aRC =
146 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aCAttrRef->object());
148 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
149 FeaturePtr aFeature = aDoc->feature(aRC);
150 if (myEntityFeatMap.find(aFeature) != myEntityFeatMap.end())
152 // search attributes of a feature to be parameters of constraint
153 std::list< boost::shared_ptr<ModelAPI_Attribute> >
154 aFeatAttrList = aFeature->data()->attributes(std::string());
155 std::list< boost::shared_ptr<ModelAPI_Attribute> >::const_iterator
156 aFAIter = aFeatAttrList.begin();
157 for ( ; aFAIter != aFeatAttrList.end(); aFAIter++)
158 if (myEntityAttrMap.find(*aFAIter) != myEntityAttrMap.end())
163 // Entities did not found
167 // ============================================================================
168 // Function: checkConstraintConsistence
169 // Class: SketchSolver_ConstraintGroup
170 // Purpose: verifies and changes parameters of the constraint
171 // ============================================================================
172 void SketchSolver_ConstraintGroup::checkConstraintConsistence(Slvs_Constraint& theConstraint)
174 if (theConstraint.type == SLVS_C_PT_LINE_DISTANCE)
176 // Get constraint parameters and check the sign of constraint value
179 int aPtPos = Search(theConstraint.ptA, myEntities);
180 int aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
181 boost::shared_ptr<GeomAPI_XY> aPoint(
182 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos+1].val));
185 int aLnPos = Search(theConstraint.entityA, myEntities);
186 aPtPos = Search(myEntities[aLnPos].point[0], myEntities);
187 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
188 boost::shared_ptr<GeomAPI_XY> aStart(
189 new GeomAPI_XY(-myParams[aPtParamPos].val, -myParams[aPtParamPos+1].val));
190 aPtPos = Search(myEntities[aLnPos].point[1], myEntities);
191 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
192 boost::shared_ptr<GeomAPI_XY> aEnd(
193 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos+1].val));
195 aEnd = aEnd->added(aStart);
196 aPoint = aPoint->added(aStart);
197 if (aPoint->cross(aEnd) * theConstraint.valA < 0.0)
198 theConstraint.valA *= -1.0;
202 // ============================================================================
203 // Function: changeConstraint
204 // Class: SketchSolver_ConstraintGroup
205 // Purpose: create/update the constraint in the group
206 // ============================================================================
207 bool SketchSolver_ConstraintGroup::changeConstraint(
208 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
210 // There is no workplane yet, something wrong
211 if (myWorkplane.h == SLVS_E_UNKNOWN)
214 // Search this constraint in the current group to update it
215 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
216 aConstrMapIter = myConstraintMap.find(theConstraint);
217 std::vector<Slvs_Constraint>::iterator aConstrIter;
218 if (aConstrMapIter != myConstraintMap.end())
220 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
221 aConstrIter = myConstraints.begin() + aConstrPos;
224 // Get constraint type and verify the constraint parameters are correct
225 SketchSolver_Constraint aConstraint(theConstraint);
226 int aConstrType = aConstraint.getType();
227 if (aConstrType == SLVS_C_UNKNOWN ||
228 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
230 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
232 // Create constraint parameters
233 double aDistance = 0.0; // scalar value of the constraint
234 AttributeDoublePtr aDistAttr =
235 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(SketchPlugin_Constraint::VALUE()));
238 aDistance = aDistAttr->value();
239 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
240 if (aConstrType == SLVS_C_DIAMETER)
242 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
244 myNeedToSolve = true;
245 aConstrIter->valA = aDistance;
249 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
250 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
252 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
253 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
254 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
255 theConstraint->data()->attribute(aConstraintAttributes[indAttr])
257 if (!aConstrAttr) continue;
259 // Convert the object of the attribute to the feature
261 if (aConstrAttr->isObject() && aConstrAttr->object())
263 ResultConstructionPtr aRC =
264 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aConstrAttr->object());
266 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
267 aFeature = aDoc->feature(aRC);
270 // For the length constraint the start and end points of the line should be added to the entities list instead of line
271 if (aConstrType == SLVS_C_PT_PT_DISTANCE && theConstraint->getKind().compare(SketchPlugin_ConstraintLength::ID()) == 0)
273 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
274 aConstrEnt[indAttr] = changeEntity(aData->attribute(SketchPlugin_Line::START_ID()));
275 aConstrEnt[indAttr+1] = changeEntity(aData->attribute(SketchPlugin_Line::END_ID()));
276 // measured object is added into the map of objects to avoid problems with interaction between constraint and group
277 myEntityFeatMap[aFeature] = 0;
278 break; // there should be no other entities
280 else if (aConstrAttr->isObject())
281 aConstrEnt[indAttr] = changeEntity(aFeature);
283 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
286 if (aConstrMapIter == myConstraintMap.end())
288 // Several points may be coincident, it is not necessary to store all constraints between them.
289 // Try to find sequence of coincident points which connects the points of new constraint
290 if (aConstrType == SLVS_C_POINTS_COINCIDENT)
292 if (aConstrEnt[0] == aConstrEnt[1]) // no need to add self coincidence
294 if (!addCoincidentPoints(aConstrEnt[0], aConstrEnt[1]))
296 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
301 // Create SolveSpace constraint structure
302 Slvs_Constraint aConstraint =
303 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
304 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
305 myConstraints.push_back(aConstraint);
306 myConstraintMap[theConstraint] = aConstraint.h;
307 int aConstrPos = Search(aConstraint.h, myConstraints);
308 aConstrIter = myConstraints.begin() + aConstrPos;
311 checkConstraintConsistence(*aConstrIter);
315 // ============================================================================
316 // Function: changeEntity
317 // Class: SketchSolver_ConstraintGroup
318 // Purpose: create/update the element affected by any constraint
319 // ============================================================================
320 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
321 boost::shared_ptr<ModelAPI_Attribute> theEntity)
323 // If the entity is already in the group, try to find it
324 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
325 aEntIter = myEntityAttrMap.find(theEntity);
326 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
327 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
328 aParamIter = myParams.end();
330 { // the entity already exists
331 int aEntPos = Search(aEntIter->second, myEntities);
332 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
333 aParamIter = myParams.begin() + aParamPos;
335 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
337 // Look over supported types of entities
340 boost::shared_ptr<GeomDataAPI_Point> aPoint =
341 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
344 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
345 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
346 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
349 return aEntIter->second;
352 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
353 myEntities.push_back(aPtEntity);
354 myEntityAttrMap[theEntity] = aPtEntity.h;
358 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
359 if (myWorkplane.h == SLVS_E_UNKNOWN)
360 return SLVS_E_UNKNOWN;
363 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
364 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
367 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
368 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
371 return aEntIter->second;
374 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
375 myEntities.push_back(aPt2DEntity);
376 myEntityAttrMap[theEntity] = aPt2DEntity.h;
377 return aPt2DEntity.h;
380 // Scalar value (used for the distance entities)
381 AttributeDoublePtr aScalar =
382 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
385 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
388 return aEntIter->second;
391 Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
392 myEntities.push_back(aDistance);
393 myEntityAttrMap[theEntity] = aDistance.h;
397 /// \todo Other types of entities
399 // Unsupported or wrong entity type
400 return SLVS_E_UNKNOWN;
404 // ============================================================================
405 // Function: changeEntity
406 // Class: SketchSolver_ConstraintGroup
407 // Purpose: create/update the element defined by the feature affected by any constraint
408 // ============================================================================
409 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
410 FeaturePtr theEntity)
412 // If the entity is already in the group, try to find it
413 std::map<FeaturePtr, Slvs_hEntity>::const_iterator
414 aEntIter = myEntityFeatMap.find(theEntity);
415 // defines that the entity already exists
416 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
418 // SketchPlugin features
419 boost::shared_ptr<SketchPlugin_Feature> aFeature =
420 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theEntity);
422 { // Verify the feature by its kind
423 const std::string& aFeatureKind = aFeature->getKind();
426 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0)
428 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
429 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
432 return aEntIter->second;
435 Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
436 myEntities.push_back(aLineEntity);
437 myEntityFeatMap[theEntity] = aLineEntity.h;
438 return aLineEntity.h;
441 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0)
443 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
444 Slvs_hEntity aRadius = changeEntity(aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
447 return aEntIter->second;
450 Slvs_Entity aCircleEntity =
451 Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter, myWorkplane.normal, aRadius);
452 myEntities.push_back(aCircleEntity);
453 myEntityFeatMap[theEntity] = aCircleEntity.h;
454 return aCircleEntity.h;
457 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0)
459 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
460 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
461 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
464 return aEntIter->second;
466 Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID,
467 myWorkplane.h, myWorkplane.normal, aCenter, aStart, aEnd);
468 myEntities.push_back(anArcEntity);
469 myEntityFeatMap[theEntity] = anArcEntity.h;
470 return anArcEntity.h;
472 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
473 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0)
475 Slvs_hEntity aPoint = changeEntity(aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
478 return aEntIter->second;
480 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
481 myEntityFeatMap[theEntity] = aPoint;
486 /// \todo Other types of features
488 // Unsupported or wrong entity type
489 return SLVS_E_UNKNOWN;
492 // ============================================================================
493 // Function: changeNormal
494 // Class: SketchSolver_ConstraintGroup
495 // Purpose: create/update the normal of workplane
496 // ============================================================================
497 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
498 boost::shared_ptr<ModelAPI_Attribute> theDirX,
499 boost::shared_ptr<ModelAPI_Attribute> theDirY,
500 boost::shared_ptr<ModelAPI_Attribute> theNorm)
502 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
503 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
504 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
505 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
506 if (!aDirX || !aDirY ||
507 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
508 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
509 return SLVS_E_UNKNOWN;
511 // quaternion parameters of normal vector
512 double qw, qx, qy, qz;
513 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
514 aDirY->x(), aDirY->y(), aDirY->z(),
516 double aNormCoord[4] = {qw, qx, qy, qz};
518 // Try to find existent normal
519 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
520 aEntIter = myEntityAttrMap.find(theNorm);
521 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
522 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
523 aParamIter = myParams.end();
525 { // the entity already exists, update it
526 int aEntPos = Search(aEntIter->second, myEntities);
527 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
528 aParamIter = myParams.begin() + aParamPos;
531 // Change parameters of the normal
532 Slvs_hParam aNormParams[4];
533 for (int i = 0; i < 4; i++)
534 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
536 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
537 return aEntIter->second;
540 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
541 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
542 myEntities.push_back(aNormal);
543 myEntityAttrMap[theNorm] = aNormal.h;
548 // ============================================================================
549 // Function: addWorkplane
550 // Class: SketchSolver_ConstraintGroup
551 // Purpose: create workplane for the group
552 // ============================================================================
553 bool SketchSolver_ConstraintGroup::addWorkplane(
554 boost::shared_ptr<SketchPlugin_Feature> theSketch)
556 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
557 return false; // the workplane already exists or the function parameter is not Sketch
559 mySketch = theSketch;
564 // ============================================================================
565 // Function: updateWorkplane
566 // Class: SketchSolver_ConstraintGroup
567 // Purpose: update parameters of workplane
568 // ============================================================================
569 bool SketchSolver_ConstraintGroup::updateWorkplane()
571 // Get parameters of workplane
572 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID());
573 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID());
574 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SketchPlugin_Sketch::NORM_ID());
575 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID());
576 // Transform them into SolveSpace format
577 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
578 if (!aNormalWP) return false;
579 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
580 if (!anOriginWP) return false;
585 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
586 // Workplane should be added to the list of entities
587 myEntities.push_back(myWorkplane);
592 // ============================================================================
593 // Function: changeParameter
594 // Class: SketchSolver_ConstraintGroup
595 // Purpose: create/update value of parameter
596 // ============================================================================
597 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
598 const double& theParam,
599 std::vector<Slvs_Param>::const_iterator& thePrmIter)
601 if (thePrmIter != myParams.end())
602 { // Parameter should be updated
603 int aParamPos = thePrmIter - myParams.begin();
604 if (fabs(thePrmIter->val - theParam) > tolerance)
606 myNeedToSolve = true; // parameter is changed, need to resolve constraints
607 myParams[aParamPos].val = theParam;
610 return myParams[aParamPos].h;
613 // Newly created parameter
614 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
615 myParams.push_back(aParam);
616 myNeedToSolve = true;
617 // The list of parameters is changed, move iterator to the end of the list to avoid problems
618 thePrmIter = myParams.end();
622 // ============================================================================
623 // Function: resolveConstraints
624 // Class: SketchSolver_ConstraintGroup
625 // Purpose: solve the set of constraints for the current group
626 // ============================================================================
627 void SketchSolver_ConstraintGroup::resolveConstraints()
632 myConstrSolver.setGroupID(myID);
633 myConstrSolver.setParameters(myParams);
634 myConstrSolver.setEntities(myEntities);
635 myConstrSolver.setConstraints(myConstraints);
636 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
638 int aResult = myConstrSolver.solve();
639 if (aResult == SLVS_RESULT_OKAY)
640 { // solution succeeded, store results into correspondent attributes
641 // Obtain result into the same list of parameters
642 if (!myConstrSolver.getResult(myParams))
645 // We should go through the attributes map, because only attributes have valued parameters
646 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
647 anEntIter = myEntityAttrMap.begin();
648 for ( ; anEntIter != myEntityAttrMap.end(); anEntIter++)
649 if (updateAttribute(anEntIter->first, anEntIter->second))
650 updateRelatedConstraints(anEntIter->first);
652 else if (!myConstraints.empty())
653 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
655 removeTemporaryConstraints();
656 myNeedToSolve = false;
659 // ============================================================================
660 // Function: mergeGroups
661 // Class: SketchSolver_ConstraintGroup
662 // Purpose: append specified group to the current group
663 // ============================================================================
664 void SketchSolver_ConstraintGroup::mergeGroups(
665 const SketchSolver_ConstraintGroup& theGroup)
667 // If specified group is empty, no need to merge
668 if (theGroup.myConstraintMap.empty())
671 // Map between old and new indexes of SolveSpace constraints
672 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
674 // Add all constraints from theGroup to the current group
675 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
676 aConstrIter = theGroup.myConstraintMap.begin();
677 for ( ; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
678 if (changeConstraint(aConstrIter->first))
679 aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
681 // Add temporary constraints from theGroup
682 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
683 for ( ; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++)
685 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(*aTempConstrIter);
686 if (aFind != aConstrMap.end())
687 myTempConstraints.push_back(aFind->second);
690 if (myTempPointWhereDragged.empty())
691 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
692 else if (!theGroup.myTempPointWhereDragged.empty())
693 { // Need to create additional transient constraint
694 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
695 aFeatureIter = theGroup.myEntityAttrMap.begin();
696 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
697 if (aFeatureIter->second == myTempPointWDrgdID)
699 addTemporaryConstraintWhereDragged(aFeatureIter->first);
704 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
707 // ============================================================================
708 // Function: splitGroup
709 // Class: SketchSolver_ConstraintGroup
710 // Purpose: divide the group into several subgroups
711 // ============================================================================
712 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
714 // Divide constraints and entities into several groups
715 std::vector< std::set<Slvs_hEntity> > aGroupsEntities;
716 std::vector< std::set<Slvs_hConstraint> > aGroupsConstr;
717 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
718 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
719 for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
721 Slvs_hEntity aConstrEnt[] = {
722 aConstrIter->ptA, aConstrIter->ptB,
723 aConstrIter->entityA, aConstrIter->entityB};
724 std::vector<int> anIndexes;
725 // Go through the groupped entities and find even one of entities of current constraint
726 std::vector< std::set<Slvs_hEntity> >::iterator aGrEntIter;
727 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++)
729 bool isFound = false;
730 for (int i = 0; i < 4 && !isFound; i++)
731 if (aConstrEnt[i] != 0)
732 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
734 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
736 // Add new group if no one is found
737 if (anIndexes.empty())
739 std::set<Slvs_hEntity> aNewGrEnt;
740 for (int i = 0; i < 4; i++)
741 if (aConstrEnt[i] != 0)
742 aNewGrEnt.insert(aConstrEnt[i]);
743 std::set<Slvs_hConstraint> aNewGrConstr;
744 aNewGrConstr.insert(aConstrIter->h);
746 aGroupsEntities.push_back(aNewGrEnt);
747 aGroupsConstr.push_back(aNewGrConstr);
748 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
749 aMaxNbEntities = aGroupsEntities.size() - 1;
751 else if (anIndexes.size() == 1)
752 { // Add entities indexes into the found group
753 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
754 for (int i = 0; i < 4; i++)
755 if (aConstrEnt[i] != 0)
756 aGrEntIter->insert(aConstrEnt[i]);
757 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
758 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
759 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
762 { // There are found several connected groups, merge them
763 std::vector< std::set<Slvs_hEntity> >::iterator aFirstGroup =
764 aGroupsEntities.begin() + anIndexes.front();
765 std::vector< std::set<Slvs_hConstraint> >::iterator aFirstConstr =
766 aGroupsConstr.begin() + anIndexes.front();
767 std::vector<int>::iterator anInd = anIndexes.begin();
768 for (++anInd; anInd != anIndexes.end(); anInd++)
770 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
771 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
773 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
774 aMaxNbEntities = anIndexes.front();
775 // Remove merged groups
776 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--)
778 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
779 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
784 if (aGroupsEntities.size() <= 1)
787 // Remove the group with maximum elements as it will be left in the current group
788 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
789 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
791 // Add new groups of constraints and divide current group
792 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
793 for (int i = aGroupsEntities.size(); i > 0; i--)
795 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
796 aNewGroups.push_back(aG);
798 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
799 aConstrMapIter = myConstraintMap.begin();
800 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
801 while (aConstrMapIter != myConstraintMap.end())
803 std::vector< std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
804 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
805 for ( ; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
806 if (aGIter->find(aConstrMapIter->second) != aGIter->end())
808 (*aGroup)->changeConstraint(aConstrMapIter->first);
809 removeConstraint(aConstrMapIter->first);
811 aConstrMapIter = myConstraintMap.begin();
812 for (int i = 0; i < aConstrMapPos; i++)
816 if (aGIter == aGroupsConstr.end())
823 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
826 // ============================================================================
827 // Function: updateGroup
828 // Class: SketchSolver_ConstraintGroup
829 // Purpose: search removed entities and constraints
830 // ============================================================================
831 bool SketchSolver_ConstraintGroup::updateGroup()
833 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
834 aConstrIter = myConstraintMap.rbegin();
835 bool isAllValid = true;
836 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
837 while (isAllValid && aConstrIter != myConstraintMap.rend())
839 if (!aConstrIter->first->data()->isValid())
841 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
843 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
844 aCopyIter = aConstrIter++;
845 removeConstraint(aCopyIter->first);
851 // Probably, need to update coincidence constraints
852 if (isCCRemoved && !myExtraCoincidence.empty())
854 // Make a copy, because the new list of unused constrtaints will be generated
855 std::set< boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
856 myExtraCoincidence.clear();
858 std::set< boost::shared_ptr<SketchPlugin_Constraint> >::iterator
859 aCIter = anExtraCopy.begin();
860 for ( ; aCIter != anExtraCopy.end(); aCIter++)
861 if ((*aCIter)->data()->isValid())
862 changeConstraint(*aCIter);
868 // ============================================================================
869 // Function: updateAttribute
870 // Class: SketchSolver_ConstraintGroup
871 // Purpose: update features of sketch after resolving constraints
872 // ============================================================================
873 bool SketchSolver_ConstraintGroup::updateAttribute(
874 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
875 const Slvs_hEntity& theEntityID)
877 // Search the position of the first parameter of the entity
878 int anEntPos = Search(theEntityID, myEntities);
879 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
881 // Look over supported types of entities
884 boost::shared_ptr<GeomDataAPI_Point> aPoint =
885 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
888 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance ||
889 fabs(aPoint->y() - myParams[aFirstParamPos+1].val) > tolerance ||
890 fabs(aPoint->z() - myParams[aFirstParamPos+2].val) > tolerance)
892 aPoint->setValue(myParams[aFirstParamPos].val,
893 myParams[aFirstParamPos+1].val,
894 myParams[aFirstParamPos+2].val);
901 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
902 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
905 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance ||
906 fabs(aPoint2D->y() - myParams[aFirstParamPos+1].val) > tolerance)
908 aPoint2D->setValue(myParams[aFirstParamPos].val,
909 myParams[aFirstParamPos+1].val);
916 AttributeDoublePtr aScalar =
917 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
920 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance)
922 aScalar->setValue(myParams[aFirstParamPos].val);
928 /// \todo Support other types of entities
932 // ============================================================================
933 // Function: updateEntityIfPossible
934 // Class: SketchSolver_ConstraintGroup
935 // Purpose: search the entity in this group and update it
936 // ============================================================================
937 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
938 boost::shared_ptr<ModelAPI_Attribute> theEntity)
940 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end())
942 // If the attribute is a point and it is changed (the group needs to rebuild),
943 // probably user has dragged this point into this position,
944 // so it is necessary to add constraint which will guarantee the point will not change
946 // Store myNeedToSolve flag to verify the entity is really changed
947 bool aNeedToSolveCopy = myNeedToSolve;
948 myNeedToSolve = false;
950 changeEntity(theEntity);
952 if (myNeedToSolve) // the entity is changed
954 // Verify the entity is a point and add temporary constraint of permanency
955 boost::shared_ptr<GeomDataAPI_Point> aPoint =
956 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
957 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
958 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
959 if (aPoint || aPoint2D)
960 addTemporaryConstraintWhereDragged(theEntity);
963 // Restore flag of changes
964 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
967 updateRelatedConstraints(theEntity);
971 // ============================================================================
972 // Function: addTemporaryConstraintWhereDragged
973 // Class: SketchSolver_ConstraintGroup
974 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
975 // which was moved by user
976 // ============================================================================
977 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
978 boost::shared_ptr<ModelAPI_Attribute> theEntity)
980 // Find identifier of the entity
981 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
982 anEntIter = myEntityAttrMap.find(theEntity);
983 if (anEntIter == myEntityAttrMap.end())
986 // If this is a first dragged point, its parameters should be placed
987 // into Slvs_System::dragged field to avoid system inconsistense
988 if (myTempPointWhereDragged.empty())
990 int anEntPos = Search(anEntIter->second, myEntities);
991 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
992 for (int i = 0; i < 4; i++, aDraggedParam++)
993 if (*aDraggedParam != 0)
994 myTempPointWhereDragged.push_back(*aDraggedParam);
995 myTempPointWDrgdID = myEntities[anEntPos].h;
999 // Get identifiers of all dragged points
1000 std::set<Slvs_hEntity> aDraggedPntID;
1001 aDraggedPntID.insert(myTempPointWDrgdID);
1002 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
1003 for ( ; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++)
1005 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
1006 if (aConstrPos < myConstraints.size())
1007 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
1009 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
1010 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1011 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1013 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
1014 continue; // the entity was not found in current set
1016 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
1017 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
1018 for ( ; aDrgIter != aDraggedPntID.end(); aDrgIter++)
1019 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
1020 return ; // the SLVS_C_WHERE_DRAGGED constraint already exists
1023 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
1024 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1025 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1026 myConstraints.push_back(aWDConstr);
1027 myTempConstraints.push_back(aWDConstr.h);
1030 // ============================================================================
1031 // Function: removeTemporaryConstraints
1032 // Class: SketchSolver_ConstraintGroup
1033 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
1034 // resolving the set of constraints
1035 // ============================================================================
1036 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
1038 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1039 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
1041 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1042 if (aConstrPos >= myConstraints.size())
1044 myConstraints.erase(myConstraints.begin() + aConstrPos);
1046 // If the removing constraint has higher index, decrease the indexer
1047 if (*aTmpConstrIter == myConstrMaxID)
1050 myTempConstraints.clear();
1052 // Clear basic dragged point
1053 myTempPointWhereDragged.clear();
1056 // ============================================================================
1057 // Function: removeConstraint
1058 // Class: SketchSolver_ConstraintGroup
1059 // Purpose: remove constraint and all unused entities
1060 // ============================================================================
1061 void SketchSolver_ConstraintGroup::removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1063 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator
1064 anIterToRemove = myConstraintMap.find(theConstraint);
1065 if (anIterToRemove == myConstraintMap.end())
1068 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
1069 // Remove constraint from the map
1070 myConstraintMap.erase(anIterToRemove);
1072 // Find unused entities
1073 int aConstrPos = Search(aCnstrToRemove, myConstraints);
1074 std::set<Slvs_hEntity> anEntToRemove;
1075 Slvs_hEntity aCnstEnt[] = {myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1076 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
1077 for (int i = 0; i < 4; i++)
1078 if (aCnstEnt[i] != 0)
1079 anEntToRemove.insert(aCnstEnt[i]);
1080 myConstraints.erase(myConstraints.begin() + aConstrPos);
1081 if (aCnstrToRemove == myConstrMaxID)
1083 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1084 for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
1086 Slvs_hEntity aEnts[] = {aConstrIter->ptA, aConstrIter->ptB,
1087 aConstrIter->entityA, aConstrIter->entityB};
1088 for (int i = 0; i < 4; i++)
1089 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1090 anEntToRemove.erase(aEnts[i]);
1093 if (anEntToRemove.empty())
1096 // Remove unused entities
1097 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1098 anEntAttrIter = myEntityAttrMap.begin();
1099 while (anEntAttrIter != myEntityAttrMap.end())
1101 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end())
1103 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1104 aRemovedIter = anEntAttrIter;
1106 myEntityAttrMap.erase(aRemovedIter);
1108 else anEntAttrIter++;
1110 std::map<FeaturePtr, Slvs_hEntity>::iterator
1111 anEntFeatIter = myEntityFeatMap.begin();
1112 while (anEntFeatIter != myEntityFeatMap.end())
1114 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end())
1116 std::map<FeaturePtr, Slvs_hEntity>::iterator
1117 aRemovedIter = anEntFeatIter;
1119 myEntityFeatMap.erase(aRemovedIter);
1121 else anEntFeatIter++;
1123 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1124 for ( ; aRemIter != anEntToRemove.rend(); aRemIter++)
1126 unsigned int anEntPos = Search(*aRemIter, myEntities);
1127 if (anEntPos >= myEntities.size())
1129 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1130 if (aParamPos >= myParams.size())
1133 while (myEntities[anEntPos].param[aNbParams] != 0)
1135 if (myEntities[anEntPos].param[aNbParams-1] == myParamMaxID)
1136 myParamMaxID -= aNbParams;
1137 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1138 if (*aRemIter == myEntityMaxID)
1140 myEntities.erase(myEntities.begin() + anEntPos);
1142 // Remove entity's ID from the lists of conincident points
1143 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1144 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1145 aCoPtIter->erase(*aRemIter);
1147 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1148 myCoincidentPoints.clear();
1152 // ============================================================================
1153 // Function: addCoincidentPoints
1154 // Class: SketchSolver_ConstraintGroup
1155 // Purpose: add coincident point the appropriate list of such points
1156 // ============================================================================
1157 bool SketchSolver_ConstraintGroup::addCoincidentPoints(
1158 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1160 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1161 std::vector< std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1162 while (aCoPtIter != myCoincidentPoints.end())
1164 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1165 aCoPtIter->find(thePoint1) != aCoPtIter->end(),
1166 aCoPtIter->find(thePoint2) != aCoPtIter->end(),
1168 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1170 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1]))
1172 if (aFirstFound != myCoincidentPoints.end())
1173 { // there are two groups of coincident points connected by created constraint => merge them
1174 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1175 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1176 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1177 myCoincidentPoints.erase(aCoPtIter);
1178 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1179 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1184 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1185 aFirstFound = aCoPtIter;
1190 // No points were found, need to create new set
1191 if (aFirstFound == myCoincidentPoints.end())
1193 std::set<Slvs_hEntity> aNewSet;
1194 aNewSet.insert(thePoint1);
1195 aNewSet.insert(thePoint2);
1196 myCoincidentPoints.push_back(aNewSet);
1203 // ============================================================================
1204 // Function: updateRelatedConstraints
1205 // Class: SketchSolver_ConstraintGroup
1206 // Purpose: emit the signal to update constraints
1207 // ============================================================================
1208 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1209 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1211 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
1212 aConstrIter = myConstraintMap.begin();
1213 for ( ; aConstrIter != myConstraintMap.end(); aConstrIter++)
1215 std::list< boost::shared_ptr<ModelAPI_Attribute> > anAttributes =
1216 aConstrIter->first->data()->attributes(std::string());
1218 std::list< boost::shared_ptr<ModelAPI_Attribute> >::iterator
1219 anAttrIter = anAttributes.begin();
1220 for ( ; anAttrIter != anAttributes.end(); anAttrIter++)
1222 bool isUpd = (*anAttrIter == theEntity);
1223 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1224 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
1225 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1230 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1231 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1238 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1239 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1241 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
1242 aConstrIter = myConstraintMap.begin();
1243 for ( ; aConstrIter != myConstraintMap.end(); aConstrIter++)
1245 std::list< boost::shared_ptr<ModelAPI_Attribute> > anAttributes =
1246 aConstrIter->first->data()->attributes(std::string());
1248 std::list< boost::shared_ptr<ModelAPI_Attribute> >::iterator
1249 anAttrIter = anAttributes.begin();
1250 for ( ; anAttrIter != anAttributes.end(); anAttrIter++)
1252 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1253 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
1254 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature)
1256 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1257 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1266 // ========================================================
1267 // ========= Auxiliary functions ===============
1268 // ========================================================
1270 template <typename T>
1271 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1273 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1274 int aVecSize = theEntities.size();
1275 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1277 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1279 if (aResIndex == -1)
1280 aResIndex = aVecSize;