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>
37 #include <SketchPlugin_Arc.h>
38 #include <SketchPlugin_Circle.h>
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_Point.h>
41 #include <SketchPlugin_Sketch.h>
47 /// \brief This class is used to give unique index to the groups
51 /// \brief Return vacant index
52 static Slvs_hGroup NEW_GROUP() { return ++myGroupIndex; }
53 /// \brief Removes the index
54 static void REMOVE_GROUP(const Slvs_hGroup& theIndex) {
55 if (myGroupIndex == theIndex)
62 static Slvs_hGroup myGroupIndex; ///< index of the group
65 Slvs_hGroup GroupIndexer::myGroupIndex = 0;
69 // ========================================================
70 // ========= SketchSolver_Group ===============
71 // ========================================================
73 SketchSolver_Group::SketchSolver_Group(
74 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
75 : myID(GroupIndexer::NEW_GROUP())
77 // Initialize workplane
78 myWorkplaneID = SLVS_E_UNKNOWN;
80 assert(addWorkplane(theWorkplane));
82 addWorkplane(theWorkplane);
86 SketchSolver_Group::~SketchSolver_Group()
88 myConstraints.clear();
89 GroupIndexer::REMOVE_GROUP(myID);
92 // ============================================================================
93 // Function: isBaseWorkplane
94 // Class: SketchSolver_Group
95 // Purpose: verify the group is based on the given workplane
96 // ============================================================================
97 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
99 return theWorkplane == mySketch;
102 // ============================================================================
103 // Function: isInteract
104 // Class: SketchSolver_Group
105 // Purpose: verify are there any entities in the group used by given constraint
106 // ============================================================================
107 bool SketchSolver_Group::isInteract(
108 std::shared_ptr<SketchPlugin_Feature> theFeature) const
110 // Empty group interacts with everything
111 if (isEmpty()) return true;
112 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
114 return myFeatureStorage->isInteract(aConstraint);
115 return myFeatureStorage->isInteract(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
118 // ============================================================================
119 // Function: getFeatureId
120 // Class: SketchSolver_Group
121 // Purpose: Find the identifier of the feature, if it already exists in the group
122 // ============================================================================
123 Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
125 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
126 if (!myFeatureStorage)
128 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
129 if (aConstraints.empty())
131 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
132 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
133 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
134 if (aCIter == myConstraints.end())
136 aResult = aCIter->second->getId(theFeature);
137 if (aResult != SLVS_E_UNKNOWN)
140 return SLVS_E_UNKNOWN;
143 // ============================================================================
144 // Function: getAttributeId
145 // Class: SketchSolver_Group
146 // Purpose: Find the identifier of the attribute, if it already exists in the group
147 // ============================================================================
148 Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
150 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
151 if (!myFeatureStorage)
153 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
154 if (aConstraints.empty())
156 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
157 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
158 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
159 if (aCIter == myConstraints.end())
161 aResult = aCIter->second->getId(theAttribute);
162 if (aResult != SLVS_E_UNKNOWN)
165 return SLVS_E_UNKNOWN;
168 // ============================================================================
169 // Function: changeConstraint
170 // Class: SketchSolver_Group
171 // Purpose: create/update the constraint in the group
172 // ============================================================================
173 bool SketchSolver_Group::changeConstraint(
174 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
176 // There is no workplane yet, something wrong
177 if (myWorkplaneID == SLVS_E_UNKNOWN)
183 bool isNewConstraint = myConstraints.find(theConstraint) == myConstraints.end();
184 if (isNewConstraint) {
185 // Add constraint to the current group
186 SolverConstraintPtr aConstraint =
187 SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
190 aConstraint->setGroup(this);
191 aConstraint->setStorage(myStorage);
192 if (!aConstraint->error().empty()) {
193 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
194 return false; // some attribute are not initialized yet, don't show message
195 Events_Error::send(aConstraint->error(), this);
198 // Additional verification of coincidence of several points
199 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
200 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
201 for (; aCIter != myConstraints.end(); aCIter++) {
202 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
203 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
206 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
207 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
208 if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
209 aCoincidence->attach(aCoinc2);
210 aConstraint = aCoincidence;
214 myConstraints[theConstraint] = aConstraint;
217 myConstraints[theConstraint]->update();
219 // Fix base features for fillet
220 if (isNewConstraint && theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
221 std::list<AttributePtr> anAttrList =
222 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
223 std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
224 for (; anAttrIter != anAttrList.end(); anAttrIter++) {
225 AttributeRefAttrPtr aRefAttr =
226 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
227 if (!aRefAttr || !aRefAttr->isObject())
229 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
230 SolverConstraintPtr aConstraint =
231 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
234 aConstraint->setGroup(this);
235 aConstraint->setStorage(myStorage);
236 setTemporary(aConstraint);
239 //// Fix base features for mirror
240 //if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
241 // AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
242 // theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_B()));
243 // fixFeaturesList(aRefList);
246 if (!myFeatureStorage)
247 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
248 myFeatureStorage->changeConstraint(theConstraint);
254 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
256 std::set<ConstraintPtr> aConstraints =
257 myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
258 if (aConstraints.empty())
260 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
261 for (; aCIter != aConstraints.end(); aCIter++) {
262 ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
263 if (aSolConIter == myConstraints.end() || !aSolConIter->first->data() ||
264 !aSolConIter->first->data()->isValid())
266 myFeatureStorage->changeFeature(theFeature, aSolConIter->first);
267 aSolConIter->second->addFeature(theFeature);
268 aSolConIter->second->update();
273 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
275 // Firstly, create temporary rigid constraint
276 SolverConstraintPtr aConstraint =
277 SketchSolver_Builder::getInstance()->createRigidConstraint(theFeature);
280 aConstraint->setGroup(this);
281 aConstraint->setStorage(myStorage);
282 if (aConstraint->error().empty())
283 setTemporary(aConstraint);
284 // Secondly, update the feature
285 updateFeature(theFeature);
288 // ============================================================================
289 // Function: fixFeaturesList
290 // Class: SketchSolver_Group
291 // Purpose: Apply temporary rigid constraints for the list of features
292 // ============================================================================
293 void SketchSolver_Group::fixFeaturesList(AttributeRefListPtr theList)
295 std::list<ObjectPtr> aList = theList->list();
296 std::list<ObjectPtr>::iterator anIt = aList.begin();
297 std::list<FeaturePtr> aFeatures;
298 // Sort features, at begining there are features used by Equal constraint
299 for (; anIt != aList.end(); anIt++) {
302 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
303 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(aFeature);
304 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
305 for (; aCIter != aConstraints.end(); aCIter++)
306 if ((*aCIter)->getKind() == SketchPlugin_ConstraintEqual::ID())
308 if (aCIter != aConstraints.end())
309 aFeatures.push_front(aFeature);
311 aFeatures.push_back(aFeature);
314 std::list<FeaturePtr>::iterator aFeatIter = aFeatures.begin();
315 for (; aFeatIter != aFeatures.end(); aFeatIter++) {
316 SolverConstraintPtr aConstraint =
317 SketchSolver_Builder::getInstance()->createRigidConstraint(*aFeatIter);
320 aConstraint->setGroup(this);
321 aConstraint->setStorage(myStorage);
322 setTemporary(aConstraint);
326 // ============================================================================
327 // Function: addWorkplane
328 // Class: SketchSolver_Group
329 // Purpose: create workplane for the group
330 // ============================================================================
331 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
333 if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
334 return false; // the workplane already exists or the function parameter is not Sketch
336 mySketch = theSketch;
341 // ============================================================================
342 // Function: updateWorkplane
343 // Class: SketchSolver_Group
344 // Purpose: update parameters of workplane
345 // ============================================================================
346 bool SketchSolver_Group::updateWorkplane()
348 if (!myStorage) // Create storage if not exists
349 myStorage = StoragePtr(new SketchSolver_Storage);
350 SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
352 std::vector<Slvs_Entity> anEntities;
353 std::vector<Slvs_Param> aParams;
354 if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
357 if (myWorkplaneID == SLVS_E_UNKNOWN) {
358 myWorkplaneID = anEntities.back().h;
359 // Add new workplane elements
360 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
361 for (; aParIter != aParams.end(); aParIter++) {
362 aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
363 aParIter->group = myID;
364 aParIter->h = myStorage->addParameter(*aParIter);
366 std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
367 for (; anEntIter != anEntities.end(); anEntIter++) {
368 anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
369 anEntIter->group = myID;
370 anEntIter->wrkpl = myWorkplaneID;
371 for (int i = 0; i < 4; i++)
372 if (anEntIter->param[i] != SLVS_E_UNKNOWN)
373 anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
374 for (int i = 0; i < 4; i++)
375 if (anEntIter->point[i] != SLVS_E_UNKNOWN)
376 anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
377 anEntIter->h = myStorage->addEntity(*anEntIter);
380 // Update existent workplane
381 const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
382 const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
383 const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
384 // Get parameters and update them
385 Slvs_hParam aWPParams[7] = {
386 anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
387 aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
389 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
390 for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
391 Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
392 aParam.val = aParIter->val;
393 myStorage->updateParameter(aParam);
396 return myWorkplaneID > 0;
399 // ============================================================================
400 // Function: resolveConstraints
401 // Class: SketchSolver_Group
402 // Purpose: solve the set of constraints for the current group
403 // ============================================================================
404 bool SketchSolver_Group::resolveConstraints()
406 bool aResolved = false;
407 if (myStorage->isNeedToResolve() && !isEmpty()) {
408 myConstrSolver.setGroupID(myID);
409 myStorage->initializeSolver(myConstrSolver);
411 int aResult = SLVS_RESULT_OKAY;
413 if (myStorage->hasDuplicatedConstraint())
414 aResult = SLVS_RESULT_INCONSISTENT;
416 // To avoid overconstraint situation, we will remove temporary constraints one-by-one
417 // and try to find the case without overconstraint
418 int aNbTemp = (int)myTempConstraints.size();
420 aResult = myConstrSolver.solve();
421 if (aResult == SLVS_RESULT_OKAY || aNbTemp <= 0)
423 aNbTemp = myStorage->deleteTemporaryConstraint();
424 myStorage->initializeSolver(myConstrSolver);
428 Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
431 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
432 myFeatureStorage->blockEvents(true);
433 ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
434 for (; aConstrIter != myConstraints.end(); aConstrIter++)
435 aConstrIter->second->refresh();
436 myFeatureStorage->blockEvents(false);
437 } else if (!myConstraints.empty())
438 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
442 removeTemporaryConstraints();
443 myStorage->setNeedToResolve(false);
447 // ============================================================================
448 // Function: mergeGroups
449 // Class: SketchSolver_Group
450 // Purpose: append specified group to the current group
451 // ============================================================================
452 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
454 // If specified group is empty, no need to merge
455 if (theGroup.isEmpty())
457 if (!myFeatureStorage)
458 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
460 std::vector<ConstraintPtr> aComplexConstraints;
461 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
462 // append simple constraints
463 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
464 if (isComplexConstraint(aConstrIter->first))
465 aComplexConstraints.push_back(aConstrIter->first);
467 changeConstraint(aConstrIter->first);
468 // append complex constraints
469 std::vector<ConstraintPtr>::iterator aComplexIter = aComplexConstraints.begin();
470 for (; aComplexIter != aComplexConstraints.end(); aComplexIter++)
471 changeConstraint(*aComplexIter);
474 // ============================================================================
475 // Function: splitGroup
476 // Class: SketchSolver_Group
477 // Purpose: divide the group into several subgroups
478 // ============================================================================
479 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
481 // Obtain constraints, which should be separated
482 FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
483 std::vector<ConstraintPtr> anUnusedConstraints;
484 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
485 for ( ; aCIter != myConstraints.end(); aCIter++) {
486 std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
487 std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
488 for (; anIter != aBaseConstraints.end(); anIter++)
489 if (aNewFeatStorage->isInteract(*anIter)) {
490 aNewFeatStorage->changeConstraint(*anIter);
492 anUnusedConstraints.push_back(*anIter);
495 // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
496 std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
497 while (aUnuseIt != anUnusedConstraints.end()) {
498 if (aNewFeatStorage->isInteract(*aUnuseIt)) {
499 size_t aShift = aUnuseIt - anUnusedConstraints.begin();
500 anUnusedConstraints.erase(aUnuseIt);
501 aUnuseIt = anUnusedConstraints.begin() + aShift;
507 std::vector<SketchSolver_Group*>::iterator aCutsIter;
508 aUnuseIt = anUnusedConstraints.begin();
509 for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
510 // Remove unused constraints
511 removeConstraint(*aUnuseIt);
512 // Try to append constraint to already existent group
513 for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
514 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
515 (*aCutsIter)->changeConstraint(*aUnuseIt);
518 if (aCutsIter == theCuts.end()) {
520 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
521 aGroup->changeConstraint(*aUnuseIt);
522 theCuts.push_back(aGroup);
527 // ============================================================================
528 // Function: isConsistent
529 // Class: SketchSolver_Group
530 // Purpose: search removed entities and constraints
531 // ============================================================================
532 bool SketchSolver_Group::isConsistent()
534 if (!myFeatureStorage) // no one constraint is initialized yet
537 bool aResult = myFeatureStorage->isConsistent();
539 // remove invalid entities
540 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
541 while (aCIter != myConstraints.end()) {
542 std::list<ConstraintPtr> aConstraints = aCIter->second->constraints();
543 std::list<ConstraintPtr>::iterator anIt = aConstraints.begin();
544 for (; anIt != aConstraints.end(); anIt++)
545 if (!(*anIt)->data() || !(*anIt)->data()->isValid())
546 if (aCIter->second->remove(*anIt)) {
547 // the constraint is fully removed, detach it from the list
548 ConstraintConstraintMap::iterator aTmpIt = aCIter++;
549 myFeatureStorage->removeConstraint(aTmpIt->first);
550 myConstraints.erase(aTmpIt);
553 if (anIt == aConstraints.end())
560 // ============================================================================
561 // Function: removeTemporaryConstraints
562 // Class: SketchSolver_Group
563 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
564 // resolving the set of constraints
565 // ============================================================================
566 void SketchSolver_Group::removeTemporaryConstraints()
568 myTempConstraints.clear();
569 myStorage->removeTemporaryConstraints();
570 // Clean lists of removed entities in the storage
571 std::set<Slvs_hParam> aRemPar;
572 std::set<Slvs_hEntity> aRemEnt;
573 std::set<Slvs_hConstraint> aRemCon;
574 myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
575 myStorage->setNeedToResolve(false);
578 // ============================================================================
579 // Function: removeConstraint
580 // Class: SketchSolver_Group
581 // Purpose: remove constraint and all unused entities
582 // ============================================================================
583 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
585 myFeatureStorage->removeConstraint(theConstraint);
586 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
587 for (; aCIter != myConstraints.end(); aCIter++)
588 if (aCIter->second->hasConstraint(theConstraint)) {
589 if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
590 aCIter = myConstraints.end();
593 if (aCIter != myConstraints.end())
594 myConstraints.erase(aCIter);
597 // ============================================================================
598 // Function: isComplexConstraint
599 // Class: SketchSolver_Group
600 // Purpose: verifies the constraint is complex, i.e. it needs another constraints to be created before
601 // ============================================================================
602 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
604 return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
605 theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
606 theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
609 // ============================================================================
610 // Function: setTemporary
611 // Class: SketchSolver_Group
612 // Purpose: append given constraint to th group of temporary constraints
613 // ============================================================================
614 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
616 theConstraint->makeTemporary();
617 myTempConstraints.insert(theConstraint);