1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_ConstraintGroup.cpp
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_ConstraintGroup.h"
9 #include <SketchSolver_Constraint.h>
11 #include <Events_Error.h>
12 #include <Events_Loop.h>
13 #include <GeomAPI_XY.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_ResultConstruction.h>
24 #include <SketchPlugin_Constraint.h>
25 #include <SketchPlugin_ConstraintLength.h>
26 #include <SketchPlugin_ConstraintCoincidence.h>
27 #include <SketchPlugin_ConstraintRigid.h>
29 #include <SketchPlugin_Arc.h>
30 #include <SketchPlugin_Circle.h>
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Point.h>
33 #include <SketchPlugin_Sketch.h>
38 /// Tolerance for value of parameters
39 const double tolerance = 1.e-10;
42 * Collects all sketch solver error' codes
43 * as inline static functions
44 * TODO: Move this class into a separate file
46 class SketchSolver_Error
49 /// The value parameter for the constraint
50 inline static const std::string& CONSTRAINTS()
52 static const std::string MY_ERROR_VALUE("Conflicting constraints");
53 return MY_ERROR_VALUE;
57 /// This value is used to give unique index to the groups
58 static Slvs_hGroup myGroupIndexer = 0;
60 /** \brief Search the entity/parameter with specified ID in the list of elements
61 * \param[in] theEntityID unique ID of the element
62 * \param[in] theEntities list of elements
63 * \return position of the found element or -1 if the element is not found
66 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
68 // ========================================================
69 // ========= SketchSolver_ConstraintGroup ===============
70 // ========================================================
72 SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup(
73 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
74 : myID(++myGroupIndexer),
84 myEntOfConstr.clear();
85 myConstraints.clear();
87 myTempConstraints.clear();
88 myTempPointWhereDragged.clear();
89 myTempPointWDrgdID = 0;
91 // Initialize workplane
92 myWorkplane.h = SLVS_E_UNKNOWN;
94 assert(addWorkplane(theWorkplane));
96 addWorkplane(theWorkplane);
100 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
104 myEntOfConstr.clear();
105 myConstraints.clear();
106 myConstraintMap.clear();
107 myTempConstraints.clear();
108 myTempPointWhereDragged.clear();
110 // If the group with maximal identifier is deleted, decrease the indexer
111 if (myID == myGroupIndexer)
115 // ============================================================================
116 // Function: isBaseWorkplane
117 // Class: SketchSolver_ConstraintGroup
118 // Purpose: verify the group is based on the given workplane
119 // ============================================================================
120 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
121 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane) const
123 return theWorkplane == mySketch;
126 // ============================================================================
127 // Function: isInteract
128 // Class: SketchSolver_ConstraintGroup
129 // Purpose: verify are there any entities in the group used by given constraint
130 // ============================================================================
131 bool SketchSolver_ConstraintGroup::isInteract(
132 std::shared_ptr<SketchPlugin_Feature> theFeature) const
134 // Check the group is empty
138 // Check if the feature is already in the group
139 if (myEntityFeatMap.find(theFeature) != myEntityFeatMap.end())
141 std::shared_ptr<SketchPlugin_Constraint> aConstr =
142 std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
143 if (aConstr && myConstraintMap.find(aConstr) != myConstraintMap.end())
146 // Go through the attributes and verify if some of them already in the group
147 std::list<std::shared_ptr<ModelAPI_Attribute>>
148 anAttrList = theFeature->data()->attributes(std::string());
149 std::list<std::shared_ptr<ModelAPI_Attribute>>::const_iterator
150 anAttrIter = anAttrList.begin();
151 for ( ; anAttrIter != anAttrList.end(); anAttrIter++) {
152 std::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
153 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
154 if (!aCAttrRef || !aCAttrRef->isObject()) {
155 std::shared_ptr<ModelAPI_Attribute> anAttr =
156 aCAttrRef ? aCAttrRef->attr() : *anAttrIter;
157 if (myEntityAttrMap.find(anAttr) != myEntityAttrMap.end())
160 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
161 aCAttrRef->object());
164 std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
165 FeaturePtr aFeature = aDoc->feature(aRC);
166 if (myEntityFeatMap.find(aFeature) != myEntityFeatMap.end())
168 // search attributes of a feature to be parameters of constraint
169 std::list<std::shared_ptr<ModelAPI_Attribute> > aFeatAttrList =
170 aFeature->data()->attributes(std::string());
171 std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aFAIter = aFeatAttrList
173 for (; aFAIter != aFeatAttrList.end(); aFAIter++)
174 if (myEntityAttrMap.find(*aFAIter) != myEntityAttrMap.end())
179 // Entities did not found
183 // ============================================================================
184 // Function: checkConstraintConsistence
185 // Class: SketchSolver_ConstraintGroup
186 // Purpose: verifies and changes parameters of the constraint
187 // ============================================================================
188 void SketchSolver_ConstraintGroup::checkConstraintConsistence(Slvs_Constraint& theConstraint)
190 if (theConstraint.type == SLVS_C_PT_LINE_DISTANCE) {
191 // Get constraint parameters and check the sign of constraint value
194 int aPtPos = Search(theConstraint.ptA, myEntities);
195 int aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
196 std::shared_ptr<GeomAPI_XY> aPoint(
197 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
200 int aLnPos = Search(theConstraint.entityA, myEntities);
201 aPtPos = Search(myEntities[aLnPos].point[0], myEntities);
202 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
203 std::shared_ptr<GeomAPI_XY> aStart(
204 new GeomAPI_XY(-myParams[aPtParamPos].val, -myParams[aPtParamPos + 1].val));
205 aPtPos = Search(myEntities[aLnPos].point[1], myEntities);
206 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
207 std::shared_ptr<GeomAPI_XY> aEnd(
208 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
210 aEnd = aEnd->added(aStart);
211 aPoint = aPoint->added(aStart);
212 if (aPoint->cross(aEnd) * theConstraint.valA < 0.0)
213 theConstraint.valA *= -1.0;
217 // ============================================================================
218 // Function: changeConstraint
219 // Class: SketchSolver_ConstraintGroup
220 // Purpose: create/update the constraint in the group
221 // ============================================================================
222 bool SketchSolver_ConstraintGroup::changeConstraint(
223 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
225 // There is no workplane yet, something wrong
226 if (myWorkplane.h == SLVS_E_UNKNOWN)
229 if (theConstraint && theConstraint->getKind() == SketchPlugin_ConstraintRigid::ID())
230 return changeRigidConstraint(theConstraint);
232 // Search this constraint in the current group to update it
233 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.find(theConstraint);
234 std::vector<Slvs_Constraint>::iterator aConstrIter;
235 if (aConstrMapIter != myConstraintMap.end()) {
236 int aConstrPos = Search(aConstrMapIter->second.front(), myConstraints);
237 aConstrIter = myConstraints.begin() + aConstrPos;
240 // Get constraint type and verify the constraint parameters are correct
241 SketchSolver_Constraint aConstraint(theConstraint);
242 int aConstrType = aConstraint.getType();
243 if (aConstrType == SLVS_C_UNKNOWN
244 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
246 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
248 // Create constraint parameters
249 double aDistance = 0.0; // scalar value of the constraint
250 AttributeDoublePtr aDistAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
251 theConstraint->data()->attribute(SketchPlugin_Constraint::VALUE()));
253 aDistance = aDistAttr->value();
254 // Issue #196: checking the positivity of the distance constraint
255 if (aDistance < tolerance &&
256 (aConstrType == SLVS_C_PT_PT_DISTANCE || aConstrType == SLVS_C_PT_LINE_DISTANCE))
258 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
259 if (aConstrType == SLVS_C_DIAMETER)
261 if (aConstrMapIter != myConstraintMap.end()
262 && fabs(aConstrIter->valA - aDistance) > tolerance) {
263 myNeedToSolve = true;
264 aConstrIter->valA = aDistance;
268 size_t aNbTmpConstraints = myTempConstraints.size();
269 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
270 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
271 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
272 std::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = std::dynamic_pointer_cast<
273 ModelAPI_AttributeRefAttr>(
274 theConstraint->data()->attribute(aConstraintAttributes[indAttr]));
278 // Convert the object of the attribute to the feature
280 if (aConstrAttr->isObject() && aConstrAttr->object()) {
281 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
282 aConstrAttr->object());
285 std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
286 aFeature = aDoc->feature(aRC);
289 // For the length constraint the start and end points of the line should be added to the entities list instead of line
290 if (aConstrType == SLVS_C_PT_PT_DISTANCE
291 && theConstraint->getKind().compare(SketchPlugin_ConstraintLength::ID()) == 0) {
292 Slvs_hEntity aLineEnt = changeEntityFeature(aFeature);
293 int aEntPos = Search(aLineEnt, myEntities);
294 aConstrEnt[indAttr++] = myEntities[aEntPos].point[0];
295 aConstrEnt[indAttr++] = myEntities[aEntPos].point[1];
296 while (indAttr < CONSTRAINT_ATTR_SIZE)
297 aConstrEnt[indAttr++] = 0;
298 break; // there should be no other entities
299 } else if (aConstrAttr->isObject())
300 aConstrEnt[indAttr] = changeEntityFeature(aFeature);
302 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
305 if (aConstrMapIter == myConstraintMap.end()) { // Add new constraint
306 // Several points may be coincident, it is not necessary to store all constraints between them.
307 // Try to find sequence of coincident points which connects the points of new constraint
308 if (aConstrType == SLVS_C_POINTS_COINCIDENT) {
309 if (aConstrEnt[0] == aConstrEnt[1]) // no need to add self coincidence
311 if (!addCoincidentPoints(aConstrEnt[0], aConstrEnt[1])) {
312 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
315 if (aNbTmpConstraints < myTempConstraints.size()) {
316 // There was added temporary constraint. Check that there is no coincident points which already rigid.
318 // Get list of already fixed points
319 std::set<Slvs_hEntity> anAlreadyFixed;
320 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
321 for (; aCIter != myConstraints.end(); aCIter++)
322 if (aCIter->type == SLVS_C_WHERE_DRAGGED) {
323 std::list<Slvs_hConstraint>::const_iterator aTmpIt = myTempConstraints.begin();
324 for (; aTmpIt != myTempConstraints.end(); aTmpIt++)
325 if (*aTmpIt == aCIter->h)
327 if (aTmpIt == myTempConstraints.end())
328 anAlreadyFixed.insert(aCIter->ptA);
331 std::set<Slvs_hConstraint> aTmpConstrToDelete;
332 std::list<Slvs_hConstraint>::reverse_iterator aTmpIter = myTempConstraints.rbegin();
333 size_t aCurSize = myTempConstraints.size();
334 for (; aCurSize > aNbTmpConstraints && aTmpIter != myTempConstraints.rend();
335 aTmpIter++, aCurSize--) {
336 int aConstrPos = Search(*aTmpIter, myConstraints);
337 std::vector<std::set<Slvs_hEntity> >::const_iterator
338 aCoincIter = myCoincidentPoints.begin();
339 for (; aCoincIter != myCoincidentPoints.end(); aCoincIter++)
340 if (aCoincIter->find(myConstraints[aConstrPos].ptA) != aCoincIter->end()) {
341 std::set<Slvs_hEntity>::const_iterator anIt;
342 for (anIt = aCoincIter->begin(); anIt != aCoincIter->end(); anIt++)
343 if (anAlreadyFixed.find(*anIt) != anAlreadyFixed.end()) {
344 aTmpConstrToDelete.insert(*aTmpIter);
350 if (!aTmpConstrToDelete.empty())
351 removeTemporaryConstraints(aTmpConstrToDelete);
355 // Create SolveSpace constraint structure
356 Slvs_Constraint aConstraint = Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType,
357 myWorkplane.h, aDistance, aConstrEnt[0],
358 aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
359 myConstraints.push_back(aConstraint);
360 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>(1, aConstraint.h);
361 int aConstrPos = Search(aConstraint.h, myConstraints);
362 aConstrIter = myConstraints.begin() + aConstrPos;
363 myNeedToSolve = true;
364 } else { // Attributes of constraint may be changed => update constraint
365 Slvs_hEntity* aCurrentAttr[] = {&aConstrIter->ptA, &aConstrIter->ptB,
366 &aConstrIter->entityA, &aConstrIter->entityB,
367 &aConstrIter->entityC, &aConstrIter->entityD};
368 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
369 if (*(aCurrentAttr[indAttr]) != aConstrEnt[indAttr])
371 *(aCurrentAttr[indAttr]) = aConstrEnt[indAttr];
372 myNeedToSolve = true;
377 // Update flags of entities to be used by constraints
378 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
379 if (aConstrEnt[indAttr] != 0) {
380 int aPos = Search(aConstrEnt[indAttr], myEntities);
381 myEntOfConstr[aPos] = true;
382 // Sub-entities should be used implcitly
383 Slvs_hEntity* aEntPtr = myEntities[aPos].point;
384 while (*aEntPtr != 0) {
385 aPos = Search(*aEntPtr, myEntities);
386 myEntOfConstr[aPos] = true;
391 checkConstraintConsistence(*aConstrIter);
395 // ============================================================================
396 // Function: changeRigidConstraint
397 // Class: SketchSolver_ConstraintGroup
398 // Purpose: create/update the "Rigid" constraint in the group
399 // ============================================================================
400 bool SketchSolver_ConstraintGroup::changeRigidConstraint(
401 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
403 // Search this constraint in the current group to update it
404 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.find(theConstraint);
405 std::vector<Slvs_Constraint>::iterator aConstrIter;
406 if (aConstrMapIter != myConstraintMap.end()) {
407 int aConstrPos = Search(aConstrMapIter->second.front(), myConstraints);
408 aConstrIter = myConstraints.begin() + aConstrPos;
411 // Get constraint type and verify the constraint parameters are correct
412 SketchSolver_Constraint aConstraint(theConstraint);
413 int aConstrType = aConstraint.getType();
414 if (aConstrType == SLVS_C_UNKNOWN
415 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
417 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
419 Slvs_hEntity aConstrEnt = SLVS_E_UNKNOWN;
420 std::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = std::dynamic_pointer_cast<
421 ModelAPI_AttributeRefAttr>(
422 theConstraint->data()->attribute(aConstraintAttributes[0]));
426 // Convert the object of the attribute to the feature
428 if (aConstrAttr->isObject() && aConstrAttr->object()) {
429 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
430 aConstrAttr->object());
433 std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
434 aFeature = aDoc->feature(aRC);
437 aConstrEnt = aConstrAttr->isObject() ? changeEntityFeature(aFeature) : changeEntity(aConstrAttr->attr());
439 if (aConstrMapIter == myConstraintMap.end()) { // Add new constraint
440 // Check the fixed entity is not a point.
441 std::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = std::dynamic_pointer_cast<
442 ModelAPI_AttributeRefAttr>(theConstraint->data()->attribute(aConstraintAttributes[0]));
443 std::shared_ptr<GeomDataAPI_Point> aPoint =
444 std::dynamic_pointer_cast<GeomDataAPI_Point>(aConstrAttr->attr());
445 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
446 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aConstrAttr->attr());
447 if (aPoint || aPoint2D) {
448 // Create SolveSpace constraint structure
449 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
450 ++myConstrMaxID, myID, aConstrType, myWorkplane.h, 0.0,
451 aConstrEnt, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
452 myConstraints.push_back(aConstraint);
453 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>(1, aConstraint.h);
454 int aConstrPos = Search(aConstraint.h, myConstraints);
455 aConstrIter = myConstraints.begin() + aConstrPos;
456 myNeedToSolve = true;
458 myConstraintMap[theConstraint] = std::vector<Slvs_hEntity>();
460 // To avoid SolveSpace problems:
461 // * if the circle is rigid, we will fix its center and radius;
462 // * if the arc is rigid, we will fix its start and end points and radius.
463 double aRadius = 0.0;
465 bool isCircle = false;
467 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
468 std::shared_ptr<GeomDataAPI_Point2D> aCenter =
469 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
470 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
471 std::shared_ptr<GeomDataAPI_Point2D> aStart =
472 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
473 aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
474 aRadius = aStart->pnt()->distance(aCenter->pnt());
476 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
477 aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
478 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
483 // Get list of already fixed points
484 std::set<Slvs_hEntity> anAlreadyFixed;
485 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
486 for (; aCIter != myConstraints.end(); aCIter++)
487 if (aCIter->type == SLVS_C_WHERE_DRAGGED)
488 anAlreadyFixed.insert(aCIter->ptA);
490 // Create constraints to fix the parameters of the entity
491 int aEntPos = Search(aConstrEnt, myEntities);
492 Slvs_hEntity* aPointsPtr = myEntities[aEntPos].point;
493 if (isArc) aPointsPtr++; // avoid to fix center of arc
494 while (*aPointsPtr != 0) {
495 // Avoid to create additional "Rigid" constraints for coincident points
496 bool isCoincAlreadyFixed = false;
497 if (!anAlreadyFixed.empty()) {
498 if (anAlreadyFixed.find(*aPointsPtr) != anAlreadyFixed.end())
499 isCoincAlreadyFixed = true;
501 std::vector<std::set<Slvs_hEntity> >::const_iterator aCoincIter =
502 myCoincidentPoints.begin();
503 for (; !isCoincAlreadyFixed && aCoincIter != myCoincidentPoints.end(); aCoincIter++) {
504 if (aCoincIter->find(*aPointsPtr) == aCoincIter->end())
506 std::set<Slvs_hEntity>::const_iterator anIter = anAlreadyFixed.begin();
507 for (; !isCoincAlreadyFixed && anIter != anAlreadyFixed.end(); anIter++)
508 if (aCoincIter->find(*anIter) != aCoincIter->end())
509 isCoincAlreadyFixed = true;
513 if (!isCoincAlreadyFixed) {
514 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
515 ++myConstrMaxID, myID, aConstrType, myWorkplane.h, 0.0,
516 *aPointsPtr, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
517 myConstraints.push_back(aConstraint);
518 myConstraintMap[theConstraint].push_back(aConstraint.h);
523 if (isArc || isCircle) { // add radius constraint
524 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
525 ++myConstrMaxID, myID, SLVS_C_DIAMETER, myWorkplane.h, 2.0 * aRadius,
526 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aConstrEnt, SLVS_E_UNKNOWN);
527 myConstraints.push_back(aConstraint);
528 myConstraintMap[theConstraint].push_back(aConstraint.h);
531 // The object is already rigid, so there is no constraints added
532 if (myConstraintMap[theConstraint].empty()) {
533 myConstraintMap.erase(theConstraint);
534 myNeedToSolve = false;
537 myNeedToSolve = true;
543 // ============================================================================
544 // Function: changeEntity
545 // Class: SketchSolver_ConstraintGroup
546 // Purpose: create/update the element affected by any constraint
547 // ============================================================================
548 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
549 std::shared_ptr<ModelAPI_Attribute> theEntity)
551 // If the entity is already in the group, try to find it
552 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
553 myEntityAttrMap.find(theEntity);
555 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
556 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
557 aParamIter = myParams.end();
558 else { // the entity already exists
559 aEntPos = Search(aEntIter->second, myEntities);
560 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
561 aParamIter = myParams.begin() + aParamPos;
563 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
564 const bool isNeedToSolve = myNeedToSolve;
565 myNeedToSolve = false;
568 // Verify that the entity is not used by "Rigid" constraint.
569 // If it is used, the object should not move.
570 std::vector<std::set<Slvs_hEntity> >::iterator aCoincIter = myCoincidentPoints.begin();
571 for (; aCoincIter != myCoincidentPoints.end(); aCoincIter++)
572 if (aCoincIter->find(aEntIter->second) != aCoincIter->end())
574 std::set<Slvs_hEntity> aCoincident;
575 if (aCoincIter != myCoincidentPoints.end()) {
576 aCoincident = *aCoincIter;
577 aCoincident.erase(aEntIter->second);
579 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
580 for (; aConstrIter != myConstraints.end(); aConstrIter++)
581 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
582 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
583 myNeedToSolve = true;
584 return aEntIter->second;
589 // Look over supported types of entities
590 Slvs_Entity aNewEntity;
591 aNewEntity.h = SLVS_E_UNKNOWN;
594 std::shared_ptr<GeomDataAPI_Point> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point>(
597 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
598 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
599 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
600 if (!isEntExists) // New entity
601 aNewEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
603 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
604 if (myWorkplane.h == SLVS_E_UNKNOWN)
605 return SLVS_E_UNKNOWN;
608 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
609 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
611 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
612 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
613 if (!isEntExists) // New entity
614 aNewEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
616 // Scalar value (used for the distance entities)
617 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
619 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
620 if (!isEntExists) // New entity
621 aNewEntity = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
625 /// \todo Other types of entities
627 Slvs_hEntity aResult = SLVS_E_UNKNOWN; // Unsupported or wrong entity type
630 myNeedToSolve = myNeedToSolve || isNeedToSolve;
631 aResult = aEntIter->second;
632 } else if (aNewEntity.h != SLVS_E_UNKNOWN) {
633 myEntities.push_back(aNewEntity);
634 myEntOfConstr.push_back(false);
635 myEntityAttrMap[theEntity] = aNewEntity.h;
636 aResult = aNewEntity.h;
639 // If the attribute was changed by the user, we need to fix it before solving
640 if (myNeedToSolve && theEntity->isImmutable())
641 addTemporaryConstraintWhereDragged(theEntity, false);
646 // ============================================================================
647 // Function: changeEntity
648 // Class: SketchSolver_ConstraintGroup
649 // Purpose: create/update the element defined by the feature affected by any constraint
650 // ============================================================================
651 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntityFeature(FeaturePtr theEntity)
653 if (!theEntity->data()->isValid())
654 return SLVS_E_UNKNOWN;
655 // If the entity is already in the group, try to find it
656 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aEntIter = myEntityFeatMap.find(theEntity);
657 // defines that the entity already exists
658 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
660 Slvs_Entity aNewEntity;
661 aNewEntity.h = SLVS_E_UNKNOWN;
663 // SketchPlugin features
664 std::shared_ptr<SketchPlugin_Feature> aFeature = std::dynamic_pointer_cast<
665 SketchPlugin_Feature>(theEntity);
666 if (aFeature) { // Verify the feature by its kind
667 const std::string& aFeatureKind = aFeature->getKind();
668 AttributePtr anAttribute;
671 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0) {
672 anAttribute = aFeature->data()->attribute(SketchPlugin_Line::START_ID());
673 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
674 Slvs_hEntity aStart = changeEntity(anAttribute);
676 anAttribute = aFeature->data()->attribute(SketchPlugin_Line::END_ID());
677 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
678 Slvs_hEntity aEnd = changeEntity(anAttribute);
680 if (!isEntExists) // New entity
681 aNewEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
684 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
685 anAttribute = aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID());
686 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
687 Slvs_hEntity aCenter = changeEntity(anAttribute);
689 anAttribute = aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID());
690 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
691 Slvs_hEntity aRadius = changeEntity(anAttribute);
693 if (!isEntExists) // New entity
694 aNewEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
695 myWorkplane.normal, aRadius);
698 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
699 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID());
700 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
701 Slvs_hEntity aCenter = changeEntity(anAttribute);
703 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::START_ID());
704 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
705 Slvs_hEntity aStart = changeEntity(anAttribute);
707 anAttribute = aFeature->data()->attribute(SketchPlugin_Arc::END_ID());
708 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
709 Slvs_hEntity aEnd = changeEntity(anAttribute);
712 aNewEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
713 myWorkplane.normal, aCenter, aStart, aEnd);
715 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
716 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
717 anAttribute = aFeature->data()->attribute(SketchPlugin_Point::COORD_ID());
718 if (!anAttribute->isInitialized()) return SLVS_E_UNKNOWN;
719 Slvs_hEntity aPoint = changeEntity(anAttribute);
722 return aEntIter->second;
724 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
725 myEntityFeatMap[theEntity] = aPoint;
726 myNeedToSolve = true;
730 /// \todo Other types of features
733 return aEntIter->second;
735 if (aNewEntity.h != SLVS_E_UNKNOWN) {
736 myEntities.push_back(aNewEntity);
737 myEntOfConstr.push_back(false);
738 myEntityFeatMap[theEntity] = aNewEntity.h;
739 myNeedToSolve = true;
743 // Unsupported or wrong entity type
744 return SLVS_E_UNKNOWN;
747 // ============================================================================
748 // Function: changeNormal
749 // Class: SketchSolver_ConstraintGroup
750 // Purpose: create/update the normal of workplane
751 // ============================================================================
752 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
753 std::shared_ptr<ModelAPI_Attribute> theDirX, std::shared_ptr<ModelAPI_Attribute> theDirY,
754 std::shared_ptr<ModelAPI_Attribute> theNorm)
756 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
757 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
758 if (!aDirX || !aDirY || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance)
759 || (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
760 return SLVS_E_UNKNOWN;
762 // quaternion parameters of normal vector
763 double qw, qx, qy, qz;
764 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
766 double aNormCoord[4] = { qw, qx, qy, qz };
768 // Try to find existent normal
769 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
770 myEntityAttrMap.find(theNorm);
771 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
772 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
773 aParamIter = myParams.end();
774 else { // the entity already exists, update it
775 int aEntPos = Search(aEntIter->second, myEntities);
776 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
777 aParamIter = myParams.begin() + aParamPos;
780 // Change parameters of the normal
781 Slvs_hParam aNormParams[4];
782 for (int i = 0; i < 4; i++)
783 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
785 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
786 return aEntIter->second;
789 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
790 aNormParams[2], aNormParams[3]);
791 myEntities.push_back(aNormal);
792 myEntOfConstr.push_back(false);
793 myEntityAttrMap[theNorm] = aNormal.h;
797 // ============================================================================
798 // Function: addWorkplane
799 // Class: SketchSolver_ConstraintGroup
800 // Purpose: create workplane for the group
801 // ============================================================================
802 bool SketchSolver_ConstraintGroup::addWorkplane(std::shared_ptr<ModelAPI_CompositeFeature> theSketch)
804 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
805 return false; // the workplane already exists or the function parameter is not Sketch
807 mySketch = theSketch;
812 // ============================================================================
813 // Function: updateWorkplane
814 // Class: SketchSolver_ConstraintGroup
815 // Purpose: update parameters of workplane
816 // ============================================================================
817 bool SketchSolver_ConstraintGroup::updateWorkplane()
819 if (!mySketch->data())
820 return false; // case sketch is deleted
821 // Get parameters of workplane
822 std::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
823 SketchPlugin_Sketch::DIRX_ID());
824 std::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
825 SketchPlugin_Sketch::DIRY_ID());
826 std::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
827 SketchPlugin_Sketch::NORM_ID());
828 std::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
829 SketchPlugin_Sketch::ORIGIN_ID());
830 // Transform them into SolveSpace format
831 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
834 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
838 if (!myWorkplane.h) {
840 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
841 // Workplane should be added to the list of entities
842 myEntities.push_back(myWorkplane);
843 myEntOfConstr.push_back(false);
848 // ============================================================================
849 // Function: changeParameter
850 // Class: SketchSolver_ConstraintGroup
851 // Purpose: create/update value of parameter
852 // ============================================================================
853 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
854 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
856 if (thePrmIter != myParams.end()) { // Parameter should be updated
857 int aParamPos = thePrmIter - myParams.begin();
858 if (fabs(thePrmIter->val - theParam) > tolerance) {
859 myNeedToSolve = true; // parameter is changed, need to resolve constraints
860 myParams[aParamPos].val = theParam;
863 return myParams[aParamPos].h;
866 // Newly created parameter
867 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
868 myParams.push_back(aParam);
869 myNeedToSolve = true;
870 // The list of parameters is changed, move iterator to the end of the list to avoid problems
871 thePrmIter = myParams.end();
875 // ============================================================================
876 // Function: resolveConstraints
877 // Class: SketchSolver_ConstraintGroup
878 // Purpose: solve the set of constraints for the current group
879 // ============================================================================
880 bool SketchSolver_ConstraintGroup::resolveConstraints()
885 myConstrSolver.setGroupID(myID);
886 myConstrSolver.setParameters(myParams);
887 myConstrSolver.setEntities(myEntities);
888 myConstrSolver.setConstraints(myConstraints);
889 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
891 int aResult = myConstrSolver.solve();
892 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
893 // Obtain result into the same list of parameters
894 if (!myConstrSolver.getResult(myParams))
897 // We should go through the attributes map, because only attributes have valued parameters
898 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
899 myEntityAttrMap.begin();
900 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
901 if (updateAttribute(anEntIter->first, anEntIter->second))
902 updateRelatedConstraints(anEntIter->first);
903 } else if (!myConstraints.empty())
904 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
906 removeTemporaryConstraints();
907 myNeedToSolve = false;
911 // ============================================================================
912 // Function: mergeGroups
913 // Class: SketchSolver_ConstraintGroup
914 // Purpose: append specified group to the current group
915 // ============================================================================
916 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
918 // If specified group is empty, no need to merge
919 if (theGroup.myConstraintMap.empty())
922 // Map between old and new indexes of SolveSpace constraints
923 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
925 // Add all constraints from theGroup to the current group
926 ConstraintMap::const_iterator aConstrIter = theGroup.myConstraintMap.begin();
927 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
928 if (changeConstraint(aConstrIter->first))
929 aConstrMap[aConstrIter->second.back()] = myConstrMaxID; // the constraint was added => store its ID
931 // Add temporary constraints from theGroup
932 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
933 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
934 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
936 if (aFind != aConstrMap.end())
937 myTempConstraints.push_back(aFind->second);
940 if (myTempPointWhereDragged.empty())
941 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
942 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
943 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
944 theGroup.myEntityAttrMap.begin();
945 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
946 if (aFeatureIter->second == myTempPointWDrgdID) {
947 addTemporaryConstraintWhereDragged(aFeatureIter->first);
952 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
955 // ============================================================================
956 // Function: splitGroup
957 // Class: SketchSolver_ConstraintGroup
958 // Purpose: divide the group into several subgroups
959 // ============================================================================
960 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
962 // Divide constraints and entities into several groups
963 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
964 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
965 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
966 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
967 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
968 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
969 aConstrIter->entityB };
970 std::vector<int> anIndexes;
971 // Go through the groupped entities and find even one of entities of current constraint
972 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
973 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
974 bool isFound = false;
975 for (int i = 0; i < 4 && !isFound; i++)
976 if (aConstrEnt[i] != 0) {
977 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
978 // Also we need to check sub-entities
979 int aEntPos = Search(aConstrEnt[i], myEntities);
980 if (aEntPos != myEntities.size()) { // MPV: to fix the crash on close
981 Slvs_hEntity* aSub = myEntities[aEntPos].point;
982 for (int j = 0; *aSub != 0 && j < 4 && !isFound; aSub++, j++)
983 isFound = (aGrEntIter->find(*aSub) != aGrEntIter->end());
987 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
989 // Add new group if no one is found
990 if (anIndexes.empty()) {
991 std::set<Slvs_hEntity> aNewGrEnt;
992 for (int i = 0; i < 4; i++)
993 if (aConstrEnt[i] != 0) {
994 aNewGrEnt.insert(aConstrEnt[i]);
995 int aEntPos = Search(aConstrEnt[i], myEntities);
996 if (aEntPos != myEntities.size()) { // MPV: to fix the crash on close
997 Slvs_hEntity* aSub = myEntities[aEntPos].point;
998 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
999 aNewGrEnt.insert(*aSub);
1002 std::set<Slvs_hConstraint> aNewGrConstr;
1003 aNewGrConstr.insert(aConstrIter->h);
1005 aGroupsEntities.push_back(aNewGrEnt);
1006 aGroupsConstr.push_back(aNewGrConstr);
1007 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
1008 aMaxNbEntities = aGroupsEntities.size() - 1;
1009 } else { // Add entities indexes into the found group
1010 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
1011 for (int i = 0; i < 4; i++)
1012 if (aConstrEnt[i] != 0) {
1013 aGrEntIter->insert(aConstrEnt[i]);
1014 int aEntPos = Search(aConstrEnt[i], myEntities);
1015 if (aEntPos != myEntities.size()) { // MPV: to fix the crash on close
1016 Slvs_hEntity* aSub = myEntities[aEntPos].point;
1017 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
1018 aGrEntIter->insert(*aSub);
1021 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
1022 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
1023 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
1024 if (anIndexes.size() > 1) { // There are found several connected groups, merge them
1025 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
1026 + anIndexes.front();
1027 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
1028 + anIndexes.front();
1029 std::vector<int>::iterator anInd = anIndexes.begin();
1030 for (++anInd; anInd != anIndexes.end(); anInd++) {
1031 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
1032 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
1034 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
1035 aMaxNbEntities = anIndexes.front();
1036 // Remove merged groups
1037 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
1038 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
1039 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
1045 if (aGroupsEntities.size() <= 1)
1048 // Remove the group with maximum elements as it will be left in the current group
1049 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
1050 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
1052 // Add new groups of constraints and divide current group
1053 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
1054 for (int i = aGroupsEntities.size(); i > 0; i--) {
1055 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
1056 aNewGroups.push_back(aG);
1058 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.begin();
1059 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
1060 while (aConstrMapIter != myConstraintMap.end()) {
1061 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
1062 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
1063 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
1064 if (aGIter->find(aConstrMapIter->second.front()) != aGIter->end()) {
1065 (*aGroup)->changeConstraint(aConstrMapIter->first);
1066 removeConstraint(aConstrMapIter->first);
1068 aConstrMapIter = myConstraintMap.begin();
1069 for (int i = 0; i < aConstrMapPos; i++)
1073 if (aGIter == aGroupsConstr.end()) {
1079 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
1082 // ============================================================================
1083 // Function: updateGroup
1084 // Class: SketchSolver_ConstraintGroup
1085 // Purpose: search removed entities and constraints
1086 // ============================================================================
1087 bool SketchSolver_ConstraintGroup::updateGroup()
1089 ConstraintMap::reverse_iterator aConstrIter = myConstraintMap.rbegin();
1090 bool isAllValid = true;
1091 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
1092 int aConstrIndex = 0;
1093 while (/*isAllValid && */aConstrIter != myConstraintMap.rend()) {
1094 if (!aConstrIter->first->data() || !aConstrIter->first->data()->isValid()) {
1095 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
1097 removeConstraint(aConstrIter->first);
1099 // Get back the correct position of iterator after the "remove" operation
1100 aConstrIter = myConstraintMap.rbegin();
1101 for (int i = 0; i < aConstrIndex && aConstrIter != myConstraintMap.rend(); i++)
1109 // Check if some entities are invalid too
1110 std::set<Slvs_hEntity> anEntToRemove;
1111 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1112 anAttrIter = myEntityAttrMap.begin();
1113 while (anAttrIter != myEntityAttrMap.end()) {
1114 if (!anAttrIter->first->owner() || !anAttrIter->first->owner()->data() ||
1115 !anAttrIter->first->owner()->data()->isValid()) {
1116 anEntToRemove.insert(anAttrIter->second);
1117 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1118 aRemovedIter = anAttrIter;
1120 myEntityAttrMap.erase(aRemovedIter);
1124 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myEntityFeatMap.begin();
1125 while (aFeatIter != myEntityFeatMap.end()) {
1126 if (!aFeatIter->first || !aFeatIter->first->data() ||
1127 !aFeatIter->first->data()->isValid()) {
1128 anEntToRemove.insert(aFeatIter->second);
1129 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = aFeatIter;
1131 myEntityFeatMap.erase(aRemovedIter);
1135 removeEntitiesById(anEntToRemove);
1137 // Probably, need to update coincidence constraints
1138 if (isCCRemoved && !myExtraCoincidence.empty()) {
1139 // Make a copy, because the new list of unused constrtaints will be generated
1140 std::set<std::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
1141 myExtraCoincidence.clear();
1143 std::set<std::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
1144 for (; aCIter != anExtraCopy.end(); aCIter++)
1145 if ((*aCIter)->data() && (*aCIter)->data()->isValid())
1146 changeConstraint(*aCIter);
1152 // ============================================================================
1153 // Function: updateAttribute
1154 // Class: SketchSolver_ConstraintGroup
1155 // Purpose: update features of sketch after resolving constraints
1156 // ============================================================================
1157 bool SketchSolver_ConstraintGroup::updateAttribute(
1158 std::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
1160 // Search the position of the first parameter of the entity
1161 int anEntPos = Search(theEntityID, myEntities);
1162 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
1164 // Look over supported types of entities
1167 std::shared_ptr<GeomDataAPI_Point> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1170 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
1171 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
1172 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
1173 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
1174 myParams[aFirstParamPos + 2].val);
1181 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1182 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
1184 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
1185 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
1186 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
1193 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1195 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
1196 aScalar->setValue(myParams[aFirstParamPos].val);
1202 /// \todo Support other types of entities
1206 // ============================================================================
1207 // Function: updateEntityIfPossible
1208 // Class: SketchSolver_ConstraintGroup
1209 // Purpose: search the entity in this group and update it
1210 // ============================================================================
1211 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
1212 std::shared_ptr<ModelAPI_Attribute> theEntity)
1214 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
1215 // If the attribute is a point and it is changed (the group needs to rebuild),
1216 // probably user has dragged this point into this position,
1217 // so it is necessary to add constraint which will guarantee the point will not change
1219 // Store myNeedToSolve flag to verify the entity is really changed
1220 bool aNeedToSolveCopy = myNeedToSolve;
1221 myNeedToSolve = false;
1223 changeEntity(theEntity);
1225 if (myNeedToSolve) // the entity is changed
1227 // Verify the entity is a point and add temporary constraint of permanency
1228 std::shared_ptr<GeomDataAPI_Point> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1230 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D = std::dynamic_pointer_cast<
1231 GeomDataAPI_Point2D>(theEntity);
1232 if (aPoint || aPoint2D)
1233 addTemporaryConstraintWhereDragged(theEntity);
1236 // Restore flag of changes
1237 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
1240 updateRelatedConstraints(theEntity);
1244 // ============================================================================
1245 // Function: addTemporaryConstraintWhereDragged
1246 // Class: SketchSolver_ConstraintGroup
1247 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
1248 // which was moved by user
1249 // ============================================================================
1250 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
1251 std::shared_ptr<ModelAPI_Attribute> theEntity,
1254 // Find identifier of the entity
1255 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
1256 myEntityAttrMap.find(theEntity);
1257 if (anEntIter == myEntityAttrMap.end())
1260 // Get identifiers of all dragged points
1261 std::set<Slvs_hEntity> aDraggedPntID;
1262 aDraggedPntID.insert(myTempPointWDrgdID);
1263 std::list<Slvs_hConstraint>::const_iterator aTmpCoIter = myTempConstraints.begin();
1264 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
1265 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
1266 if (aConstrPos < myConstraints.size())
1267 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
1269 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1270 for (; aConstrIter != myConstraints.end(); aConstrIter++)
1271 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED)
1272 aDraggedPntID.insert(aConstrIter->ptA);
1273 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
1274 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1275 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
1276 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
1277 continue; // the entity was not found in current set
1279 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
1280 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
1281 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
1282 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
1283 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
1285 if (aDraggedPntID.find(anEntIter->second) != aDraggedPntID.end())
1288 // If this is a first dragged point, its parameters should be placed
1289 // into Slvs_System::dragged field to avoid system inconsistense
1290 if (myTempPointWhereDragged.empty() && theAllowToFit) {
1291 int anEntPos = Search(anEntIter->second, myEntities);
1292 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
1293 for (int i = 0; i < 4; i++, aDraggedParam++)
1294 if (*aDraggedParam != 0)
1295 myTempPointWhereDragged.push_back(*aDraggedParam);
1296 myTempPointWDrgdID = myEntities[anEntPos].h;
1300 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
1301 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1302 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1303 myConstraints.push_back(aWDConstr);
1304 myTempConstraints.push_back(aWDConstr.h);
1307 // ============================================================================
1308 // Function: removeTemporaryConstraints
1309 // Class: SketchSolver_ConstraintGroup
1310 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
1311 // resolving the set of constraints
1312 // ============================================================================
1313 void SketchSolver_ConstraintGroup::removeTemporaryConstraints(
1314 const std::set<Slvs_hConstraint>& theRemoved)
1316 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1317 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
1319 if (!theRemoved.empty() && theRemoved.find(*aTmpConstrIter) == theRemoved.end())
1321 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1322 if (aConstrPos >= myConstraints.size())
1324 myConstraints.erase(myConstraints.begin() + aConstrPos);
1326 // If the removing constraint has higher index, decrease the indexer
1327 if (*aTmpConstrIter == myConstrMaxID)
1330 myTempConstraints.clear();
1332 // Clear basic dragged point
1333 myTempPointWhereDragged.clear();
1334 myTempPointWDrgdID = SLVS_E_UNKNOWN;
1337 // ============================================================================
1338 // Function: removeConstraint
1339 // Class: SketchSolver_ConstraintGroup
1340 // Purpose: remove constraint and all unused entities
1341 // ============================================================================
1342 void SketchSolver_ConstraintGroup::removeConstraint(
1343 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
1345 ConstraintMap::iterator anIterToRemove = myConstraintMap.find(theConstraint);
1346 if (anIterToRemove == myConstraintMap.end())
1349 std::vector<Slvs_hConstraint> aCnstrToRemove = anIterToRemove->second;
1350 // Remove constraint from the map
1351 myConstraintMap.erase(anIterToRemove);
1353 std::set<Slvs_hEntity> anEntToRemove;
1355 // Find unused entities
1356 std::vector<Slvs_hConstraint>::iterator aCnstrToRemoveIter = aCnstrToRemove.begin();
1357 for (; aCnstrToRemoveIter != aCnstrToRemove.end(); aCnstrToRemoveIter++) {
1358 int aConstrPos = Search(*aCnstrToRemoveIter, myConstraints);
1359 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1360 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1361 for (int i = 0; i < 4; i++)
1362 if (aCnstEnt[i] != 0)
1363 anEntToRemove.insert(aCnstEnt[i]);
1364 myConstraints.erase(myConstraints.begin() + aConstrPos);
1365 if (*aCnstrToRemoveIter == myConstrMaxID)
1369 // Find all entities which are based on these unused
1370 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
1371 for ( ; anEntIter != myEntities.end(); anEntIter++)
1372 if (anEntIter->type == SLVS_E_LINE_SEGMENT || anEntIter->type == SLVS_E_CIRCLE ||
1373 anEntIter->type == SLVS_E_ARC_OF_CIRCLE) {
1374 for (int i = 0; i < 4; i++)
1375 if (anEntToRemove.find(anEntIter->point[i]) != anEntToRemove.end()) {
1376 anEntToRemove.insert(anEntIter->h);
1377 for (int j = 0; j < 4; j++)
1378 if (anEntIter->param[j] != 0)
1379 anEntToRemove.insert(anEntIter->point[j]);
1384 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1385 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1386 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1388 for (int i = 0; i < 4; i++)
1389 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1390 anEntToRemove.erase(aEnts[i]);
1393 if (anEntToRemove.empty())
1396 // Remove unused entities
1397 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1398 myEntityAttrMap.begin();
1399 while (anEntAttrIter != myEntityAttrMap.end()) {
1400 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1401 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1404 myEntityAttrMap.erase(aRemovedIter);
1408 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1409 while (anEntFeatIter != myEntityFeatMap.end()) {
1410 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1411 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1413 myEntityFeatMap.erase(aRemovedIter);
1418 removeEntitiesById(anEntToRemove);
1420 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1421 myCoincidentPoints.clear();
1424 // ============================================================================
1425 // Function: removeEntitiesById
1426 // Class: SketchSolver_ConstraintGroup
1427 // Purpose: Removes specified entities and their parameters
1428 // ============================================================================
1429 void SketchSolver_ConstraintGroup::removeEntitiesById(const std::set<Slvs_hEntity>& theEntities)
1431 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = theEntities.rbegin();
1432 for (; aRemIter != theEntities.rend(); aRemIter++) {
1433 unsigned int anEntPos = Search(*aRemIter, myEntities);
1434 if (anEntPos >= myEntities.size())
1436 if (myEntities[anEntPos].param[0] != 0) {
1437 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1438 if (aParamPos >= myParams.size())
1441 while (myEntities[anEntPos].param[aNbParams] != 0)
1443 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1444 myParamMaxID -= aNbParams;
1445 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1446 if (*aRemIter == myEntityMaxID)
1449 myEntities.erase(myEntities.begin() + anEntPos);
1450 myEntOfConstr.erase(myEntOfConstr.begin() + anEntPos);
1452 // Remove entity's ID from the lists of conincident points
1453 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1454 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1455 aCoPtIter->erase(*aRemIter);
1459 // ============================================================================
1460 // Function: addCoincidentPoints
1461 // Class: SketchSolver_ConstraintGroup
1462 // Purpose: add coincident point the appropriate list of such points
1463 // ============================================================================
1464 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1465 const Slvs_hEntity& thePoint2)
1467 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1468 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1469 while (aCoPtIter != myCoincidentPoints.end()) {
1470 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1471 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1472 != aCoPtIter->end(), };
1473 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1475 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1476 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1477 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1478 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1479 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1480 myCoincidentPoints.erase(aCoPtIter);
1481 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1482 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1485 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1486 aFirstFound = aCoPtIter;
1491 // No points were found, need to create new set
1492 if (aFirstFound == myCoincidentPoints.end()) {
1493 std::set<Slvs_hEntity> aNewSet;
1494 aNewSet.insert(thePoint1);
1495 aNewSet.insert(thePoint2);
1496 myCoincidentPoints.push_back(aNewSet);
1502 // ============================================================================
1503 // Function: updateRelatedConstraints
1504 // Class: SketchSolver_ConstraintGroup
1505 // Purpose: emit the signal to update constraints
1506 // ============================================================================
1507 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1508 std::shared_ptr<ModelAPI_Attribute> theEntity) const
1510 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1511 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1512 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1513 ->attributes(std::string());
1515 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1516 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1517 bool isUpd = (*anAttrIter == theEntity);
1518 std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = std::dynamic_pointer_cast<
1519 ModelAPI_AttributeRefAttr>(*anAttrIter);
1520 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1524 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1525 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1532 void SketchSolver_ConstraintGroup::updateRelatedConstraintsFeature(
1533 std::shared_ptr<ModelAPI_Feature> theFeature) const
1535 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1536 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1537 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1538 ->attributes(std::string());
1540 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1541 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1542 std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = std::dynamic_pointer_cast<
1543 ModelAPI_AttributeRefAttr>(*anAttrIter);
1544 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1545 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1546 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1553 // ========================================================
1554 // ========= Auxiliary functions ===============
1555 // ========================================================
1557 template<typename T>
1558 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1560 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1561 int aVecSize = theEntities.size();
1562 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1564 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1566 if (aResIndex == -1)
1567 aResIndex = aVecSize;