1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Group.cpp
4 // Created: 27 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchSolver_Group.h"
9 #include <SketchSolver_Constraint.h>
10 #include <SketchSolver_ConstraintCoincidence.h>
11 #include <SketchSolver_ConstraintMulti.h>
12 #include <SketchSolver_Error.h>
13 #include <SketchSolver_Manager.h>
15 #include <Events_InfoMessage.h>
16 #include <Events_Loop.h>
17 #include <ModelAPI_AttributeString.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_ConstraintAngle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintDistance.h>
26 #include <SketchPlugin_ConstraintEqual.h>
27 #include <SketchPlugin_ConstraintHorizontal.h>
28 #include <SketchPlugin_ConstraintLength.h>
29 #include <SketchPlugin_ConstraintFillet.h>
30 #include <SketchPlugin_ConstraintMirror.h>
31 #include <SketchPlugin_ConstraintParallel.h>
32 #include <SketchPlugin_ConstraintPerpendicular.h>
33 #include <SketchPlugin_ConstraintRadius.h>
34 #include <SketchPlugin_ConstraintRigid.h>
35 #include <SketchPlugin_ConstraintTangent.h>
36 #include <SketchPlugin_ConstraintVertical.h>
37 #include <SketchPlugin_MultiRotation.h>
38 #include <SketchPlugin_MultiTranslation.h>
44 /// \brief This class is used to give unique index to the groups
48 /// \brief Return vacant index
49 static GroupID NEW_GROUP() { return ++myGroupIndex; }
50 /// \brief Removes the index
51 static void REMOVE_GROUP(const GroupID& theIndex) {
52 if (myGroupIndex == theIndex)
59 static GroupID myGroupIndex; ///< index of the group
62 GroupID GroupIndexer::myGroupIndex = GID_OUTOFGROUP;
65 static void sendMessage(const char* theMessageName)
67 std::shared_ptr<Events_Message> aMessage = std::shared_ptr<Events_Message>(
68 new Events_Message(Events_Loop::eventByName(theMessageName)));
69 Events_Loop::loop()->send(aMessage);
72 static void sendMessage(const char* theMessageName, const std::set<ObjectPtr>& theConflicting)
74 std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
75 std::shared_ptr<ModelAPI_SolverFailedMessage>(
76 new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName)));
77 aMessage->setObjects(theConflicting);
78 Events_Loop::loop()->send(aMessage);
83 // ========================================================
84 // ========= SketchSolver_Group ===============
85 // ========================================================
87 SketchSolver_Group::SketchSolver_Group(
88 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
89 : myID(GroupIndexer::NEW_GROUP()),
90 myPrevResult(STATUS_UNKNOWN)
92 // Initialize workplane
93 myWorkplaneID = EID_UNKNOWN;
94 addWorkplane(theWorkplane);
97 SketchSolver_Group::~SketchSolver_Group()
99 myConstraints.clear();
100 GroupIndexer::REMOVE_GROUP(myID);
101 // send the message that there is no more conflicting constraints
102 if (!myConflictingConstraints.empty()) {
103 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
104 myConflictingConstraints.clear();
108 // ============================================================================
109 // Function: isBaseWorkplane
110 // Class: SketchSolver_Group
111 // Purpose: verify the group is based on the given workplane
112 // ============================================================================
113 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
115 return theWorkplane == mySketch;
118 // ============================================================================
119 // Function: isInteract
120 // Class: SketchSolver_Group
121 // Purpose: verify are there any entities in the group used by given constraint
122 // ============================================================================
123 bool SketchSolver_Group::isInteract(FeaturePtr theFeature) const
125 // Empty group interacts with everything
128 // Check interaction with the storage
129 bool isInteracted = myStorage->isInteract(theFeature);
130 ConstraintConstraintMap::const_iterator anIt = myConstraints.begin();
131 for (; !isInteracted && anIt != myConstraints.end(); ++anIt)
132 if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() ||
133 anIt->first->getKind() == SketchPlugin_MultiTranslation::ID())
134 isInteracted = anIt->second->isUsed(theFeature);
138 // ============================================================================
139 // Function: changeConstraint
140 // Class: SketchSolver_Group
141 // Purpose: create/update the constraint in the group
142 // ============================================================================
143 bool SketchSolver_Group::changeConstraint(
144 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
146 // There is no workplane yet, something wrong
147 if (myWorkplaneID == EID_UNKNOWN)
150 if (!theConstraint || !theConstraint->data())
153 if (!checkFeatureValidity(theConstraint))
156 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
157 myStorage->blockEvents(true);
159 bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
160 if (isNewConstraint) {
161 // Add constraint to the current group
162 SolverConstraintPtr aConstraint = aBuilder->createConstraint(theConstraint);
165 aConstraint->process(myStorage, getId(), getWorkplaneId());
166 if (!aConstraint->error().empty()) {
167 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
168 return false; // some attribute are not initialized yet, don't show message
169 Events_InfoMessage("SketchSolver_Group", aConstraint->error(), this).send();
171 myConstraints[theConstraint] = aConstraint;
173 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID())
174 notifyCoincidenceChanged(myConstraints[theConstraint]);
177 myConstraints[theConstraint]->update();
180 if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
181 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
182 theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_A()));
183 if (aRefAttr && aRefAttr->isObject()) {
184 std::shared_ptr<SketchPlugin_Feature> aFeature =
185 std::dynamic_pointer_cast<SketchPlugin_Feature>(
186 ModelAPI_Feature::feature(aRefAttr->object()));
188 SolverConstraintPtr aConstraint = aBuilder->createFixedConstraint(aFeature);
190 aConstraint->process(myStorage, getId(), getWorkplaneId());
191 setTemporary(aConstraint);
199 // Update constraints if they contain specific feature
200 static void updateMultiConstraints(ConstraintConstraintMap& theConstraints, FeaturePtr theFeature)
202 ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
203 for (; aCIt != theConstraints.end(); ++aCIt) {
204 SketchSolver_ConstraintType aType = aCIt->second->getType();
205 if ((aType == CONSTRAINT_MULTI_ROTATION ||
206 aType == CONSTRAINT_MULTI_TRANSLATION)
207 && aCIt->second->isUsed(theFeature))
208 std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
209 else if ((aType == CONSTRAINT_TANGENT_CIRCLE_LINE ||
210 aType == CONSTRAINT_SYMMETRIC || aType == CONSTRAINT_ANGLE)
211 && aCIt->second->isUsed(theFeature))
212 aCIt->second->update();
216 // Recalculate slave features of the Multi constraints
217 static void updateMultiConstraints(ConstraintConstraintMap& theConstraints)
219 ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
220 for (; aCIt != theConstraints.end(); ++aCIt) {
221 SketchSolver_ConstraintType aType = aCIt->second->getType();
222 if ((aType == CONSTRAINT_MULTI_ROTATION ||
223 aType == CONSTRAINT_MULTI_TRANSLATION))
224 std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
228 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
230 if (!checkFeatureValidity(theFeature))
233 bool isBlocked = myStorage->isEventsBlocked();
235 myStorage->blockEvents(true);
237 myStorage->refresh(true);
238 bool isUpdated = myStorage->update(theFeature);
240 updateMultiConstraints(myConstraints, theFeature);
242 // events were not blocked before, the feature has not been updated,
243 // so it is necessary to revert blocking
244 if (!isUpdated && !isBlocked)
245 myStorage->blockEvents(false);
249 bool SketchSolver_Group::moveFeature(FeaturePtr theFeature)
251 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
253 // Firstly, revert changes in the fixed entities
254 myStorage->blockEvents(true);
255 myStorage->refresh(true);
257 // Secondly, search attributes of the feature in the list of the Multi constraints and update them
258 updateMultiConstraints(myConstraints, theFeature);
260 // Then, create temporary Fixed constraint
261 SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature);
264 aConstraint->process(myStorage, getId(), getWorkplaneId());
265 if (aConstraint->error().empty())
266 setTemporary(aConstraint);
268 // Workaround to process arcs.
269 // When move unconstrained arc, add temporary constraint to fix radius.
270 if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
271 bool hasDup = myStorage->hasDuplicatedConstraint();
272 SolverConstraintPtr aFixedRadius = aBuilder->createFixedArcRadiusConstraint(theFeature);
274 aFixedRadius->process(myStorage, getId(), getWorkplaneId());
275 hasDup = myStorage->hasDuplicatedConstraint() && !hasDup;
276 if (aFixedRadius->error().empty() && !hasDup)
277 setTemporary(aFixedRadius);
279 aFixedRadius->remove();
285 // ============================================================================
286 // Function: addWorkplane
287 // Class: SketchSolver_Group
288 // Purpose: create workplane for the group
289 // ============================================================================
290 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
292 if (myWorkplaneID != EID_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
293 return false; // the workplane already exists or the function parameter is not Sketch
295 mySketch = theSketch;
296 if (!updateWorkplane()) {
297 mySketch = CompositeFeaturePtr();
303 // ============================================================================
304 // Function: updateWorkplane
305 // Class: SketchSolver_Group
306 // Purpose: update parameters of workplane
307 // ============================================================================
308 bool SketchSolver_Group::updateWorkplane()
310 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
311 if (!myStorage) // Create storage if not exists
312 myStorage = aBuilder->createStorage(getId());
314 // sketch should be unchanged, set it out of current group
315 bool isUpdated = myStorage->update(FeaturePtr(mySketch), GID_OUTOFGROUP);
317 EntityWrapperPtr anEntity = myStorage->entity(FeaturePtr(mySketch));
318 myWorkplaneID = anEntity->id();
323 // ============================================================================
324 // Function: resolveConstraints
325 // Class: SketchSolver_Group
326 // Purpose: solve the set of constraints for the current group
327 // ============================================================================
328 bool SketchSolver_Group::resolveConstraints()
330 bool aResolved = false;
331 bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
332 if (myStorage->isNeedToResolve() &&
333 (!isGroupEmpty || !myConflictingConstraints.empty() || myPrevResult == STATUS_FAILED)) {
335 mySketchSolver = SketchSolver_Manager::instance()->builder()->createSolver();
337 mySketchSolver->setGroup(myID);
338 mySketchSolver->calculateFailedConstraints(false);
339 myStorage->initializeSolver(mySketchSolver);
340 mySketchSolver->prepare();
342 SketchSolver_SolveStatus aResult = STATUS_OK;
344 if (myStorage->hasDuplicatedConstraint())
345 aResult = STATUS_INCONSISTENT;
346 else if (!isGroupEmpty) {
347 // To avoid overconstraint situation, we will remove temporary constraints one-by-one
348 // and try to find the case without overconstraint
349 bool isLastChance = false;
351 aResult = mySketchSolver->solve();
352 if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET || isLastChance)
354 //// // try to update parameters and resolve once again
355 //// ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin();
356 //// for (; aConstrIt != myConstraints.end(); ++aConstrIt)
357 //// aConstrIt->second->update();
360 removeTemporaryConstraints();
361 mySketchSolver->calculateFailedConstraints(true); // something failed => need to find it
362 myStorage->initializeSolver(mySketchSolver);
366 // Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
367 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
368 if (myPrevResult == STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
369 // the error message should be changed before sending the message
370 sendMessage(EVENT_SOLVER_FAILED);
371 myPrevResult = STATUS_FAILED;
373 mySketchSolver->undo();
376 if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) { // solution succeeded, store results into correspondent attributes
377 myStorage->refresh();
378 updateMultiConstraints(myConstraints);
379 if (myPrevResult != STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
380 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
381 std::set<ObjectPtr> aConflicting = myConflictingConstraints;
382 myConflictingConstraints.clear();
383 myPrevResult = STATUS_OK;
384 // the error message should be changed before sending the message
385 sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
388 mySketchSolver->undo();
389 if (!myConstraints.empty()) {
390 // the error message should be changed before sending the message
391 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue(SketchSolver_Error::CONSTRAINTS());
392 if (myPrevResult != aResult || myPrevResult == STATUS_UNKNOWN) {
393 // Obtain list of conflicting constraints
394 std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
396 if (myConflictingConstraints.empty())
397 sendMessage(EVENT_SOLVER_FAILED, aConflicting);
399 std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
400 for (; anIt != aConflicting.end(); ++anIt)
401 myConflictingConstraints.erase(*anIt);
402 if (!myConflictingConstraints.empty()) {
403 // some constraints does not conflict, send corresponding message
404 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
407 myConflictingConstraints = aConflicting;
408 myPrevResult = aResult;
414 } else if (!isGroupEmpty) {
415 // Check there are constraints Fixed. If they exist, update parameters by stored values
416 ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
417 for (; aCIt != myConstraints.end(); ++aCIt)
418 if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID()) {
422 if (aCIt != myConstraints.end())
423 myStorage->refresh();
425 removeTemporaryConstraints();
426 myStorage->blockEvents(false);
427 myStorage->setNeedToResolve(false);
431 // ============================================================================
432 // Function: mergeGroups
433 // Class: SketchSolver_Group
434 // Purpose: append specified group to the current group
435 // ============================================================================
436 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
438 // If specified group is empty, no need to merge
439 if (theGroup.isEmpty())
442 std::set<ObjectPtr> aConstraints;
443 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
444 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
445 aConstraints.insert(aConstrIter->first);
447 std::list<FeaturePtr> aSortedConstraints = selectApplicableFeatures(aConstraints);
448 std::list<FeaturePtr>::iterator aSCIter = aSortedConstraints.begin();
449 for (; aSCIter != aSortedConstraints.end(); ++aSCIter) {
450 ConstraintPtr aConstr = std::dynamic_pointer_cast<SketchPlugin_Constraint>(*aSCIter);
453 changeConstraint(aConstr);
457 // ============================================================================
458 // Function: splitGroup
459 // Class: SketchSolver_Group
460 // Purpose: divide the group into several subgroups
461 // ============================================================================
462 void SketchSolver_Group::splitGroup(std::list<SketchSolver_Group*>& theCuts)
464 // New storage will be used in trimmed way to store the list of constraint interacted together.
465 StoragePtr aNewStorage = SketchSolver_Manager::instance()->builder()->createStorage(getId());
466 std::list<ConstraintWrapperPtr> aDummyVec; // empty vector to avoid creation of solver's constraints
468 // Obtain constraints, which should be separated
469 std::list<ConstraintPtr> anUnusedConstraints;
470 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
471 for ( ; aCIter != myConstraints.end(); aCIter++) {
472 if (aNewStorage->isInteract(FeaturePtr(aCIter->first)))
473 aNewStorage->addConstraint(aCIter->first, aDummyVec);
475 anUnusedConstraints.push_back(aCIter->first);
478 // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
479 std::list<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
480 while (aUnuseIt != anUnusedConstraints.end()) {
481 if (aNewStorage->isInteract(FeaturePtr(*aUnuseIt))) {
482 aNewStorage->addConstraint(*aUnuseIt, aDummyVec);
483 anUnusedConstraints.erase(aUnuseIt);
484 aUnuseIt = anUnusedConstraints.begin();
490 std::list<SketchSolver_Group*>::iterator aCutsIter;
491 // Remove unused constraints
492 for (aUnuseIt = anUnusedConstraints.begin(); aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt)
493 removeConstraint(*aUnuseIt);
495 SketchSolver_Group* aBaseGroup;
496 for (aUnuseIt = anUnusedConstraints.begin(); aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt) {
498 aCutsIter = theCuts.begin();
499 // Try to append constraint to the current group
500 if (isInteract(*aUnuseIt)) {
501 changeConstraint(*aUnuseIt);
504 // Try to append constraint to already existent group
505 for (; aCutsIter != theCuts.end(); ++aCutsIter)
506 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
507 (*aCutsIter)->changeConstraint(*aUnuseIt);
512 if (aCutsIter == theCuts.end() && !aBaseGroup) {
514 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
515 aGroup->changeConstraint(*aUnuseIt);
516 theCuts.push_back(aGroup);
519 aBaseGroup = *aCutsIter++;
520 // Find other groups interacting with constraint
521 for (; aCutsIter != theCuts.end(); ++aCutsIter)
522 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
523 aBaseGroup->mergeGroups(**aCutsIter);
524 std::list<SketchSolver_Group*>::iterator aRemoveIt = aCutsIter--;
525 theCuts.erase(aRemoveIt);
531 // ============================================================================
532 // Function: isConsistent
533 // Class: SketchSolver_Group
534 // Purpose: search removed entities and constraints
535 // ============================================================================
536 bool SketchSolver_Group::isConsistent()
538 if (isEmpty()) // no one constraint is initialized yet
541 // Check the features and constraint is the storage are valid
542 bool aResult = myStorage->isConsistent();
544 // additional check of consistency of the Fixed constraint,
545 // because they are not added to the storage
546 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
547 for (; aCIter != myConstraints.end(); ++aCIter)
548 if (aCIter->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
549 (!aCIter->first->data() || !aCIter->first->data()->isValid())) {
555 // remove invalid constraints
556 std::set<ConstraintPtr> anInvalidConstraints;
557 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
558 for (; aCIter != myConstraints.end(); ++aCIter) {
559 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
560 anInvalidConstraints.insert(aCIter->first);
562 std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
563 for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
564 removeConstraint(*aRemoveIt);
565 // remove invalid features
566 myStorage->removeInvalidEntities();
571 // ============================================================================
572 // Function: removeTemporaryConstraints
573 // Class: SketchSolver_Group
574 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
575 // resolving the set of constraints
576 // ============================================================================
577 void SketchSolver_Group::removeTemporaryConstraints()
579 std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
580 for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
583 if (!myTempConstraints.empty())
584 myStorage->verifyFixed();
585 myStorage->setNeedToResolve(false);
586 myTempConstraints.clear();
589 // ============================================================================
590 // Function: removeConstraint
591 // Class: SketchSolver_Group
592 // Purpose: remove constraint and all unused entities
593 // ============================================================================
594 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
596 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
597 for (; aCIter != myConstraints.end(); aCIter++)
598 if (aCIter->first == theConstraint) {
599 aCIter->second->remove(); // the constraint is not fully removed
600 if (aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID())
601 notifyCoincidenceChanged(aCIter->second);
604 if (aCIter != myConstraints.end())
605 myConstraints.erase(aCIter);
608 // ============================================================================
609 // Function: setTemporary
610 // Class: SketchSolver_Group
611 // Purpose: append given constraint to the group of temporary constraints
612 // ============================================================================
613 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
615 myTempConstraints.insert(theConstraint);
619 // ============================================================================
620 // Function: checkFeatureValidity
621 // Class: SketchSolver_Group
622 // Purpose: verifies is the feature valid
623 // ============================================================================
624 bool SketchSolver_Group::checkFeatureValidity(FeaturePtr theFeature)
626 if (!theFeature || !theFeature->data()->isValid())
629 SessionPtr aMgr = ModelAPI_Session::get();
630 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
631 return aFactory->validate(theFeature);
637 // =========== Auxiliary functions ========================================
638 static double featureToVal(FeaturePtr theFeature)
640 if (theFeature->getKind() == SketchPlugin_Sketch::ID())
641 return 0.0; // sketch
642 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
644 return 1.0; // features (arc, circle, line, point)
646 const std::string& anID = aConstraint->getKind();
647 if (anID == SketchPlugin_ConstraintCoincidence::ID()) {
648 AttributeRefAttrPtr anAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
649 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
650 AttributeRefAttrPtr anAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
651 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
652 if (anAttrA && anAttrB && (anAttrA->isObject() || anAttrB->isObject()))
653 return 2.0; // point-on-line and point-on-circle should go before points coincidence constraint
656 if (anID == SketchPlugin_ConstraintDistance::ID() ||
657 anID == SketchPlugin_ConstraintLength::ID() ||
658 anID == SketchPlugin_ConstraintRadius::ID())
660 if (anID == SketchPlugin_ConstraintAngle::ID())
662 if (anID == SketchPlugin_ConstraintHorizontal::ID() ||
663 anID == SketchPlugin_ConstraintVertical::ID() ||
664 anID == SketchPlugin_ConstraintParallel::ID() ||
665 anID == SketchPlugin_ConstraintPerpendicular::ID())
667 if (anID == SketchPlugin_ConstraintEqual::ID())
669 if (anID == SketchPlugin_ConstraintTangent::ID() ||
670 anID == SketchPlugin_ConstraintMirror::ID())
672 if (anID == SketchPlugin_ConstraintRigid::ID())
674 if (anID == SketchPlugin_MultiRotation::ID() ||
675 anID == SketchPlugin_MultiTranslation::ID())
678 // all other constraints are placed between Equal and Tangent constraints
682 static bool isLess(FeaturePtr theFeature1, FeaturePtr theFeature2)
684 return featureToVal(theFeature1) < featureToVal(theFeature2);
687 std::list<FeaturePtr> SketchSolver_Group::selectApplicableFeatures(const std::set<ObjectPtr>& theObjects)
689 std::list<FeaturePtr> aResult;
690 std::list<FeaturePtr>::iterator aResIt;
692 std::set<ObjectPtr>::const_iterator anObjIter = theObjects.begin();
693 for (; anObjIter != theObjects.end(); ++anObjIter) {
694 // Operate sketch itself and SketchPlugin features only.
695 // Also, the Fillet need to be skipped, because there are several separated constraints composing it.
696 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
699 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
700 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
701 if ((aFeature->getKind() != SketchPlugin_Sketch::ID() && !aSketchFeature) ||
702 aFeature->getKind() == SketchPlugin_ConstraintFillet::ID())
705 // Find the place where to insert a feature
706 for (aResIt = aResult.begin(); aResIt != aResult.end(); ++aResIt)
707 if (isLess(aFeature, *aResIt))
709 aResult.insert(aResIt, aFeature);
715 void SketchSolver_Group::notifyCoincidenceChanged(SolverConstraintPtr theCoincidence)
717 const std::list<EntityWrapperPtr>& aCoincident = theCoincidence->attributes();
718 EntityWrapperPtr anAttr1 = aCoincident.front();
719 EntityWrapperPtr anAttr2 = aCoincident.back();
721 ConstraintConstraintMap::iterator anIt = myConstraints.begin();
722 for (; anIt != myConstraints.end(); ++anIt)
723 anIt->second->notifyCoincidenceChanged(anAttr1, anAttr2);