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_KIND) != 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 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
660 for (int i = 0; i < 4; i++)
661 if (aConstrEnt[i] != 0)
662 aGrEntIter->insert(aConstrEnt[i]);
663 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
664 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
665 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
668 { // There are found several connected groups, merge them
669 std::vector< std::set<Slvs_hEntity> >::iterator aFirstGroup =
670 aGroupsEntities.begin() + anIndexes.front();
671 std::vector< std::set<Slvs_hConstraint> >::iterator aFirstConstr =
672 aGroupsConstr.begin() + anIndexes.front();
673 std::vector<int>::iterator anInd = anIndexes.begin();
674 for (++anInd; anInd != anIndexes.end(); anInd++)
676 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
677 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
679 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
680 aMaxNbEntities = anIndexes.front();
681 // Remove merged groups
682 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--)
684 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
685 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
690 if (aGroupsEntities.size() <= 1)
693 // Remove the group with maximum elements as it will be left in the current group
694 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
695 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
697 // Add new groups of constraints and divide current group
698 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
699 for (int i = aGroupsEntities.size(); i > 0; i--)
701 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
702 aNewGroups.push_back(aG);
704 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
705 aConstrMapIter = myConstraintMap.begin();
706 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
707 while (aConstrMapIter != myConstraintMap.end())
709 std::vector< std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
710 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
711 for ( ; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
712 if (aGIter->find(aConstrMapIter->second) != aGIter->end())
714 (*aGroup)->changeConstraint(aConstrMapIter->first);
715 removeConstraint(aConstrMapIter->first);
717 aConstrMapIter = myConstraintMap.begin();
718 for (int i = 0; i < aConstrMapPos; i++)
722 if (aGIter == aGroupsConstr.end())
729 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
732 // ============================================================================
733 // Function: updateGroup
734 // Class: SketchSolver_ConstraintGroup
735 // Purpose: search removed entities and constraints
736 // ============================================================================
737 bool SketchSolver_ConstraintGroup::updateGroup()
739 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
740 aConstrIter = myConstraintMap.rbegin();
741 bool isAllValid = true;
742 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
743 while (isAllValid && aConstrIter != myConstraintMap.rend())
745 if (!aConstrIter->first->data()->isValid())
747 if (aConstrIter->first->getKind().compare("SketchConstraintCoincidence") == 0)
749 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
750 aCopyIter = aConstrIter++;
751 removeConstraint(aCopyIter->first);
757 // Probably, need to update coincidence constraints
758 if (isCCRemoved && !myExtraCoincidence.empty())
760 // Make a copy, because the new list of unused constrtaints will be generated
761 std::set< boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
762 myExtraCoincidence.clear();
764 std::set< boost::shared_ptr<SketchPlugin_Constraint> >::iterator
765 aCIter = anExtraCopy.begin();
766 for ( ; aCIter != anExtraCopy.end(); aCIter++)
767 if ((*aCIter)->data()->isValid())
768 changeConstraint(*aCIter);
774 // ============================================================================
775 // Function: updateAttribute
776 // Class: SketchSolver_ConstraintGroup
777 // Purpose: update features of sketch after resolving constraints
778 // ============================================================================
779 void SketchSolver_ConstraintGroup::updateAttribute(
780 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
781 const Slvs_hEntity& theEntityID)
783 // Search the position of the first parameter of the entity
784 int anEntPos = Search(theEntityID, myEntities);
785 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
787 // Look over supported types of entities
790 boost::shared_ptr<GeomDataAPI_Point> aPoint =
791 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
794 aPoint->setValue(myParams[aFirstParamPos].val,
795 myParams[aFirstParamPos+1].val,
796 myParams[aFirstParamPos+2].val);
801 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
802 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
805 aPoint2D->setValue(myParams[aFirstParamPos].val,
806 myParams[aFirstParamPos+1].val);
811 boost::shared_ptr<ModelAPI_AttributeDouble> aScalar =
812 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
815 aScalar->setValue(myParams[aFirstParamPos].val);
819 /// \todo Support other types of entities
822 // ============================================================================
823 // Function: updateEntityIfPossible
824 // Class: SketchSolver_ConstraintGroup
825 // Purpose: search the entity in this group and update it
826 // ============================================================================
827 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
828 boost::shared_ptr<ModelAPI_Attribute> theEntity)
830 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end())
832 // If the attribute is a point and it is changed (the group needs to rebuild),
833 // probably user has dragged this point into this position,
834 // so it is necessary to add constraint which will guarantee the point will not change
836 // Store myNeedToSolve flag to verify the entity is really changed
837 bool aNeedToSolveCopy = myNeedToSolve;
838 myNeedToSolve = false;
840 changeEntity(theEntity);
842 if (myNeedToSolve) // the entity is changed
844 // Verify the entity is a point and add temporary constraint of permanency
845 boost::shared_ptr<GeomDataAPI_Point> aPoint =
846 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
847 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
848 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
849 if (aPoint || aPoint2D)
850 addTemporaryConstraintWhereDragged(theEntity);
853 // Restore flag of changes
854 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
858 // ============================================================================
859 // Function: addTemporaryConstraintWhereDragged
860 // Class: SketchSolver_ConstraintGroup
861 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
862 // which was moved by user
863 // ============================================================================
864 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
865 boost::shared_ptr<ModelAPI_Attribute> theEntity)
867 // Find identifier of the entity
868 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
869 anEntIter = myEntityAttrMap.find(theEntity);
870 if (anEntIter == myEntityAttrMap.end())
873 // If this is a first dragged point, its parameters should be placed
874 // into Slvs_System::dragged field to avoid system inconsistense
875 if (myTempPointWhereDragged.empty())
877 int anEntPos = Search(anEntIter->second, myEntities);
878 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
879 for (int i = 0; i < 4; i++, aDraggedParam++)
880 if (*aDraggedParam != 0)
881 myTempPointWhereDragged.push_back(*aDraggedParam);
882 myTempPointWDrgdID = myEntities[anEntPos].h;
886 // Get identifiers of all dragged points
887 std::set<Slvs_hEntity> aDraggedPntID;
888 aDraggedPntID.insert(myTempPointWDrgdID);
889 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
890 for ( ; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++)
892 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
893 if (aConstrPos < myConstraints.size())
894 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
896 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
897 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
898 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
900 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
901 continue; // the entity was not found in current set
903 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
904 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
905 for ( ; aDrgIter != aDraggedPntID.end(); aDrgIter++)
906 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
907 return ; // the SLVS_C_WHERE_DRAGGED constraint already exists
910 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
911 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
912 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
913 myConstraints.push_back(aWDConstr);
914 myTempConstraints.push_back(aWDConstr.h);
917 // ============================================================================
918 // Function: removeTemporaryConstraints
919 // Class: SketchSolver_ConstraintGroup
920 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
921 // resolving the set of constraints
922 // ============================================================================
923 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
925 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
926 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
928 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
929 if (aConstrPos >= myConstraints.size())
931 myConstraints.erase(myConstraints.begin() + aConstrPos);
933 // If the removing constraint has higher index, decrease the indexer
934 if (*aTmpConstrIter == myConstrMaxID)
937 myTempConstraints.clear();
939 // Clear basic dragged point
940 myTempPointWhereDragged.clear();
943 // ============================================================================
944 // Function: removeConstraint
945 // Class: SketchSolver_ConstraintGroup
946 // Purpose: remove constraint and all unused entities
947 // ============================================================================
948 void SketchSolver_ConstraintGroup::removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
950 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator
951 anIterToRemove = myConstraintMap.find(theConstraint);
952 if (anIterToRemove == myConstraintMap.end())
955 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
956 // Remove constraint from the map
957 myConstraintMap.erase(anIterToRemove);
959 // Find unused entities
960 int aConstrPos = Search(aCnstrToRemove, myConstraints);
961 std::set<Slvs_hEntity> anEntToRemove;
962 Slvs_hEntity aCnstEnt[] = {myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
963 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
964 for (int i = 0; i < 4; i++)
965 if (aCnstEnt[i] != 0)
966 anEntToRemove.insert(aCnstEnt[i]);
967 myConstraints.erase(myConstraints.begin() + aConstrPos);
968 if (aCnstrToRemove == myConstrMaxID)
970 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
971 for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
973 Slvs_hEntity aEnts[] = {aConstrIter->ptA, aConstrIter->ptB,
974 aConstrIter->entityA, aConstrIter->entityB};
975 for (int i = 0; i < 4; i++)
976 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
977 anEntToRemove.erase(aEnts[i]);
980 if (anEntToRemove.empty())
983 // Remove unused entities
984 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
985 anEntAttrIter = myEntityAttrMap.begin();
986 while (anEntAttrIter != myEntityAttrMap.end())
988 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end())
990 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
991 aRemovedIter = anEntAttrIter;
993 myEntityAttrMap.erase(aRemovedIter);
995 else anEntAttrIter++;
997 std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
998 anEntFeatIter = myEntityFeatMap.begin();
999 while (anEntFeatIter != myEntityFeatMap.end())
1001 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end())
1003 std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
1004 aRemovedIter = anEntFeatIter;
1006 myEntityFeatMap.erase(aRemovedIter);
1008 else anEntFeatIter++;
1010 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1011 for ( ; aRemIter != anEntToRemove.rend(); aRemIter++)
1013 unsigned int anEntPos = Search(*aRemIter, myEntities);
1014 if (anEntPos >= myEntities.size())
1016 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1017 if (aParamPos >= myParams.size())
1020 while (myEntities[anEntPos].param[aNbParams] != 0)
1022 if (myEntities[anEntPos].param[aNbParams-1] == myParamMaxID)
1023 myParamMaxID -= aNbParams;
1024 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1025 if (*aRemIter == myEntityMaxID)
1027 myEntities.erase(myEntities.begin() + anEntPos);
1029 // Remove entity's ID from the lists of conincident points
1030 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1031 for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1032 aCoPtIter->erase(*aRemIter);
1034 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1035 myCoincidentPoints.clear();
1039 // ============================================================================
1040 // Function: addCoincidentPoints
1041 // Class: SketchSolver_ConstraintGroup
1042 // Purpose: add coincident point the appropriate list of such points
1043 // ============================================================================
1044 bool SketchSolver_ConstraintGroup::addCoincidentPoints(
1045 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1047 std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1048 std::vector< std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1049 while (aCoPtIter != myCoincidentPoints.end())
1051 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1052 aCoPtIter->find(thePoint1) != aCoPtIter->end(),
1053 aCoPtIter->find(thePoint2) != aCoPtIter->end(),
1055 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1057 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1]))
1059 if (aFirstFound != myCoincidentPoints.end())
1060 { // there are two groups of coincident points connected by created constraint => merge them
1061 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1062 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1063 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1064 myCoincidentPoints.erase(aCoPtIter);
1065 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1066 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1071 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1072 aFirstFound = aCoPtIter;
1077 // No points were found, need to create new set
1078 if (aFirstFound == myCoincidentPoints.end())
1080 std::set<Slvs_hEntity> aNewSet;
1081 aNewSet.insert(thePoint1);
1082 aNewSet.insert(thePoint2);
1083 myCoincidentPoints.push_back(aNewSet);
1092 // ========================================================
1093 // ========= Auxiliary functions ===============
1094 // ========================================================
1096 template <typename T>
1097 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1099 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1100 int aVecSize = theEntities.size();
1101 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1103 while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
1105 if (aResIndex == -1)
1106 aResIndex = aVecSize;