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 std::vector<std::set<Slvs_hEntity> >::const_iterator aCoincIter =
485 myCoincidentPoints.begin();
486 for (; !isCoincAlreadyFixed && aCoincIter != myCoincidentPoints.end(); aCoincIter++) {
487 if (aCoincIter->find(*aPointsPtr) == aCoincIter->end())
489 std::set<Slvs_hEntity>::const_iterator anIter = anAlreadyFixed.begin();
490 for (; !isCoincAlreadyFixed && anIter != anAlreadyFixed.end(); anIter++)
491 if (aCoincIter->find(*anIter) != aCoincIter->end())
492 isCoincAlreadyFixed = true;
496 if (!isCoincAlreadyFixed) {
497 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
498 ++myConstrMaxID, myID, aConstrType, myWorkplane.h, 0.0,
499 *aPointsPtr, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
500 myConstraints.push_back(aConstraint);
501 myConstraintMap[theConstraint].push_back(aConstraint.h);
506 if (isArc || isCircle) { // add radius constraint
507 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
508 ++myConstrMaxID, myID, SLVS_C_DIAMETER, myWorkplane.h, 2.0 * aRadius,
509 SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aConstrEnt, SLVS_E_UNKNOWN);
510 myConstraints.push_back(aConstraint);
511 myConstraintMap[theConstraint].push_back(aConstraint.h);
514 // The object is already rigid, so there is no constraints added
515 if (myConstraintMap[theConstraint].empty()) {
516 myConstraintMap.erase(theConstraint);
517 myNeedToSolve = false;
520 myNeedToSolve = true;
526 // ============================================================================
527 // Function: changeEntity
528 // Class: SketchSolver_ConstraintGroup
529 // Purpose: create/update the element affected by any constraint
530 // ============================================================================
531 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
532 boost::shared_ptr<ModelAPI_Attribute> theEntity)
534 // If the entity is already in the group, try to find it
535 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
536 myEntityAttrMap.find(theEntity);
538 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
539 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
540 aParamIter = myParams.end();
541 else { // the entity already exists
542 aEntPos = Search(aEntIter->second, myEntities);
543 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
544 aParamIter = myParams.begin() + aParamPos;
546 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
547 const bool isNeedToSolve = myNeedToSolve;
548 myNeedToSolve = false;
551 // Verify that the entity is not used by "Rigid" constraint.
552 // If it is used, the object should not move.
553 std::vector<std::set<Slvs_hEntity> >::iterator aCoincIter = myCoincidentPoints.begin();
554 for (; aCoincIter != myCoincidentPoints.end(); aCoincIter++)
555 if (aCoincIter->find(aEntIter->second) != aCoincIter->end())
557 std::set<Slvs_hEntity> aCoincident;
558 if (aCoincIter != myCoincidentPoints.end())
559 aCoincident = *aCoincIter;
561 aCoincident.insert(aEntIter->second);
563 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
564 for (; aConstrIter != myConstraints.end(); aConstrIter++)
565 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
566 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
567 myNeedToSolve = true;
568 return aEntIter->second;
572 // Look over supported types of entities
573 Slvs_Entity aNewEntity;
574 aNewEntity.h = SLVS_E_UNKNOWN;
577 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
580 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
581 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
582 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
583 if (!isEntExists) // New entity
584 aNewEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
586 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
587 if (myWorkplane.h == SLVS_E_UNKNOWN)
588 return SLVS_E_UNKNOWN;
591 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
592 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
594 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
595 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
596 if (!isEntExists) // New entity
597 aNewEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
599 // Scalar value (used for the distance entities)
600 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
602 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
603 if (!isEntExists) // New entity
604 aNewEntity = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
608 /// \todo Other types of entities
610 Slvs_hEntity aResult = SLVS_E_UNKNOWN; // Unsupported or wrong entity type
613 if (!myEntOfConstr[aEntPos]) // the entity is not used by constraints, no need to resolve them
614 myNeedToSolve = isNeedToSolve;
616 myNeedToSolve = myNeedToSolve || isNeedToSolve;
617 aResult = aEntIter->second;
618 } else if (aNewEntity.h != SLVS_E_UNKNOWN) {
619 myEntities.push_back(aNewEntity);
620 myEntOfConstr.push_back(false);
621 myEntityAttrMap[theEntity] = aNewEntity.h;
622 aResult = aNewEntity.h;
625 // If the attribute was changed by the user, we need to fix it before solving
626 if (myNeedToSolve && theEntity->isImmutable())
627 addTemporaryConstraintWhereDragged(theEntity, false);
632 // ============================================================================
633 // Function: changeEntity
634 // Class: SketchSolver_ConstraintGroup
635 // Purpose: create/update the element defined by the feature affected by any constraint
636 // ============================================================================
637 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(FeaturePtr theEntity)
639 if (!theEntity->data()->isValid())
640 return SLVS_E_UNKNOWN;
641 // If the entity is already in the group, try to find it
642 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aEntIter = myEntityFeatMap.find(theEntity);
643 // defines that the entity already exists
644 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
646 Slvs_Entity aNewEntity;
647 aNewEntity.h = SLVS_E_UNKNOWN;
649 // SketchPlugin features
650 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
651 SketchPlugin_Feature>(theEntity);
652 if (aFeature) { // Verify the feature by its kind
653 const std::string& aFeatureKind = aFeature->getKind();
655 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(std::string());
656 std::list<AttributePtr>::iterator anAttrIter = anAttributes.begin();
657 for ( ; anAttrIter != anAttributes.end(); anAttrIter++)
658 if (!(*anAttrIter)->isInitialized()) // the entity is not fully initialized, don't add it into solver
659 return SLVS_E_UNKNOWN;
662 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0) {
663 Slvs_hEntity aStart = changeEntity(
664 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
665 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
667 if (!isEntExists) // New entity
668 aNewEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
671 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
672 Slvs_hEntity aCenter = changeEntity(
673 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
674 Slvs_hEntity aRadius = changeEntity(
675 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
677 if (!isEntExists) // New entity
678 aNewEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
679 myWorkplane.normal, aRadius);
682 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
683 Slvs_hEntity aCenter = changeEntity(
684 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
685 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
686 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
689 aNewEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
690 myWorkplane.normal, aCenter, aStart, aEnd);
692 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
693 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
694 Slvs_hEntity aPoint = changeEntity(
695 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
698 return aEntIter->second;
700 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
701 myEntityFeatMap[theEntity] = aPoint;
702 myNeedToSolve = true;
706 /// \todo Other types of features
709 return aEntIter->second;
711 if (aNewEntity.h != SLVS_E_UNKNOWN) {
712 myEntities.push_back(aNewEntity);
713 myEntOfConstr.push_back(false);
714 myEntityFeatMap[theEntity] = aNewEntity.h;
715 myNeedToSolve = true;
720 // Unsupported or wrong entity type
721 return SLVS_E_UNKNOWN;
724 // ============================================================================
725 // Function: changeNormal
726 // Class: SketchSolver_ConstraintGroup
727 // Purpose: create/update the normal of workplane
728 // ============================================================================
729 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
730 boost::shared_ptr<ModelAPI_Attribute> theDirX, boost::shared_ptr<ModelAPI_Attribute> theDirY,
731 boost::shared_ptr<ModelAPI_Attribute> theNorm)
733 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
734 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
735 if (!aDirX || !aDirY || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance)
736 || (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
737 return SLVS_E_UNKNOWN;
739 // quaternion parameters of normal vector
740 double qw, qx, qy, qz;
741 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
743 double aNormCoord[4] = { qw, qx, qy, qz };
745 // Try to find existent normal
746 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
747 myEntityAttrMap.find(theNorm);
748 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
749 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
750 aParamIter = myParams.end();
751 else { // the entity already exists, update it
752 int aEntPos = Search(aEntIter->second, myEntities);
753 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
754 aParamIter = myParams.begin() + aParamPos;
757 // Change parameters of the normal
758 Slvs_hParam aNormParams[4];
759 for (int i = 0; i < 4; i++)
760 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
762 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
763 return aEntIter->second;
766 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
767 aNormParams[2], aNormParams[3]);
768 myEntities.push_back(aNormal);
769 myEntOfConstr.push_back(false);
770 myEntityAttrMap[theNorm] = aNormal.h;
774 // ============================================================================
775 // Function: addWorkplane
776 // Class: SketchSolver_ConstraintGroup
777 // Purpose: create workplane for the group
778 // ============================================================================
779 bool SketchSolver_ConstraintGroup::addWorkplane(boost::shared_ptr<ModelAPI_CompositeFeature> theSketch)
781 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
782 return false; // the workplane already exists or the function parameter is not Sketch
784 mySketch = theSketch;
789 // ============================================================================
790 // Function: updateWorkplane
791 // Class: SketchSolver_ConstraintGroup
792 // Purpose: update parameters of workplane
793 // ============================================================================
794 bool SketchSolver_ConstraintGroup::updateWorkplane()
796 // Get parameters of workplane
797 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
798 SketchPlugin_Sketch::DIRX_ID());
799 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
800 SketchPlugin_Sketch::DIRY_ID());
801 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
802 SketchPlugin_Sketch::NORM_ID());
803 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
804 SketchPlugin_Sketch::ORIGIN_ID());
805 // Transform them into SolveSpace format
806 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
809 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
813 if (!myWorkplane.h) {
815 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
816 // Workplane should be added to the list of entities
817 myEntities.push_back(myWorkplane);
818 myEntOfConstr.push_back(false);
823 // ============================================================================
824 // Function: changeParameter
825 // Class: SketchSolver_ConstraintGroup
826 // Purpose: create/update value of parameter
827 // ============================================================================
828 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
829 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
831 if (thePrmIter != myParams.end()) { // Parameter should be updated
832 int aParamPos = thePrmIter - myParams.begin();
833 if (fabs(thePrmIter->val - theParam) > tolerance) {
834 myNeedToSolve = true; // parameter is changed, need to resolve constraints
835 myParams[aParamPos].val = theParam;
838 return myParams[aParamPos].h;
841 // Newly created parameter
842 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
843 myParams.push_back(aParam);
844 myNeedToSolve = true;
845 // The list of parameters is changed, move iterator to the end of the list to avoid problems
846 thePrmIter = myParams.end();
850 // ============================================================================
851 // Function: resolveConstraints
852 // Class: SketchSolver_ConstraintGroup
853 // Purpose: solve the set of constraints for the current group
854 // ============================================================================
855 bool SketchSolver_ConstraintGroup::resolveConstraints()
860 myConstrSolver.setGroupID(myID);
861 myConstrSolver.setParameters(myParams);
862 myConstrSolver.setEntities(myEntities);
863 myConstrSolver.setConstraints(myConstraints);
864 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
866 int aResult = myConstrSolver.solve();
867 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
868 // Obtain result into the same list of parameters
869 if (!myConstrSolver.getResult(myParams))
872 // We should go through the attributes map, because only attributes have valued parameters
873 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
874 myEntityAttrMap.begin();
875 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
876 if (updateAttribute(anEntIter->first, anEntIter->second))
877 updateRelatedConstraints(anEntIter->first);
878 } else if (!myConstraints.empty())
879 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
881 removeTemporaryConstraints();
882 myNeedToSolve = false;
886 // ============================================================================
887 // Function: mergeGroups
888 // Class: SketchSolver_ConstraintGroup
889 // Purpose: append specified group to the current group
890 // ============================================================================
891 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
893 // If specified group is empty, no need to merge
894 if (theGroup.myConstraintMap.empty())
897 // Map between old and new indexes of SolveSpace constraints
898 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
900 // Add all constraints from theGroup to the current group
901 ConstraintMap::const_iterator aConstrIter = theGroup.myConstraintMap.begin();
902 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
903 if (changeConstraint(aConstrIter->first))
904 aConstrMap[aConstrIter->second.back()] = myConstrMaxID; // the constraint was added => store its ID
906 // Add temporary constraints from theGroup
907 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
908 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
909 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
911 if (aFind != aConstrMap.end())
912 myTempConstraints.push_back(aFind->second);
915 if (myTempPointWhereDragged.empty())
916 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
917 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
918 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
919 theGroup.myEntityAttrMap.begin();
920 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
921 if (aFeatureIter->second == myTempPointWDrgdID) {
922 addTemporaryConstraintWhereDragged(aFeatureIter->first);
927 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
930 // ============================================================================
931 // Function: splitGroup
932 // Class: SketchSolver_ConstraintGroup
933 // Purpose: divide the group into several subgroups
934 // ============================================================================
935 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
937 // Divide constraints and entities into several groups
938 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
939 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
940 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
941 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
942 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
943 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
944 aConstrIter->entityB };
945 std::vector<int> anIndexes;
946 // Go through the groupped entities and find even one of entities of current constraint
947 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
948 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
949 bool isFound = false;
950 for (int i = 0; i < 4 && !isFound; i++)
951 if (aConstrEnt[i] != 0) {
952 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
953 // Also we need to check sub-entities
954 int aEntPos = Search(aConstrEnt[i], myEntities);
955 Slvs_hEntity* aSub = myEntities[aEntPos].point;
956 for (int j = 0; *aSub != 0 && j < 4 && !isFound; aSub++, j++)
957 isFound = (aGrEntIter->find(*aSub) != aGrEntIter->end());
960 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
962 // Add new group if no one is found
963 if (anIndexes.empty()) {
964 std::set<Slvs_hEntity> aNewGrEnt;
965 for (int i = 0; i < 4; i++)
966 if (aConstrEnt[i] != 0) {
967 aNewGrEnt.insert(aConstrEnt[i]);
968 int aEntPos = Search(aConstrEnt[i], myEntities);
969 Slvs_hEntity* aSub = myEntities[aEntPos].point;
970 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
971 aNewGrEnt.insert(*aSub);
973 std::set<Slvs_hConstraint> aNewGrConstr;
974 aNewGrConstr.insert(aConstrIter->h);
976 aGroupsEntities.push_back(aNewGrEnt);
977 aGroupsConstr.push_back(aNewGrConstr);
978 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
979 aMaxNbEntities = aGroupsEntities.size() - 1;
980 } else { // Add entities indexes into the found group
981 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
982 for (int i = 0; i < 4; i++)
983 if (aConstrEnt[i] != 0) {
984 aGrEntIter->insert(aConstrEnt[i]);
985 int aEntPos = Search(aConstrEnt[i], myEntities);
986 Slvs_hEntity* aSub = myEntities[aEntPos].point;
987 for (int j = 0; *aSub != 0 && j < 4; aSub++, j++)
988 aGrEntIter->insert(*aSub);
990 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
991 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
992 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
993 if (anIndexes.size() > 1) { // There are found several connected groups, merge them
994 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
996 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
998 std::vector<int>::iterator anInd = anIndexes.begin();
999 for (++anInd; anInd != anIndexes.end(); anInd++) {
1000 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
1001 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
1003 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
1004 aMaxNbEntities = anIndexes.front();
1005 // Remove merged groups
1006 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
1007 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
1008 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
1014 if (aGroupsEntities.size() <= 1)
1017 // Remove the group with maximum elements as it will be left in the current group
1018 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
1019 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
1021 // Add new groups of constraints and divide current group
1022 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
1023 for (int i = aGroupsEntities.size(); i > 0; i--) {
1024 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
1025 aNewGroups.push_back(aG);
1027 ConstraintMap::const_iterator aConstrMapIter = myConstraintMap.begin();
1028 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
1029 while (aConstrMapIter != myConstraintMap.end()) {
1030 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
1031 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
1032 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
1033 if (aGIter->find(aConstrMapIter->second.front()) != aGIter->end()) {
1034 (*aGroup)->changeConstraint(aConstrMapIter->first);
1035 removeConstraint(aConstrMapIter->first);
1037 aConstrMapIter = myConstraintMap.begin();
1038 for (int i = 0; i < aConstrMapPos; i++)
1042 if (aGIter == aGroupsConstr.end()) {
1048 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
1051 // ============================================================================
1052 // Function: updateGroup
1053 // Class: SketchSolver_ConstraintGroup
1054 // Purpose: search removed entities and constraints
1055 // ============================================================================
1056 bool SketchSolver_ConstraintGroup::updateGroup()
1058 ConstraintMap::reverse_iterator aConstrIter = myConstraintMap.rbegin();
1059 bool isAllValid = true;
1060 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
1061 int aConstrIndex = 0;
1062 while (/*isAllValid && */aConstrIter != myConstraintMap.rend()) {
1063 if (!aConstrIter->first->data() || !aConstrIter->first->data()->isValid()) {
1064 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
1066 removeConstraint(aConstrIter->first);
1068 // Get back the correct position of iterator after the "remove" operation
1069 aConstrIter = myConstraintMap.rbegin();
1070 for (int i = 0; i < aConstrIndex && aConstrIter != myConstraintMap.rend(); i++)
1078 // Check if some entities are invalid too
1079 std::set<Slvs_hEntity> anEntToRemove;
1080 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1081 anAttrIter = myEntityAttrMap.begin();
1082 while (anAttrIter != myEntityAttrMap.end()) {
1083 if (!anAttrIter->first->owner() || !anAttrIter->first->owner()->data() ||
1084 !anAttrIter->first->owner()->data()->isValid()) {
1085 anEntToRemove.insert(anAttrIter->second);
1086 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
1087 aRemovedIter = anAttrIter;
1089 myEntityAttrMap.erase(aRemovedIter);
1093 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myEntityFeatMap.begin();
1094 while (aFeatIter != myEntityFeatMap.end()) {
1095 if (!aFeatIter->first || !aFeatIter->first->data() ||
1096 !aFeatIter->first->data()->isValid()) {
1097 anEntToRemove.insert(aFeatIter->second);
1098 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = aFeatIter;
1100 myEntityFeatMap.erase(aRemovedIter);
1104 removeEntitiesById(anEntToRemove);
1106 // Probably, need to update coincidence constraints
1107 if (isCCRemoved && !myExtraCoincidence.empty()) {
1108 // Make a copy, because the new list of unused constrtaints will be generated
1109 std::set<boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
1110 myExtraCoincidence.clear();
1112 std::set<boost::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
1113 for (; aCIter != anExtraCopy.end(); aCIter++)
1114 if ((*aCIter)->data() && (*aCIter)->data()->isValid())
1115 changeConstraint(*aCIter);
1121 // ============================================================================
1122 // Function: updateAttribute
1123 // Class: SketchSolver_ConstraintGroup
1124 // Purpose: update features of sketch after resolving constraints
1125 // ============================================================================
1126 bool SketchSolver_ConstraintGroup::updateAttribute(
1127 boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
1129 // Search the position of the first parameter of the entity
1130 int anEntPos = Search(theEntityID, myEntities);
1131 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
1133 // Look over supported types of entities
1136 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
1139 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
1140 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
1141 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
1142 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
1143 myParams[aFirstParamPos + 2].val);
1150 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1151 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
1153 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
1154 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
1155 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
1162 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
1164 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
1165 aScalar->setValue(myParams[aFirstParamPos].val);
1171 /// \todo Support other types of entities
1175 // ============================================================================
1176 // Function: updateEntityIfPossible
1177 // Class: SketchSolver_ConstraintGroup
1178 // Purpose: search the entity in this group and update it
1179 // ============================================================================
1180 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
1181 boost::shared_ptr<ModelAPI_Attribute> theEntity)
1183 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
1184 // If the attribute is a point and it is changed (the group needs to rebuild),
1185 // probably user has dragged this point into this position,
1186 // so it is necessary to add constraint which will guarantee the point will not change
1188 // Store myNeedToSolve flag to verify the entity is really changed
1189 bool aNeedToSolveCopy = myNeedToSolve;
1190 myNeedToSolve = false;
1192 changeEntity(theEntity);
1194 if (myNeedToSolve) // the entity is changed
1196 // Verify the entity is a point and add temporary constraint of permanency
1197 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
1199 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = boost::dynamic_pointer_cast<
1200 GeomDataAPI_Point2D>(theEntity);
1201 if (aPoint || aPoint2D)
1202 addTemporaryConstraintWhereDragged(theEntity);
1205 // Restore flag of changes
1206 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
1209 updateRelatedConstraints(theEntity);
1213 // ============================================================================
1214 // Function: addTemporaryConstraintWhereDragged
1215 // Class: SketchSolver_ConstraintGroup
1216 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
1217 // which was moved by user
1218 // ============================================================================
1219 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
1220 boost::shared_ptr<ModelAPI_Attribute> theEntity,
1223 // Find identifier of the entity
1224 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
1225 myEntityAttrMap.find(theEntity);
1226 if (anEntIter == myEntityAttrMap.end())
1229 // Get identifiers of all dragged points
1230 std::set<Slvs_hEntity> aDraggedPntID;
1231 aDraggedPntID.insert(myTempPointWDrgdID);
1232 std::list<Slvs_hConstraint>::const_iterator aTmpCoIter = myTempConstraints.begin();
1233 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
1234 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
1235 if (aConstrPos < myConstraints.size())
1236 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
1238 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1239 for (; aConstrIter != myConstraints.end(); aConstrIter++)
1240 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED)
1241 aDraggedPntID.insert(aConstrIter->ptA);
1242 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
1243 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1244 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
1245 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
1246 continue; // the entity was not found in current set
1248 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
1249 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
1250 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
1251 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
1252 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
1254 if (aDraggedPntID.find(anEntIter->second) != aDraggedPntID.end())
1257 // If this is a first dragged point, its parameters should be placed
1258 // into Slvs_System::dragged field to avoid system inconsistense
1259 if (myTempPointWhereDragged.empty() && theAllowToFit) {
1260 int anEntPos = Search(anEntIter->second, myEntities);
1261 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
1262 for (int i = 0; i < 4; i++, aDraggedParam++)
1263 if (*aDraggedParam != 0)
1264 myTempPointWhereDragged.push_back(*aDraggedParam);
1265 myTempPointWDrgdID = myEntities[anEntPos].h;
1269 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
1270 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1271 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1272 myConstraints.push_back(aWDConstr);
1273 myTempConstraints.push_back(aWDConstr.h);
1276 // ============================================================================
1277 // Function: removeTemporaryConstraints
1278 // Class: SketchSolver_ConstraintGroup
1279 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
1280 // resolving the set of constraints
1281 // ============================================================================
1282 void SketchSolver_ConstraintGroup::removeTemporaryConstraints(
1283 const std::set<Slvs_hConstraint>& theRemoved)
1285 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1286 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
1288 if (!theRemoved.empty() && theRemoved.find(*aTmpConstrIter) == theRemoved.end())
1290 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1291 if (aConstrPos >= myConstraints.size())
1293 myConstraints.erase(myConstraints.begin() + aConstrPos);
1295 // If the removing constraint has higher index, decrease the indexer
1296 if (*aTmpConstrIter == myConstrMaxID)
1299 myTempConstraints.clear();
1301 // Clear basic dragged point
1302 myTempPointWhereDragged.clear();
1303 myTempPointWDrgdID = SLVS_E_UNKNOWN;
1306 // ============================================================================
1307 // Function: removeConstraint
1308 // Class: SketchSolver_ConstraintGroup
1309 // Purpose: remove constraint and all unused entities
1310 // ============================================================================
1311 void SketchSolver_ConstraintGroup::removeConstraint(
1312 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1314 ConstraintMap::iterator anIterToRemove = myConstraintMap.find(theConstraint);
1315 if (anIterToRemove == myConstraintMap.end())
1318 std::vector<Slvs_hConstraint> aCnstrToRemove = anIterToRemove->second;
1319 // Remove constraint from the map
1320 myConstraintMap.erase(anIterToRemove);
1322 std::set<Slvs_hEntity> anEntToRemove;
1324 // Find unused entities
1325 std::vector<Slvs_hConstraint>::iterator aCnstrToRemoveIter = aCnstrToRemove.begin();
1326 for (; aCnstrToRemoveIter != aCnstrToRemove.end(); aCnstrToRemoveIter++) {
1327 int aConstrPos = Search(*aCnstrToRemoveIter, myConstraints);
1328 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1329 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1330 for (int i = 0; i < 4; i++)
1331 if (aCnstEnt[i] != 0)
1332 anEntToRemove.insert(aCnstEnt[i]);
1333 myConstraints.erase(myConstraints.begin() + aConstrPos);
1334 if (*aCnstrToRemoveIter == myConstrMaxID)
1338 // Find all entities which are based on these unused
1339 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
1340 for ( ; anEntIter != myEntities.end(); anEntIter++)
1341 if (anEntIter->type == SLVS_E_LINE_SEGMENT || anEntIter->type == SLVS_E_CIRCLE ||
1342 anEntIter->type == SLVS_E_ARC_OF_CIRCLE) {
1343 for (int i = 0; i < 4; i++)
1344 if (anEntToRemove.find(anEntIter->point[i]) != anEntToRemove.end()) {
1345 anEntToRemove.insert(anEntIter->h);
1346 for (int j = 0; j < 4; j++)
1347 if (anEntIter->param[j] != 0)
1348 anEntToRemove.insert(anEntIter->point[j]);
1353 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1354 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1355 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1357 for (int i = 0; i < 4; i++)
1358 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1359 anEntToRemove.erase(aEnts[i]);
1362 if (anEntToRemove.empty())
1365 // Remove unused entities
1366 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1367 myEntityAttrMap.begin();
1368 while (anEntAttrIter != myEntityAttrMap.end()) {
1369 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1370 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1373 myEntityAttrMap.erase(aRemovedIter);
1377 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1378 while (anEntFeatIter != myEntityFeatMap.end()) {
1379 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1380 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1382 myEntityFeatMap.erase(aRemovedIter);
1387 removeEntitiesById(anEntToRemove);
1389 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1390 myCoincidentPoints.clear();
1393 // ============================================================================
1394 // Function: removeEntitiesById
1395 // Class: SketchSolver_ConstraintGroup
1396 // Purpose: Removes specified entities and their parameters
1397 // ============================================================================
1398 void SketchSolver_ConstraintGroup::removeEntitiesById(const std::set<Slvs_hEntity>& theEntities)
1400 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = theEntities.rbegin();
1401 for (; aRemIter != theEntities.rend(); aRemIter++) {
1402 unsigned int anEntPos = Search(*aRemIter, myEntities);
1403 if (anEntPos >= myEntities.size())
1405 if (myEntities[anEntPos].param[0] != 0) {
1406 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1407 if (aParamPos >= myParams.size())
1410 while (myEntities[anEntPos].param[aNbParams] != 0)
1412 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1413 myParamMaxID -= aNbParams;
1414 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1415 if (*aRemIter == myEntityMaxID)
1418 myEntities.erase(myEntities.begin() + anEntPos);
1419 myEntOfConstr.erase(myEntOfConstr.begin() + anEntPos);
1421 // Remove entity's ID from the lists of conincident points
1422 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1423 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1424 aCoPtIter->erase(*aRemIter);
1428 // ============================================================================
1429 // Function: addCoincidentPoints
1430 // Class: SketchSolver_ConstraintGroup
1431 // Purpose: add coincident point the appropriate list of such points
1432 // ============================================================================
1433 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1434 const Slvs_hEntity& thePoint2)
1436 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1437 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1438 while (aCoPtIter != myCoincidentPoints.end()) {
1439 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1440 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1441 != aCoPtIter->end(), };
1442 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1444 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1445 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1446 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1447 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1448 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1449 myCoincidentPoints.erase(aCoPtIter);
1450 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1451 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1454 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1455 aFirstFound = aCoPtIter;
1460 // No points were found, need to create new set
1461 if (aFirstFound == myCoincidentPoints.end()) {
1462 std::set<Slvs_hEntity> aNewSet;
1463 aNewSet.insert(thePoint1);
1464 aNewSet.insert(thePoint2);
1465 myCoincidentPoints.push_back(aNewSet);
1471 // ============================================================================
1472 // Function: updateRelatedConstraints
1473 // Class: SketchSolver_ConstraintGroup
1474 // Purpose: emit the signal to update constraints
1475 // ============================================================================
1476 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1477 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1479 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1480 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1481 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1482 ->attributes(std::string());
1484 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1485 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1486 bool isUpd = (*anAttrIter == theEntity);
1487 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1488 ModelAPI_AttributeRefAttr>(*anAttrIter);
1489 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1493 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1494 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1501 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1502 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1504 ConstraintMap::const_iterator aConstrIter = myConstraintMap.begin();
1505 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1506 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1507 ->attributes(std::string());
1509 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1510 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1511 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1512 ModelAPI_AttributeRefAttr>(*anAttrIter);
1513 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1514 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1515 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1522 // ========================================================
1523 // ========= Auxiliary functions ===============
1524 // ========================================================
1526 template<typename T>
1527 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1529 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1530 int aVecSize = theEntities.size();
1531 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1533 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1535 if (aResIndex == -1)
1536 aResIndex = aVecSize;