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 <GeomAPI_Pnt2d.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeRefList.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_ResultConstruction.h>
22 #include <SketchPlugin_Constraint.h>
23 #include <SketchPlugin_ConstraintLength.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintRigid.h>
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Circle.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_Point.h>
31 #include <SketchPlugin_Sketch.h>
36 /// Tolerance for value of parameters
37 const double tolerance = 1.e-10;
40 * Collects all sketch solver error' codes
41 * as inline static functions
42 * TODO: Move this class into a separate file
44 class SketchSolver_Error
47 /// The value parameter for the constraint
48 inline static const std::string& CONSTRAINTS()
50 static const std::string MY_ERROR_VALUE("Conflicting constraints");
51 return MY_ERROR_VALUE;
55 /// This value is used to give unique index to the groups
56 static Slvs_hGroup myGroupIndexer = 0;
58 /** \brief Search the entity/parameter with specified ID in the list of elements
59 * \param[in] theEntityID unique ID of the element
60 * \param[in] theEntities list of elements
61 * \return position of the found element or -1 if the element is not found
64 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
66 // ========================================================
67 // ========= SketchSolver_ConstraintGroup ===============
68 // ========================================================
70 SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup(
71 boost::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
72 : myID(++myGroupIndexer),
82 myEntOfConstr.clear();
83 myConstraints.clear();
85 myTempConstraints.clear();
86 myTempPointWhereDragged.clear();
87 myTempPointWDrgdID = 0;
89 // Initialize workplane
90 myWorkplane.h = SLVS_E_UNKNOWN;
92 assert(addWorkplane(theWorkplane));
94 addWorkplane(theWorkplane);
98 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
102 myEntOfConstr.clear();
103 myConstraints.clear();
104 myConstraintMap.clear();
105 myTempConstraints.clear();
106 myTempPointWhereDragged.clear();
108 // If the group with maximal identifier is deleted, decrease the indexer
109 if (myID == myGroupIndexer)
113 // ============================================================================
114 // Function: isBaseWorkplane
115 // Class: SketchSolver_ConstraintGroup
116 // Purpose: verify the group is based on the given workplane
117 // ============================================================================
118 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
119 boost::shared_ptr<ModelAPI_CompositeFeature> theWorkplane) const
121 return theWorkplane == mySketch;
124 // ============================================================================
125 // Function: isInteract
126 // Class: SketchSolver_ConstraintGroup
127 // Purpose: verify are there any entities in the group used by given constraint
128 // ============================================================================
129 bool SketchSolver_ConstraintGroup::isInteract(
130 boost::shared_ptr<SketchPlugin_Feature> theFeature) const
132 // Check the group is empty
136 // Go through the attributes and verify if some of them already in the group
137 std::list<boost::shared_ptr<ModelAPI_Attribute>>
138 anAttrList = theFeature->data()->attributes(std::string());
139 std::list<boost::shared_ptr<ModelAPI_Attribute>>::const_iterator
140 anAttrIter = anAttrList.begin();
141 for ( ; anAttrIter != anAttrList.end(); anAttrIter++) {
142 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
143 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
144 if (!aCAttrRef || !aCAttrRef->isObject()) {
145 boost::shared_ptr<ModelAPI_Attribute> anAttr =
146 aCAttrRef ? aCAttrRef->attr() : *anAttrIter;
147 if (myEntityAttrMap.find(anAttr) != myEntityAttrMap.end())
150 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
151 aCAttrRef->object());
154 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
155 FeaturePtr aFeature = aDoc->feature(aRC);
156 if (myEntityFeatMap.find(aFeature) != myEntityFeatMap.end())
158 // search attributes of a feature to be parameters of constraint
159 std::list<boost::shared_ptr<ModelAPI_Attribute> > aFeatAttrList =
160 aFeature->data()->attributes(std::string());
161 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator aFAIter = aFeatAttrList
163 for (; aFAIter != aFeatAttrList.end(); aFAIter++)
164 if (myEntityAttrMap.find(*aFAIter) != myEntityAttrMap.end())
169 // Entities did not found
173 // ============================================================================
174 // Function: checkConstraintConsistence
175 // Class: SketchSolver_ConstraintGroup
176 // Purpose: verifies and changes parameters of the constraint
177 // ============================================================================
178 void SketchSolver_ConstraintGroup::checkConstraintConsistence(Slvs_Constraint& theConstraint)
180 if (theConstraint.type == SLVS_C_PT_LINE_DISTANCE) {
181 // Get constraint parameters and check the sign of constraint value
184 int aPtPos = Search(theConstraint.ptA, myEntities);
185 int aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
186 boost::shared_ptr<GeomAPI_XY> aPoint(
187 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
190 int aLnPos = Search(theConstraint.entityA, myEntities);
191 aPtPos = Search(myEntities[aLnPos].point[0], myEntities);
192 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
193 boost::shared_ptr<GeomAPI_XY> aStart(
194 new GeomAPI_XY(-myParams[aPtParamPos].val, -myParams[aPtParamPos + 1].val));
195 aPtPos = Search(myEntities[aLnPos].point[1], myEntities);
196 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
197 boost::shared_ptr<GeomAPI_XY> aEnd(
198 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
200 aEnd = aEnd->added(aStart);
201 aPoint = aPoint->added(aStart);
202 if (aPoint->cross(aEnd) * theConstraint.valA < 0.0)
203 theConstraint.valA *= -1.0;
207 // ============================================================================
208 // Function: changeConstraint
209 // Class: SketchSolver_ConstraintGroup
210 // Purpose: create/update the constraint in the group
211 // ============================================================================
212 bool SketchSolver_ConstraintGroup::changeConstraint(
213 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
215 // There is no workplane yet, something wrong
216 if (myWorkplane.h == SLVS_E_UNKNOWN)
219 if (theConstraint && theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID())
220 return changeRigidConstraint(theConstraint);
222 // Search this constraint in the current group to update it
223 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.find(theConstraint);
224 std::vector<Slvs_Constraint>::iterator aConstrIter;
225 if (aConstrMapIter != myConstraintMap.end()) {
226 int aConstrPos = Search(aConstrMapIter->second.front(), myConstraints);
227 aConstrIter = myConstraints.begin() + aConstrPos;
230 // Get constraint type and verify the constraint parameters are correct
231 SketchSolver_Constraint aConstraint(theConstraint);
232 int aConstrType = aConstraint.getType();
233 if (aConstrType == SLVS_C_UNKNOWN
234 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
236 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
238 // Create constraint parameters
239 double aDistance = 0.0; // scalar value of the constraint
240 AttributeDoublePtr aDistAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
241 theConstraint->data()->attribute(SketchPlugin_Constraint::VALUE()));
243 aDistance = aDistAttr->value();
244 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
245 if (aConstrType == SLVS_C_DIAMETER)
247 if (aConstrMapIter != myConstraintMap.end()
248 && fabs(aConstrIter->valA - aDistance) > tolerance) {
249 myNeedToSolve = true;
250 aConstrIter->valA = aDistance;
254 size_t aNbTmpConstraints = myTempConstraints.size();
255 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
256 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
257 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
258 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = boost::dynamic_pointer_cast<
259 ModelAPI_AttributeRefAttr>(
260 theConstraint->data()->attribute(aConstraintAttributes[indAttr]));
264 // Convert the object of the attribute to the feature
266 if (aConstrAttr->isObject() && aConstrAttr->object()) {
267 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
268 aConstrAttr->object());
271 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
272 aFeature = aDoc->feature(aRC);
275 // For the length constraint the start and end points of the line should be added to the entities list instead of line
276 if (aConstrType == SLVS_C_PT_PT_DISTANCE
277 && theConstraint->getKind().compare(SketchPlugin_ConstraintLength::ID()) == 0) {
278 Slvs_hEntity aLineEnt = changeEntity(aFeature);
279 int aEntPos = Search(aLineEnt, myEntities);
280 aConstrEnt[indAttr++] = myEntities[aEntPos].point[0];
281 aConstrEnt[indAttr++] = myEntities[aEntPos].point[1];
282 while (indAttr < CONSTRAINT_ATTR_SIZE)
283 aConstrEnt[indAttr++] = 0;
284 break; // there should be no other entities
285 } else if (aConstrAttr->isObject())
286 aConstrEnt[indAttr] = changeEntity(aFeature);
288 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
291 if (aConstrMapIter == myConstraintMap.end()) { // Add new constraint
292 // Several points may be coincident, it is not necessary to store all constraints between them.
293 // Try to find sequence of coincident points which connects the points of new constraint
294 if (aConstrType == SLVS_C_POINTS_COINCIDENT) {
295 if (aConstrEnt[0] == aConstrEnt[1]) // no need to add self coincidence
297 if (!addCoincidentPoints(aConstrEnt[0], aConstrEnt[1])) {
298 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
301 if (aNbTmpConstraints < myTempConstraints.size()) {
302 // There was added temporary constraint. Check that there is no coincident points which already rigid.
304 // Get list of already fixed points
305 std::set<Slvs_hEntity> anAlreadyFixed;
306 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
307 for (; aCIter != myConstraints.end(); aCIter++)
308 if (aCIter->type == SLVS_C_WHERE_DRAGGED) {
309 std::list<Slvs_hConstraint>::const_iterator aTmpIt = myTempConstraints.begin();
310 for (; aTmpIt != myTempConstraints.end(); aTmpIt++)
311 if (*aTmpIt == aCIter->h)
313 if (aTmpIt == myTempConstraints.end())
314 anAlreadyFixed.insert(aCIter->ptA);
317 std::set<Slvs_hConstraint> aTmpConstrToDelete;
318 std::list<Slvs_hConstraint>::reverse_iterator aTmpIter = myTempConstraints.rbegin();
319 size_t aCurSize = myTempConstraints.size();
320 for (; aCurSize > aNbTmpConstraints && aTmpIter != myTempConstraints.rend();
321 aTmpIter++, aCurSize--) {
322 int aConstrPos = Search(*aTmpIter, myConstraints);
323 std::vector<std::set<Slvs_hEntity> >::const_iterator
324 aCoincIter = myCoincidentPoints.begin();
325 for (; aCoincIter != myCoincidentPoints.end(); aCoincIter++)
326 if (aCoincIter->find(myConstraints[aConstrPos].ptA) != aCoincIter->end()) {
327 std::set<Slvs_hEntity>::const_iterator anIt;
328 for (anIt = aCoincIter->begin(); anIt != aCoincIter->end(); anIt++)
329 if (anAlreadyFixed.find(*anIt) != anAlreadyFixed.end()) {
330 aTmpConstrToDelete.insert(*aTmpIter);
336 if (!aTmpConstrToDelete.empty())
337 removeTemporaryConstraints(aTmpConstrToDelete);
341 // Create SolveSpace constraint structure
342 Slvs_Constraint aConstraint = Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType,
343 myWorkplane.h, aDistance, aConstrEnt[0],
344 aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
345 myConstraints.push_back(aConstraint);
346 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>(1, aConstraint.h);
347 int aConstrPos = Search(aConstraint.h, myConstraints);
348 aConstrIter = myConstraints.begin() + aConstrPos;
349 myNeedToSolve = true;
350 } else { // Attributes of constraint may be changed => update constraint
351 Slvs_hEntity* aCurrentAttr[] = {&aConstrIter->ptA, &aConstrIter->ptB,
352 &aConstrIter->entityA, &aConstrIter->entityB,
353 &aConstrIter->entityC, &aConstrIter->entityD};
354 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
355 if (*(aCurrentAttr[indAttr]) != aConstrEnt[indAttr])
357 *(aCurrentAttr[indAttr]) = aConstrEnt[indAttr];
358 myNeedToSolve = true;
363 // Update flags of entities to be used by constraints
364 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
365 if (aConstrEnt[indAttr] != 0) {
366 int aPos = Search(aConstrEnt[indAttr], myEntities);
367 myEntOfConstr[aPos] = true;
368 // Sub-entities should be used implcitly
369 Slvs_hEntity* aEntPtr = myEntities[aPos].point;
370 while (*aEntPtr != 0) {
371 aPos = Search(*aEntPtr, myEntities);
372 myEntOfConstr[aPos] = true;
377 checkConstraintConsistence(*aConstrIter);
381 // ============================================================================
382 // Function: changeRigidConstraint
383 // Class: SketchSolver_ConstraintGroup
384 // Purpose: create/update the "Rigid" constraint in the group
385 // ============================================================================
386 bool SketchSolver_ConstraintGroup::changeRigidConstraint(
387 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
389 // Search this constraint in the current group to update it
390 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.find(theConstraint);
391 std::vector<Slvs_Constraint>::iterator aConstrIter;
392 if (aConstrMapIter != myConstraintMap.end()) {
393 int aConstrPos = Search(aConstrMapIter->second.front(), myConstraints);
394 aConstrIter = myConstraints.begin() + aConstrPos;
397 // Get constraint type and verify the constraint parameters are correct
398 SketchSolver_Constraint aConstraint(theConstraint);
399 int aConstrType = aConstraint.getType();
400 if (aConstrType == SLVS_C_UNKNOWN
401 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
403 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
405 Slvs_hEntity aConstrEnt = SLVS_E_UNKNOWN;
406 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = boost::dynamic_pointer_cast<
407 ModelAPI_AttributeRefAttr>(
408 theConstraint->data()->attribute(aConstraintAttributes[0]));
412 // Convert the object of the attribute to the feature
414 if (aConstrAttr->isObject() && aConstrAttr->object()) {
415 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
416 aConstrAttr->object());
419 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
420 aFeature = aDoc->feature(aRC);
423 aConstrEnt = aConstrAttr->isObject() ? changeEntity(aFeature) : changeEntity(aConstrAttr->attr());
425 if (aConstrMapIter == myConstraintMap.end()) { // Add new constraint
426 // Check the fixed entity is not a point.
427 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = boost::dynamic_pointer_cast<
428 ModelAPI_AttributeRefAttr>(theConstraint->data()->attribute(aConstraintAttributes[0]));
429 boost::shared_ptr<GeomDataAPI_Point> aPoint =
430 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aConstrAttr->attr());
431 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
432 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aConstrAttr->attr());
433 if (aPoint || aPoint2D) {
434 // Create SolveSpace constraint structure
435 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
436 ++myConstrMaxID, myID, aConstrType, myWorkplane.h, 0.0,
437 aConstrEnt, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
438 myConstraints.push_back(aConstraint);
439 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>(1, aConstraint.h);
440 int aConstrPos = Search(aConstraint.h, myConstraints);
441 aConstrIter = myConstraints.begin() + aConstrPos;
442 myNeedToSolve = true;
444 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>();
446 // To avoid SolveSpace problems:
447 // * if the circle is rigid, we will fix its center and radius;
448 // * if the arc is rigid, we will fix its start and end points and radius.
449 double aRadius = 0.0;
451 bool isCircle = false;
453 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
454 boost::shared_ptr<GeomDataAPI_Point2D> aCenter =
455 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
456 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
457 boost::shared_ptr<GeomDataAPI_Point2D> aStart =
458 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
459 aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
460 aRadius = aStart->pnt()->distance(aCenter->pnt());
462 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
463 aRadius = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
464 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
469 // Get list of already fixed points
470 std::set<Slvs_hEntity> anAlreadyFixed;
471 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
472 for (; aCIter != myConstraints.end(); aCIter++)
473 if (aCIter->type == SLVS_C_WHERE_DRAGGED)
474 anAlreadyFixed.insert(aCIter->ptA);
476 // Create constraints to fix the parameters of the entity
477 int aEntPos = Search(aConstrEnt, myEntities);
478 Slvs_hEntity* aPointsPtr = myEntities[aEntPos].point;
479 if (isArc) aPointsPtr++; // avoid to fix center of arc
480 while (*aPointsPtr != 0) {
481 // Avoid to create additional "Rigid" constraints for coincident points
482 bool isCoincAlreadyFixed = false;
483 if (!anAlreadyFixed.empty()) {
484 if (anAlreadyFixed.find(*aPointsPtr) != anAlreadyFixed.end())
485 isCoincAlreadyFixed = true;
487 std::vector<std::set<Slvs_hEntity> >::const_iterator aCoincIter =
488 myCoincidentPoints.begin();
489 for (; !isCoincAlreadyFixed && aCoincIter != myCoincidentPoints.end(); aCoincIter++) {
490 if (aCoincIter->find(*aPointsPtr) == aCoincIter->end())
492 std::set<Slvs_hEntity>::const_iterator anIter = anAlreadyFixed.begin();
493 for (; !isCoincAlreadyFixed && anIter != anAlreadyFixed.end(); anIter++)
494 if (aCoincIter->find(*anIter) != aCoincIter->end())
495 isCoincAlreadyFixed = true;
499 if (!isCoincAlreadyFixed) {
500 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
501 ++myConstrMaxID, myID, aConstrType, myWorkplane.h, 0.0,
502 *aPointsPtr, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
503 myConstraints.push_back(aConstraint);
504 myConstraintMap[theConstraint].push_back(aConstraint.h);
509 if (isArc || isCircle) { // add radius constraint
510 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
511 ++myConstrMaxID, myID, SLVS_C_DIAMETER, myWorkplane.h, 2.0 * aRadius,
512 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aConstrEnt, SLVS_E_UNKNOWN);
513 myConstraints.push_back(aConstraint);
514 myConstraintMap[theConstraint].push_back(aConstraint.h);
517 // The object is already rigid, so there is no constraints added
518 if (myConstraintMap[theConstraint].empty()) {
519 myConstraintMap.erase(theConstraint);
520 myNeedToSolve = false;
523 myNeedToSolve = true;
529 // ============================================================================
530 // Function: changeEntity
531 // Class: SketchSolver_ConstraintGroup
532 // Purpose: create/update the element affected by any constraint
533 // ============================================================================
534 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
535 boost::shared_ptr<ModelAPI_Attribute> theEntity)
537 // If the entity is already in the group, try to find it
538 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
539 myEntityAttrMap.find(theEntity);
541 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
542 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
543 aParamIter = myParams.end();
544 else { // the entity already exists
545 aEntPos = Search(aEntIter->second, myEntities);
546 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
547 aParamIter = myParams.begin() + aParamPos;
549 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
550 const bool isNeedToSolve = myNeedToSolve;
551 myNeedToSolve = false;
554 // Verify that the entity is not used by "Rigid" constraint.
555 // If it is used, the object should not move.
556 std::vector<std::set<Slvs_hEntity> >::iterator aCoincIter = myCoincidentPoints.begin();
557 for (; aCoincIter != myCoincidentPoints.end(); aCoincIter++)
558 if (aCoincIter->find(aEntIter->second) != aCoincIter->end())
560 std::set<Slvs_hEntity> aCoincident;
561 if (aCoincIter != myCoincidentPoints.end())
562 aCoincident = *aCoincIter;
564 aCoincident.insert(aEntIter->second);
566 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
567 for (; aConstrIter != myConstraints.end(); aConstrIter++)
568 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
569 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
570 myNeedToSolve = true;
571 return aEntIter->second;
575 // Look over supported types of entities
576 Slvs_Entity aNewEntity;
577 aNewEntity.h = SLVS_E_UNKNOWN;
580 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
583 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
584 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
585 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
586 if (!isEntExists) // New entity
587 aNewEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
589 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
590 if (myWorkplane.h == SLVS_E_UNKNOWN)
591 return SLVS_E_UNKNOWN;
594 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
595 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
597 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
598 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
599 if (!isEntExists) // New entity
600 aNewEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
602 // Scalar value (used for the distance entities)
603 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
605 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
606 if (!isEntExists) // New entity
607 aNewEntity = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
611 /// \todo Other types of entities
613 Slvs_hEntity aResult = SLVS_E_UNKNOWN; // Unsupported or wrong entity type
616 if (!myEntOfConstr[aEntPos]) // the entity is not used by constraints, no need to resolve them
617 myNeedToSolve = isNeedToSolve;
619 myNeedToSolve = myNeedToSolve || isNeedToSolve;
620 aResult = aEntIter->second;
621 } else if (aNewEntity.h != SLVS_E_UNKNOWN) {
622 myEntities.push_back(aNewEntity);
623 myEntOfConstr.push_back(false);
624 myEntityAttrMap[theEntity] = aNewEntity.h;
625 aResult = aNewEntity.h;
628 // If the attribute was changed by the user, we need to fix it before solving
629 if (myNeedToSolve && theEntity->isImmutable())
630 addTemporaryConstraintWhereDragged(theEntity, false);
635 // ============================================================================
636 // Function: changeEntity
637 // Class: SketchSolver_ConstraintGroup
638 // Purpose: create/update the element defined by the feature affected by any constraint
639 // ============================================================================
640 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(FeaturePtr theEntity)
642 if (!theEntity->data()->isValid())
643 return SLVS_E_UNKNOWN;
644 // If the entity is already in the group, try to find it
645 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aEntIter = myEntityFeatMap.find(theEntity);
646 // defines that the entity already exists
647 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
649 Slvs_Entity aNewEntity;
650 aNewEntity.h = SLVS_E_UNKNOWN;
652 // SketchPlugin features
653 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
654 SketchPlugin_Feature>(theEntity);
655 if (aFeature) { // Verify the feature by its kind
656 const std::string& aFeatureKind = aFeature->getKind();
657 AttributePtr anAttribute;
660 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0) {
661 anAttribute = aFeature->data()->attribute(SketchPlugin_Line::START_ID());
662 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
663 Slvs_hEntity aStart = changeEntity(anAttribute);
665 anAttribute = aFeature->data()->attribute(SketchPlugin_Line::END_ID());
666 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
667 Slvs_hEntity aEnd = changeEntity(anAttribute);
669 if (!isEntExists) // New entity
670 aNewEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
673 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
674 anAttribute = aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID());
675 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
676 Slvs_hEntity aCenter = changeEntity(anAttribute);
678 anAttribute = aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID());
679 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
680 Slvs_hEntity aRadius = changeEntity(anAttribute);
682 if (!isEntExists) // New entity
683 aNewEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
684 myWorkplane.normal, aRadius);
687 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
688 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID());
689 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
690 Slvs_hEntity aCenter = changeEntity(anAttribute);
692 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::START_ID());
693 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
694 Slvs_hEntity aStart = changeEntity(anAttribute);
696 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::END_ID());
697 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
698 Slvs_hEntity aEnd = changeEntity(anAttribute);
701 aNewEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
702 myWorkplane.normal, aCenter, aStart, aEnd);
704 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
705 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
706 anAttribute = aFeature->data()->attribute(SketchPlugin_Point::COORD_ID());
707 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
708 Slvs_hEntity aPoint = changeEntity(anAttribute);
711 return aEntIter->second;
713 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
714 myEntityFeatMap[theEntity] = aPoint;
715 myNeedToSolve = true;
719 /// \todo Other types of features
722 return aEntIter->second;
724 if (aNewEntity.h != SLVS_E_UNKNOWN) {
725 myEntities.push_back(aNewEntity);
726 myEntOfConstr.push_back(false);
727 myEntityFeatMap[theEntity] = aNewEntity.h;
728 myNeedToSolve = true;
732 // Unsupported or wrong entity type
733 return SLVS_E_UNKNOWN;
736 // ============================================================================
737 // Function: changeNormal
738 // Class: SketchSolver_ConstraintGroup
739 // Purpose: create/update the normal of workplane
740 // ============================================================================
741 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
742 boost::shared_ptr<ModelAPI_Attribute> theDirX, boost::shared_ptr<ModelAPI_Attribute> theDirY,
743 boost::shared_ptr<ModelAPI_Attribute> theNorm)
745 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
746 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
747 if (!aDirX || !aDirY || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance)
748 || (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
749 return SLVS_E_UNKNOWN;
751 // quaternion parameters of normal vector
752 double qw, qx, qy, qz;
753 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
755 double aNormCoord[4] = { qw, qx, qy, qz };
757 // Try to find existent normal
758 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
759 myEntityAttrMap.find(theNorm);
760 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
761 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
762 aParamIter = myParams.end();
763 else { // the entity already exists, update it
764 int aEntPos = Search(aEntIter->second, myEntities);
765 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
766 aParamIter = myParams.begin() + aParamPos;
769 // Change parameters of the normal
770 Slvs_hParam aNormParams[4];
771 for (int i = 0; i < 4; i++)
772 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
774 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
775 return aEntIter->second;
778 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
779 aNormParams[2], aNormParams[3]);
780 myEntities.push_back(aNormal);
781 myEntOfConstr.push_back(false);
782 myEntityAttrMap[theNorm] = aNormal.h;
786 // ============================================================================
787 // Function: addWorkplane
788 // Class: SketchSolver_ConstraintGroup
789 // Purpose: create workplane for the group
790 // ============================================================================
791 bool SketchSolver_ConstraintGroup::addWorkplane(boost::shared_ptr<ModelAPI_CompositeFeature> theSketch)
793 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
794 return false; // the workplane already exists or the function parameter is not Sketch
796 mySketch = theSketch;
801 // ============================================================================
802 // Function: updateWorkplane
803 // Class: SketchSolver_ConstraintGroup
804 // Purpose: update parameters of workplane
805 // ============================================================================
806 bool SketchSolver_ConstraintGroup::updateWorkplane()
808 // Get parameters of workplane
809 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
810 SketchPlugin_Sketch::DIRX_ID());
811 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
812 SketchPlugin_Sketch::DIRY_ID());
813 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
814 SketchPlugin_Sketch::NORM_ID());
815 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
816 SketchPlugin_Sketch::ORIGIN_ID());
817 // Transform them into SolveSpace format
818 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
821 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
825 if (!myWorkplane.h) {
827 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
828 // Workplane should be added to the list of entities
829 myEntities.push_back(myWorkplane);
830 myEntOfConstr.push_back(false);
835 // ============================================================================
836 // Function: changeParameter
837 // Class: SketchSolver_ConstraintGroup
838 // Purpose: create/update value of parameter
839 // ============================================================================
840 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
841 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
843 if (thePrmIter != myParams.end()) { // Parameter should be updated
844 int aParamPos = thePrmIter - myParams.begin();
845 if (fabs(thePrmIter->val - theParam) > tolerance) {
846 myNeedToSolve = true; // parameter is changed, need to resolve constraints
847 myParams[aParamPos].val = theParam;
850 return myParams[aParamPos].h;
853 // Newly created parameter
854 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
855 myParams.push_back(aParam);
856 myNeedToSolve = true;
857 // The list of parameters is changed, move iterator to the end of the list to avoid problems
858 thePrmIter = myParams.end();
862 // ============================================================================
863 // Function: resolveConstraints
864 // Class: SketchSolver_ConstraintGroup
865 // Purpose: solve the set of constraints for the current group
866 // ============================================================================
867 bool SketchSolver_ConstraintGroup::resolveConstraints()
872 myConstrSolver.setGroupID(myID);
873 myConstrSolver.setParameters(myParams);
874 myConstrSolver.setEntities(myEntities);
875 myConstrSolver.setConstraints(myConstraints);
876 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
878 int aResult = myConstrSolver.solve();
879 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
880 // Obtain result into the same list of parameters
881 if (!myConstrSolver.getResult(myParams))
884 // We should go through the attributes map, because only attributes have valued parameters
885 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
886 myEntityAttrMap.begin();
887 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
888 if (updateAttribute(anEntIter->first, anEntIter->second))
889 updateRelatedConstraints(anEntIter->first);
890 } else if (!myConstraints.empty())
891 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
893 removeTemporaryConstraints();
894 myNeedToSolve = false;
898 // ============================================================================
899 // Function: mergeGroups
900 // Class: SketchSolver_ConstraintGroup
901 // Purpose: append specified group to the current group
902 // ============================================================================
903 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
905 // If specified group is empty, no need to merge
906 if (theGroup.myConstraintMap.empty())
909 // Map between old and new indexes of SolveSpace constraints
910 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
912 // Add all constraints from theGroup to the current group
913 ConstraintMap::const_iterator aConstrIter = theGroup.myConstraintMap.begin();
914 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
915 if (changeConstraint(aConstrIter->first))
916 aConstrMap[aConstrIter->second.back()] = myConstrMaxID; // the constraint was added => store its ID
918 // Add temporary constraints from theGroup
919 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
920 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
921 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
923 if (aFind != aConstrMap.end())
924 myTempConstraints.push_back(aFind->second);
927 if (myTempPointWhereDragged.empty())
928 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
929 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
930 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
931 theGroup.myEntityAttrMap.begin();
932 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
933 if (aFeatureIter->second == myTempPointWDrgdID) {
934 addTemporaryConstraintWhereDragged(aFeatureIter->first);
939 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
942 // ============================================================================
943 // Function: splitGroup
944 // Class: SketchSolver_ConstraintGroup
945 // Purpose: divide the group into several subgroups
946 // ============================================================================
947 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
949 // Divide constraints and entities into several groups
950 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
951 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
952 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
953 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
954 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
955 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
956 aConstrIter->entityB };
957 std::vector<int> anIndexes;
958 // Go through the groupped entities and find even one of entities of current constraint
959 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
960 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
961 bool isFound = false;
962 for (int i = 0; i < 4 && !isFound; i++)
963 if (aConstrEnt[i] != 0) {
964 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
965 // Also we need to check sub-entities
966 int aEntPos = Search(aConstrEnt[i], myEntities);
967 Slvs_hEntity* aSub = myEntities[aEntPos].point;
968 for (int j = 0; *aSub != 0 && j < 4 && !isFound; aSub++, j++)
969 isFound = (aGrEntIter->find(*aSub) != aGrEntIter->end());
972 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
974 // Add new group if no one is found
975 if (anIndexes.empty()) {
976 std::set<Slvs_hEntity> aNewGrEnt;
977 for (int i = 0; i < 4; i++)
978 if (aConstrEnt[i] != 0) {
979 aNewGrEnt.insert(aConstrEnt[i]);
980 int aEntPos = Search(aConstrEnt[i], myEntities);
981 Slvs_hEntity* aSub = myEntities[aEntPos].point;
982 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
983 aNewGrEnt.insert(*aSub);
985 std::set<Slvs_hConstraint> aNewGrConstr;
986 aNewGrConstr.insert(aConstrIter->h);
988 aGroupsEntities.push_back(aNewGrEnt);
989 aGroupsConstr.push_back(aNewGrConstr);
990 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
991 aMaxNbEntities = aGroupsEntities.size() - 1;
992 } else { // Add entities indexes into the found group
993 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
994 for (int i = 0; i < 4; i++)
995 if (aConstrEnt[i] != 0) {
996 aGrEntIter->insert(aConstrEnt[i]);
997 int aEntPos = Search(aConstrEnt[i], myEntities);
998 Slvs_hEntity* aSub = myEntities[aEntPos].point;
999 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
1000 aGrEntIter->insert(*aSub);
1002 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
1003 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
1004 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
1005 if (anIndexes.size() > 1) { // There are found several connected groups, merge them
1006 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
1007 + anIndexes.front();
1008 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
1009 + anIndexes.front();
1010 std::vector<int>::iterator anInd = anIndexes.begin();
1011 for (++anInd; anInd != anIndexes.end(); anInd++) {
1012 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
1013 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
1015 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
1016 aMaxNbEntities = anIndexes.front();
1017 // Remove merged groups
1018 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
1019 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
1020 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
1026 if (aGroupsEntities.size() <= 1)
1029 // Remove the group with maximum elements as it will be left in the current group
1030 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
1031 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
1033 // Add new groups of constraints and divide current group
1034 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
1035 for (int i = aGroupsEntities.size(); i > 0; i--) {
1036 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
1037 aNewGroups.push_back(aG);
1039 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.begin();
1040 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
1041 while (aConstrMapIter != myConstraintMap.end()) {
1042 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
1043 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
1044 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
1045 if (aGIter->find(aConstrMapIter->second.front()) != aGIter->end()) {
1046 (*aGroup)->changeConstraint(aConstrMapIter->first);
1047 removeConstraint(aConstrMapIter->first);
1049 aConstrMapIter = myConstraintMap.begin();
1050 for (int i = 0; i < aConstrMapPos; i++)
1054 if (aGIter == aGroupsConstr.end()) {
1060 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
1063 // ============================================================================
1064 // Function: updateGroup
1065 // Class: SketchSolver_ConstraintGroup
1066 // Purpose: search removed entities and constraints
1067 // ============================================================================
1068 bool SketchSolver_ConstraintGroup::updateGroup()
1070 ConstraintMap::reverse_iterator aConstrIter = myConstraintMap.rbegin();
1071 bool isAllValid = true;
1072 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
1073 int aConstrIndex = 0;
1074 while (/*isAllValid && */aConstrIter != myConstraintMap.rend()) {
1075 if (!aConstrIter->first->data() || !aConstrIter->first->data()->isValid()) {
1076 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
1078 removeConstraint(aConstrIter->first);
1080 // Get back the correct position of iterator after the "remove" operation
1081 aConstrIter = myConstraintMap.rbegin();
1082 for (int i = 0; i < aConstrIndex && aConstrIter != myConstraintMap.rend(); i++)
1090 // Check if some entities are invalid too
1091 std::set<Slvs_hEntity> anEntToRemove;
1092 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1093 anAttrIter = myEntityAttrMap.begin();
1094 while (anAttrIter != myEntityAttrMap.end()) {
1095 if (!anAttrIter->first->owner() || !anAttrIter->first->owner()->data() ||
1096 !anAttrIter->first->owner()->data()->isValid()) {
1097 anEntToRemove.insert(anAttrIter->second);
1098 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1099 aRemovedIter = anAttrIter;
1101 myEntityAttrMap.erase(aRemovedIter);
1105 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myEntityFeatMap.begin();
1106 while (aFeatIter != myEntityFeatMap.end()) {
1107 if (!aFeatIter->first || !aFeatIter->first->data() ||
1108 !aFeatIter->first->data()->isValid()) {
1109 anEntToRemove.insert(aFeatIter->second);
1110 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = aFeatIter;
1112 myEntityFeatMap.erase(aRemovedIter);
1116 removeEntitiesById(anEntToRemove);
1118 // Probably, need to update coincidence constraints
1119 if (isCCRemoved && !myExtraCoincidence.empty()) {
1120 // Make a copy, because the new list of unused constrtaints will be generated
1121 std::set<boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
1122 myExtraCoincidence.clear();
1124 std::set<boost::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
1125 for (; aCIter != anExtraCopy.end(); aCIter++)
1126 if ((*aCIter)->data() && (*aCIter)->data()->isValid())
1127 changeConstraint(*aCIter);
1133 // ============================================================================
1134 // Function: updateAttribute
1135 // Class: SketchSolver_ConstraintGroup
1136 // Purpose: update features of sketch after resolving constraints
1137 // ============================================================================
1138 bool SketchSolver_ConstraintGroup::updateAttribute(
1139 boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
1141 // Search the position of the first parameter of the entity
1142 int anEntPos = Search(theEntityID, myEntities);
1143 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
1145 // Look over supported types of entities
1148 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
1151 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
1152 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
1153 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
1154 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
1155 myParams[aFirstParamPos + 2].val);
1162 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1163 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
1165 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
1166 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
1167 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
1174 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1176 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
1177 aScalar->setValue(myParams[aFirstParamPos].val);
1183 /// \todo Support other types of entities
1187 // ============================================================================
1188 // Function: updateEntityIfPossible
1189 // Class: SketchSolver_ConstraintGroup
1190 // Purpose: search the entity in this group and update it
1191 // ============================================================================
1192 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
1193 boost::shared_ptr<ModelAPI_Attribute> theEntity)
1195 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
1196 // If the attribute is a point and it is changed (the group needs to rebuild),
1197 // probably user has dragged this point into this position,
1198 // so it is necessary to add constraint which will guarantee the point will not change
1200 // Store myNeedToSolve flag to verify the entity is really changed
1201 bool aNeedToSolveCopy = myNeedToSolve;
1202 myNeedToSolve = false;
1204 changeEntity(theEntity);
1206 if (myNeedToSolve) // the entity is changed
1208 // Verify the entity is a point and add temporary constraint of permanency
1209 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
1211 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = boost::dynamic_pointer_cast<
1212 GeomDataAPI_Point2D>(theEntity);
1213 if (aPoint || aPoint2D)
1214 addTemporaryConstraintWhereDragged(theEntity);
1217 // Restore flag of changes
1218 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
1221 updateRelatedConstraints(theEntity);
1225 // ============================================================================
1226 // Function: addTemporaryConstraintWhereDragged
1227 // Class: SketchSolver_ConstraintGroup
1228 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
1229 // which was moved by user
1230 // ============================================================================
1231 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
1232 boost::shared_ptr<ModelAPI_Attribute> theEntity,
1235 // Find identifier of the entity
1236 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
1237 myEntityAttrMap.find(theEntity);
1238 if (anEntIter == myEntityAttrMap.end())
1241 // Get identifiers of all dragged points
1242 std::set<Slvs_hEntity> aDraggedPntID;
1243 aDraggedPntID.insert(myTempPointWDrgdID);
1244 std::list<Slvs_hConstraint>::const_iterator aTmpCoIter = myTempConstraints.begin();
1245 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
1246 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
1247 if (aConstrPos < myConstraints.size())
1248 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
1250 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1251 for (; aConstrIter != myConstraints.end(); aConstrIter++)
1252 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED)
1253 aDraggedPntID.insert(aConstrIter->ptA);
1254 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
1255 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1256 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
1257 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
1258 continue; // the entity was not found in current set
1260 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
1261 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
1262 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
1263 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
1264 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
1266 if (aDraggedPntID.find(anEntIter->second) != aDraggedPntID.end())
1269 // If this is a first dragged point, its parameters should be placed
1270 // into Slvs_System::dragged field to avoid system inconsistense
1271 if (myTempPointWhereDragged.empty() && theAllowToFit) {
1272 int anEntPos = Search(anEntIter->second, myEntities);
1273 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
1274 for (int i = 0; i < 4; i++, aDraggedParam++)
1275 if (*aDraggedParam != 0)
1276 myTempPointWhereDragged.push_back(*aDraggedParam);
1277 myTempPointWDrgdID = myEntities[anEntPos].h;
1281 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
1282 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1283 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1284 myConstraints.push_back(aWDConstr);
1285 myTempConstraints.push_back(aWDConstr.h);
1288 // ============================================================================
1289 // Function: removeTemporaryConstraints
1290 // Class: SketchSolver_ConstraintGroup
1291 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
1292 // resolving the set of constraints
1293 // ============================================================================
1294 void SketchSolver_ConstraintGroup::removeTemporaryConstraints(
1295 const std::set<Slvs_hConstraint>& theRemoved)
1297 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1298 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
1300 if (!theRemoved.empty() && theRemoved.find(*aTmpConstrIter) == theRemoved.end())
1302 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1303 if (aConstrPos >= myConstraints.size())
1305 myConstraints.erase(myConstraints.begin() + aConstrPos);
1307 // If the removing constraint has higher index, decrease the indexer
1308 if (*aTmpConstrIter == myConstrMaxID)
1311 myTempConstraints.clear();
1313 // Clear basic dragged point
1314 myTempPointWhereDragged.clear();
1315 myTempPointWDrgdID = SLVS_E_UNKNOWN;
1318 // ============================================================================
1319 // Function: removeConstraint
1320 // Class: SketchSolver_ConstraintGroup
1321 // Purpose: remove constraint and all unused entities
1322 // ============================================================================
1323 void SketchSolver_ConstraintGroup::removeConstraint(
1324 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1326 ConstraintMap::iterator anIterToRemove = myConstraintMap.find(theConstraint);
1327 if (anIterToRemove == myConstraintMap.end())
1330 std::vector<Slvs_hConstraint> aCnstrToRemove = anIterToRemove->second;
1331 // Remove constraint from the map
1332 myConstraintMap.erase(anIterToRemove);
1334 std::set<Slvs_hEntity> anEntToRemove;
1336 // Find unused entities
1337 std::vector<Slvs_hConstraint>::iterator aCnstrToRemoveIter = aCnstrToRemove.begin();
1338 for (; aCnstrToRemoveIter != aCnstrToRemove.end(); aCnstrToRemoveIter++) {
1339 int aConstrPos = Search(*aCnstrToRemoveIter, myConstraints);
1340 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1341 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1342 for (int i = 0; i < 4; i++)
1343 if (aCnstEnt[i] != 0)
1344 anEntToRemove.insert(aCnstEnt[i]);
1345 myConstraints.erase(myConstraints.begin() + aConstrPos);
1346 if (*aCnstrToRemoveIter == myConstrMaxID)
1350 // Find all entities which are based on these unused
1351 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
1352 for ( ; anEntIter != myEntities.end(); anEntIter++)
1353 if (anEntIter->type == SLVS_E_LINE_SEGMENT || anEntIter->type == SLVS_E_CIRCLE ||
1354 anEntIter->type == SLVS_E_ARC_OF_CIRCLE) {
1355 for (int i = 0; i < 4; i++)
1356 if (anEntToRemove.find(anEntIter->point[i]) != anEntToRemove.end()) {
1357 anEntToRemove.insert(anEntIter->h);
1358 for (int j = 0; j < 4; j++)
1359 if (anEntIter->param[j] != 0)
1360 anEntToRemove.insert(anEntIter->point[j]);
1365 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1366 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1367 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1369 for (int i = 0; i < 4; i++)
1370 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1371 anEntToRemove.erase(aEnts[i]);
1374 if (anEntToRemove.empty())
1377 // Remove unused entities
1378 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1379 myEntityAttrMap.begin();
1380 while (anEntAttrIter != myEntityAttrMap.end()) {
1381 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1382 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1385 myEntityAttrMap.erase(aRemovedIter);
1389 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1390 while (anEntFeatIter != myEntityFeatMap.end()) {
1391 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1392 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1394 myEntityFeatMap.erase(aRemovedIter);
1399 removeEntitiesById(anEntToRemove);
1401 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1402 myCoincidentPoints.clear();
1405 // ============================================================================
1406 // Function: removeEntitiesById
1407 // Class: SketchSolver_ConstraintGroup
1408 // Purpose: Removes specified entities and their parameters
1409 // ============================================================================
1410 void SketchSolver_ConstraintGroup::removeEntitiesById(const std::set<Slvs_hEntity>& theEntities)
1412 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = theEntities.rbegin();
1413 for (; aRemIter != theEntities.rend(); aRemIter++) {
1414 unsigned int anEntPos = Search(*aRemIter, myEntities);
1415 if (anEntPos >= myEntities.size())
1417 if (myEntities[anEntPos].param[0] != 0) {
1418 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1419 if (aParamPos >= myParams.size())
1422 while (myEntities[anEntPos].param[aNbParams] != 0)
1424 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1425 myParamMaxID -= aNbParams;
1426 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1427 if (*aRemIter == myEntityMaxID)
1430 myEntities.erase(myEntities.begin() + anEntPos);
1431 myEntOfConstr.erase(myEntOfConstr.begin() + anEntPos);
1433 // Remove entity's ID from the lists of conincident points
1434 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1435 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1436 aCoPtIter->erase(*aRemIter);
1440 // ============================================================================
1441 // Function: addCoincidentPoints
1442 // Class: SketchSolver_ConstraintGroup
1443 // Purpose: add coincident point the appropriate list of such points
1444 // ============================================================================
1445 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1446 const Slvs_hEntity& thePoint2)
1448 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1449 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1450 while (aCoPtIter != myCoincidentPoints.end()) {
1451 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1452 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1453 != aCoPtIter->end(), };
1454 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1456 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1457 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1458 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1459 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1460 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1461 myCoincidentPoints.erase(aCoPtIter);
1462 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1463 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1466 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1467 aFirstFound = aCoPtIter;
1472 // No points were found, need to create new set
1473 if (aFirstFound == myCoincidentPoints.end()) {
1474 std::set<Slvs_hEntity> aNewSet;
1475 aNewSet.insert(thePoint1);
1476 aNewSet.insert(thePoint2);
1477 myCoincidentPoints.push_back(aNewSet);
1483 // ============================================================================
1484 // Function: updateRelatedConstraints
1485 // Class: SketchSolver_ConstraintGroup
1486 // Purpose: emit the signal to update constraints
1487 // ============================================================================
1488 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1489 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1491 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1492 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1493 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1494 ->attributes(std::string());
1496 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1497 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1498 bool isUpd = (*anAttrIter == theEntity);
1499 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1500 ModelAPI_AttributeRefAttr>(*anAttrIter);
1501 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1505 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1506 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1513 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1514 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1516 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1517 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1518 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1519 ->attributes(std::string());
1521 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1522 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1523 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1524 ModelAPI_AttributeRefAttr>(*anAttrIter);
1525 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1526 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1527 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1534 // ========================================================
1535 // ========= Auxiliary functions ===============
1536 // ========================================================
1538 template<typename T>
1539 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1541 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1542 int aVecSize = theEntities.size();
1543 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1545 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1547 if (aResIndex == -1)
1548 aResIndex = aVecSize;