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 return aLineEntity.h;
424 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
425 Slvs_hEntity aCenter = changeEntity(
426 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
427 Slvs_hEntity aRadius = changeEntity(
428 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
431 return aEntIter->second;
434 Slvs_Entity aCircleEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
435 myWorkplane.normal, aRadius);
436 myEntities.push_back(aCircleEntity);
437 myEntityFeatMap[theEntity] = aCircleEntity.h;
438 return aCircleEntity.h;
441 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
442 Slvs_hEntity aCenter = changeEntity(
443 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
444 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
445 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
448 return aEntIter->second;
450 Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
451 myWorkplane.normal, aCenter, aStart, aEnd);
452 myEntities.push_back(anArcEntity);
453 myEntityFeatMap[theEntity] = anArcEntity.h;
454 return anArcEntity.h;
456 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
457 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
458 Slvs_hEntity aPoint = changeEntity(
459 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
462 return aEntIter->second;
464 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
465 myEntityFeatMap[theEntity] = aPoint;
470 /// \todo Other types of features
472 // Unsupported or wrong entity type
473 return SLVS_E_UNKNOWN;
476 // ============================================================================
477 // Function: changeNormal
478 // Class: SketchSolver_ConstraintGroup
479 // Purpose: create/update the normal of workplane
480 // ============================================================================
481 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
482 boost::shared_ptr<ModelAPI_Attribute> theDirX, boost::shared_ptr<ModelAPI_Attribute> theDirY,
483 boost::shared_ptr<ModelAPI_Attribute> theNorm)
485 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
486 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
487 if (!aDirX || !aDirY || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance)
488 || (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
489 return SLVS_E_UNKNOWN;
491 // quaternion parameters of normal vector
492 double qw, qx, qy, qz;
493 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
495 double aNormCoord[4] = { qw, qx, qy, qz };
497 // Try to find existent normal
498 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
499 myEntityAttrMap.find(theNorm);
500 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
501 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
502 aParamIter = myParams.end();
503 else { // the entity already exists, update it
504 int aEntPos = Search(aEntIter->second, myEntities);
505 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
506 aParamIter = myParams.begin() + aParamPos;
509 // Change parameters of the normal
510 Slvs_hParam aNormParams[4];
511 for (int i = 0; i < 4; i++)
512 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
514 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
515 return aEntIter->second;
518 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
519 aNormParams[2], aNormParams[3]);
520 myEntities.push_back(aNormal);
521 myEntityAttrMap[theNorm] = aNormal.h;
525 // ============================================================================
526 // Function: addWorkplane
527 // Class: SketchSolver_ConstraintGroup
528 // Purpose: create workplane for the group
529 // ============================================================================
530 bool SketchSolver_ConstraintGroup::addWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
532 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
533 return false; // the workplane already exists or the function parameter is not Sketch
535 mySketch = theSketch;
540 // ============================================================================
541 // Function: updateWorkplane
542 // Class: SketchSolver_ConstraintGroup
543 // Purpose: update parameters of workplane
544 // ============================================================================
545 bool SketchSolver_ConstraintGroup::updateWorkplane()
547 // Get parameters of workplane
548 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
549 SketchPlugin_Sketch::DIRX_ID());
550 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
551 SketchPlugin_Sketch::DIRY_ID());
552 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
553 SketchPlugin_Sketch::NORM_ID());
554 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
555 SketchPlugin_Sketch::ORIGIN_ID());
556 // Transform them into SolveSpace format
557 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
560 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
564 if (!myWorkplane.h) {
566 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
567 // Workplane should be added to the list of entities
568 myEntities.push_back(myWorkplane);
573 // ============================================================================
574 // Function: changeParameter
575 // Class: SketchSolver_ConstraintGroup
576 // Purpose: create/update value of parameter
577 // ============================================================================
578 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
579 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
581 if (thePrmIter != myParams.end()) { // Parameter should be updated
582 int aParamPos = thePrmIter - myParams.begin();
583 if (fabs(thePrmIter->val - theParam) > tolerance) {
584 myNeedToSolve = true; // parameter is changed, need to resolve constraints
585 myParams[aParamPos].val = theParam;
588 return myParams[aParamPos].h;
591 // Newly created parameter
592 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
593 myParams.push_back(aParam);
594 myNeedToSolve = true;
595 // The list of parameters is changed, move iterator to the end of the list to avoid problems
596 thePrmIter = myParams.end();
600 // ============================================================================
601 // Function: resolveConstraints
602 // Class: SketchSolver_ConstraintGroup
603 // Purpose: solve the set of constraints for the current group
604 // ============================================================================
605 void SketchSolver_ConstraintGroup::resolveConstraints()
610 myConstrSolver.setGroupID(myID);
611 myConstrSolver.setParameters(myParams);
612 myConstrSolver.setEntities(myEntities);
613 myConstrSolver.setConstraints(myConstraints);
614 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
616 int aResult = myConstrSolver.solve();
617 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
618 // Obtain result into the same list of parameters
619 if (!myConstrSolver.getResult(myParams))
622 // We should go through the attributes map, because only attributes have valued parameters
623 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
624 myEntityAttrMap.begin();
625 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
626 if (updateAttribute(anEntIter->first, anEntIter->second))
627 updateRelatedConstraints(anEntIter->first);
628 } else if (!myConstraints.empty())
629 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
631 removeTemporaryConstraints();
632 myNeedToSolve = false;
635 // ============================================================================
636 // Function: mergeGroups
637 // Class: SketchSolver_ConstraintGroup
638 // Purpose: append specified group to the current group
639 // ============================================================================
640 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
642 // If specified group is empty, no need to merge
643 if (theGroup.myConstraintMap.empty())
646 // Map between old and new indexes of SolveSpace constraints
647 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
649 // Add all constraints from theGroup to the current group
650 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
651 theGroup.myConstraintMap.begin();
652 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
653 if (changeConstraint(aConstrIter->first))
654 aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
656 // Add temporary constraints from theGroup
657 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
658 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
659 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
661 if (aFind != aConstrMap.end())
662 myTempConstraints.push_back(aFind->second);
665 if (myTempPointWhereDragged.empty())
666 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
667 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
668 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
669 theGroup.myEntityAttrMap.begin();
670 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
671 if (aFeatureIter->second == myTempPointWDrgdID) {
672 addTemporaryConstraintWhereDragged(aFeatureIter->first);
677 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
680 // ============================================================================
681 // Function: splitGroup
682 // Class: SketchSolver_ConstraintGroup
683 // Purpose: divide the group into several subgroups
684 // ============================================================================
685 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
687 // Divide constraints and entities into several groups
688 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
689 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
690 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
691 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
692 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
693 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
694 aConstrIter->entityB };
695 std::vector<int> anIndexes;
696 // Go through the groupped entities and find even one of entities of current constraint
697 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
698 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
699 bool isFound = false;
700 for (int i = 0; i < 4 && !isFound; i++)
701 if (aConstrEnt[i] != 0)
702 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
704 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
706 // Add new group if no one is found
707 if (anIndexes.empty()) {
708 std::set<Slvs_hEntity> aNewGrEnt;
709 for (int i = 0; i < 4; i++)
710 if (aConstrEnt[i] != 0)
711 aNewGrEnt.insert(aConstrEnt[i]);
712 std::set<Slvs_hConstraint> aNewGrConstr;
713 aNewGrConstr.insert(aConstrIter->h);
715 aGroupsEntities.push_back(aNewGrEnt);
716 aGroupsConstr.push_back(aNewGrConstr);
717 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
718 aMaxNbEntities = aGroupsEntities.size() - 1;
719 } else if (anIndexes.size() == 1) { // Add entities indexes into the found group
720 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
721 for (int i = 0; i < 4; i++)
722 if (aConstrEnt[i] != 0)
723 aGrEntIter->insert(aConstrEnt[i]);
724 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
725 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
726 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
727 } else { // There are found several connected groups, merge them
728 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
730 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
732 std::vector<int>::iterator anInd = anIndexes.begin();
733 for (++anInd; anInd != anIndexes.end(); anInd++) {
734 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
735 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
737 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
738 aMaxNbEntities = anIndexes.front();
739 // Remove merged groups
740 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
741 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
742 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
747 if (aGroupsEntities.size() <= 1)
750 // Remove the group with maximum elements as it will be left in the current group
751 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
752 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
754 // Add new groups of constraints and divide current group
755 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
756 for (int i = aGroupsEntities.size(); i > 0; i--) {
757 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
758 aNewGroups.push_back(aG);
760 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrMapIter =
761 myConstraintMap.begin();
762 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
763 while (aConstrMapIter != myConstraintMap.end()) {
764 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
765 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
766 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
767 if (aGIter->find(aConstrMapIter->second) != aGIter->end()) {
768 (*aGroup)->changeConstraint(aConstrMapIter->first);
769 removeConstraint(aConstrMapIter->first);
771 aConstrMapIter = myConstraintMap.begin();
772 for (int i = 0; i < aConstrMapPos; i++)
776 if (aGIter == aGroupsConstr.end()) {
782 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
785 // ============================================================================
786 // Function: updateGroup
787 // Class: SketchSolver_ConstraintGroup
788 // Purpose: search removed entities and constraints
789 // ============================================================================
790 bool SketchSolver_ConstraintGroup::updateGroup()
792 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator aConstrIter =
793 myConstraintMap.rbegin();
794 bool isAllValid = true;
795 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
796 while (isAllValid && aConstrIter != myConstraintMap.rend()) {
797 if (!aConstrIter->first->data()->isValid()) {
798 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
800 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator aCopyIter =
802 removeConstraint(aCopyIter->first);
808 // Probably, need to update coincidence constraints
809 if (isCCRemoved && !myExtraCoincidence.empty()) {
810 // Make a copy, because the new list of unused constrtaints will be generated
811 std::set<boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
812 myExtraCoincidence.clear();
814 std::set<boost::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
815 for (; aCIter != anExtraCopy.end(); aCIter++)
816 if ((*aCIter)->data()->isValid())
817 changeConstraint(*aCIter);
823 // ============================================================================
824 // Function: updateAttribute
825 // Class: SketchSolver_ConstraintGroup
826 // Purpose: update features of sketch after resolving constraints
827 // ============================================================================
828 bool SketchSolver_ConstraintGroup::updateAttribute(
829 boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
831 // Search the position of the first parameter of the entity
832 int anEntPos = Search(theEntityID, myEntities);
833 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
835 // Look over supported types of entities
838 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
841 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
842 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
843 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
844 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
845 myParams[aFirstParamPos + 2].val);
852 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
853 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
855 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
856 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
857 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
864 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
866 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
867 aScalar->setValue(myParams[aFirstParamPos].val);
873 /// \todo Support other types of entities
877 // ============================================================================
878 // Function: updateEntityIfPossible
879 // Class: SketchSolver_ConstraintGroup
880 // Purpose: search the entity in this group and update it
881 // ============================================================================
882 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
883 boost::shared_ptr<ModelAPI_Attribute> theEntity)
885 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
886 // If the attribute is a point and it is changed (the group needs to rebuild),
887 // probably user has dragged this point into this position,
888 // so it is necessary to add constraint which will guarantee the point will not change
890 // Store myNeedToSolve flag to verify the entity is really changed
891 bool aNeedToSolveCopy = myNeedToSolve;
892 myNeedToSolve = false;
894 changeEntity(theEntity);
896 if (myNeedToSolve) // the entity is changed
898 // Verify the entity is a point and add temporary constraint of permanency
899 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
901 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = boost::dynamic_pointer_cast<
902 GeomDataAPI_Point2D>(theEntity);
903 if (aPoint || aPoint2D)
904 addTemporaryConstraintWhereDragged(theEntity);
907 // Restore flag of changes
908 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
911 updateRelatedConstraints(theEntity);
915 // ============================================================================
916 // Function: addTemporaryConstraintWhereDragged
917 // Class: SketchSolver_ConstraintGroup
918 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
919 // which was moved by user
920 // ============================================================================
921 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
922 boost::shared_ptr<ModelAPI_Attribute> theEntity)
924 // Find identifier of the entity
925 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
926 myEntityAttrMap.find(theEntity);
927 if (anEntIter == myEntityAttrMap.end())
930 // If this is a first dragged point, its parameters should be placed
931 // into Slvs_System::dragged field to avoid system inconsistense
932 if (myTempPointWhereDragged.empty()) {
933 int anEntPos = Search(anEntIter->second, myEntities);
934 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
935 for (int i = 0; i < 4; i++, aDraggedParam++)
936 if (*aDraggedParam != 0)
937 myTempPointWhereDragged.push_back(*aDraggedParam);
938 myTempPointWDrgdID = myEntities[anEntPos].h;
942 // Get identifiers of all dragged points
943 std::set<Slvs_hEntity> aDraggedPntID;
944 aDraggedPntID.insert(myTempPointWDrgdID);
945 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
946 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
947 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
948 if (aConstrPos < myConstraints.size())
949 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
951 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
952 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
953 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
954 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
955 continue; // the entity was not found in current set
957 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
958 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
959 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
960 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
961 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
964 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
965 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
966 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
967 myConstraints.push_back(aWDConstr);
968 myTempConstraints.push_back(aWDConstr.h);
971 // ============================================================================
972 // Function: removeTemporaryConstraints
973 // Class: SketchSolver_ConstraintGroup
974 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
975 // resolving the set of constraints
976 // ============================================================================
977 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
979 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
980 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
982 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
983 if (aConstrPos >= myConstraints.size())
985 myConstraints.erase(myConstraints.begin() + aConstrPos);
987 // If the removing constraint has higher index, decrease the indexer
988 if (*aTmpConstrIter == myConstrMaxID)
991 myTempConstraints.clear();
993 // Clear basic dragged point
994 myTempPointWhereDragged.clear();
997 // ============================================================================
998 // Function: removeConstraint
999 // Class: SketchSolver_ConstraintGroup
1000 // Purpose: remove constraint and all unused entities
1001 // ============================================================================
1002 void SketchSolver_ConstraintGroup::removeConstraint(
1003 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1005 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator anIterToRemove =
1006 myConstraintMap.find(theConstraint);
1007 if (anIterToRemove == myConstraintMap.end())
1010 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
1011 // Remove constraint from the map
1012 myConstraintMap.erase(anIterToRemove);
1014 // Find unused entities
1015 int aConstrPos = Search(aCnstrToRemove, myConstraints);
1016 std::set<Slvs_hEntity> anEntToRemove;
1017 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1018 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1019 for (int i = 0; i < 4; i++)
1020 if (aCnstEnt[i] != 0)
1021 anEntToRemove.insert(aCnstEnt[i]);
1022 myConstraints.erase(myConstraints.begin() + aConstrPos);
1023 if (aCnstrToRemove == myConstrMaxID)
1025 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1026 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1027 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1029 for (int i = 0; i < 4; i++)
1030 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1031 anEntToRemove.erase(aEnts[i]);
1034 if (anEntToRemove.empty())
1037 // Remove unused entities
1038 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1039 myEntityAttrMap.begin();
1040 while (anEntAttrIter != myEntityAttrMap.end()) {
1041 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1042 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1045 myEntityAttrMap.erase(aRemovedIter);
1049 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1050 while (anEntFeatIter != myEntityFeatMap.end()) {
1051 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1052 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1054 myEntityFeatMap.erase(aRemovedIter);
1058 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1059 for (; aRemIter != anEntToRemove.rend(); aRemIter++) {
1060 unsigned int anEntPos = Search(*aRemIter, myEntities);
1061 if (anEntPos >= myEntities.size())
1063 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1064 if (aParamPos >= myParams.size())
1067 while (myEntities[anEntPos].param[aNbParams] != 0)
1069 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1070 myParamMaxID -= aNbParams;
1071 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1072 if (*aRemIter == myEntityMaxID)
1074 myEntities.erase(myEntities.begin() + anEntPos);
1076 // Remove entity's ID from the lists of conincident points
1077 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1078 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1079 aCoPtIter->erase(*aRemIter);
1081 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1082 myCoincidentPoints.clear();
1085 // ============================================================================
1086 // Function: addCoincidentPoints
1087 // Class: SketchSolver_ConstraintGroup
1088 // Purpose: add coincident point the appropriate list of such points
1089 // ============================================================================
1090 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1091 const Slvs_hEntity& thePoint2)
1093 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1094 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1095 while (aCoPtIter != myCoincidentPoints.end()) {
1096 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1097 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1098 != aCoPtIter->end(), };
1099 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1101 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1102 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1103 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1104 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1105 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1106 myCoincidentPoints.erase(aCoPtIter);
1107 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1108 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1111 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1112 aFirstFound = aCoPtIter;
1117 // No points were found, need to create new set
1118 if (aFirstFound == myCoincidentPoints.end()) {
1119 std::set<Slvs_hEntity> aNewSet;
1120 aNewSet.insert(thePoint1);
1121 aNewSet.insert(thePoint2);
1122 myCoincidentPoints.push_back(aNewSet);
1128 // ============================================================================
1129 // Function: updateRelatedConstraints
1130 // Class: SketchSolver_ConstraintGroup
1131 // Purpose: emit the signal to update constraints
1132 // ============================================================================
1133 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1134 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1136 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1137 myConstraintMap.begin();
1138 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1139 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1140 ->attributes(std::string());
1142 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1143 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1144 bool isUpd = (*anAttrIter == theEntity);
1145 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1146 ModelAPI_AttributeRefAttr>(*anAttrIter);
1147 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1151 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1152 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1159 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1160 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1162 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1163 myConstraintMap.begin();
1164 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1165 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1166 ->attributes(std::string());
1168 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1169 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1170 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1171 ModelAPI_AttributeRefAttr>(*anAttrIter);
1172 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1173 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1174 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1181 // ========================================================
1182 // ========= Auxiliary functions ===============
1183 // ========================================================
1185 template<typename T>
1186 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1188 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1189 int aVecSize = theEntities.size();
1190 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1192 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1194 if (aResIndex == -1)
1195 aResIndex = aVecSize;