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_Builder.h>
10 #include <SketchSolver_Constraint.h>
11 #include <SketchSolver_ConstraintCoincidence.h>
12 #include <SketchSolver_Error.h>
14 #include <Events_Error.h>
15 #include <Events_Loop.h>
16 #include <GeomAPI_XY.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomDataAPI_Dir.h>
20 #include <GeomDataAPI_Point.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_ResultConstruction.h>
27 #include <SketchPlugin_Constraint.h>
28 #include <SketchPlugin_ConstraintEqual.h>
29 #include <SketchPlugin_ConstraintFillet.h>
30 #include <SketchPlugin_ConstraintLength.h>
31 #include <SketchPlugin_ConstraintCoincidence.h>
32 #include <SketchPlugin_ConstraintMirror.h>
33 #include <SketchPlugin_ConstraintRigid.h>
34 #include <SketchPlugin_ConstraintTangent.h>
35 #include <SketchPlugin_Feature.h>
36 #include <SketchPlugin_MultiTranslation.h>
38 #include <SketchPlugin_Arc.h>
39 #include <SketchPlugin_Circle.h>
40 #include <SketchPlugin_Line.h>
41 #include <SketchPlugin_Point.h>
42 #include <SketchPlugin_Sketch.h>
48 /// \brief This class is used to give unique index to the groups
52 /// \brief Return vacant index
53 static Slvs_hGroup NEW_GROUP() { return ++myGroupIndex; }
54 /// \brief Removes the index
55 static void REMOVE_GROUP(const Slvs_hGroup& theIndex) {
56 if (myGroupIndex == theIndex)
63 static Slvs_hGroup myGroupIndex; ///< index of the group
66 Slvs_hGroup GroupIndexer::myGroupIndex = 0;
70 // ========================================================
71 // ========= SketchSolver_Group ===============
72 // ========================================================
74 SketchSolver_Group::SketchSolver_Group(
75 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
76 : myID(GroupIndexer::NEW_GROUP())
78 // Initialize workplane
79 myWorkplaneID = SLVS_E_UNKNOWN;
81 assert(addWorkplane(theWorkplane));
83 addWorkplane(theWorkplane);
87 SketchSolver_Group::~SketchSolver_Group()
89 myConstraints.clear();
90 GroupIndexer::REMOVE_GROUP(myID);
93 // ============================================================================
94 // Function: isBaseWorkplane
95 // Class: SketchSolver_Group
96 // Purpose: verify the group is based on the given workplane
97 // ============================================================================
98 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
100 return theWorkplane == mySketch;
103 // ============================================================================
104 // Function: isInteract
105 // Class: SketchSolver_Group
106 // Purpose: verify are there any entities in the group used by given constraint
107 // ============================================================================
108 bool SketchSolver_Group::isInteract(
109 std::shared_ptr<SketchPlugin_Feature> theFeature) const
111 // Empty group interacts with everything
112 if (isEmpty()) return true;
113 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
115 return myFeatureStorage->isInteract(aConstraint);
116 return myFeatureStorage->isInteract(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
119 // ============================================================================
120 // Function: getFeatureId
121 // Class: SketchSolver_Group
122 // Purpose: Find the identifier of the feature, if it already exists in the group
123 // ============================================================================
124 Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
126 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
127 if (!myFeatureStorage)
129 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
130 if (aConstraints.empty())
132 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
133 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
134 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
135 if (aCIter == myConstraints.end())
137 aResult = aCIter->second->getId(theFeature);
138 if (aResult != SLVS_E_UNKNOWN)
141 return SLVS_E_UNKNOWN;
144 // ============================================================================
145 // Function: getAttributeId
146 // Class: SketchSolver_Group
147 // Purpose: Find the identifier of the attribute, if it already exists in the group
148 // ============================================================================
149 Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
151 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
152 if (!myFeatureStorage)
154 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
155 if (aConstraints.empty())
157 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
158 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
159 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
160 if (aCIter == myConstraints.end())
162 aResult = aCIter->second->getId(theAttribute);
163 if (aResult != SLVS_E_UNKNOWN)
166 return SLVS_E_UNKNOWN;
169 // ============================================================================
170 // Function: changeConstraint
171 // Class: SketchSolver_Group
172 // Purpose: create/update the constraint in the group
173 // ============================================================================
174 bool SketchSolver_Group::changeConstraint(
175 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
177 // There is no workplane yet, something wrong
178 if (myWorkplaneID == SLVS_E_UNKNOWN)
184 bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
185 if (isNewConstraint) {
186 // Add constraint to the current group
187 SolverConstraintPtr aConstraint =
188 SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
191 aConstraint->setGroup(this);
192 aConstraint->setStorage(myStorage);
193 if (!aConstraint->error().empty()) {
194 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
195 return false; // some attribute are not initialized yet, don't show message
196 Events_Error::send(aConstraint->error(), this);
199 // Additional verification of coincidence of several points
200 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
201 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
202 for (; aCIter != myConstraints.end(); aCIter++) {
203 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
204 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
207 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
208 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
209 if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
210 aCoincidence->attach(aCoinc2);
211 aConstraint = aCoincidence;
215 myConstraints[theConstraint] = aConstraint;
218 myConstraints[theConstraint]->update();
220 // Fix base features for fillet
221 if (isNewConstraint && theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
222 std::list<AttributePtr> anAttrList =
223 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
224 std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
225 for (; anAttrIter != anAttrList.end(); anAttrIter++) {
226 AttributeRefAttrPtr aRefAttr =
227 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
228 if (!aRefAttr || !aRefAttr->isObject())
230 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
231 SolverConstraintPtr aConstraint =
232 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
235 aConstraint->setGroup(this);
236 aConstraint->setStorage(myStorage);
237 setTemporary(aConstraint);
240 //// Fix base features for mirror
241 //if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
242 // AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
243 // theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_B()));
244 // fixFeaturesList(aRefList);
247 if (!myFeatureStorage)
248 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
249 myFeatureStorage->changeConstraint(theConstraint);
255 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
257 std::set<ConstraintPtr> aConstraints =
258 myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
259 if (aConstraints.empty())
261 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
262 for (; aCIter != aConstraints.end(); aCIter++) {
263 ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
264 if (aSolConIter == myConstraints.end() || !aSolConIter->first->data() ||
265 !aSolConIter->first->data()->isValid())
267 myFeatureStorage->changeFeature(theFeature, aSolConIter->first);
268 aSolConIter->second->addFeature(theFeature);
269 aSolConIter->second->update();
274 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
276 // Firstly, create temporary rigid constraint
277 SolverConstraintPtr aConstraint =
278 SketchSolver_Builder::getInstance()->createRigidConstraint(theFeature);
281 aConstraint->setGroup(this);
282 aConstraint->setStorage(myStorage);
283 if (aConstraint->error().empty())
284 setTemporary(aConstraint);
285 // Secondly, update the feature
286 updateFeature(theFeature);
289 // ============================================================================
290 // Function: fixFeaturesList
291 // Class: SketchSolver_Group
292 // Purpose: Apply temporary rigid constraints for the list of features
293 // ============================================================================
294 void SketchSolver_Group::fixFeaturesList(AttributeRefListPtr theList)
296 std::list<ObjectPtr> aList = theList->list();
297 std::list<ObjectPtr>::iterator anIt = aList.begin();
298 std::list<FeaturePtr> aFeatures;
299 // Sort features, at begining there are features used by Equal constraint
300 for (; anIt != aList.end(); anIt++) {
303 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
304 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(aFeature);
305 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
306 for (; aCIter != aConstraints.end(); aCIter++)
307 if ((*aCIter)->getKind() == SketchPlugin_ConstraintEqual::ID())
309 if (aCIter != aConstraints.end())
310 aFeatures.push_front(aFeature);
312 aFeatures.push_back(aFeature);
315 std::list<FeaturePtr>::iterator aFeatIter = aFeatures.begin();
316 for (; aFeatIter != aFeatures.end(); aFeatIter++) {
317 SolverConstraintPtr aConstraint =
318 SketchSolver_Builder::getInstance()->createRigidConstraint(*aFeatIter);
321 aConstraint->setGroup(this);
322 aConstraint->setStorage(myStorage);
323 setTemporary(aConstraint);
327 // ============================================================================
328 // Function: addWorkplane
329 // Class: SketchSolver_Group
330 // Purpose: create workplane for the group
331 // ============================================================================
332 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
334 if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
335 return false; // the workplane already exists or the function parameter is not Sketch
337 mySketch = theSketch;
342 // ============================================================================
343 // Function: updateWorkplane
344 // Class: SketchSolver_Group
345 // Purpose: update parameters of workplane
346 // ============================================================================
347 bool SketchSolver_Group::updateWorkplane()
349 if (!myStorage) // Create storage if not exists
350 myStorage = StoragePtr(new SketchSolver_Storage);
351 SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
353 std::vector<Slvs_Entity> anEntities;
354 std::vector<Slvs_Param> aParams;
355 if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
358 if (myWorkplaneID == SLVS_E_UNKNOWN) {
359 myWorkplaneID = anEntities.back().h;
360 // Add new workplane elements
361 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
362 for (; aParIter != aParams.end(); aParIter++) {
363 aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
364 aParIter->group = myID;
365 aParIter->h = myStorage->addParameter(*aParIter);
367 std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
368 for (; anEntIter != anEntities.end(); anEntIter++) {
369 anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
370 anEntIter->group = myID;
371 anEntIter->wrkpl = myWorkplaneID;
372 for (int i = 0; i < 4; i++)
373 if (anEntIter->param[i] != SLVS_E_UNKNOWN)
374 anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
375 for (int i = 0; i < 4; i++)
376 if (anEntIter->point[i] != SLVS_E_UNKNOWN)
377 anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
378 anEntIter->h = myStorage->addEntity(*anEntIter);
381 // Update existent workplane
382 const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
383 const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
384 const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
385 // Get parameters and update them
386 Slvs_hParam aWPParams[7] = {
387 anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
388 aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
390 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
391 for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
392 Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
393 aParam.val = aParIter->val;
394 myStorage->updateParameter(aParam);
397 return myWorkplaneID > 0;
400 // ============================================================================
401 // Function: resolveConstraints
402 // Class: SketchSolver_Group
403 // Purpose: solve the set of constraints for the current group
404 // ============================================================================
405 bool SketchSolver_Group::resolveConstraints()
407 bool aResolved = false;
408 if (myStorage->isNeedToResolve() && !isEmpty()) {
409 myConstrSolver.setGroupID(myID);
410 myStorage->initializeSolver(myConstrSolver);
412 int aResult = SLVS_RESULT_OKAY;
414 if (myStorage->hasDuplicatedConstraint())
415 aResult = SLVS_RESULT_INCONSISTENT;
417 // To avoid overconstraint situation, we will remove temporary constraints one-by-one
418 // and try to find the case without overconstraint
419 int aNbTemp = (int)myTempConstraints.size();
421 aResult = myConstrSolver.solve();
422 if (aResult == SLVS_RESULT_OKAY || aNbTemp <= 0)
424 aNbTemp = myStorage->deleteTemporaryConstraint();
425 myStorage->initializeSolver(myConstrSolver);
429 Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
432 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
433 myFeatureStorage->blockEvents(true);
434 ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
435 for (; aConstrIter != myConstraints.end(); aConstrIter++)
436 aConstrIter->second->refresh();
437 myFeatureStorage->blockEvents(false);
438 } else if (!myConstraints.empty())
439 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
443 removeTemporaryConstraints();
444 myStorage->setNeedToResolve(false);
448 // ============================================================================
449 // Function: mergeGroups
450 // Class: SketchSolver_Group
451 // Purpose: append specified group to the current group
452 // ============================================================================
453 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
455 // If specified group is empty, no need to merge
456 if (theGroup.isEmpty())
458 if (!myFeatureStorage)
459 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
461 std::vector<ConstraintPtr> aComplexConstraints;
462 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
463 // append simple constraints
464 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
465 if (isComplexConstraint(aConstrIter->first))
466 aComplexConstraints.push_back(aConstrIter->first);
468 changeConstraint(aConstrIter->first);
469 // append complex constraints
470 std::vector<ConstraintPtr>::iterator aComplexIter = aComplexConstraints.begin();
471 for (; aComplexIter != aComplexConstraints.end(); aComplexIter++)
472 changeConstraint(*aComplexIter);
475 // ============================================================================
476 // Function: splitGroup
477 // Class: SketchSolver_Group
478 // Purpose: divide the group into several subgroups
479 // ============================================================================
480 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
482 // Obtain constraints, which should be separated
483 FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
484 std::vector<ConstraintPtr> anUnusedConstraints;
485 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
486 for ( ; aCIter != myConstraints.end(); aCIter++) {
487 std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
488 std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
489 for (; anIter != aBaseConstraints.end(); anIter++)
490 if (aNewFeatStorage->isInteract(*anIter)) {
491 aNewFeatStorage->changeConstraint(*anIter);
493 anUnusedConstraints.push_back(*anIter);
496 // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
497 std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
498 while (aUnuseIt != anUnusedConstraints.end()) {
499 if (aNewFeatStorage->isInteract(*aUnuseIt)) {
500 size_t aShift = aUnuseIt - anUnusedConstraints.begin();
501 anUnusedConstraints.erase(aUnuseIt);
502 aUnuseIt = anUnusedConstraints.begin() + aShift;
508 std::vector<SketchSolver_Group*>::iterator aCutsIter;
509 aUnuseIt = anUnusedConstraints.begin();
510 for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
511 // Remove unused constraints
512 removeConstraint(*aUnuseIt);
513 // Try to append constraint to already existent group
514 for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
515 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
516 (*aCutsIter)->changeConstraint(*aUnuseIt);
519 if (aCutsIter == theCuts.end()) {
521 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
522 aGroup->changeConstraint(*aUnuseIt);
523 theCuts.push_back(aGroup);
528 // ============================================================================
529 // Function: isConsistent
530 // Class: SketchSolver_Group
531 // Purpose: search removed entities and constraints
532 // ============================================================================
533 bool SketchSolver_Group::isConsistent()
535 if (!myFeatureStorage) // no one constraint is initialized yet
538 bool aResult = myFeatureStorage->isConsistent();
540 // remove invalid entities
541 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
542 while (aCIter != myConstraints.end()) {
543 std::list<ConstraintPtr> aConstraints = aCIter->second->constraints();
544 std::list<ConstraintPtr>::iterator anIt = aConstraints.begin();
545 for (; anIt != aConstraints.end(); anIt++)
546 if (!(*anIt)->data() || !(*anIt)->data()->isValid())
547 if (aCIter->second->remove(*anIt)) {
548 // the constraint is fully removed, detach it from the list
549 ConstraintConstraintMap::iterator aTmpIt = aCIter++;
550 myFeatureStorage->removeConstraint(aTmpIt->first);
551 myConstraints.erase(aTmpIt);
554 if (anIt == aConstraints.end())
561 // ============================================================================
562 // Function: removeTemporaryConstraints
563 // Class: SketchSolver_Group
564 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
565 // resolving the set of constraints
566 // ============================================================================
567 void SketchSolver_Group::removeTemporaryConstraints()
569 myTempConstraints.clear();
570 myStorage->removeTemporaryConstraints();
571 // Clean lists of removed entities in the storage
572 std::set<Slvs_hParam> aRemPar;
573 std::set<Slvs_hEntity> aRemEnt;
574 std::set<Slvs_hConstraint> aRemCon;
575 myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
576 myStorage->setNeedToResolve(false);
579 // ============================================================================
580 // Function: removeConstraint
581 // Class: SketchSolver_Group
582 // Purpose: remove constraint and all unused entities
583 // ============================================================================
584 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
586 myFeatureStorage->removeConstraint(theConstraint);
587 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
588 for (; aCIter != myConstraints.end(); aCIter++)
589 if (aCIter->second->hasConstraint(theConstraint)) {
590 if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
591 aCIter = myConstraints.end();
594 if (aCIter != myConstraints.end())
595 myConstraints.erase(aCIter);
598 // ============================================================================
599 // Function: isComplexConstraint
600 // Class: SketchSolver_Group
601 // Purpose: verifies the constraint is complex, i.e. it needs another constraints to be created before
602 // ============================================================================
603 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
605 return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
606 theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
607 theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
610 // ============================================================================
611 // Function: setTemporary
612 // Class: SketchSolver_Group
613 // Purpose: append given constraint to th group of temporary constraints
614 // ============================================================================
615 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
617 theConstraint->makeTemporary();
618 myTempConstraints.insert(theConstraint);