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_Loop.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefList.h>
15 #include <Model_Events.h>
17 #include <SketchPlugin_Constraint.h>
19 #include <SketchPlugin_Arc.h>
20 #include <SketchPlugin_Circle.h>
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Point.h>
23 #include <SketchPlugin_Sketch.h>
28 /// Tolerance for value of parameters
29 const double tolerance = 1.e-10;
31 /// This value is used to give unique index to the groups
32 static Slvs_hGroup myGroupIndexer = 0;
34 /** \brief Search the entity/parameter with specified ID in the list of elements
35 * \param[in] theEntityID unique ID of the element
36 * \param[in] theEntities list of elements
37 * \return position of the found element or -1 if the element is not found
40 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
43 // ========================================================
44 // ========= SketchSolver_ConstraintGroup ===============
45 // ========================================================
47 SketchSolver_ConstraintGroup::
48 SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
49 : myID(++myGroupIndexer),
59 myConstraints.clear();
61 myTempConstraints.clear();
62 myTempPointWhereDragged.clear();
63 myTempPointWDrgdID = 0;
65 // Initialize workplane
66 myWorkplane.h = SLVS_E_UNKNOWN;
68 assert(addWorkplane(theWorkplane));
70 addWorkplane(theWorkplane);
74 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
78 myConstraints.clear();
79 myConstraintMap.clear();
80 myTempConstraints.clear();
81 myTempPointWhereDragged.clear();
83 // If the group with maximal identifier is deleted, decrease the indexer
84 if (myID == myGroupIndexer)
88 // ============================================================================
89 // Function: isBaseWorkplane
90 // Class: SketchSolver_ConstraintGroup
91 // Purpose: verify the group is based on the given workplane
92 // ============================================================================
93 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
94 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
96 return theWorkplane == mySketch;
99 // ============================================================================
100 // Function: isInteract
101 // Class: SketchSolver_ConstraintGroup
102 // Purpose: verify are there any entities in the group used by given constraint
103 // ============================================================================
104 bool SketchSolver_ConstraintGroup::isInteract(
105 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
107 // Check the group is empty
108 if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
111 // Go through constraint entities and verify if some of them already in the group
112 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
114 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
115 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
116 theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
118 if (!aCAttrRef) continue;
119 if (!aCAttrRef->isFeature() &&
120 myEntityAttrMap.find(aCAttrRef->attr()) != myEntityAttrMap.end())
122 if (aCAttrRef->isFeature() &&
123 myEntityFeatMap.find(aCAttrRef->feature()) != myEntityFeatMap.end())
127 // Entities did not found
131 // ============================================================================
132 // Function: changeConstraint
133 // Class: SketchSolver_ConstraintGroup
134 // Purpose: create/update the constraint in the group
135 // ============================================================================
136 bool SketchSolver_ConstraintGroup::changeConstraint(
137 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
139 // There is no workplane yet, something wrong
140 if (myWorkplane.h == SLVS_E_UNKNOWN)
143 // Search this constraint in the current group to update it
144 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
145 aConstrMapIter = myConstraintMap.find(theConstraint);
146 std::vector<Slvs_Constraint>::iterator aConstrIter;
147 if (aConstrMapIter != myConstraintMap.end())
149 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
150 aConstrIter = myConstraints.begin() + aConstrPos;
153 // Get constraint type and verify the constraint parameters are correct
154 SketchSolver_Constraint aConstraint(theConstraint);
155 int aConstrType = aConstraint.getType();
156 if (aConstrType == SLVS_C_UNKNOWN ||
157 (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
159 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
161 // Create constraint parameters
162 double aDistance = 0.0; // scalar value of the constraint
163 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
164 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
167 aDistance = aDistAttr->value();
168 if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
170 myNeedToSolve = true;
171 aConstrIter->valA = aDistance;
173 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
174 if (aConstrType == SLVS_C_DIAMETER)
178 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
179 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
181 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
182 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
183 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
184 theConstraint->data()->attribute(aConstraintAttributes[indAttr])
186 if (!aConstrAttr) continue;
188 // For the length constraint the start and end points of the line should be added to the entities list instead of line
189 if (aConstrType == SLVS_C_PT_PT_DISTANCE && theConstraint->getKind().compare("SketchConstraintLength") == 0)
191 boost::shared_ptr<ModelAPI_Data> aData = aConstrAttr->feature()->data();
192 aConstrEnt[indAttr] = changeEntity(aData->attribute(LINE_ATTR_START));
193 aConstrEnt[indAttr+1] = changeEntity(aData->attribute(LINE_ATTR_END));
194 break; // there should be no other entities
196 else if (aConstrAttr->isFeature())
197 aConstrEnt[indAttr] = changeEntity(aConstrAttr->feature());
199 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
202 if (aConstrMapIter == myConstraintMap.end())
204 // Several points may be coincident, it is not necessary to store all constraints between them.
205 // Try to find sequence of coincident points which connects the points of new constraint
206 if (aConstrType == SLVS_C_POINTS_COINCIDENT &&
207 !addCoincidentPoints(aConstrEnt[0], aConstrEnt[1]))
209 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
213 // Create SolveSpace constraint structure
214 Slvs_Constraint aConstraint =
215 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
216 aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
217 myConstraints.push_back(aConstraint);
218 myConstraintMap[theConstraint] = aConstraint.h;
223 // ============================================================================
224 // Function: changeEntity
225 // Class: SketchSolver_ConstraintGroup
226 // Purpose: create/update the element affected by any constraint
227 // ============================================================================
228 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
229 boost::shared_ptr<ModelAPI_Attribute> theEntity)
231 // If the entity is already in the group, try to find it
232 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
233 aEntIter = myEntityAttrMap.find(theEntity);
234 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
235 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
236 aParamIter = myParams.end();
238 { // the entity already exists
239 int aEntPos = Search(aEntIter->second, myEntities);
240 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
241 aParamIter = myParams.begin() + aParamPos;
243 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
245 // Look over supported types of entities
248 boost::shared_ptr<GeomDataAPI_Point> aPoint =
249 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
252 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
253 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
254 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
257 return aEntIter->second;
260 Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
261 myEntities.push_back(aPtEntity);
262 myEntityAttrMap[theEntity] = aPtEntity.h;
266 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
267 if (myWorkplane.h == SLVS_E_UNKNOWN)
268 return SLVS_E_UNKNOWN;
271 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
272 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
275 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
276 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
279 return aEntIter->second;
282 Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
283 myEntities.push_back(aPt2DEntity);
284 myEntityAttrMap[theEntity] = aPt2DEntity.h;
285 return aPt2DEntity.h;
288 // Scalar value (used for the distance entities)
289 boost::shared_ptr<ModelAPI_AttributeDouble> aScalar =
290 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
293 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
296 return aEntIter->second;
299 Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
300 myEntities.push_back(aDistance);
301 myEntityAttrMap[theEntity] = aDistance.h;
305 /// \todo Other types of entities
307 // Unsupported or wrong entity type
308 return SLVS_E_UNKNOWN;
312 // ============================================================================
313 // Function: changeEntity
314 // Class: SketchSolver_ConstraintGroup
315 // Purpose: create/update the element defined by the feature affected by any constraint
316 // ============================================================================
317 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
318 boost::shared_ptr<ModelAPI_Feature> theEntity)
320 // If the entity is already in the group, try to find it
321 std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::const_iterator
322 aEntIter = myEntityFeatMap.find(theEntity);
323 // defines that the entity already exists
324 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
326 // SketchPlugin features
327 boost::shared_ptr<SketchPlugin_Feature> aFeature;
328 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theEntity);
330 { // Verify the feature by its kind
331 const std::string& aFeatureKind = aFeature->getKind();
334 if (aFeatureKind.compare("SketchLine") == 0)
336 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(LINE_ATTR_START));
337 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(LINE_ATTR_END));
340 return aEntIter->second;
343 Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
344 myEntities.push_back(aLineEntity);
345 myEntityFeatMap[theEntity] = aLineEntity.h;
346 return aLineEntity.h;
349 else if (aFeatureKind.compare("SketchCircle") == 0)
351 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_CENTER));
352 Slvs_hEntity aRadius = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
355 return aEntIter->second;
358 Slvs_Entity aCircleEntity =
359 Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter, myWorkplane.normal, aRadius);
360 myEntities.push_back(aCircleEntity);
361 myEntityFeatMap[theEntity] = aCircleEntity.h;
362 return aCircleEntity.h;
365 else if (aFeatureKind.compare("SketchArc") == 0)
367 Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(ARC_ATTR_CENTER));
368 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(ARC_ATTR_START));
369 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(ARC_ATTR_END));
372 return aEntIter->second;
374 Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID,
375 myWorkplane.h, myWorkplane.normal, aCenter, aStart, aEnd);
376 myEntities.push_back(anArcEntity);
377 myEntityFeatMap[theEntity] = anArcEntity.h;
378 return anArcEntity.h;
380 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
381 else if (aFeatureKind.compare("SketchPoint") == 0)
383 Slvs_hEntity aPoint = changeEntity(aFeature->data()->attribute(POINT_ATTR_COORD));
386 return aEntIter->second;
388 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
389 myEntityFeatMap[theEntity] = aPoint;
394 /// \todo Other types of features
396 // Unsupported or wrong entity type
397 return SLVS_E_UNKNOWN;
400 // ============================================================================
401 // Function: changeNormal
402 // Class: SketchSolver_ConstraintGroup
403 // Purpose: create/update the normal of workplane
404 // ============================================================================
405 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
406 boost::shared_ptr<ModelAPI_Attribute> theDirX,
407 boost::shared_ptr<ModelAPI_Attribute> theDirY,
408 boost::shared_ptr<ModelAPI_Attribute> theNorm)
410 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
411 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
412 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
413 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
414 if (!aDirX || !aDirY ||
415 (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
416 (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
417 return SLVS_E_UNKNOWN;
419 // quaternion parameters of normal vector
420 double qw, qx, qy, qz;
421 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
422 aDirY->x(), aDirY->y(), aDirY->z(),
424 double aNormCoord[4] = {qw, qx, qy, qz};
426 // Try to find existent normal
427 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
428 aEntIter = myEntityAttrMap.find(theNorm);
429 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
430 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
431 aParamIter = myParams.end();
433 { // the entity already exists, update it
434 int aEntPos = Search(aEntIter->second, myEntities);
435 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
436 aParamIter = myParams.begin() + aParamPos;
439 // Change parameters of the normal
440 Slvs_hParam aNormParams[4];
441 for (int i = 0; i < 4; i++)
442 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
444 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
445 return aEntIter->second;
448 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
449 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
450 myEntities.push_back(aNormal);
451 myEntityAttrMap[theNorm] = aNormal.h;
456 // ============================================================================
457 // Function: addWorkplane
458 // Class: SketchSolver_ConstraintGroup
459 // Purpose: create workplane for the group
460 // ============================================================================
461 bool SketchSolver_ConstraintGroup::addWorkplane(
462 boost::shared_ptr<SketchPlugin_Feature> theSketch)
464 if (myWorkplane.h || theSketch->getKind().compare("Sketch") != 0)
465 return false; // the workplane already exists or the function parameter is not Sketch
467 mySketch = theSketch;
472 // ============================================================================
473 // Function: updateWorkplane
474 // Class: SketchSolver_ConstraintGroup
475 // Purpose: update parameters of workplane
476 // ============================================================================
477 bool SketchSolver_ConstraintGroup::updateWorkplane()
479 // Get parameters of workplane
480 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
481 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
482 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(SKETCH_ATTR_NORM);
483 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
484 // Transform them into SolveSpace format
485 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
486 if (!aNormalWP) return false;
487 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
488 if (!anOriginWP) return false;
493 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
494 // Workplane should be added to the list of entities
495 myEntities.push_back(myWorkplane);
500 // ============================================================================
501 // Function: changeParameter
502 // Class: SketchSolver_ConstraintGroup
503 // Purpose: create/update value of parameter
504 // ============================================================================
505 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
506 const double& theParam,
507 std::vector<Slvs_Param>::const_iterator& thePrmIter)
509 if (thePrmIter != myParams.end())
510 { // Parameter should be updated
511 int aParamPos = thePrmIter - myParams.begin();
512 if (fabs(thePrmIter->val - theParam) > tolerance)
514 myNeedToSolve = true; // parameter is changed, need to resolve constraints
515 myParams[aParamPos].val = theParam;
518 return myParams[aParamPos].h;
521 // Newly created parameter
522 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
523 myParams.push_back(aParam);
524 myNeedToSolve = true;
525 // The list of parameters is changed, move iterator to the end of the list to avoid problems
526 thePrmIter = myParams.end();
530 // ============================================================================
531 // Function: resolveConstraints
532 // Class: SketchSolver_ConstraintGroup
533 // Purpose: solve the set of constraints for the current group
534 // ============================================================================
535 void SketchSolver_ConstraintGroup::resolveConstraints()
540 myConstrSolver.setGroupID(myID);
541 myConstrSolver.setParameters(myParams);
542 myConstrSolver.setEntities(myEntities);
543 myConstrSolver.setConstraints(myConstraints);
544 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
546 int aResult = myConstrSolver.solve();
547 if (aResult == SLVS_RESULT_OKAY)
548 { // solution succeeded, store results into correspondent attributes
549 // Obtain result into the same list of parameters
550 if (!myConstrSolver.getResult(myParams))
553 // We should go through the attributes map, because only attributes have valued parameters
554 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
555 anEntIter = myEntityAttrMap.begin();
556 for ( ; anEntIter != myEntityAttrMap.end(); anEntIter++)
557 updateAttribute(anEntIter->first, anEntIter->second);
559 /// \todo Implement error handling
561 removeTemporaryConstraints();
562 myNeedToSolve = false;
565 // ============================================================================
566 // Function: mergeGroups
567 // Class: SketchSolver_ConstraintGroup
568 // Purpose: append specified group to the current group
569 // ============================================================================
570 void SketchSolver_ConstraintGroup::mergeGroups(
571 const SketchSolver_ConstraintGroup& theGroup)
573 // If specified group is empty, no need to merge
574 if (theGroup.myConstraintMap.empty())
577 // Map between old and new indexes of SolveSpace constraints
578 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
580 // Add all constraints from theGroup to the current group
581 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
582 aConstrIter = theGroup.myConstraintMap.begin();
583 for ( ; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
584 if (changeConstraint(aConstrIter->first))
585 aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
587 // Add temporary constraints from theGroup
588 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
589 for ( ; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++)
591 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(*aTempConstrIter);
592 if (aFind != aConstrMap.end())
593 myTempConstraints.push_back(aFind->second);
596 if (myTempPointWhereDragged.empty())
597 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
598 else if (!theGroup.myTempPointWhereDragged.empty())
599 { // Need to create additional transient constraint
600 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
601 aFeatureIter = theGroup.myEntityAttrMap.begin();
602 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
603 if (aFeatureIter->second == myTempPointWDrgdID)
605 addTemporaryConstraintWhereDragged(aFeatureIter->first);
610 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
613 // ============================================================================
614 // Function: splitGroup
615 // Class: SketchSolver_ConstraintGroup
616 // Purpose: divide the group into several subgroups
617 // ============================================================================
618 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
620 // Divide constraints and entities into several groups
621 std::vector< std::set<Slvs_hEntity> > aGroupsEntities;
622 std::vector< std::set<Slvs_hConstraint> > aGroupsConstr;
623 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
624 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
625 for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
627 Slvs_hEntity aConstrEnt[] = {
628 aConstrIter->ptA, aConstrIter->ptB,
629 aConstrIter->entityA, aConstrIter->entityB};
630 std::vector<int> anIndexes;
631 // Go through the groupped entities and find even one of entities of current constraint
632 std::vector< std::set<Slvs_hEntity> >::iterator aGrEntIter;
633 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++)
635 bool isFound = false;
636 for (int i = 0; i < 4 && !isFound; i++)
637 if (aConstrEnt[i] != 0)
638 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
640 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
642 // Add new group if no one is found
643 if (anIndexes.empty())
645 std::set<Slvs_hEntity> aNewGrEnt;
646 for (int i = 0; i < 4; i++)
647 if (aConstrEnt[i] != 0)
648 aNewGrEnt.insert(aConstrEnt[i]);
649 std::set<Slvs_hConstraint> aNewGrConstr;
650 aNewGrConstr.insert(aConstrIter->h);
652 aGroupsEntities.push_back(aNewGrEnt);
653 aGroupsConstr.push_back(aNewGrConstr);
654 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
655 aMaxNbEntities = aGroupsEntities.size() - 1;
657 else if (anIndexes.size() == 1)
658 { // Add entities indexes into the found group
659 for (int i = 0; i < 4; i++)
660 if (aConstrEnt[i] != 0)
661 aGrEntIter->insert(aConstrEnt[i]);
662 aGroupsConstr[aGrEntIter - aGroupsEntities.begin()].insert(aConstrIter->h);
663 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
664 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
667 { // There are found several connected groups, merge them
668 std::vector< std::set<Slvs_hEntity> >::iterator aFirstGroup =
669 aGroupsEntities.begin() + anIndexes.front();
670 std::vector< std::set<Slvs_hConstraint> >::iterator aFirstConstr =
671 aGroupsConstr.begin() + anIndexes.front();
672 std::vector<int>::iterator anInd = anIndexes.begin();
673 for (++anInd; anInd != anIndexes.end(); anInd++)
675 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
676 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
678 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
679 aMaxNbEntities = anIndexes.front();
680 // Remove merged groups
681 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--)
683 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
684 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
689 if (aGroupsEntities.size() <= 1)
692 // Remove the group with maximum elements as it will be left in the current group
693 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
694 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
696 // Add new groups of constraints and divide current group
697 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
698 for (int i = aGroupsEntities.size(); i > 0; i--)
700 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
701 aNewGroups.push_back(aG);
703 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
704 aConstrMapIter = myConstraintMap.begin();
705 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
706 while (aConstrMapIter != myConstraintMap.end())
708 std::vector< std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
709 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
710 for ( ; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
711 if (aGIter->find(aConstrMapIter->second) != aGIter->end())
713 (*aGroup)->changeConstraint(aConstrMapIter->first);
714 removeConstraint(aConstrMapIter->first);
716 aConstrMapIter = myConstraintMap.begin();
717 for (int i = 0; i < aConstrMapPos; i++)
721 if (aGIter == aGroupsConstr.end())
728 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
731 // ============================================================================
732 // Function: updateGroup
733 // Class: SketchSolver_ConstraintGroup
734 // Purpose: search removed entities and constraints
735 // ============================================================================
736 bool SketchSolver_ConstraintGroup::updateGroup()
738 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
739 aConstrIter = myConstraintMap.rbegin();
740 bool isAllValid = true;
741 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
742 while (isAllValid && aConstrIter != myConstraintMap.rend())
744 if (!aConstrIter->first->data()->isValid())
746 if (aConstrIter->first->getKind().compare("SketchConstraintCoincidence") == 0)
748 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
749 aCopyIter = aConstrIter++;
750 removeConstraint(aCopyIter->first);
756 // Probably, need to update coincidence constraints
757 if (isCCRemoved && !myExtraCoincidence.empty())
759 // Make a copy, because the new list of unused constrtaints will be generated
760 std::set< boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
761 myExtraCoincidence.clear();
763 std::set< boost::shared_ptr<SketchPlugin_Constraint> >::iterator
764 aCIter = anExtraCopy.begin();
765 for ( ; aCIter != anExtraCopy.end(); aCIter++)
766 if ((*aCIter)->data()->isValid())
767 changeConstraint(*aCIter);
773 // ============================================================================
774 // Function: updateAttribute
775 // Class: SketchSolver_ConstraintGroup
776 // Purpose: update features of sketch after resolving constraints
777 // ============================================================================
778 void SketchSolver_ConstraintGroup::updateAttribute(
779 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
780 const Slvs_hEntity& theEntityID)
782 // Search the position of the first parameter of the entity
783 int anEntPos = Search(theEntityID, myEntities);
784 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
786 // Look over supported types of entities
789 boost::shared_ptr<GeomDataAPI_Point> aPoint =
790 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
793 aPoint->setValue(myParams[aFirstParamPos].val,
794 myParams[aFirstParamPos+1].val,
795 myParams[aFirstParamPos+2].val);
800 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
801 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
804 aPoint2D->setValue(myParams[aFirstParamPos].val,
805 myParams[aFirstParamPos+1].val);
810 boost::shared_ptr<ModelAPI_AttributeDouble> aScalar =
811 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
814 aScalar->setValue(myParams[aFirstParamPos].val);
818 /// \todo Support other types of entities
821 // ============================================================================
822 // Function: updateEntityIfPossible
823 // Class: SketchSolver_ConstraintGroup
824 // Purpose: search the entity in this group and update it
825 // ============================================================================
826 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
827 boost::shared_ptr<ModelAPI_Attribute> theEntity)
829 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end())
831 // If the attribute is a point and it is changed (the group needs to rebuild),
832 // probably user has dragged this point into this position,
833 // so it is necessary to add constraint which will guarantee the point will not change
835 // Store myNeedToSolve flag to verify the entity is really changed
836 bool aNeedToSolveCopy = myNeedToSolve;
837 myNeedToSolve = false;
839 changeEntity(theEntity);
841 if (myNeedToSolve) // the entity is changed
843 // Verify the entity is a point and add temporary constraint of permanency
844 boost::shared_ptr<GeomDataAPI_Point> aPoint =
845 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
846 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
847 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
848 if (aPoint || aPoint2D)
849 addTemporaryConstraintWhereDragged(theEntity);
852 // Restore flag of changes
853 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
857 // ============================================================================
858 // Function: addTemporaryConstraintWhereDragged
859 // Class: SketchSolver_ConstraintGroup
860 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
861 // which was moved by user
862 // ============================================================================
863 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
864 boost::shared_ptr<ModelAPI_Attribute> theEntity)
866 // Find identifier of the entity
867 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
868 anEntIter = myEntityAttrMap.find(theEntity);
869 if (anEntIter == myEntityAttrMap.end())
872 // If this is a first dragged point, its parameters should be placed
873 // into Slvs_System::dragged field to avoid system inconsistense
874 if (myTempPointWhereDragged.empty())
876 int anEntPos = Search(anEntIter->second, myEntities);
877 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
878 for (int i = 0; i < 4; i++, aDraggedParam++)
879 if (*aDraggedParam != 0)
880 myTempPointWhereDragged.push_back(*aDraggedParam);
881 myTempPointWDrgdID = myEntities[anEntPos].h;
885 // Get identifiers of all dragged points
886 std::set<Slvs_hEntity> aDraggedPntID;
887 aDraggedPntID.insert(myTempPointWDrgdID);
888 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
889 for ( ; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++)
891 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
892 if (aConstrPos < myConstraints.size())
893 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
895 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
896 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
897 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
899 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
900 continue; // the entity was not found in current set
902 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
903 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
904 for ( ; aDrgIter != aDraggedPntID.end(); aDrgIter++)
905 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
906 return ; // the SLVS_C_WHERE_DRAGGED constraint already exists
909 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
910 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
911 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
912 myConstraints.push_back(aWDConstr);
913 myTempConstraints.push_back(aWDConstr.h);
916 // ============================================================================
917 // Function: removeTemporaryConstraints
918 // Class: SketchSolver_ConstraintGroup
919 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
920 // resolving the set of constraints
921 // ============================================================================
922 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
924 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
925 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
927 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
928 if (aConstrPos >= myConstraints.size())
930 myConstraints.erase(myConstraints.begin() + aConstrPos);
932 // If the removing constraint has higher index, decrease the indexer
933 if (*aTmpConstrIter == myConstrMaxID)
936 myTempConstraints.clear();
938 // Clear basic dragged point
939 myTempPointWhereDragged.clear();
942 // ============================================================================
943 // Function: removeConstraint
944 // Class: SketchSolver_ConstraintGroup
945 // Purpose: remove constraint and all unused entities
946 // ============================================================================
947 void SketchSolver_ConstraintGroup::removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
949 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator
950 anIterToRemove = myConstraintMap.find(theConstraint);
951 if (anIterToRemove == myConstraintMap.end())
954 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
955 // Remove constraint from the map
956 myConstraintMap.erase(anIterToRemove);
958 // Find unused entities
959 int aConstrPos = Search(aCnstrToRemove, myConstraints);
960 std::set<Slvs_hEntity> anEntToRemove;
961 Slvs_hEntity aCnstEnt[] = {myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
962 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
963 for (int i = 0; i < 4; i++)
964 if (aCnstEnt[i] != 0)
965 anEntToRemove.insert(aCnstEnt[i]);
966 myConstraints.erase(myConstraints.begin() + aConstrPos);
967 if (aCnstrToRemove == myConstrMaxID)
969 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
970 for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
972 Slvs_hEntity aEnts[] = {aConstrIter->ptA, aConstrIter->ptB,
973 aConstrIter->entityA, aConstrIter->entityB};
974 for (int i = 0; i < 4; i++)
975 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
976 anEntToRemove.erase(aEnts[i]);
979 if (anEntToRemove.empty())
982 // Remove unused entities
983 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
984 anEntAttrIter = myEntityAttrMap.begin();
985 while (anEntAttrIter != myEntityAttrMap.end())
987 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end())
989 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
990 aRemovedIter = anEntAttrIter;
992 myEntityAttrMap.erase(aRemovedIter);
994 else anEntAttrIter++;
996 std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
997 anEntFeatIter = myEntityFeatMap.begin();
998 while (anEntFeatIter != myEntityFeatMap.end())
1000 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end())
1002 std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
1003 aRemovedIter = anEntFeatIter;
1005 myEntityFeatMap.erase(aRemovedIter);
1007 else anEntFeatIter++;
1009 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1010 for ( ; aRemIter != anEntToRemove.rend(); aRemIter++)
1012 unsigned int anEntPos = Search(*aRemIter, myEntities);
1013 if (anEntPos >= myEntities.size())
1015 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1016 if (aParamPos >= myParams.size())
1019 while (myEntities[anEntPos].param[aNbParams] != 0)
1021 if (myEntities[anEntPos].param[aNbParams-1] == myParamMaxID)
1022 myParamMaxID -= aNbParams;
1023 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1024 if (*aRemIter == myEntityMaxID)
1026 myEntities.erase(myEntities.begin() + anEntPos);
1028 // Remove entity's ID from the lists of conincident points
1029 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1030 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1031 aCoPtIter->erase(*aRemIter);
1033 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1034 myCoincidentPoints.clear();
1038 // ============================================================================
1039 // Function: addCoincidentPoints
1040 // Class: SketchSolver_ConstraintGroup
1041 // Purpose: add coincident point the appropriate list of such points
1042 // ============================================================================
1043 bool SketchSolver_ConstraintGroup::addCoincidentPoints(
1044 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1046 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1047 std::vector< std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1048 while (aCoPtIter != myCoincidentPoints.end())
1050 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1051 aCoPtIter->find(thePoint1) != aCoPtIter->end(),
1052 aCoPtIter->find(thePoint2) != aCoPtIter->end(),
1054 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1056 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1]))
1058 if (aFirstFound != myCoincidentPoints.end())
1059 { // there are two groups of coincident points connected by created constraint => merge them
1060 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1061 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1062 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1063 myCoincidentPoints.erase(aCoPtIter);
1064 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1065 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1070 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1071 aFirstFound = aCoPtIter;
1076 // No points were found, need to create new set
1077 if (aFirstFound == myCoincidentPoints.end())
1079 std::set<Slvs_hEntity> aNewSet;
1080 aNewSet.insert(thePoint1);
1081 aNewSet.insert(thePoint2);
1082 myCoincidentPoints.push_back(aNewSet);
1091 // ========================================================
1092 // ========= Auxiliary functions ===============
1093 // ========================================================
1095 template <typename T>
1096 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1098 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1099 int aVecSize = theEntities.size();
1100 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1102 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
1104 if (aResIndex == -1)
1105 aResIndex = aVecSize;