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 <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_ResultConstruction.h>
21 #include <SketchPlugin_Constraint.h>
22 #include <SketchPlugin_ConstraintLength.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Circle.h>
27 #include <SketchPlugin_Line.h>
28 #include <SketchPlugin_Point.h>
29 #include <SketchPlugin_Sketch.h>
34 /// Tolerance for value of parameters
35 const double tolerance = 1.e-10;
38 * Collects all sketch solver error' codes
39 * as inline static functions
40 * TODO: Move this class into a separate file
42 class SketchSolver_Error
45 /// The value parameter for the constraint
46 inline static const std::string& CONSTRAINTS()
48 static const std::string MY_ERROR_VALUE("Conflicting constraints");
49 return MY_ERROR_VALUE;
53 /// This value is used to give unique index to the groups
54 static Slvs_hGroup myGroupIndexer = 0;
56 /** \brief Search the entity/parameter with specified ID in the list of elements
57 * \param[in] theEntityID unique ID of the element
58 * \param[in] theEntities list of elements
59 * \return position of the found element or -1 if the element is not found
62 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
64 // ========================================================
65 // ========= SketchSolver_ConstraintGroup ===============
66 // ========================================================
68 SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup(
69 boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
70 : myID(++myGroupIndexer),
80 myEntOfConstr.clear();
81 myConstraints.clear();
83 myTempConstraints.clear();
84 myTempPointWhereDragged.clear();
85 myTempPointWDrgdID = 0;
87 // Initialize workplane
88 myWorkplane.h = SLVS_E_UNKNOWN;
90 assert(addWorkplane(theWorkplane));
92 addWorkplane(theWorkplane);
96 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
100 myEntOfConstr.clear();
101 myConstraints.clear();
102 myConstraintMap.clear();
103 myTempConstraints.clear();
104 myTempPointWhereDragged.clear();
106 // If the group with maximal identifier is deleted, decrease the indexer
107 if (myID == myGroupIndexer)
111 // ============================================================================
112 // Function: isBaseWorkplane
113 // Class: SketchSolver_ConstraintGroup
114 // Purpose: verify the group is based on the given workplane
115 // ============================================================================
116 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
117 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
119 return theWorkplane == mySketch;
122 // ============================================================================
123 // Function: isInteract
124 // Class: SketchSolver_ConstraintGroup
125 // Purpose: verify are there any entities in the group used by given constraint
126 // ============================================================================
127 bool SketchSolver_ConstraintGroup::isInteract(
128 boost::shared_ptr<SketchPlugin_Feature> theFeature) const
130 // Check the group is empty
134 // Go through the attributes and verify if some of them already in the group
135 std::list<boost::shared_ptr<ModelAPI_Attribute>>
136 anAttrList = theFeature->data()->attributes(std::string());
137 std::list<boost::shared_ptr<ModelAPI_Attribute>>::const_iterator
138 anAttrIter = anAttrList.begin();
139 for ( ; anAttrIter != anAttrList.end(); anAttrIter++) {
140 boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
141 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
142 if (!aCAttrRef || !aCAttrRef->isObject()) {
143 boost::shared_ptr<ModelAPI_Attribute> anAttr =
144 aCAttrRef ? aCAttrRef->attr() : *anAttrIter;
145 if (myEntityAttrMap.find(anAttr) != myEntityAttrMap.end())
148 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
149 aCAttrRef->object());
152 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
153 FeaturePtr aFeature = aDoc->feature(aRC);
154 if (myEntityFeatMap.find(aFeature) != myEntityFeatMap.end())
156 // search attributes of a feature to be parameters of constraint
157 std::list<boost::shared_ptr<ModelAPI_Attribute> > aFeatAttrList =
158 aFeature->data()->attributes(std::string());
159 std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator aFAIter = aFeatAttrList
161 for (; aFAIter != aFeatAttrList.end(); aFAIter++)
162 if (myEntityAttrMap.find(*aFAIter) != myEntityAttrMap.end())
167 // Entities did not found
171 // ============================================================================
172 // Function: checkConstraintConsistence
173 // Class: SketchSolver_ConstraintGroup
174 // Purpose: verifies and changes parameters of the constraint
175 // ============================================================================
176 void SketchSolver_ConstraintGroup::checkConstraintConsistence(Slvs_Constraint& theConstraint)
178 if (theConstraint.type == SLVS_C_PT_LINE_DISTANCE) {
179 // Get constraint parameters and check the sign of constraint value
182 int aPtPos = Search(theConstraint.ptA, myEntities);
183 int aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
184 boost::shared_ptr<GeomAPI_XY> aPoint(
185 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
188 int aLnPos = Search(theConstraint.entityA, myEntities);
189 aPtPos = Search(myEntities[aLnPos].point[0], myEntities);
190 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
191 boost::shared_ptr<GeomAPI_XY> aStart(
192 new GeomAPI_XY(-myParams[aPtParamPos].val, -myParams[aPtParamPos + 1].val));
193 aPtPos = Search(myEntities[aLnPos].point[1], myEntities);
194 aPtParamPos = Search(myEntities[aPtPos].param[0], myParams);
195 boost::shared_ptr<GeomAPI_XY> aEnd(
196 new GeomAPI_XY(myParams[aPtParamPos].val, myParams[aPtParamPos + 1].val));
198 aEnd = aEnd->added(aStart);
199 aPoint = aPoint->added(aStart);
200 if (aPoint->cross(aEnd) * theConstraint.valA < 0.0)
201 theConstraint.valA *= -1.0;
205 // ============================================================================
206 // Function: changeConstraint
207 // Class: SketchSolver_ConstraintGroup
208 // Purpose: create/update the constraint in the group
209 // ============================================================================
210 bool SketchSolver_ConstraintGroup::changeConstraint(
211 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
213 // There is no workplane yet, something wrong
214 if (myWorkplane.h == SLVS_E_UNKNOWN)
217 // Search this constraint in the current group to update it
218 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrMapIter =
219 myConstraintMap.find(theConstraint);
220 std::vector<Slvs_Constraint>::iterator aConstrIter;
221 if (aConstrMapIter != myConstraintMap.end()) {
222 int aConstrPos = Search(aConstrMapIter->second, myConstraints);
223 aConstrIter = myConstraints.begin() + aConstrPos;
226 // Get constraint type and verify the constraint parameters are correct
227 SketchSolver_Constraint aConstraint(theConstraint);
228 int aConstrType = aConstraint.getType();
229 if (aConstrType == SLVS_C_UNKNOWN
230 || (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
232 const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
234 // Create constraint parameters
235 double aDistance = 0.0; // scalar value of the constraint
236 AttributeDoublePtr aDistAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
237 theConstraint->data()->attribute(SketchPlugin_Constraint::VALUE()));
239 aDistance = aDistAttr->value();
240 // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
241 if (aConstrType == SLVS_C_DIAMETER)
243 if (aConstrMapIter != myConstraintMap.end()
244 && fabs(aConstrIter->valA - aDistance) > tolerance) {
245 myNeedToSolve = true;
246 aConstrIter->valA = aDistance;
250 Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
251 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
252 aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
253 boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = boost::dynamic_pointer_cast<
254 ModelAPI_AttributeRefAttr>(
255 theConstraint->data()->attribute(aConstraintAttributes[indAttr]));
259 // Convert the object of the attribute to the feature
261 if (aConstrAttr->isObject() && aConstrAttr->object()) {
262 ResultConstructionPtr aRC = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
263 aConstrAttr->object());
266 boost::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
267 aFeature = aDoc->feature(aRC);
270 // For the length constraint the start and end points of the line should be added to the entities list instead of line
271 if (aConstrType == SLVS_C_PT_PT_DISTANCE
272 && theConstraint->getKind().compare(SketchPlugin_ConstraintLength::ID()) == 0) {
273 Slvs_hEntity aLineEnt = changeEntity(aFeature);
274 int aEntPos = Search(aLineEnt, myEntities);
275 aConstrEnt[indAttr++] = myEntities[aEntPos].point[0];
276 aConstrEnt[indAttr++] = myEntities[aEntPos].point[1];
277 while (indAttr < CONSTRAINT_ATTR_SIZE)
278 aConstrEnt[indAttr++] = 0;
279 break; // there should be no other entities
280 } else if (aConstrAttr->isObject())
281 aConstrEnt[indAttr] = changeEntity(aFeature);
283 aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
286 if (aConstrMapIter == myConstraintMap.end()) { // Add new constraint
287 // Several points may be coincident, it is not necessary to store all constraints between them.
288 // Try to find sequence of coincident points which connects the points of new constraint
289 if (aConstrType == SLVS_C_POINTS_COINCIDENT) {
290 if (aConstrEnt[0] == aConstrEnt[1]) // no need to add self coincidence
292 if (!addCoincidentPoints(aConstrEnt[0], aConstrEnt[1])) {
293 myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
298 // Create SolveSpace constraint structure
299 Slvs_Constraint aConstraint = Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType,
300 myWorkplane.h, aDistance, aConstrEnt[0],
301 aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
302 myConstraints.push_back(aConstraint);
303 myConstraintMap[theConstraint] = aConstraint.h;
304 int aConstrPos = Search(aConstraint.h, myConstraints);
305 aConstrIter = myConstraints.begin() + aConstrPos;
306 myNeedToSolve = true;
307 } else { // Attributes of constraint may be changed => update constraint
308 Slvs_hEntity* aCurrentAttr[] = {&aConstrIter->ptA, &aConstrIter->ptB,
309 &aConstrIter->entityA, &aConstrIter->entityB,
310 &aConstrIter->entityC, &aConstrIter->entityD};
311 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
312 if (*(aCurrentAttr[indAttr]) != aConstrEnt[indAttr])
314 *(aCurrentAttr[indAttr]) = aConstrEnt[indAttr];
315 myNeedToSolve = true;
320 // Update flags of entities to be used by constraints
321 for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
322 if (aConstrEnt[indAttr] != 0) {
323 int aPos = Search(aConstrEnt[indAttr], myEntities);
324 myEntOfConstr[aPos] = true;
325 // Sub-entities should be used implcitly
326 Slvs_hEntity* aEntPtr = myEntities[aPos].point;
327 while (*aEntPtr != 0) {
328 aPos = Search(*aEntPtr, myEntities);
329 myEntOfConstr[aPos] = true;
334 checkConstraintConsistence(*aConstrIter);
338 // ============================================================================
339 // Function: changeEntity
340 // Class: SketchSolver_ConstraintGroup
341 // Purpose: create/update the element affected by any constraint
342 // ============================================================================
343 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
344 boost::shared_ptr<ModelAPI_Attribute> theEntity)
346 // If the entity is already in the group, try to find it
347 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
348 myEntityAttrMap.find(theEntity);
350 std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
351 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
352 aParamIter = myParams.end();
353 else { // the entity already exists
354 aEntPos = Search(aEntIter->second, myEntities);
355 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
356 aParamIter = myParams.begin() + aParamPos;
358 const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
359 const bool isNeedToSolve = myNeedToSolve;
360 myNeedToSolve = false;
362 // Look over supported types of entities
363 Slvs_Entity aNewEntity;
364 aNewEntity.h = SLVS_E_UNKNOWN;
367 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
370 Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
371 Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
372 Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
373 if (!isEntExists) // New entity
374 aNewEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
376 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
377 if (myWorkplane.h == SLVS_E_UNKNOWN)
378 return SLVS_E_UNKNOWN;
381 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
382 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
384 Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
385 Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
386 if (!isEntExists) // New entity
387 aNewEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
389 // Scalar value (used for the distance entities)
390 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
392 Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
393 if (!isEntExists) // New entity
394 aNewEntity = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
398 /// \todo Other types of entities
401 if (!myEntOfConstr[aEntPos]) // the entity is not used by constraints, no need to resolve them
402 myNeedToSolve = isNeedToSolve;
404 myNeedToSolve = myNeedToSolve || isNeedToSolve;
405 return aEntIter->second;
408 if (aNewEntity.h != SLVS_E_UNKNOWN) {
409 myEntities.push_back(aNewEntity);
410 myEntOfConstr.push_back(false);
411 myEntityAttrMap[theEntity] = aNewEntity.h;
415 // Unsupported or wrong entity type
416 return SLVS_E_UNKNOWN;
419 // ============================================================================
420 // Function: changeEntity
421 // Class: SketchSolver_ConstraintGroup
422 // Purpose: create/update the element defined by the feature affected by any constraint
423 // ============================================================================
424 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(FeaturePtr theEntity)
426 if (!theEntity->data()->isValid())
427 return SLVS_E_UNKNOWN;
428 // If the entity is already in the group, try to find it
429 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aEntIter = myEntityFeatMap.find(theEntity);
430 // defines that the entity already exists
431 const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
433 Slvs_Entity aNewEntity;
434 aNewEntity.h = SLVS_E_UNKNOWN;
436 // SketchPlugin features
437 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
438 SketchPlugin_Feature>(theEntity);
439 if (aFeature) { // Verify the feature by its kind
440 const std::string& aFeatureKind = aFeature->getKind();
443 if (aFeatureKind.compare(SketchPlugin_Line::ID()) == 0) {
444 Slvs_hEntity aStart = changeEntity(
445 aFeature->data()->attribute(SketchPlugin_Line::START_ID()));
446 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Line::END_ID()));
448 if (!isEntExists) // New entity
449 aNewEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
452 else if (aFeatureKind.compare(SketchPlugin_Circle::ID()) == 0) {
453 Slvs_hEntity aCenter = changeEntity(
454 aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
455 Slvs_hEntity aRadius = changeEntity(
456 aFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
458 if (!isEntExists) // New entity
459 aNewEntity = Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter,
460 myWorkplane.normal, aRadius);
463 else if (aFeatureKind.compare(SketchPlugin_Arc::ID()) == 0) {
464 Slvs_hEntity aCenter = changeEntity(
465 aFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
466 Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
467 Slvs_hEntity aEnd = changeEntity(aFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
470 aNewEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, myWorkplane.h,
471 myWorkplane.normal, aCenter, aStart, aEnd);
473 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
474 else if (aFeatureKind.compare(SketchPlugin_Point::ID()) == 0) {
475 Slvs_hEntity aPoint = changeEntity(
476 aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
479 return aEntIter->second;
481 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
482 myEntityFeatMap[theEntity] = aPoint;
483 myNeedToSolve = true;
487 /// \todo Other types of features
490 return aEntIter->second;
492 if (aNewEntity.h != SLVS_E_UNKNOWN) {
493 myEntities.push_back(aNewEntity);
494 myEntOfConstr.push_back(false);
495 myEntityFeatMap[theEntity] = aNewEntity.h;
496 myNeedToSolve = true;
501 // Unsupported or wrong entity type
502 return SLVS_E_UNKNOWN;
505 // ============================================================================
506 // Function: changeNormal
507 // Class: SketchSolver_ConstraintGroup
508 // Purpose: create/update the normal of workplane
509 // ============================================================================
510 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
511 boost::shared_ptr<ModelAPI_Attribute> theDirX, boost::shared_ptr<ModelAPI_Attribute> theDirY,
512 boost::shared_ptr<ModelAPI_Attribute> theNorm)
514 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
515 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
516 if (!aDirX || !aDirY || (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance)
517 || (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
518 return SLVS_E_UNKNOWN;
520 // quaternion parameters of normal vector
521 double qw, qx, qy, qz;
522 Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), aDirY->x(), aDirY->y(), aDirY->z(), &qw,
524 double aNormCoord[4] = { qw, qx, qy, qz };
526 // Try to find existent normal
527 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aEntIter =
528 myEntityAttrMap.find(theNorm);
529 std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
530 if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
531 aParamIter = myParams.end();
532 else { // the entity already exists, update it
533 int aEntPos = Search(aEntIter->second, myEntities);
534 int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
535 aParamIter = myParams.begin() + aParamPos;
538 // Change parameters of the normal
539 Slvs_hParam aNormParams[4];
540 for (int i = 0; i < 4; i++)
541 aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
543 if (aEntIter != myEntityAttrMap.end()) // the entity already exists
544 return aEntIter->second;
547 Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, aNormParams[0], aNormParams[1],
548 aNormParams[2], aNormParams[3]);
549 myEntities.push_back(aNormal);
550 myEntOfConstr.push_back(false);
551 myEntityAttrMap[theNorm] = aNormal.h;
555 // ============================================================================
556 // Function: addWorkplane
557 // Class: SketchSolver_ConstraintGroup
558 // Purpose: create workplane for the group
559 // ============================================================================
560 bool SketchSolver_ConstraintGroup::addWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
562 if (myWorkplane.h || theSketch->getKind().compare(SketchPlugin_Sketch::ID()) != 0)
563 return false; // the workplane already exists or the function parameter is not Sketch
565 mySketch = theSketch;
570 // ============================================================================
571 // Function: updateWorkplane
572 // Class: SketchSolver_ConstraintGroup
573 // Purpose: update parameters of workplane
574 // ============================================================================
575 bool SketchSolver_ConstraintGroup::updateWorkplane()
577 // Get parameters of workplane
578 boost::shared_ptr<ModelAPI_Attribute> aDirX = mySketch->data()->attribute(
579 SketchPlugin_Sketch::DIRX_ID());
580 boost::shared_ptr<ModelAPI_Attribute> aDirY = mySketch->data()->attribute(
581 SketchPlugin_Sketch::DIRY_ID());
582 boost::shared_ptr<ModelAPI_Attribute> aNorm = mySketch->data()->attribute(
583 SketchPlugin_Sketch::NORM_ID());
584 boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(
585 SketchPlugin_Sketch::ORIGIN_ID());
586 // Transform them into SolveSpace format
587 Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
590 Slvs_hEntity anOriginWP = changeEntity(anOrigin);
594 if (!myWorkplane.h) {
596 myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
597 // Workplane should be added to the list of entities
598 myEntities.push_back(myWorkplane);
599 myEntOfConstr.push_back(false);
604 // ============================================================================
605 // Function: changeParameter
606 // Class: SketchSolver_ConstraintGroup
607 // Purpose: create/update value of parameter
608 // ============================================================================
609 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
610 const double& theParam, std::vector<Slvs_Param>::const_iterator& thePrmIter)
612 if (thePrmIter != myParams.end()) { // Parameter should be updated
613 int aParamPos = thePrmIter - myParams.begin();
614 if (fabs(thePrmIter->val - theParam) > tolerance) {
615 myNeedToSolve = true; // parameter is changed, need to resolve constraints
616 myParams[aParamPos].val = theParam;
619 return myParams[aParamPos].h;
622 // Newly created parameter
623 Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
624 myParams.push_back(aParam);
625 myNeedToSolve = true;
626 // The list of parameters is changed, move iterator to the end of the list to avoid problems
627 thePrmIter = myParams.end();
631 // ============================================================================
632 // Function: resolveConstraints
633 // Class: SketchSolver_ConstraintGroup
634 // Purpose: solve the set of constraints for the current group
635 // ============================================================================
636 bool SketchSolver_ConstraintGroup::resolveConstraints()
641 myConstrSolver.setGroupID(myID);
642 myConstrSolver.setParameters(myParams);
643 myConstrSolver.setEntities(myEntities);
644 myConstrSolver.setConstraints(myConstraints);
645 myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
647 int aResult = myConstrSolver.solve();
648 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
649 // Obtain result into the same list of parameters
650 if (!myConstrSolver.getResult(myParams))
653 // We should go through the attributes map, because only attributes have valued parameters
654 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntIter =
655 myEntityAttrMap.begin();
656 for (; anEntIter != myEntityAttrMap.end(); anEntIter++)
657 if (updateAttribute(anEntIter->first, anEntIter->second))
658 updateRelatedConstraints(anEntIter->first);
659 } else if (!myConstraints.empty())
660 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
662 removeTemporaryConstraints();
663 myNeedToSolve = false;
667 // ============================================================================
668 // Function: mergeGroups
669 // Class: SketchSolver_ConstraintGroup
670 // Purpose: append specified group to the current group
671 // ============================================================================
672 void SketchSolver_ConstraintGroup::mergeGroups(const SketchSolver_ConstraintGroup& theGroup)
674 // If specified group is empty, no need to merge
675 if (theGroup.myConstraintMap.empty())
678 // Map between old and new indexes of SolveSpace constraints
679 std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
681 // Add all constraints from theGroup to the current group
682 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
683 theGroup.myConstraintMap.begin();
684 for (; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
685 if (changeConstraint(aConstrIter->first))
686 aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
688 // Add temporary constraints from theGroup
689 std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
690 for (; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++) {
691 std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(
693 if (aFind != aConstrMap.end())
694 myTempConstraints.push_back(aFind->second);
697 if (myTempPointWhereDragged.empty())
698 myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
699 else if (!theGroup.myTempPointWhereDragged.empty()) { // Need to create additional transient constraint
700 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator aFeatureIter =
701 theGroup.myEntityAttrMap.begin();
702 for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
703 if (aFeatureIter->second == myTempPointWDrgdID) {
704 addTemporaryConstraintWhereDragged(aFeatureIter->first);
709 myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
712 // ============================================================================
713 // Function: splitGroup
714 // Class: SketchSolver_ConstraintGroup
715 // Purpose: divide the group into several subgroups
716 // ============================================================================
717 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
719 // Divide constraints and entities into several groups
720 std::vector<std::set<Slvs_hEntity> > aGroupsEntities;
721 std::vector<std::set<Slvs_hConstraint> > aGroupsConstr;
722 int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
723 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
724 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
725 Slvs_hEntity aConstrEnt[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA,
726 aConstrIter->entityB };
727 std::vector<int> anIndexes;
728 // Go through the groupped entities and find even one of entities of current constraint
729 std::vector<std::set<Slvs_hEntity> >::iterator aGrEntIter;
730 for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++) {
731 bool isFound = false;
732 for (int i = 0; i < 4 && !isFound; i++)
733 if (aConstrEnt[i] != 0) {
734 isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
735 // Also we need to check sub-entities
736 int aEntPos = Search(aConstrEnt[i], myEntities);
737 Slvs_hEntity* aSub = myEntities[aEntPos].point;
738 for (int j = 0; *aSub != 0 && j < 4 && !isFound; aSub++)
739 isFound = (aGrEntIter->find(*aSub) != aGrEntIter->end());
742 anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
744 // Add new group if no one is found
745 if (anIndexes.empty()) {
746 std::set<Slvs_hEntity> aNewGrEnt;
747 for (int i = 0; i < 4; i++)
748 if (aConstrEnt[i] != 0)
749 aNewGrEnt.insert(aConstrEnt[i]);
750 std::set<Slvs_hConstraint> aNewGrConstr;
751 aNewGrConstr.insert(aConstrIter->h);
753 aGroupsEntities.push_back(aNewGrEnt);
754 aGroupsConstr.push_back(aNewGrConstr);
755 if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
756 aMaxNbEntities = aGroupsEntities.size() - 1;
757 } else if (anIndexes.size() == 1) { // Add entities indexes into the found group
758 aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
759 for (int i = 0; i < 4; i++)
760 if (aConstrEnt[i] != 0)
761 aGrEntIter->insert(aConstrEnt[i]);
762 aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
763 if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
764 aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
765 } else { // There are found several connected groups, merge them
766 std::vector<std::set<Slvs_hEntity> >::iterator aFirstGroup = aGroupsEntities.begin()
768 std::vector<std::set<Slvs_hConstraint> >::iterator aFirstConstr = aGroupsConstr.begin()
770 std::vector<int>::iterator anInd = anIndexes.begin();
771 for (++anInd; anInd != anIndexes.end(); anInd++) {
772 aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
773 aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
775 if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
776 aMaxNbEntities = anIndexes.front();
777 // Remove merged groups
778 for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--) {
779 aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
780 aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
785 if (aGroupsEntities.size() <= 1)
788 // Remove the group with maximum elements as it will be left in the current group
789 aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
790 aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
792 // Add new groups of constraints and divide current group
793 std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
794 for (int i = aGroupsEntities.size(); i > 0; i--) {
795 SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
796 aNewGroups.push_back(aG);
798 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrMapIter =
799 myConstraintMap.begin();
800 int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
801 while (aConstrMapIter != myConstraintMap.end()) {
802 std::vector<std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
803 std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
804 for (; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
805 if (aGIter->find(aConstrMapIter->second) != aGIter->end()) {
806 (*aGroup)->changeConstraint(aConstrMapIter->first);
807 removeConstraint(aConstrMapIter->first);
809 aConstrMapIter = myConstraintMap.begin();
810 for (int i = 0; i < aConstrMapPos; i++)
814 if (aGIter == aGroupsConstr.end()) {
820 theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
823 // ============================================================================
824 // Function: updateGroup
825 // Class: SketchSolver_ConstraintGroup
826 // Purpose: search removed entities and constraints
827 // ============================================================================
828 bool SketchSolver_ConstraintGroup::updateGroup()
830 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator aConstrIter =
831 myConstraintMap.rbegin();
832 bool isAllValid = true;
833 bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
834 int aConstrIndex = 0;
835 while (/*isAllValid && */aConstrIter != myConstraintMap.rend()) {
836 if (!aConstrIter->first->data() || !aConstrIter->first->data()->isValid()) {
837 if (aConstrIter->first->getKind().compare(SketchPlugin_ConstraintCoincidence::ID()) == 0)
839 removeConstraint(aConstrIter->first);
841 // Get back the correct position of iterator after the "remove" operation
842 aConstrIter = myConstraintMap.rbegin();
843 for (int i = 0; i < aConstrIndex && aConstrIter != myConstraintMap.rend(); i++)
851 // Check if some entities are invalid too
852 std::set<Slvs_hEntity> anEntToRemove;
853 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
854 anAttrIter = myEntityAttrMap.begin();
855 while (anAttrIter != myEntityAttrMap.end()) {
856 if (!anAttrIter->first->owner() || !anAttrIter->first->owner()->data() ||
857 !anAttrIter->first->owner()->data()->isValid()) {
858 anEntToRemove.insert(anAttrIter->second);
859 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
860 aRemovedIter = anAttrIter;
862 myEntityAttrMap.erase(aRemovedIter);
866 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myEntityFeatMap.begin();
867 while (aFeatIter != myEntityFeatMap.end()) {
868 if (!aFeatIter->first || !aFeatIter->first->data() ||
869 !aFeatIter->first->data()->isValid()) {
870 anEntToRemove.insert(aFeatIter->second);
871 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = aFeatIter;
873 myEntityFeatMap.erase(aRemovedIter);
877 removeEntitiesById(anEntToRemove);
879 // Probably, need to update coincidence constraints
880 if (isCCRemoved && !myExtraCoincidence.empty()) {
881 // Make a copy, because the new list of unused constrtaints will be generated
882 std::set<boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
883 myExtraCoincidence.clear();
885 std::set<boost::shared_ptr<SketchPlugin_Constraint> >::iterator aCIter = anExtraCopy.begin();
886 for (; aCIter != anExtraCopy.end(); aCIter++)
887 if ((*aCIter)->data() && (*aCIter)->data()->isValid())
888 changeConstraint(*aCIter);
894 // ============================================================================
895 // Function: updateAttribute
896 // Class: SketchSolver_ConstraintGroup
897 // Purpose: update features of sketch after resolving constraints
898 // ============================================================================
899 bool SketchSolver_ConstraintGroup::updateAttribute(
900 boost::shared_ptr<ModelAPI_Attribute> theAttribute, const Slvs_hEntity& theEntityID)
902 // Search the position of the first parameter of the entity
903 int anEntPos = Search(theEntityID, myEntities);
904 int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
906 // Look over supported types of entities
909 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
912 if (fabs(aPoint->x() - myParams[aFirstParamPos].val) > tolerance
913 || fabs(aPoint->y() - myParams[aFirstParamPos + 1].val) > tolerance
914 || fabs(aPoint->z() - myParams[aFirstParamPos + 2].val) > tolerance) {
915 aPoint->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val,
916 myParams[aFirstParamPos + 2].val);
923 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
924 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
926 if (fabs(aPoint2D->x() - myParams[aFirstParamPos].val) > tolerance
927 || fabs(aPoint2D->y() - myParams[aFirstParamPos + 1].val) > tolerance) {
928 aPoint2D->setValue(myParams[aFirstParamPos].val, myParams[aFirstParamPos + 1].val);
935 AttributeDoublePtr aScalar = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
937 if (fabs(aScalar->value() - myParams[aFirstParamPos].val) > tolerance) {
938 aScalar->setValue(myParams[aFirstParamPos].val);
944 /// \todo Support other types of entities
948 // ============================================================================
949 // Function: updateEntityIfPossible
950 // Class: SketchSolver_ConstraintGroup
951 // Purpose: search the entity in this group and update it
952 // ============================================================================
953 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
954 boost::shared_ptr<ModelAPI_Attribute> theEntity)
956 if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end()) {
957 // If the attribute is a point and it is changed (the group needs to rebuild),
958 // probably user has dragged this point into this position,
959 // so it is necessary to add constraint which will guarantee the point will not change
961 // Store myNeedToSolve flag to verify the entity is really changed
962 bool aNeedToSolveCopy = myNeedToSolve;
963 myNeedToSolve = false;
965 changeEntity(theEntity);
967 if (myNeedToSolve) // the entity is changed
969 // Verify the entity is a point and add temporary constraint of permanency
970 boost::shared_ptr<GeomDataAPI_Point> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
972 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = boost::dynamic_pointer_cast<
973 GeomDataAPI_Point2D>(theEntity);
974 if (aPoint || aPoint2D)
975 addTemporaryConstraintWhereDragged(theEntity);
978 // Restore flag of changes
979 myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
982 updateRelatedConstraints(theEntity);
986 // ============================================================================
987 // Function: addTemporaryConstraintWhereDragged
988 // Class: SketchSolver_ConstraintGroup
989 // Purpose: add transient constraint SLVS_C_WHERE_DRAGGED for the entity,
990 // which was moved by user
991 // ============================================================================
992 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
993 boost::shared_ptr<ModelAPI_Attribute> theEntity)
995 // Find identifier of the entity
996 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
997 myEntityAttrMap.find(theEntity);
998 if (anEntIter == myEntityAttrMap.end())
1001 // If this is a first dragged point, its parameters should be placed
1002 // into Slvs_System::dragged field to avoid system inconsistense
1003 if (myTempPointWhereDragged.empty()) {
1004 int anEntPos = Search(anEntIter->second, myEntities);
1005 Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
1006 for (int i = 0; i < 4; i++, aDraggedParam++)
1007 if (*aDraggedParam != 0)
1008 myTempPointWhereDragged.push_back(*aDraggedParam);
1009 myTempPointWDrgdID = myEntities[anEntPos].h;
1013 // Get identifiers of all dragged points
1014 std::set<Slvs_hEntity> aDraggedPntID;
1015 aDraggedPntID.insert(myTempPointWDrgdID);
1016 std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
1017 for (; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++) {
1018 unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
1019 if (aConstrPos < myConstraints.size())
1020 aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
1022 // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
1023 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1024 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++) {
1025 if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
1026 continue; // the entity was not found in current set
1028 // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
1029 std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
1030 for (; aDrgIter != aDraggedPntID.end(); aDrgIter++)
1031 if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
1032 return; // the SLVS_C_WHERE_DRAGGED constraint already exists
1035 // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
1036 Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
1037 myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
1038 myConstraints.push_back(aWDConstr);
1039 myTempConstraints.push_back(aWDConstr.h);
1042 // ============================================================================
1043 // Function: removeTemporaryConstraints
1044 // Class: SketchSolver_ConstraintGroup
1045 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
1046 // resolving the set of constraints
1047 // ============================================================================
1048 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
1050 std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
1051 for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend();
1053 unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
1054 if (aConstrPos >= myConstraints.size())
1056 myConstraints.erase(myConstraints.begin() + aConstrPos);
1058 // If the removing constraint has higher index, decrease the indexer
1059 if (*aTmpConstrIter == myConstrMaxID)
1062 myTempConstraints.clear();
1064 // Clear basic dragged point
1065 myTempPointWhereDragged.clear();
1068 // ============================================================================
1069 // Function: removeConstraint
1070 // Class: SketchSolver_ConstraintGroup
1071 // Purpose: remove constraint and all unused entities
1072 // ============================================================================
1073 void SketchSolver_ConstraintGroup::removeConstraint(
1074 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
1076 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator anIterToRemove =
1077 myConstraintMap.find(theConstraint);
1078 if (anIterToRemove == myConstraintMap.end())
1081 Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
1082 // Remove constraint from the map
1083 myConstraintMap.erase(anIterToRemove);
1085 // Find unused entities
1086 int aConstrPos = Search(aCnstrToRemove, myConstraints);
1087 std::set<Slvs_hEntity> anEntToRemove;
1088 Slvs_hEntity aCnstEnt[] = { myConstraints[aConstrPos].ptA, myConstraints[aConstrPos].ptB,
1089 myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB };
1090 for (int i = 0; i < 4; i++)
1091 if (aCnstEnt[i] != 0)
1092 anEntToRemove.insert(aCnstEnt[i]);
1093 myConstraints.erase(myConstraints.begin() + aConstrPos);
1094 if (aCnstrToRemove == myConstrMaxID)
1097 // Find all entities which are based on these unused
1098 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
1099 for ( ; anEntIter != myEntities.end(); anEntIter++)
1100 if (anEntIter->type == SLVS_E_LINE_SEGMENT || anEntIter->type == SLVS_E_CIRCLE ||
1101 anEntIter->type == SLVS_E_ARC_OF_CIRCLE) {
1102 for (int i = 0; i < 4; i++)
1103 if (anEntToRemove.find(anEntIter->point[i]) != anEntToRemove.end()) {
1104 anEntToRemove.insert(anEntIter->h);
1105 for (int j = 0; j < 4; j++)
1106 if (anEntIter->param[j] != 0)
1107 anEntToRemove.insert(anEntIter->point[j]);
1112 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
1113 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
1114 Slvs_hEntity aEnts[] = { aConstrIter->ptA, aConstrIter->ptB, aConstrIter->entityA, aConstrIter
1116 for (int i = 0; i < 4; i++)
1117 if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
1118 anEntToRemove.erase(aEnts[i]);
1121 if (anEntToRemove.empty())
1124 // Remove unused entities
1125 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator anEntAttrIter =
1126 myEntityAttrMap.begin();
1127 while (anEntAttrIter != myEntityAttrMap.end()) {
1128 if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end()) {
1129 std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator aRemovedIter =
1132 myEntityAttrMap.erase(aRemovedIter);
1136 std::map<FeaturePtr, Slvs_hEntity>::iterator anEntFeatIter = myEntityFeatMap.begin();
1137 while (anEntFeatIter != myEntityFeatMap.end()) {
1138 if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end()) {
1139 std::map<FeaturePtr, Slvs_hEntity>::iterator aRemovedIter = anEntFeatIter;
1141 myEntityFeatMap.erase(aRemovedIter);
1146 removeEntitiesById(anEntToRemove);
1148 if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1149 myCoincidentPoints.clear();
1152 // ============================================================================
1153 // Function: removeEntitiesById
1154 // Class: SketchSolver_ConstraintGroup
1155 // Purpose: Removes specified entities and their parameters
1156 // ============================================================================
1157 void SketchSolver_ConstraintGroup::removeEntitiesById(const std::set<Slvs_hEntity>& theEntities)
1159 std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = theEntities.rbegin();
1160 for (; aRemIter != theEntities.rend(); aRemIter++) {
1161 unsigned int anEntPos = Search(*aRemIter, myEntities);
1162 if (anEntPos >= myEntities.size())
1164 if (myEntities[anEntPos].param[0] != 0) {
1165 unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1166 if (aParamPos >= myParams.size())
1169 while (myEntities[anEntPos].param[aNbParams] != 0)
1171 if (myEntities[anEntPos].param[aNbParams - 1] == myParamMaxID)
1172 myParamMaxID -= aNbParams;
1173 myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1174 if (*aRemIter == myEntityMaxID)
1177 myEntities.erase(myEntities.begin() + anEntPos);
1178 myEntOfConstr.erase(myEntOfConstr.begin() + anEntPos);
1180 // Remove entity's ID from the lists of conincident points
1181 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1182 for (; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1183 aCoPtIter->erase(*aRemIter);
1187 // ============================================================================
1188 // Function: addCoincidentPoints
1189 // Class: SketchSolver_ConstraintGroup
1190 // Purpose: add coincident point the appropriate list of such points
1191 // ============================================================================
1192 bool SketchSolver_ConstraintGroup::addCoincidentPoints(const Slvs_hEntity& thePoint1,
1193 const Slvs_hEntity& thePoint2)
1195 std::vector<std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1196 std::vector<std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1197 while (aCoPtIter != myCoincidentPoints.end()) {
1198 bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1199 aCoPtIter->find(thePoint1) != aCoPtIter->end(), aCoPtIter->find(thePoint2)
1200 != aCoPtIter->end(), };
1201 if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1203 if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1])) {
1204 if (aFirstFound != myCoincidentPoints.end()) { // there are two groups of coincident points connected by created constraint => merge them
1205 int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1206 int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1207 aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1208 myCoincidentPoints.erase(aCoPtIter);
1209 aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1210 aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1213 aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1214 aFirstFound = aCoPtIter;
1219 // No points were found, need to create new set
1220 if (aFirstFound == myCoincidentPoints.end()) {
1221 std::set<Slvs_hEntity> aNewSet;
1222 aNewSet.insert(thePoint1);
1223 aNewSet.insert(thePoint2);
1224 myCoincidentPoints.push_back(aNewSet);
1230 // ============================================================================
1231 // Function: updateRelatedConstraints
1232 // Class: SketchSolver_ConstraintGroup
1233 // Purpose: emit the signal to update constraints
1234 // ============================================================================
1235 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1236 boost::shared_ptr<ModelAPI_Attribute> theEntity) const
1238 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1239 myConstraintMap.begin();
1240 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1241 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1242 ->attributes(std::string());
1244 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1245 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1246 bool isUpd = (*anAttrIter == theEntity);
1247 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1248 ModelAPI_AttributeRefAttr>(*anAttrIter);
1249 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theEntity)
1253 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1254 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1261 void SketchSolver_ConstraintGroup::updateRelatedConstraints(
1262 boost::shared_ptr<ModelAPI_Feature> theFeature) const
1264 std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator aConstrIter =
1265 myConstraintMap.begin();
1266 for (; aConstrIter != myConstraintMap.end(); aConstrIter++) {
1267 std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttributes = aConstrIter->first->data()
1268 ->attributes(std::string());
1270 std::list<boost::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttributes.begin();
1271 for (; anAttrIter != anAttributes.end(); anAttrIter++) {
1272 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr = boost::dynamic_pointer_cast<
1273 ModelAPI_AttributeRefAttr>(*anAttrIter);
1274 if (aRefAttr && aRefAttr->isObject() && aRefAttr->object() == theFeature) {
1275 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
1276 ModelAPI_EventCreator::get()->sendUpdated(aConstrIter->first, anEvent);
1283 // ========================================================
1284 // ========= Auxiliary functions ===============
1285 // ========================================================
1287 template<typename T>
1288 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1290 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1291 int aVecSize = theEntities.size();
1292 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1294 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1296 if (aResIndex == -1)
1297 aResIndex = aVecSize;