Salome HOME
[PythonAPI] Fix error in parameter wrapper
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
1 #include <SketchSolver_ConstraintMulti.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <ModelAPI_AttributeInteger.h>
6 #include <ModelAPI_AttributeRefAttr.h>
7 #include <ModelAPI_AttributeRefList.h>
8
9 void SketchSolver_ConstraintMulti::getEntitiesAndCopies(
10     std::list< std::list<EntityWrapperPtr> >& theEntAndCopies)
11 {
12   DataPtr aData = myBaseConstraint->data();
13
14   // Lists of objects and number of copies
15   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
16       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
17   myNumberOfObjects = anInitialRefList->size();
18   myNumberOfCopies = aData->integer(nameNbObjects())->value() - 1;
19   if (myNumberOfCopies <= 0)
20     return;
21
22   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
23       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
24   if (!aRefList || aRefList->size() == 0) {
25     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
26     return;
27   }
28
29   FeaturePtr aFeature;
30   std::list<EntityWrapperPtr> anEntities; // list of transformed entities
31   std::list<ObjectPtr> anObjectList = aRefList->list();
32   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
33   while (anObjIt != anObjectList.end()) {
34     anEntities.clear();
35     for (int i = 0; i <= myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
36       aFeature = ModelAPI_Feature::feature(*anObjIt);
37       if (!aFeature)
38         continue;
39
40       myStorage->update(aFeature);
41       anEntities.push_back(myStorage->entity(aFeature));
42     }
43     if (!anEntities.empty())
44       theEntAndCopies.push_back(anEntities);
45   }
46 }
47
48 void SketchSolver_ConstraintMulti::update()
49 {
50   update(false);
51 }
52
53
54 void SketchSolver_ConstraintMulti::update(bool isForce)
55 {
56   cleanErrorMsg();
57   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
58       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
59   AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
60   if (anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies) {
61     remove();
62     process();
63     return;
64   }
65
66   // update derivative object
67   updateLocal();
68   if (isForce)
69     myAdjusted = false;
70   // update parent object
71   SketchSolver_Constraint::update();
72 }
73
74 void SketchSolver_ConstraintMulti::adjustConstraint()
75 {
76   if (myAdjusted)
77     return; // constraint already adjusted, don't do it once again
78
79   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
80
81   const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
82   std::list<ConstraintWrapperPtr>::const_iterator anIt = aConstraints.begin();
83   for (; anIt != aConstraints.end(); ++anIt)
84     aBuilder->adjustConstraint(*anIt);
85   myStorage->addConstraint(myBaseConstraint, aConstraints);
86   myAdjusted = true;
87 }