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_ConstraintFillet.h>
29 #include <SketchPlugin_ConstraintLength.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_ConstraintMirror.h>
32 #include <SketchPlugin_ConstraintRigid.h>
33 #include <SketchPlugin_ConstraintTangent.h>
34 #include <SketchPlugin_Feature.h>
36 #include <SketchPlugin_Arc.h>
37 #include <SketchPlugin_Circle.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Point.h>
40 #include <SketchPlugin_Sketch.h>
46 /// \brief This class is used to give unique index to the groups
50 /// \brief Return vacant index
51 static Slvs_hGroup NEW_GROUP() { return ++myGroupIndex; }
52 /// \brief Removes the index
53 static void REMOVE_GROUP(const Slvs_hGroup& theIndex) {
54 if (myGroupIndex == theIndex)
61 static Slvs_hGroup myGroupIndex; ///< index of the group
64 Slvs_hGroup GroupIndexer::myGroupIndex = 0;
68 // ========================================================
69 // ========= SketchSolver_Group ===============
70 // ========================================================
72 SketchSolver_Group::SketchSolver_Group(
73 std::shared_ptr<ModelAPI_CompositeFeature> theWorkplane)
74 : myID(GroupIndexer::NEW_GROUP())
76 // Initialize workplane
77 myWorkplaneID = SLVS_E_UNKNOWN;
79 assert(addWorkplane(theWorkplane));
81 addWorkplane(theWorkplane);
85 SketchSolver_Group::~SketchSolver_Group()
87 myConstraints.clear();
88 GroupIndexer::REMOVE_GROUP(myID);
91 // ============================================================================
92 // Function: isBaseWorkplane
93 // Class: SketchSolver_Group
94 // Purpose: verify the group is based on the given workplane
95 // ============================================================================
96 bool SketchSolver_Group::isBaseWorkplane(CompositeFeaturePtr theWorkplane) const
98 return theWorkplane == mySketch;
101 // ============================================================================
102 // Function: isInteract
103 // Class: SketchSolver_Group
104 // Purpose: verify are there any entities in the group used by given constraint
105 // ============================================================================
106 bool SketchSolver_Group::isInteract(
107 std::shared_ptr<SketchPlugin_Feature> theFeature) const
109 // Empty group interacts with everything
110 if (isEmpty()) return true;
111 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
113 return myFeatureStorage->isInteract(aConstraint);
114 return myFeatureStorage->isInteract(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
117 // ============================================================================
118 // Function: getFeatureId
119 // Class: SketchSolver_Group
120 // Purpose: Find the identifier of the feature, if it already exists in the group
121 // ============================================================================
122 Slvs_hEntity SketchSolver_Group::getFeatureId(FeaturePtr theFeature) const
124 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
125 if (!myFeatureStorage)
127 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theFeature);
128 if (aConstraints.empty())
130 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
131 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
132 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
133 if (aCIter == myConstraints.end())
135 aResult = aCIter->second->getId(theFeature);
136 if (aResult != SLVS_E_UNKNOWN)
139 return SLVS_E_UNKNOWN;
142 // ============================================================================
143 // Function: getAttributeId
144 // Class: SketchSolver_Group
145 // Purpose: Find the identifier of the attribute, if it already exists in the group
146 // ============================================================================
147 Slvs_hEntity SketchSolver_Group::getAttributeId(AttributePtr theAttribute) const
149 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
150 if (!myFeatureStorage)
152 std::set<ConstraintPtr> aConstraints = myFeatureStorage->getConstraints(theAttribute);
153 if (aConstraints.empty())
155 std::set<ConstraintPtr>::iterator aConstrIter = aConstraints.begin();
156 for (; aConstrIter != aConstraints.end(); aConstrIter++) {
157 ConstraintConstraintMap::const_iterator aCIter = myConstraints.find(*aConstrIter);
158 if (aCIter == myConstraints.end())
160 aResult = aCIter->second->getId(theAttribute);
161 if (aResult != SLVS_E_UNKNOWN)
164 return SLVS_E_UNKNOWN;
167 // ============================================================================
168 // Function: changeConstraint
169 // Class: SketchSolver_Group
170 // Purpose: create/update the constraint in the group
171 // ============================================================================
172 bool SketchSolver_Group::changeConstraint(
173 std::shared_ptr<SketchPlugin_Constraint> theConstraint)
175 // There is no workplane yet, something wrong
176 if (myWorkplaneID == SLVS_E_UNKNOWN)
182 if (myConstraints.find(theConstraint) == myConstraints.end()) {
183 // Add constraint to the current group
184 SolverConstraintPtr aConstraint =
185 SketchSolver_Builder::getInstance()->createConstraint(theConstraint);
188 aConstraint->setGroup(this);
189 aConstraint->setStorage(myStorage);
190 if (!aConstraint->error().empty()) {
191 if (aConstraint->error() == SketchSolver_Error::NOT_INITIALIZED())
192 return false; // some attribute are not initialized yet, don't show message
193 Events_Error::send(aConstraint->error(), this);
196 // Additional verification of coincidence of several points
197 if (theConstraint->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
198 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
199 for (; aCIter != myConstraints.end(); aCIter++) {
200 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoincidence =
201 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aCIter->second);
204 std::shared_ptr<SketchSolver_ConstraintCoincidence> aCoinc2 =
205 std::dynamic_pointer_cast<SketchSolver_ConstraintCoincidence>(aConstraint);
206 if (aCoincidence != aCoinc2 && aCoincidence->isCoincide(aCoinc2)) {
207 aCoincidence->attach(aCoinc2);
208 aConstraint = aCoincidence;
212 myConstraints[theConstraint] = aConstraint;
215 myConstraints[theConstraint]->update();
217 // Fix base features for fillet
218 if (theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID()) {
219 std::list<AttributePtr> anAttrList =
220 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
221 std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
222 for (; anAttrIter != anAttrList.end(); anAttrIter++) {
223 AttributeRefAttrPtr aRefAttr =
224 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
225 if (!aRefAttr || !aRefAttr->isObject())
227 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
228 SolverConstraintPtr aConstraint =
229 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
232 aConstraint->setGroup(this);
233 aConstraint->setStorage(myStorage);
234 myTempConstraints.insert(aConstraint);
237 // Fix base features for mirror
238 if (theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID()) {
239 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
240 theConstraint->attribute(SketchPlugin_ConstraintMirror::ENTITY_B()));
241 fixFeaturesList(aRefList);
244 if (!myFeatureStorage)
245 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
246 myFeatureStorage->changeConstraint(theConstraint);
252 bool SketchSolver_Group::updateFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
254 std::set<ConstraintPtr> aConstraints =
255 myFeatureStorage->getConstraints(std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature));
256 if (aConstraints.empty())
258 std::set<ConstraintPtr>::iterator aCIter = aConstraints.begin();
259 for (; aCIter != aConstraints.end(); aCIter++) {
260 ConstraintConstraintMap::iterator aSolConIter = myConstraints.find(*aCIter);
261 aSolConIter->second->update();
266 void SketchSolver_Group::moveFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
268 updateFeature(theFeature);
269 // Temporary rigid constraint
270 SolverConstraintPtr aConstraint =
271 SketchSolver_Builder::getInstance()->createRigidConstraint(theFeature);
274 aConstraint->setGroup(this);
275 aConstraint->setStorage(myStorage);
276 myTempConstraints.insert(aConstraint);
279 // ============================================================================
280 // Function: fixFeaturesList
281 // Class: SketchSolver_Group
282 // Purpose: Apply temporary rigid constraints for the list of features
283 // ============================================================================
284 void SketchSolver_Group::fixFeaturesList(AttributeRefListPtr theList)
286 std::list<ObjectPtr> aList = theList->list();
287 std::list<ObjectPtr>::iterator anIt = aList.begin();
288 for (; anIt != aList.end(); anIt++) {
291 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
292 SolverConstraintPtr aConstraint =
293 SketchSolver_Builder::getInstance()->createRigidConstraint(aFeature);
296 aConstraint->setGroup(this);
297 aConstraint->setStorage(myStorage);
298 myTempConstraints.insert(aConstraint);
302 // ============================================================================
303 // Function: addWorkplane
304 // Class: SketchSolver_Group
305 // Purpose: create workplane for the group
306 // ============================================================================
307 bool SketchSolver_Group::addWorkplane(CompositeFeaturePtr theSketch)
309 if (myWorkplaneID != SLVS_E_UNKNOWN || theSketch->getKind() != SketchPlugin_Sketch::ID())
310 return false; // the workplane already exists or the function parameter is not Sketch
312 mySketch = theSketch;
317 // ============================================================================
318 // Function: updateWorkplane
319 // Class: SketchSolver_Group
320 // Purpose: update parameters of workplane
321 // ============================================================================
322 bool SketchSolver_Group::updateWorkplane()
324 if (!myStorage) // Create storage if not exists
325 myStorage = StoragePtr(new SketchSolver_Storage);
326 SketchSolver_Builder* aBuilder = SketchSolver_Builder::getInstance();
328 std::vector<Slvs_Entity> anEntities;
329 std::vector<Slvs_Param> aParams;
330 if (!aBuilder->createWorkplane(mySketch, anEntities, aParams))
333 if (myWorkplaneID == SLVS_E_UNKNOWN) {
334 myWorkplaneID = anEntities.back().h;
335 // Add new workplane elements
336 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
337 for (; aParIter != aParams.end(); aParIter++) {
338 aParIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
339 aParIter->group = myID;
340 aParIter->h = myStorage->addParameter(*aParIter);
342 std::vector<Slvs_Entity>::iterator anEntIter = anEntities.begin();
343 for (; anEntIter != anEntities.end(); anEntIter++) {
344 anEntIter->h = SLVS_E_UNKNOWN; // the ID should be generated by storage
345 anEntIter->group = myID;
346 anEntIter->wrkpl = myWorkplaneID;
347 for (int i = 0; i < 4; i++)
348 if (anEntIter->param[i] != SLVS_E_UNKNOWN)
349 anEntIter->param[i] = aParams[anEntIter->param[i]-1].h;
350 for (int i = 0; i < 4; i++)
351 if (anEntIter->point[i] != SLVS_E_UNKNOWN)
352 anEntIter->point[i] = anEntities[anEntIter->point[i]-1].h;
353 anEntIter->h = myStorage->addEntity(*anEntIter);
356 // Update existent workplane
357 const Slvs_Entity& aWP = myStorage->getEntity(myWorkplaneID);
358 const Slvs_Entity& anOrigin = myStorage->getEntity(aWP.point[0]);
359 const Slvs_Entity& aNormal = myStorage->getEntity(aWP.normal);
360 // Get parameters and update them
361 Slvs_hParam aWPParams[7] = {
362 anOrigin.param[0], anOrigin.param[1], anOrigin.param[2],
363 aNormal.param[0], aNormal.param[1], aNormal.param[2], aNormal.param[3]
365 std::vector<Slvs_Param>::iterator aParIter = aParams.begin();
366 for (int i = 0; aParIter != aParams.end(); aParIter++, i++) {
367 Slvs_Param aParam = myStorage->getParameter(aWPParams[i]);
368 aParam.val = aParIter->val;
369 myStorage->updateParameter(aParam);
372 return myWorkplaneID > 0;
375 // ============================================================================
376 // Function: resolveConstraints
377 // Class: SketchSolver_Group
378 // Purpose: solve the set of constraints for the current group
379 // ============================================================================
380 bool SketchSolver_Group::resolveConstraints()
382 bool aResolved = false;
383 if (myStorage->isNeedToResolve() && !isEmpty()) {
384 myConstrSolver.setGroupID(myID);
385 myStorage->initializeSolver(myConstrSolver);
387 int aResult = myConstrSolver.solve();
388 if (aResult == SLVS_RESULT_OKAY) { // solution succeeded, store results into correspondent attributes
389 myFeatureStorage->blockEvents(true);
390 ConstraintConstraintMap::iterator aConstrIter = myConstraints.begin();
391 for (; aConstrIter != myConstraints.end(); aConstrIter++)
392 aConstrIter->second->refresh();
393 myFeatureStorage->blockEvents(false);
394 } else if (!myConstraints.empty())
395 Events_Error::send(SketchSolver_Error::CONSTRAINTS(), this);
399 removeTemporaryConstraints();
400 myStorage->setNeedToResolve(false);
404 // ============================================================================
405 // Function: mergeGroups
406 // Class: SketchSolver_Group
407 // Purpose: append specified group to the current group
408 // ============================================================================
409 void SketchSolver_Group::mergeGroups(const SketchSolver_Group& theGroup)
411 // If specified group is empty, no need to merge
412 if (theGroup.isEmpty())
414 if (!myFeatureStorage)
415 myFeatureStorage = FeatureStoragePtr(new SketchSolver_FeatureStorage);
417 std::vector<ConstraintPtr> aComplexConstraints;
418 ConstraintConstraintMap::const_iterator aConstrIter = theGroup.myConstraints.begin();
419 // append simple constraints
420 for (; aConstrIter != theGroup.myConstraints.end(); aConstrIter++)
421 if (isComplexConstraint(aConstrIter->first))
422 aComplexConstraints.push_back(aConstrIter->first);
424 changeConstraint(aConstrIter->first);
425 // append complex constraints
426 std::vector<ConstraintPtr>::iterator aComplexIter = aComplexConstraints.begin();
427 for (; aComplexIter != aComplexConstraints.end(); aComplexIter++)
428 changeConstraint(*aComplexIter);
431 // ============================================================================
432 // Function: splitGroup
433 // Class: SketchSolver_Group
434 // Purpose: divide the group into several subgroups
435 // ============================================================================
436 void SketchSolver_Group::splitGroup(std::vector<SketchSolver_Group*>& theCuts)
438 // Obtain constraints, which should be separated
439 FeatureStoragePtr aNewFeatStorage(new SketchSolver_FeatureStorage);
440 std::vector<ConstraintPtr> anUnusedConstraints;
441 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
442 for ( ; aCIter != myConstraints.end(); aCIter++) {
443 std::list<ConstraintPtr> aBaseConstraints = aCIter->second->constraints();
444 std::list<ConstraintPtr>::iterator anIter = aBaseConstraints.begin();
445 for (; anIter != aBaseConstraints.end(); anIter++)
446 if (aNewFeatStorage->isInteract(*anIter)) {
447 aNewFeatStorage->changeConstraint(*anIter);
449 anUnusedConstraints.push_back(*anIter);
452 // Check the unused constraints once again, because they may become interacted with new storage since adding constraints
453 std::vector<ConstraintPtr>::iterator aUnuseIt = anUnusedConstraints.begin();
454 while (aUnuseIt != anUnusedConstraints.end()) {
455 if (aNewFeatStorage->isInteract(*aUnuseIt)) {
456 size_t aShift = aUnuseIt - anUnusedConstraints.begin();
457 anUnusedConstraints.erase(aUnuseIt);
458 aUnuseIt = anUnusedConstraints.begin() + aShift;
464 std::vector<SketchSolver_Group*>::iterator aCutsIter;
465 aUnuseIt = anUnusedConstraints.begin();
466 for ( ; aUnuseIt != anUnusedConstraints.end(); aUnuseIt++) {
467 // Remove unused constraints
468 removeConstraint(*aUnuseIt);
469 // Try to append constraint to already existent group
470 for (aCutsIter = theCuts.begin(); aCutsIter != theCuts.end(); aCutsIter++)
471 if ((*aCutsIter)->isInteract(*aUnuseIt)) {
472 (*aCutsIter)->changeConstraint(*aUnuseIt);
475 if (aCutsIter == theCuts.end()) {
477 SketchSolver_Group* aGroup = new SketchSolver_Group(mySketch);
478 aGroup->changeConstraint(*aUnuseIt);
479 theCuts.push_back(aGroup);
484 // ============================================================================
485 // Function: isConsistent
486 // Class: SketchSolver_Group
487 // Purpose: search removed entities and constraints
488 // ============================================================================
489 bool SketchSolver_Group::isConsistent()
491 if (!myFeatureStorage) // no one constraint is initialized yet
494 bool aResult = myFeatureStorage->isConsistent();
496 // remove invalid entities
497 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
498 while (aCIter != myConstraints.end()) {
499 std::list<ConstraintPtr> aConstraints = aCIter->second->constraints();
500 std::list<ConstraintPtr>::iterator anIt = aConstraints.begin();
501 for (; anIt != aConstraints.end(); anIt++)
502 if (!(*anIt)->data() || !(*anIt)->data()->isValid())
503 if (aCIter->second->remove(*anIt)) {
504 // the constraint is fully removed, detach it from the list
505 ConstraintConstraintMap::iterator aTmpIt = aCIter++;
506 myFeatureStorage->removeConstraint(aTmpIt->first);
507 myConstraints.erase(aTmpIt);
510 if (anIt == aConstraints.end())
517 // ============================================================================
518 // Function: removeTemporaryConstraints
519 // Class: SketchSolver_Group
520 // Purpose: remove all transient SLVS_C_WHERE_DRAGGED constraints after
521 // resolving the set of constraints
522 // ============================================================================
523 void SketchSolver_Group::removeTemporaryConstraints()
525 myTempConstraints.clear();
526 // Clean lists of removed entities in the storage
527 std::set<Slvs_hParam> aRemPar;
528 std::set<Slvs_hEntity> aRemEnt;
529 std::set<Slvs_hConstraint> aRemCon;
530 myStorage->getRemoved(aRemPar, aRemEnt, aRemCon);
531 myStorage->setNeedToResolve(false);
534 // ============================================================================
535 // Function: removeConstraint
536 // Class: SketchSolver_Group
537 // Purpose: remove constraint and all unused entities
538 // ============================================================================
539 void SketchSolver_Group::removeConstraint(ConstraintPtr theConstraint)
541 myFeatureStorage->removeConstraint(theConstraint);
542 ConstraintConstraintMap::iterator aCIter = myConstraints.begin();
543 for (; aCIter != myConstraints.end(); aCIter++)
544 if (aCIter->second->hasConstraint(theConstraint)) {
545 if (!aCIter->second->remove(theConstraint)) // the constraint is not fully removed
546 aCIter = myConstraints.end();
549 if (aCIter != myConstraints.end())
550 myConstraints.erase(aCIter);
553 // ============================================================================
554 // Function: isComplexConstraint
555 // Class: SketchSolver_Group
556 // Purpose: verifies the constraint is complex, i.e. it needs another constraints to be created before
557 // ============================================================================
558 bool SketchSolver_Group::isComplexConstraint(FeaturePtr theConstraint)
560 return theConstraint->getKind() == SketchPlugin_ConstraintFillet::ID() ||
561 theConstraint->getKind() == SketchPlugin_ConstraintMirror::ID() ||
562 theConstraint->getKind() == SketchPlugin_ConstraintTangent::ID();