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 if (myConstraints.find(theConstraint) == myConstraints.end()) {
184 // Add constraint to the current group
185 SolverConstraintPtr aConstraint =
186 SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
189 aConstraint->setGroup(this);
190 aConstraint->setStorage(myStorage);
191 if (!aConstraint->error().empty()) {
192 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
193 return false; // some attribute are not initialized yet, don't show message
194 Events_Error::send(aConstraint->error(), this);
197 // Additional verification of coincidence of several points
198 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
199 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
200 for (; aCIter != myConstraints.end(); aCIter++) {
201 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
202 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
205 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
206 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
207 if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
208 aCoincidence->attach(aCoinc2);
209 aConstraint = aCoincidence;
213 myConstraints[theConstraint] = aConstraint;
216 myConstraints[theConstraint]->update();
218 // Fix base features for fillet
219 if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
220 std::list<AttributePtr> anAttrList =
221 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
222 std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
223 for (; anAttrIter != anAttrList.end(); anAttrIter++) {
224 AttributeRefAttrPtr aRefAttr =
225 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
226 if (!aRefAttr || !aRefAttr->isObject())
228 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
229 SolverConstraintPtr aConstraint =
230 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
233 aConstraint->setGroup(this);
234 aConstraint->setStorage(myStorage);
235 setTemporary(aConstraint);
238 //// Fix base features for mirror
239 //if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
240 // AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
241 // theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_B()));
242 // fixFeaturesList(aRefList);
245 if (!myFeatureStorage)
246 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
247 myFeatureStorage->changeConstraint(theConstraint);
253 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
255 std::set<ConstraintPtr> aConstraints =
256 myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
257 if (aConstraints.empty())
259 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
260 for (; aCIter != aConstraints.end(); aCIter++) {
261 ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
262 if (aSolConIter == myConstraints.end() || !aSolConIter->first->data() ||
263 !aSolConIter->first->data()->isValid())
265 myFeatureStorage->changeFeature(theFeature, aSolConIter->first);
266 aSolConIter->second->addFeature(theFeature);
267 aSolConIter->second->update();
272 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
274 updateFeature(theFeature);
275 // 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);
286 // ============================================================================
287 // Function: fixFeaturesList
288 // Class: SketchSolver_Group
289 // Purpose: Apply temporary rigid constraints for the list of features
290 // ============================================================================
291 void SketchSolver_Group::fixFeaturesList(AttributeRefListPtr theList)
293 std::list<ObjectPtr> aList = theList->list();
294 std::list<ObjectPtr>::iterator anIt = aList.begin();
295 std::list<FeaturePtr> aFeatures;
296 // Sort features, at begining there are features used by Equal constraint
297 for (; anIt != aList.end(); anIt++) {
300 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
301 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(aFeature);
302 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
303 for (; aCIter != aConstraints.end(); aCIter++)
304 if ((*aCIter)->getKind() == SketchPlugin_ConstraintEqual::ID())
306 if (aCIter != aConstraints.end())
307 aFeatures.push_front(aFeature);
309 aFeatures.push_back(aFeature);
312 std::list<FeaturePtr>::iterator aFeatIter = aFeatures.begin();
313 for (; aFeatIter != aFeatures.end(); aFeatIter++) {
314 SolverConstraintPtr aConstraint =
315 SketchSolver_Builder::getInstance()->createRigidConstraint(*aFeatIter);
318 aConstraint->setGroup(this);
319 aConstraint->setStorage(myStorage);
320 setTemporary(aConstraint);
324 // ============================================================================
325 // Function: addWorkplane
326 // Class: SketchSolver_Group
327 // Purpose: create workplane for the group
328 // ============================================================================
329 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
331 if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
332 return false; // the workplane already exists or the function parameter is not Sketch
334 mySketch = theSketch;
339 // ============================================================================
340 // Function: updateWorkplane
341 // Class: SketchSolver_Group
342 // Purpose: update parameters of workplane
343 // ============================================================================
344 bool SketchSolver_Group::updateWorkplane()
346 if (!myStorage) // Create storage if not exists
347 myStorage = StoragePtr(new SketchSolver_Storage);
348 SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
350 std::vector<Slvs_Entity> anEntities;
351 std::vector<Slvs_Param> aParams;
352 if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
355 if (myWorkplaneID == SLVS_E_UNKNOWN) {
356 myWorkplaneID = anEntities.back().h;
357 // Add new workplane elements
358 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
359 for (; aParIter != aParams.end(); aParIter++) {
360 aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
361 aParIter->group = myID;
362 aParIter->h = myStorage->addParameter(*aParIter);
364 std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
365 for (; anEntIter != anEntities.end(); anEntIter++) {
366 anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
367 anEntIter->group = myID;
368 anEntIter->wrkpl = myWorkplaneID;
369 for (int i = 0; i < 4; i++)
370 if (anEntIter->param[i] != SLVS_E_UNKNOWN)
371 anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
372 for (int i = 0; i < 4; i++)
373 if (anEntIter->point[i] != SLVS_E_UNKNOWN)
374 anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
375 anEntIter->h = myStorage->addEntity(*anEntIter);
378 // Update existent workplane
379 const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
380 const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
381 const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
382 // Get parameters and update them
383 Slvs_hParam aWPParams[7] = {
384 anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
385 aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
387 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
388 for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
389 Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
390 aParam.val = aParIter->val;
391 myStorage->updateParameter(aParam);
394 return myWorkplaneID > 0;
397 // ============================================================================
398 // Function: resolveConstraints
399 // Class: SketchSolver_Group
400 // Purpose: solve the set of constraints for the current group
401 // ============================================================================
402 bool SketchSolver_Group::resolveConstraints()
404 bool aResolved = false;
405 if (myStorage->isNeedToResolve() && !isEmpty()) {
406 myConstrSolver.setGroupID(myID);
407 myStorage->initializeSolver(myConstrSolver);
409 int aResult = SLVS_RESULT_OKAY;
411 aResult = myConstrSolver.solve();
413 Events_Error::send(SketchSolver_Error::SOLVESPACE_CRASH(), this);
416 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
417 myFeatureStorage->blockEvents(true);
418 ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
419 for (; aConstrIter != myConstraints.end(); aConstrIter++)
420 aConstrIter->second->refresh();
421 myFeatureStorage->blockEvents(false);
422 } else if (!myConstraints.empty())
423 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
427 removeTemporaryConstraints();
428 myStorage->setNeedToResolve(false);
432 // ============================================================================
433 // Function: mergeGroups
434 // Class: SketchSolver_Group
435 // Purpose: append specified group to the current group
436 // ============================================================================
437 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
439 // If specified group is empty, no need to merge
440 if (theGroup.isEmpty())
442 if (!myFeatureStorage)
443 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
445 std::vector<ConstraintPtr> aComplexConstraints;
446 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
447 // append simple constraints
448 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
449 if (isComplexConstraint(aConstrIter->first))
450 aComplexConstraints.push_back(aConstrIter->first);
452 changeConstraint(aConstrIter->first);
453 // append complex constraints
454 std::vector<ConstraintPtr>::iterator aComplexIter = aComplexConstraints.begin();
455 for (; aComplexIter != aComplexConstraints.end(); aComplexIter++)
456 changeConstraint(*aComplexIter);
459 // ============================================================================
460 // Function: splitGroup
461 // Class: SketchSolver_Group
462 // Purpose: divide the group into several subgroups
463 // ============================================================================
464 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
466 // Obtain constraints, which should be separated
467 FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
468 std::vector<ConstraintPtr> anUnusedConstraints;
469 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
470 for ( ; aCIter != myConstraints.end(); aCIter++) {
471 std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
472 std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
473 for (; anIter != aBaseConstraints.end(); anIter++)
474 if (aNewFeatStorage->isInteract(*anIter)) {
475 aNewFeatStorage->changeConstraint(*anIter);
477 anUnusedConstraints.push_back(*anIter);
480 // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
481 std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
482 while (aUnuseIt != anUnusedConstraints.end()) {
483 if (aNewFeatStorage->isInteract(*aUnuseIt)) {
484 size_t aShift = aUnuseIt - anUnusedConstraints.begin();
485 anUnusedConstraints.erase(aUnuseIt);
486 aUnuseIt = anUnusedConstraints.begin() + aShift;
492 std::vector<SketchSolver_Group*>::iterator aCutsIter;
493 aUnuseIt = anUnusedConstraints.begin();
494 for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
495 // Remove unused constraints
496 removeConstraint(*aUnuseIt);
497 // Try to append constraint to already existent group
498 for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
499 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
500 (*aCutsIter)->changeConstraint(*aUnuseIt);
503 if (aCutsIter == theCuts.end()) {
505 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
506 aGroup->changeConstraint(*aUnuseIt);
507 theCuts.push_back(aGroup);
512 // ============================================================================
513 // Function: isConsistent
514 // Class: SketchSolver_Group
515 // Purpose: search removed entities and constraints
516 // ============================================================================
517 bool SketchSolver_Group::isConsistent()
519 if (!myFeatureStorage) // no one constraint is initialized yet
522 bool aResult = myFeatureStorage->isConsistent();
524 // remove invalid entities
525 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
526 while (aCIter != myConstraints.end()) {
527 std::list<ConstraintPtr> aConstraints = aCIter->second->constraints();
528 std::list<ConstraintPtr>::iterator anIt = aConstraints.begin();
529 for (; anIt != aConstraints.end(); anIt++)
530 if (!(*anIt)->data() || !(*anIt)->data()->isValid())
531 if (aCIter->second->remove(*anIt)) {
532 // the constraint is fully removed, detach it from the list
533 ConstraintConstraintMap::iterator aTmpIt = aCIter++;
534 myFeatureStorage->removeConstraint(aTmpIt->first);
535 myConstraints.erase(aTmpIt);
538 if (anIt == aConstraints.end())
545 // ============================================================================
546 // Function: removeTemporaryConstraints
547 // Class: SketchSolver_Group
548 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
549 // resolving the set of constraints
550 // ============================================================================
551 void SketchSolver_Group::removeTemporaryConstraints()
553 myTempConstraints.clear();
554 myStorage->removeTemporaryConstraints();
555 // Clean lists of removed entities in the storage
556 std::set<Slvs_hParam> aRemPar;
557 std::set<Slvs_hEntity> aRemEnt;
558 std::set<Slvs_hConstraint> aRemCon;
559 myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
560 myStorage->setNeedToResolve(false);
563 // ============================================================================
564 // Function: removeConstraint
565 // Class: SketchSolver_Group
566 // Purpose: remove constraint and all unused entities
567 // ============================================================================
568 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
570 myFeatureStorage->removeConstraint(theConstraint);
571 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
572 for (; aCIter != myConstraints.end(); aCIter++)
573 if (aCIter->second->hasConstraint(theConstraint)) {
574 if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
575 aCIter = myConstraints.end();
578 if (aCIter != myConstraints.end())
579 myConstraints.erase(aCIter);
582 // ============================================================================
583 // Function: isComplexConstraint
584 // Class: SketchSolver_Group
585 // Purpose: verifies the constraint is complex, i.e. it needs another constraints to be created before
586 // ============================================================================
587 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
589 return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
590 theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
591 theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();
594 // ============================================================================
595 // Function: setTemporary
596 // Class: SketchSolver_Group
597 // Purpose: append given constraint to th group of temporary constraints
598 // ============================================================================
599 void SketchSolver_Group::setTemporary(SolverConstraintPtr theConstraint)
601 theConstraint->makeTemporary();
602 myTempConstraints.insert(theConstraint);