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_ConstraintSplit.h>
36 #include <SketchPlugin_ConstraintTangent.h>
37 #include <SketchPlugin_ConstraintVertical.h>
38 #include <SketchPlugin_MultiRotation.h>
39 #include <SketchPlugin_MultiTranslation.h>
45 /// \brief This class is used to give unique index to the groups
49 /// \brief Return vacant index
50 static GroupID NEW_GROUP() { return ++myGroupIndex; }
51 /// \brief Removes the index
52 static void REMOVE_GROUP(const GroupID& theIndex) {
53 if (myGroupIndex == theIndex)
60 static GroupID myGroupIndex; ///< index of the group
63 GroupID GroupIndexer::myGroupIndex = GID_OUTOFGROUP;
66 static void sendMessage(const char* theMessageName)
68 std::shared_ptr<Events_Message> aMessage = std::shared_ptr<Events_Message>(
69 new Events_Message(Events_Loop::eventByName(theMessageName)));
70 Events_Loop::loop()->send(aMessage);
73 static void sendMessage(const char* theMessageName, const std::set<ObjectPtr>& theConflicting)
75 std::shared_ptr<ModelAPI_SolverFailedMessage> aMessage =
76 std::shared_ptr<ModelAPI_SolverFailedMessage>(
77 new ModelAPI_SolverFailedMessage(Events_Loop::eventByName(theMessageName)));
78 aMessage->setObjects(theConflicting);
79 Events_Loop::loop()->send(aMessage);
84 // ========================================================
85 // ========= SketchSolver_Group ===============
86 // ========================================================
88 SketchSolver_Group::SketchSolver_Group(
89 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
90 : myID(GroupIndexer::NEW_GROUP()),
91 myPrevResult(STATUS_UNKNOWN)
93 // Initialize workplane
94 myWorkplaneID = EID_UNKNOWN;
95 addWorkplane(theWorkplane);
98 SketchSolver_Group::~SketchSolver_Group()
100 myConstraints.clear();
101 GroupIndexer::REMOVE_GROUP(myID);
102 // send the message that there is no more conflicting constraints
103 if (!myConflictingConstraints.empty()) {
104 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
105 myConflictingConstraints.clear();
109 // ============================================================================
110 // Function: isBaseWorkplane
111 // Class: SketchSolver_Group
112 // Purpose: verify the group is based on the given workplane
113 // ============================================================================
114 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
116 return theWorkplane == mySketch;
119 // ============================================================================
120 // Function: isInteract
121 // Class: SketchSolver_Group
122 // Purpose: verify are there any entities in the group used by given constraint
123 // ============================================================================
124 bool SketchSolver_Group::isInteract(FeaturePtr theFeature) const
126 // Empty group interacts with everything
129 // Check interaction with the storage
130 bool isInteracted = myStorage->isInteract(theFeature);
131 ConstraintConstraintMap::const_iterator anIt = myConstraints.begin();
132 for (; !isInteracted && anIt != myConstraints.end(); ++anIt)
133 if (anIt->first->getKind() == SketchPlugin_MultiRotation::ID() ||
134 anIt->first->getKind() == SketchPlugin_MultiTranslation::ID()) {
135 isInteracted = anIt->second->isUsed(theFeature);
138 // if theFeature is a constraint, check its attributes
139 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
142 for (int i = 0; i < 4 && !isInteracted; ++i) {
143 AttributeRefAttrPtr aRefAttr = aConstraint->refattr(aConstraint->ATTRIBUTE(i));
146 isInteracted = anIt->second->isUsed((AttributePtr)aRefAttr);
152 // ============================================================================
153 // Function: changeConstraint
154 // Class: SketchSolver_Group
155 // Purpose: create/update the constraint in the group
156 // ============================================================================
157 bool SketchSolver_Group::changeConstraint(
158 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
160 // There is no workplane yet, something wrong
161 if (myWorkplaneID == EID_UNKNOWN)
164 if (!theConstraint || !theConstraint->data())
167 if (!checkFeatureValidity(theConstraint))
170 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
171 myStorage->blockEvents(true);
173 bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
174 if (isNewConstraint) {
175 // Add constraint to the current group
176 SolverConstraintPtr aConstraint = aBuilder->createConstraint(theConstraint);
179 aConstraint->process(myStorage, getId(), getWorkplaneId());
180 if (!aConstraint->error().empty()) {
181 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
182 return false; // some attribute are not initialized yet, don't show message
183 Events_InfoMessage("SketchSolver_Group", aConstraint->error(), this).send();
185 myConstraints[theConstraint] = aConstraint;
187 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID())
188 notifyCoincidenceChanged(myConstraints[theConstraint]);
191 myConstraints[theConstraint]->update();
194 if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
195 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
196 theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_A()));
197 if (aRefAttr && aRefAttr->isObject()) {
198 std::shared_ptr<SketchPlugin_Feature> aFeature =
199 std::dynamic_pointer_cast<SketchPlugin_Feature>(
200 ModelAPI_Feature::feature(aRefAttr->object()));
202 SolverConstraintPtr aConstraint = aBuilder->createFixedConstraint(aFeature);
204 aConstraint->process(myStorage, getId(), getWorkplaneId());
205 setTemporary(aConstraint);
213 // Update constraints if they contain specific feature
214 static void updateMultiConstraints(ConstraintConstraintMap& theConstraints, FeaturePtr theFeature)
216 ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
217 for (; aCIt != theConstraints.end(); ++aCIt) {
218 SketchSolver_ConstraintType aType = aCIt->second->getType();
219 if ((aType == CONSTRAINT_MULTI_ROTATION ||
220 aType == CONSTRAINT_MULTI_TRANSLATION)
221 && aCIt->second->isUsed(theFeature))
222 std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
223 else if ((aType == CONSTRAINT_TANGENT_CIRCLE_LINE || aType == CONSTRAINT_TANGENT_ARC_ARC ||
224 aType == CONSTRAINT_SYMMETRIC || aType == CONSTRAINT_ANGLE)
225 && aCIt->second->isUsed(theFeature))
226 aCIt->second->update();
230 // Recalculate slave features of the Multi constraints
231 static void updateMultiConstraints(ConstraintConstraintMap& theConstraints)
233 ConstraintConstraintMap::iterator aCIt = theConstraints.begin();
234 for (; aCIt != theConstraints.end(); ++aCIt) {
235 SketchSolver_ConstraintType aType = aCIt->second->getType();
236 if ((aType == CONSTRAINT_MULTI_ROTATION ||
237 aType == CONSTRAINT_MULTI_TRANSLATION))
238 std::dynamic_pointer_cast<SketchSolver_ConstraintMulti>(aCIt->second)->update(true);
242 bool SketchSolver_Group::updateFeature(FeaturePtr theFeature)
244 if (!checkFeatureValidity(theFeature))
247 bool isBlocked = myStorage->isEventsBlocked();
249 myStorage->blockEvents(true);
251 myStorage->refresh(true);
252 bool isUpdated = myStorage->update(theFeature);
254 updateMultiConstraints(myConstraints, theFeature);
256 // events were not blocked before, the feature has not been updated,
257 // so it is necessary to revert blocking
258 if (!isUpdated && !isBlocked)
259 myStorage->blockEvents(false);
263 bool SketchSolver_Group::moveFeature(FeaturePtr theFeature)
265 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
267 // Firstly, revert changes in the fixed entities
268 myStorage->blockEvents(true);
269 myStorage->refresh(true);
271 // Secondly, search attributes of the feature in the list of the Multi constraints and update them
272 updateMultiConstraints(myConstraints, theFeature);
274 // Then, create temporary Fixed constraint
275 SolverConstraintPtr aConstraint = aBuilder->createMovementConstraint(theFeature);
278 aConstraint->process(myStorage, getId(), getWorkplaneId());
279 if (aConstraint->error().empty())
280 setTemporary(aConstraint);
282 // Workaround to process arcs.
283 // When move unconstrained arc, add temporary constraint to fix radius.
284 if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
285 bool hasDup = myStorage->hasDuplicatedConstraint();
286 SolverConstraintPtr aFixedRadius = aBuilder->createFixedArcRadiusConstraint(theFeature);
288 aFixedRadius->process(myStorage, getId(), getWorkplaneId());
289 hasDup = myStorage->hasDuplicatedConstraint() && !hasDup;
290 if (aFixedRadius->error().empty() && !hasDup)
291 setTemporary(aFixedRadius);
293 aFixedRadius->remove();
299 // ============================================================================
300 // Function: addWorkplane
301 // Class: SketchSolver_Group
302 // Purpose: create workplane for the group
303 // ============================================================================
304 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
306 if (myWorkplaneID != EID_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
307 return false; // the workplane already exists or the function parameter is not Sketch
309 mySketch = theSketch;
310 if (!updateWorkplane()) {
311 mySketch = CompositeFeaturePtr();
317 // ============================================================================
318 // Function: updateWorkplane
319 // Class: SketchSolver_Group
320 // Purpose: update parameters of workplane
321 // ============================================================================
322 bool SketchSolver_Group::updateWorkplane()
324 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
325 if (!myStorage) // Create storage if not exists
326 myStorage = aBuilder->createStorage(getId());
328 // sketch should be unchanged, set it out of current group
329 bool isUpdated = myStorage->update(FeaturePtr(mySketch), GID_OUTOFGROUP);
331 EntityWrapperPtr anEntity = myStorage->entity(FeaturePtr(mySketch));
332 myWorkplaneID = anEntity->id();
337 // ============================================================================
338 // Function: resolveConstraints
339 // Class: SketchSolver_Group
340 // Purpose: solve the set of constraints for the current group
341 // ============================================================================
342 bool SketchSolver_Group::resolveConstraints()
344 bool aResolved = false;
345 bool isGroupEmpty = isEmpty() && myStorage->isEmpty();
346 if (myStorage->isNeedToResolve() &&
347 (!isGroupEmpty || !myConflictingConstraints.empty() || myPrevResult == STATUS_FAILED)) {
349 mySketchSolver = SketchSolver_Manager::instance()->builder()->createSolver();
351 mySketchSolver->setGroup(myID);
352 mySketchSolver->calculateFailedConstraints(false);
353 myStorage->initializeSolver(mySketchSolver);
354 mySketchSolver->prepare();
356 SketchSolver_SolveStatus aResult = STATUS_OK;
358 if (myStorage->hasDuplicatedConstraint())
359 aResult = STATUS_INCONSISTENT;
360 else if (!isGroupEmpty) {
361 // To avoid overconstraint situation, we will remove temporary constraints one-by-one
362 // and try to find the case without overconstraint
363 bool isLastChance = false;
365 aResult = mySketchSolver->solve();
366 if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET || isLastChance)
368 //// // try to update parameters and resolve once again
369 //// ConstraintConstraintMap::iterator aConstrIt = myConstraints.begin();
370 //// for (; aConstrIt != myConstraints.end(); ++aConstrIt)
371 //// aConstrIt->second->update();
374 removeTemporaryConstraints();
375 mySketchSolver->calculateFailedConstraints(true); // something failed => need to find it
376 myStorage->initializeSolver(mySketchSolver);
380 // Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
381 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
382 ->setValue(SketchSolver_Error::SOLVESPACE_CRASH());
383 if (myPrevResult == STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
384 // the error message should be changed before sending the message
385 sendMessage(EVENT_SOLVER_FAILED);
386 myPrevResult = STATUS_FAILED;
388 mySketchSolver->undo();
391 // solution succeeded, store results into correspondent attributes
392 if (aResult == STATUS_OK || aResult == STATUS_EMPTYSET) {
393 myStorage->setNeedToResolve(false);
394 myStorage->refresh();
395 updateMultiConstraints(myConstraints);
396 // multi-constraints updated some parameters, need to store them
397 if (myStorage->isNeedToResolve())
398 resolveConstraints();
400 if (myPrevResult != STATUS_OK || myPrevResult == STATUS_UNKNOWN) {
401 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())->setValue("");
402 std::set<ObjectPtr> aConflicting = myConflictingConstraints;
403 myConflictingConstraints.clear();
404 myPrevResult = STATUS_OK;
405 // the error message should be changed before sending the message
406 sendMessage(EVENT_SOLVER_REPAIRED, aConflicting);
409 mySketchSolver->undo();
410 if (!myConstraints.empty()) {
411 // the error message should be changed before sending the message
412 getWorkplane()->string(SketchPlugin_Sketch::SOLVER_ERROR())
413 ->setValue(SketchSolver_Error::CONSTRAINTS());
414 if (myPrevResult != aResult ||
415 myPrevResult == STATUS_UNKNOWN ||
416 myPrevResult == STATUS_FAILED) {
417 // Obtain list of conflicting constraints
418 std::set<ObjectPtr> aConflicting = myStorage->getConflictingConstraints(mySketchSolver);
420 if (!myConflictingConstraints.empty()) {
421 std::set<ObjectPtr>::iterator anIt = aConflicting.begin();
422 for (; anIt != aConflicting.end(); ++anIt)
423 myConflictingConstraints.erase(*anIt);
424 if (!myConflictingConstraints.empty()) {
425 // some constraints does not conflict, send corresponding message
426 sendMessage(EVENT_SOLVER_REPAIRED, myConflictingConstraints);
429 myConflictingConstraints = aConflicting;
430 if (!myConflictingConstraints.empty())
431 sendMessage(EVENT_SOLVER_FAILED, myConflictingConstraints);
432 myPrevResult = aResult;
438 } else if (!isGroupEmpty) {
439 // Check if the group contains only constraints Fixed, update parameters by stored values
441 ConstraintConstraintMap::iterator aCIt = myConstraints.begin();
442 for (; aCIt != myConstraints.end(); ++aCIt)
443 if (aCIt->first->getKind() != SketchPlugin_ConstraintRigid::ID()) {
447 if (aCIt == myConstraints.end())
448 myStorage->refresh();
450 removeTemporaryConstraints();
451 myStorage->blockEvents(false);
452 myStorage->setNeedToResolve(false);
456 // ============================================================================
457 // Function: mergeGroups
458 // Class: SketchSolver_Group
459 // Purpose: append specified group to the current group
460 // ============================================================================
461 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
463 // If specified group is empty, no need to merge
464 if (theGroup.isEmpty())
467 std::set<ObjectPtr> aConstraints;
468 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
469 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
470 aConstraints.insert(aConstrIter->first);
472 std::list<FeaturePtr> aSortedConstraints = selectApplicableFeatures(aConstraints);
473 std::list<FeaturePtr>::iterator aSCIter = aSortedConstraints.begin();
474 for (; aSCIter != aSortedConstraints.end(); ++aSCIter) {
475 ConstraintPtr aConstr = std::dynamic_pointer_cast<SketchPlugin_Constraint>(*aSCIter);
478 changeConstraint(aConstr);
482 // ============================================================================
483 // Function: splitGroup
484 // Class: SketchSolver_Group
485 // Purpose: divide the group into several subgroups
486 // ============================================================================
487 void SketchSolver_Group::splitGroup(std::list<SketchSolver_Group*>& theCuts)
489 // New storage will be used in trimmed way to store the list of constraint interacted together.
490 StoragePtr aNewStorage = SketchSolver_Manager::instance()->builder()->createStorage(getId());
491 // empty vector to avoid creation of solver's constraints
492 std::list<ConstraintWrapperPtr> aDummyVec;
494 // Obtain constraints, which should be separated
495 std::list<ConstraintPtr> anUnusedConstraints;
496 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
497 for ( ; aCIter != myConstraints.end(); aCIter++) {
498 if (aNewStorage->isInteract(FeaturePtr(aCIter->first)))
499 aNewStorage->addConstraint(aCIter->first, aDummyVec);
501 anUnusedConstraints.push_back(aCIter->first);
504 // Check the unused constraints once again,
505 // because they may become interacted with new storage since adding constraints
506 std::list<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
507 while (aUnuseIt != anUnusedConstraints.end()) {
508 if (aNewStorage->isInteract(FeaturePtr(*aUnuseIt))) {
509 aNewStorage->addConstraint(*aUnuseIt, aDummyVec);
510 anUnusedConstraints.erase(aUnuseIt);
511 aUnuseIt = anUnusedConstraints.begin();
517 std::list<SketchSolver_Group*>::iterator aCutsIter;
518 // Remove unused constraints
519 for (aUnuseIt = anUnusedConstraints.begin(); aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt)
520 removeConstraint(*aUnuseIt);
522 SketchSolver_Group* aBaseGroup;
523 for (aUnuseIt = anUnusedConstraints.begin(); aUnuseIt != anUnusedConstraints.end(); ++aUnuseIt) {
525 aCutsIter = theCuts.begin();
526 // Try to append constraint to the current group
527 if (isInteract(*aUnuseIt)) {
528 changeConstraint(*aUnuseIt);
531 // Try to append constraint to already existent group
532 for (; aCutsIter != theCuts.end(); ++aCutsIter)
533 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
534 (*aCutsIter)->changeConstraint(*aUnuseIt);
539 if (aCutsIter == theCuts.end() && !aBaseGroup) {
541 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
542 aGroup->changeConstraint(*aUnuseIt);
543 theCuts.push_back(aGroup);
546 aBaseGroup = *aCutsIter++;
547 // Find other groups interacting with constraint
548 for (; aCutsIter != theCuts.end(); ++aCutsIter)
549 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
550 aBaseGroup->mergeGroups(**aCutsIter);
551 std::list<SketchSolver_Group*>::iterator aRemoveIt = aCutsIter--;
552 theCuts.erase(aRemoveIt);
558 // ============================================================================
559 // Function: isConsistent
560 // Class: SketchSolver_Group
561 // Purpose: search removed entities and constraints
562 // ============================================================================
563 bool SketchSolver_Group::isConsistent()
565 if (isEmpty()) // no one constraint is initialized yet
568 // Check the features and constraint is the storage are valid
569 bool aResult = myStorage->isConsistent();
571 // additional check of consistency of the Fixed constraint,
572 // because they are not added to the storage
573 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
574 for (; aCIter != myConstraints.end(); ++aCIter)
575 if (aCIter->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
576 (!aCIter->first->data() || !aCIter->first->data()->isValid())) {
582 // remove invalid constraints
583 std::set<ConstraintPtr> anInvalidConstraints;
584 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
585 for (; aCIter != myConstraints.end(); ++aCIter) {
586 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
587 anInvalidConstraints.insert(aCIter->first);
589 std::set<ConstraintPtr>::const_iterator aRemoveIt = anInvalidConstraints.begin();
590 for (; aRemoveIt != anInvalidConstraints.end(); ++aRemoveIt)
591 removeConstraint(*aRemoveIt);
592 // remove invalid features
593 myStorage->removeInvalidEntities();
598 // ============================================================================
599 // Function: removeTemporaryConstraints
600 // Class: SketchSolver_Group
601 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
602 // resolving the set of constraints
603 // ============================================================================
604 void SketchSolver_Group::removeTemporaryConstraints()
606 std::set<SolverConstraintPtr>::iterator aTmpIt = myTempConstraints.begin();
607 for (; aTmpIt != myTempConstraints.end(); ++aTmpIt)
610 if (!myTempConstraints.empty())
611 myStorage->verifyFixed();
612 myStorage->setNeedToResolve(false);
613 myTempConstraints.clear();
616 // ============================================================================
617 // Function: removeConstraint
618 // Class: SketchSolver_Group
619 // Purpose: remove constraint and all unused entities
620 // ============================================================================
621 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
623 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
624 for (; aCIter != myConstraints.end(); aCIter++)
625 if (aCIter->first == theConstraint) {
626 aCIter->second->remove(); // the constraint is not fully removed
627 if (aCIter->first->getKind() == SketchPlugin_ConstraintCoincidence::ID())
628 notifyCoincidenceChanged(aCIter->second);
631 if (aCIter != myConstraints.end())
632 myConstraints.erase(aCIter);
633 // empty group => clear storage
634 if (myConstraints.empty()) {
635 myStorage = StoragePtr();
636 mySketchSolver = SolverPtr();
641 // ============================================================================
642 // Function: setTemporary
643 // Class: SketchSolver_Group
644 // Purpose: append given constraint to the group of temporary constraints
645 // ============================================================================
646 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
648 myTempConstraints.insert(theConstraint);
652 // ============================================================================
653 // Function: checkFeatureValidity
654 // Class: SketchSolver_Group
655 // Purpose: verifies is the feature valid
656 // ============================================================================
657 bool SketchSolver_Group::checkFeatureValidity(FeaturePtr theFeature)
659 if (!theFeature || !theFeature->data()->isValid())
662 SessionPtr aMgr = ModelAPI_Session::get();
663 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
664 return aFactory->validate(theFeature);
670 // =========== Auxiliary functions ========================================
671 static double featureToVal(FeaturePtr theFeature)
673 if (theFeature->getKind() == SketchPlugin_Sketch::ID())
674 return 0.0; // sketch
675 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
677 return 1.0; // features (arc, circle, line, point)
679 const std::string& anID = aConstraint->getKind();
680 if (anID == SketchPlugin_ConstraintCoincidence::ID()) {
681 AttributeRefAttrPtr anAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
682 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
683 AttributeRefAttrPtr anAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
684 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
685 if (anAttrA && anAttrB && (anAttrA->isObject() || anAttrB->isObject()))
686 // point-on-line and point-on-circle should go before points coincidence constraint
690 if (anID == SketchPlugin_ConstraintDistance::ID() ||
691 anID == SketchPlugin_ConstraintLength::ID() ||
692 anID == SketchPlugin_ConstraintRadius::ID())
694 if (anID == SketchPlugin_ConstraintAngle::ID())
696 if (anID == SketchPlugin_ConstraintHorizontal::ID() ||
697 anID == SketchPlugin_ConstraintVertical::ID() ||
698 anID == SketchPlugin_ConstraintParallel::ID() ||
699 anID == SketchPlugin_ConstraintPerpendicular::ID())
701 if (anID == SketchPlugin_ConstraintEqual::ID())
703 if (anID == SketchPlugin_ConstraintTangent::ID() ||
704 anID == SketchPlugin_ConstraintMirror::ID())
706 if (anID == SketchPlugin_ConstraintRigid::ID())
708 if (anID == SketchPlugin_MultiRotation::ID() ||
709 anID == SketchPlugin_MultiTranslation::ID())
712 // all other constraints are placed between Equal and Tangent constraints
716 static bool isLess(FeaturePtr theFeature1, FeaturePtr theFeature2)
718 return featureToVal(theFeature1) < featureToVal(theFeature2);
721 std::list<FeaturePtr> SketchSolver_Group::
722 selectApplicableFeatures(const std::set<ObjectPtr>& theObjects)
724 std::list<FeaturePtr> aResult;
725 std::list<FeaturePtr>::iterator aResIt;
727 std::set<ObjectPtr>::const_iterator anObjIter = theObjects.begin();
728 for (; anObjIter != theObjects.end(); ++anObjIter) {
729 // Operate sketch itself and SketchPlugin features only.
730 // Also, the Fillet and Split need to be skipped,
731 // because there are several separated constraints composing it.
732 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
735 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
736 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
737 if ((aFeature->getKind() != SketchPlugin_Sketch::ID() && !aSketchFeature) ||
738 aFeature->getKind() == SketchPlugin_ConstraintFillet::ID() ||
739 aFeature->getKind() == SketchPlugin_ConstraintSplit::ID())
742 // Find the place where to insert a feature
743 for (aResIt = aResult.begin(); aResIt != aResult.end(); ++aResIt)
744 if (isLess(aFeature, *aResIt))
746 aResult.insert(aResIt, aFeature);
752 void SketchSolver_Group::notifyCoincidenceChanged(SolverConstraintPtr theCoincidence)
754 const std::list<EntityWrapperPtr>& aCoincident = theCoincidence->attributes();
755 EntityWrapperPtr anAttr1 = aCoincident.front();
756 EntityWrapperPtr anAttr2 = aCoincident.back();
758 ConstraintConstraintMap::iterator anIt = myConstraints.begin();
759 for (; anIt != myConstraints.end(); ++anIt)
760 anIt->second->notifyCoincidenceChanged(anAttr1, anAttr2);